123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package service
- import (
- "errors"
- "time"
- . "app.yhyue.com/moapp/jybase/date"
- . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
- "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
- )
- /*
- * 开通权益
- * @param appid
- * @param goods_code 商品代码
- * @param goods_spec_id 商品规格id
- * @param account_id 账户id
- * @param ent_id 企业id
- * @param buy_num 购买数量
- * @param start_time 开始时间
- * @param end_time 结束时间
- * @return error
- */
- func OpenPower(appid, goods_code string, goods_spec_id, account_id, ent_account_id, ent_id int64, buy_num int64, start_time, end_time string) error {
- if appid == "" {
- return errors.New("无效的参数appid")
- } else if goods_code == "" {
- return errors.New("无效的参数goods_code")
- } else if account_id == 0 && ent_account_id == 0 {
- return errors.New("无效的参数account_id、ent_account_id")
- }
- if start_time != "" {
- if _, err := time.ParseInLocation(Date_Full_Layout, start_time, time.Local); err != nil {
- return errors.New("无效的参数start_time," + err.Error())
- }
- }
- if end_time != "" {
- if _, err := time.ParseInLocation(Date_Full_Layout, end_time, time.Local); err != nil {
- return errors.New("无效的参数end_time," + err.Error())
- }
- }
- list := Base_goods_spec.FindById(goods_spec_id, appid, goods_code)
- if list == nil || len(*list) == 0 {
- return errors.New("没有找到该商品规格")
- } else if Base_power.OpenPower(goods_spec_id, appid, goods_code, account_id, ent_account_id, ent_id, buy_num, start_time, end_time, list) {
- return nil
- } else {
- return errors.New("开通失败")
- }
- }
- /*
- * 取消权益
- * @param appid
- * @param goods_code 商品代码
- * @param goods_spec_id 商品规格id
- * @param account_id 账户id
- * @return 是否成功
- */
- func CancelPower(appid, goods_code string, goods_spec_id, account_id, ent_account_id int64) (bool, error) {
- if appid == "" {
- return false, errors.New("无效的参数appid")
- } else if goods_code == "" {
- return false, errors.New("无效的参数goods_code")
- } else if account_id == 0 && ent_account_id == 0 {
- return false, errors.New("无效的参数account_id、ent_account_id")
- }
- return Base_power.CancelPower(appid, goods_code, goods_spec_id, account_id, ent_account_id), nil
- }
- /*
- * 获取待授权详情
- * @param appid
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @return 待授权详情
- */
- func WaitEmpowerDetail(appid, function_code string, ent_id int64) (*pb.WaitEmpowerDetailResp, error) {
- result := &pb.WaitEmpowerDetailResp{}
- if appid == "" {
- return result, errors.New("无效的参数appid")
- } else if function_code == "" {
- return result, errors.New("无效的参数function_code")
- } else if ent_id == 0 {
- return result, errors.New("无效的参数ent_id")
- }
- bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
- if bewes != nil {
- for _, v := range *bewes {
- result.Id = v.Id
- result.EmpowerCount = v.Empower_count
- result.LimitStrategy = v.Limit_strategy
- result.StartTime = v.Start_time
- result.EndTime = v.End_time
- break
- }
- }
- return result, nil
- }
- /*
- * 获取已有的权益
- * @param appid
- * @param account_id 账户id
- * @param ent_account_id 企业账户id
- * @param ent_id 企业id
- * @param ent_user_id 企业用户id
- * @return 所有已开通的权益
- */
- func HasPowers(appid string, account_id, ent_account_id, ent_id, ent_user_id int64) ([]string, error) {
- result := []string{}
- if appid == "" {
- return result, errors.New("无效的参数appid")
- } else if account_id == 0 && ent_account_id == 0 {
- return result, errors.New("无效的参数account_id、ent_account_id")
- }
- m := map[string]bool{}
- bps := Base_power.FindMyPowers(appid, account_id, ent_account_id)
- if bps != nil {
- for _, v := range *bps {
- if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
- if m[v.Function_code] {
- continue
- }
- m[v.Function_code] = true
- result = append(result, v.Function_code)
- }
- }
- }
- return result, nil
- }
- /*
- * 重新授权
- * @param appid
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @param ent_user_id 企业用户id
- * @return 0:失败 1:成功 -2:数量不足
- */
- func ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
- if appid == "" {
- return 0, errors.New("无效的参数appid")
- } else if function_code == "" {
- return 0, errors.New("无效的参数function_code")
- } else if ent_id == 0 {
- return 0, errors.New("无效的参数ent_id")
- } else if len(ent_user_id) == 0 {
- return 0, errors.New("无效的参数ent_user_id")
- } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
- return 0, errors.New("无效的参数function_code")
- }
- bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
- if bewes == nil {
- return -1, nil
- }
- var count int64
- for _, v := range *bewes {
- if v.Empower_count == -1 {
- count = -1
- break
- }
- count += v.Empower_count
- }
- if count != -1 && int64(len(ent_user_id)) > count {
- return -2, nil
- }
- if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
- return 1, nil
- }
- return 0, nil
- }
- /*
- * 授权
- * @param appid
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @param ent_user_id 企业用户id
- * @return 0:失败 1:成功
- */
- func Empower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
- if appid == "" {
- return 0, errors.New("无效的参数appid")
- } else if function_code == "" {
- return 0, errors.New("无效的参数function_code")
- } else if ent_id == 0 {
- return 0, errors.New("无效的参数ent_id")
- } else if len(ent_user_id) == 0 {
- return 0, errors.New("无效的参数ent_user_id")
- } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
- return 0, errors.New("无效的参数function_code")
- }
- if Base_ent_empower.Empower(appid, function_code, ent_id, ent_user_id) {
- return 1, nil
- }
- return 0, nil
- }
- /*
- * 取消授权
- * @param appid
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @param ent_user_id 企业用户id
- * @return 0:失败 1:成功
- */
- func CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
- if appid == "" {
- return 0, errors.New("无效的参数appid")
- } else if function_code == "" {
- return 0, errors.New("无效的参数function_code")
- } else if ent_id == 0 {
- return 0, errors.New("无效的参数ent_id")
- } else if len(ent_user_id) == 0 {
- return 0, errors.New("无效的参数ent_user_id")
- } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
- return 0, errors.New("无效的参数function_code")
- }
- if Base_ent_empower.CancelEmpower(appid, function_code, ent_id, ent_user_id) {
- return 1, nil
- }
- return 0, nil
- }
- /*
- * 授权列表
- * @param appid
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
- * @param page_size 每页大小
- * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
- * @return 列表,如果企业下所有人都有权限,返回空数组
- */
- func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
- var count int64
- result := []*pb.Empower{}
- if appid == "" {
- return count, result, errors.New("无效的参数appid")
- } else if function_code == "" {
- return count, result, errors.New("无效的参数function_code")
- } else if ent_id == 0 {
- return count, result, errors.New("无效的参数ent_id")
- }
- if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
- count = -1
- } else {
- list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
- if list != nil {
- if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
- count = -1
- } else {
- for _, v := range *list {
- result = append(result, &pb.Empower{
- EntUserId: v.Ent_user_id,
- })
- }
- if page_num == 1 {
- count = Base_ent_empower.Count(appid, function_code, ent_id)
- }
- }
- }
- }
- return count, result, nil
- }
|