package service import ( "database/sql" "errors" "time" . "app.yhyue.com/moapp/jybase/date" . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db" . "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 function_code 功能代码 * @param ent_id 企业id * @return 待授权详情 */ func WaitEmpowerDetailBySpecId(appid string, spec_id, ent_id int64) (*pb.WaitEmpowerDetailsResp, error) { result := &pb.WaitEmpowerDetailsResp{} if appid == "" { return result, errors.New("无效的参数appid") } else if spec_id == 0 { return result, errors.New("无效的参数spec_id") } else if ent_id == 0 { return result, errors.New("无效的参数ent_id") } bewes := Base_ent_wait_empower.WaitEmpowersBySpecId(appid, spec_id, ent_id) if bewes != nil { for _, v := range *bewes { result.WaitEmpowerDetails = append(result.WaitEmpowerDetails, &pb.WaitEmpowerDetailResp{ Id: v.Id, EmpowerCount: v.Empower_count, LimitStrategy: v.Limit_strategy, StartTime: v.Start_time, EndTime: v.End_time, }) } } 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 string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) { if appid == "" { return 0, errors.New("无效的参数appid") } else if len(function_code) == 0 { 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 status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil { return status, err } 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 string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) { if appid == "" { return 0, errors.New("无效的参数appid") } else if len(function_code) == 0 { 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 status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil { return status, err } 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 string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) { if appid == "" { return 0, errors.New("无效的参数appid") } else if len(function_code) == 0 { 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 Mysql_BaseService.ExecTx("根据功能代码取消授权", func(tx *sql.Tx) bool { return Base_ent_empower.CancelEmpower(tx, 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 CancelAllEmpower(appid string, function_module []string, ent_id int64, ent_user_id []int64) (int64, error) { if appid == "" { return 0, errors.New("无效的参数appid") } else if len(function_module) == 0 { return 0, errors.New("无效的参数function_module") } 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 Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool { return Base_ent_empower.CancelAllEmpower(tx, appid, function_module, 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 }