123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- 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.CheckFunctionCode(appid, function_code, ent_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_module 功能分类
- * @param function_code 功能代码
- * @param ent_id 企业id
- * @param ent_user_id 企业用户id
- * @return 0:失败 1:成功 -2:数量不足
- */
- func CrReEmpower(appid string, function_module, function_code []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 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.CheckFunctionCode(appid, function_code, ent_id); err != nil {
- return status, err
- }
- var err error
- var status int64
- if Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool {
- ok1 := Base_ent_empower.CancelAllEmpower(tx, appid, function_module, ent_id, ent_user_id)
- ok2 := Base_ent_empower.Empower(tx, appid, function_code, ent_id, ent_user_id)
- if status, err = Base_ent_empower.CheckEmpowerCount(tx, appid, function_code, ent_id); err != nil {
- return false
- }
- return ok1 && ok2
- }) {
- status = 1
- }
- return status, err
- }
- /*
- * 授权
- * @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.CheckFunctionCode(appid, function_code, ent_id); err != nil {
- return status, err
- }
- if Mysql_BaseService.ExecTx("授权", func(tx *sql.Tx) bool {
- Base_ent_empower.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
- return Base_ent_empower.Empower(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 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_module 功能分类
- * @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
- }
|