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) (*WaitEmpowerData, error) { result := &WaitEmpowerData{} 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.Empower_count = v.Empower_count result.Limit_strategy = v.Limit_strategy result.Start_time = v.Start_time result.End_time = v.End_time result.Use_count = v.Use_count 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") } bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id) if bewes == nil { return -1, nil } count := 0 for _, v := range *bewes { count += v.Empower_count } if 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 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") } 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") } 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 }