123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package entity
- import (
- "database/sql"
- "strings"
- "time"
- . "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/date"
- . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
- "github.com/zeromicro/go-zero/core/logx"
- )
- var Base_power = base_power{}
- //企业/用户权益表
- type base_power struct {
- Id int64
- Appid string
- Function_code string //功能代码
- Power_type int64 //权益所属类型;1:个人 2:企业
- Power_ofid int64 //权益所属id;个人id/企业id/部门id/员工id
- Start_time string //开始时间
- End_time string //结束时间
- Use_count int64 //使用数量
- Surplus_count int64 //剩余数量
- Limit_strategy string //限制频率;1d:1天 1m:1个月
- Strategy_count int64 //频率数量
- }
- //获取所有权益
- func (b *base_power) FindMyPowers(appid string, user_id, ent_id int64) *[]*base_power {
- now := NowFormat(Date_Full_Layout)
- list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and ((power_type=1 and power_ofid=?) or (power_type=2 and power_ofid=?)) and end_time>? and status=1 order by power_type`, appid, user_id, ent_id, now)
- return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
- }
- //查找我的权益
- func (b *base_power) FindMyPower(appid, function_code string, user_id, ent_id int64) *[]*base_power {
- now := NowFormat(Date_Full_Layout)
- list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and ((power_type=1 and power_ofid=?) or (power_type=2 and power_ofid=?)) and end_time>? and status=1 order by power_type`, appid, function_code, user_id, ent_id, now)
- return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
- }
- //开通权益
- func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, user_id, ent_id int64, buy_num int64, bgss *[]*base_goods_spec) bool {
- return Mysql_BaseService.ExecTx("开通权益", func(tx *sql.Tx) bool {
- ok1, ok2 := true, true
- //企业互斥的功能失效掉
- if ent_id > 0 {
- ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
- on (a.appid=? and a.power_type=2 and a.power_ofid=? and b.appid=? and b.goods_code=? and b.spec_id=? and a.function_code=b.function_code)
- inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code)
- set a.status=0`, appid, ent_id, appid, goods_code, goods_spec_id, appid) > -1
- }
- //用户互斥的功能失效掉
- if user_id > 0 {
- ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
- on (a.appid=? and a.power_type=2 and a.power_ofid=? and b.appid=? and b.goods_code=? and b.spec_id=? and a.function_code=b.function_code)
- inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code)
- set a.status=0`, appid, user_id, appid, goods_code, goods_spec_id, appid) > -1
- }
- power_values, empower_values, wait_empower_values := []interface{}{}, []interface{}{}, []interface{}{}
- for _, bgs := range *bgss {
- if bgs.Base_goods_spec_powers == nil {
- continue
- }
- for _, v := range bgs.Base_goods_spec_powers {
- if v.Base_function == nil {
- continue
- } else if v.Base_function.Haspower == 0 {
- continue
- }
- power_values = append(power_values, appid, v.Id, v.Function_code, v.Power_type)
- if v.Power_type == 1 {
- power_values = append(power_values, user_id)
- } else if v.Power_type == 2 {
- power_values = append(power_values, ent_id)
- } else {
- logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的power_type", v.Power_type)
- continue
- }
- now := time.Now()
- end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
- if strings.HasSuffix(v.Cycle, "d") {
- cycle := IntAll(strings.TrimSuffix(v.Cycle, "d"))
- end = end.AddDate(0, 0, cycle)
- } else if strings.HasSuffix(v.Cycle, "m") {
- cycle := IntAll(strings.TrimSuffix(v.Cycle, "m"))
- end = end.AddDate(0, cycle, 0)
- } else if strings.HasSuffix(v.Cycle, "y") {
- cycle := IntAll(strings.TrimSuffix(v.Cycle, "y"))
- end = end.AddDate(cycle, 0, 0)
- } else {
- logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的cycle", v.Cycle)
- continue
- }
- start_time, end_time := NowFormat(Date_Full_Layout), FormatDate(&end, Date_Full_Layout)
- power_values = append(power_values, start_time, end_time)
- use_count := buy_num
- if bgs.Calculation_type == 2 {
- use_count = buy_num * v.Use_count
- }
- power_values = append(power_values, use_count, v.Limit_strategy, v.Strategy_count, 1, NowFormat(Date_Full_Layout))
- //
- if v.Power_type == 2 && v.Power_count == -1 && Base_ent_empower.Count(appid, v.Function_code, ent_id) == 0 {
- empower_values = append(empower_values, appid, ent_id, 0, v.Function_code, NowFormat(Date_Full_Layout))
- } else if v.Power_type == 2 && v.Power_count > 0 {
- wait_empower_values = append(wait_empower_values, appid, v.Id, ent_id, start_time, end_time, use_count, v.Power_count, v.Limit_strategy, NowFormat(Date_Full_Layout))
- }
- }
- }
- if len(power_values) == 0 {
- logx.Error(appid, goods_code, goods_spec_id, user_id, ent_id, "没有找到需要开通的权益")
- return false
- }
- v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_power", []string{"appid", "goods_spec_power_id", "function_code", "power_type", "power_ofid", "start_time", "end_time", "use_count", "limit_strategy", "strategy_count", "status", "create_time"}, power_values)
- ok3 := true
- if len(empower_values) > 0 {
- v3, v4 := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, empower_values)
- ok3 = v3 > 0 && v4 > 0
- }
- ok4 := true
- if len(wait_empower_values) > 0 {
- v5, v6 := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_wait_empower", []string{"appid", "goods_spec_power_id", "ent_id", "start_time", "end_time", "use_count", "empower_count", "limit_strategy", "create_time"}, wait_empower_values)
- ok4 = v5 > 0 && v6 > 0
- }
- return ok1 && ok2 && v1 > 0 && v2 > 0 && ok3 && ok4
- })
- }
- //取消权益
- func (b *base_power) CancelPower(appid, goods_code string, spec_id, user_id, ent_id int64) bool {
- return Mysql_BaseService.ExecTx("取消权益", func(tx *sql.Tx) bool {
- ok1, ok2 := false, false
- if user_id > 0 {
- ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
- on (a.appid=? and a.power_type=1 and a.power_ofid=? and b.appid=? and b.goods_code=? and b.spec_id=? and b.power_type=1 and a.function_code=b.function_code)
- set a.status=-1`, appid, user_id, appid, goods_code, spec_id) > 0
- }
- if ent_id > 0 {
- ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
- on (a.appid=? and a.power_type=2 and a.power_ofid=? and b.appid=? and b.goods_code=? and b.spec_id=? and b.power_type=2 and a.function_code=b.function_code)
- set a.status=-1`, appid, ent_id, appid, goods_code, spec_id) > 0
- }
- return ok1 || ok2
- })
- }
|