base_power.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package entity
  2. import (
  3. "database/sql"
  4. "strings"
  5. "time"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/date"
  8. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. var Base_power = base_power{}
  12. //企业/用户权益表
  13. type base_power struct {
  14. Id int64
  15. Appid string
  16. Function_code string //功能代码
  17. Power_type int64 //权益所属类型;1:个人 2:企业
  18. Account_id int64 //账户id
  19. Start_time string //开始时间
  20. End_time string //结束时间
  21. Use_count int64 //使用数量
  22. Surplus_count int64 //剩余数量
  23. Limit_strategy string //限制频率;1d:1天 1m:1个月
  24. Strategy_count int64 //频率数量
  25. }
  26. //获取所有权益
  27. func (b *base_power) FindMyPowers(appid string, account_id, ent_account_id int64) *[]*base_power {
  28. now := NowFormat(Date_Full_Layout)
  29. list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and ((power_type=1 and account_id=?) or (power_type=2 and account_id=?)) and end_time>? and status=1 order by power_type`, appid, account_id, ent_account_id, now)
  30. return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
  31. }
  32. //查找我的权益
  33. func (b *base_power) FindMyPowersByFc(appid, function_code string, account_id, ent_account_id int64) *[]*base_power {
  34. now := NowFormat(Date_Full_Layout)
  35. list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and ((power_type=1 and account_id=?) or (power_type=2 and account_id=?)) and end_time>? and status=1 order by power_type`, appid, function_code, account_id, ent_account_id, now)
  36. return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
  37. }
  38. //查找我最新一条的权益,没有过期限制
  39. func (b *base_power) FindMyLastPowerByFc(appid, function_code string, account_id, ent_account_id int64) *base_power {
  40. list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and ((power_type=1 and account_id=?) or (power_type=2 and account_id=?)) order by update_time desc limit 1`, appid, function_code, account_id, ent_account_id)
  41. if len(*list) == 0 {
  42. return nil
  43. }
  44. return JsonUnmarshal((*list)[0], &base_power{}).(*base_power)
  45. }
  46. //查找我的权益,加锁
  47. func (b *base_power) FindMyPowerForUpdate(id int64) *base_power {
  48. list := Mysql_BaseService.SelectBySql(`select * from base_power where id=? for update wait 5`, id)
  49. if list == nil || len(*list) == 0 {
  50. return nil
  51. }
  52. return JsonUnmarshal((*list)[0], &base_power{}).(*base_power)
  53. }
  54. //开通权益
  55. func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, account_id, ent_account_id, ent_id int64, buy_num int64, start_time, end_time string, bgss *[]*base_goods_spec) bool {
  56. return Mysql_BaseService.ExecTx("开通权益", func(tx *sql.Tx) bool {
  57. //互斥的功能失效掉
  58. ok1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  59. on (a.appid=? and ((a.account_id=? and a.power_type=1) or (a.account_id=? and a.power_type=2)) and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and a.function_code=b.function_code)
  60. inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code)
  61. set a.status=0,update_time=?`, appid, account_id, ent_account_id, appid, goods_code, goods_spec_id, appid, NowFormat(Date_Full_Layout)) > -1
  62. power_values, wait_empower_values := []interface{}{}, []interface{}{}
  63. if start_time == "" {
  64. start_time = NowFormat(Date_Full_Layout)
  65. }
  66. for _, bgs := range *bgss {
  67. if bgs.Base_goods_spec_powers == nil {
  68. continue
  69. }
  70. for _, v := range bgs.Base_goods_spec_powers {
  71. if v.Base_function == nil {
  72. continue
  73. } else if v.Base_function.Haspower == 0 {
  74. continue
  75. } else if v.Power_type == 1 && account_id == 0 {
  76. continue
  77. } else if v.Power_type == 2 && (ent_account_id == 0 || ent_id == 0) {
  78. continue
  79. }
  80. power_values = append(power_values, appid, v.Id, v.Function_code, v.Power_type)
  81. if v.Power_type == 2 {
  82. power_values = append(power_values, ent_account_id)
  83. } else {
  84. power_values = append(power_values, account_id)
  85. }
  86. if end_time == "" {
  87. now := time.Now()
  88. end := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  89. if strings.HasSuffix(v.Cycle, "d") {
  90. cycle := IntAll(strings.TrimSuffix(v.Cycle, "d"))
  91. end = end.AddDate(0, 0, cycle-1)
  92. } else if strings.HasSuffix(v.Cycle, "m") {
  93. cycle := IntAll(strings.TrimSuffix(v.Cycle, "m"))
  94. end = end.AddDate(0, cycle, 0)
  95. } else if strings.HasSuffix(v.Cycle, "y") {
  96. cycle := IntAll(strings.TrimSuffix(v.Cycle, "y"))
  97. end = end.AddDate(cycle, 0, 0)
  98. } else {
  99. logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的cycle", v.Cycle)
  100. continue
  101. }
  102. end_time = FormatDate(&end, Date_Full_Layout)
  103. }
  104. power_values = append(power_values, start_time, end_time)
  105. use_count := buy_num
  106. if bgs.Calculation_type == 2 {
  107. use_count = buy_num * v.Use_count
  108. }
  109. surplus_count := use_count
  110. if v.Base_function.Function_type == 2 {
  111. lastPower := b.FindMyLastPowerByFc(appid, v.Function_code, account_id, account_id)
  112. if lastPower != nil {
  113. surplus_count = lastPower.Surplus_count
  114. }
  115. }
  116. power_values = append(power_values, use_count, surplus_count, v.Limit_strategy, v.Strategy_count, 1, start_time, start_time)
  117. //
  118. if v.Power_type == 2 {
  119. wait_empower_values = append(wait_empower_values, appid, v.Id, v.Function_code, ent_id, start_time, end_time, use_count, v.Power_count, v.Limit_strategy, start_time)
  120. }
  121. }
  122. }
  123. if len(power_values) == 0 {
  124. logx.Error(appid, goods_code, goods_spec_id, account_id, ent_id, "没有找到需要开通的权益")
  125. return false
  126. }
  127. v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_power", []string{"appid", "goods_spec_power_id", "function_code", "power_type", "account_id", "start_time", "end_time", "use_count", "surplus_count", "limit_strategy", "strategy_count", "status", "create_time", "update_time"}, power_values)
  128. ok2 := true
  129. if len(wait_empower_values) > 0 {
  130. v5, v6 := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_wait_empower", []string{"appid", "goods_spec_power_id", "function_code", "ent_id", "start_time", "end_time", "use_count", "empower_count", "limit_strategy", "create_time"}, wait_empower_values)
  131. ok2 = v5 > 0 && v6 > 0
  132. }
  133. return ok1 && ok2 && v1 > 0 && v2 > 0
  134. })
  135. }
  136. //取消权益
  137. func (b *base_power) CancelPower(appid, goods_code string, spec_id, account_id, ent_account_id, ent_id int64) bool {
  138. accountId := account_id
  139. if accountId == 0 {
  140. accountId = ent_account_id
  141. }
  142. return Mysql_BaseService.ExecTx("取消权益", func(tx *sql.Tx) bool {
  143. nowFormat := NowFormat(Date_Full_Layout)
  144. var r1 int64 = 1
  145. if ent_id > 0 {
  146. r1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_ent_wait_empower a inner join base_goods_spec_power b
  147. on (a.appid=? and a.ent_id=? and a.end_time>? and b.appid=? and b.goods_code=? and b.spec_id=? and a.goods_spec_power_id=b.id)
  148. set a.end_time=?`, appid, ent_id, nowFormat, appid, goods_code, spec_id, nowFormat)
  149. }
  150. r2 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  151. on (a.appid=? and a.account_id=? and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and a.goods_spec_power_id=b.id)
  152. set a.status=-1,a.update_time=?`, appid, accountId, appid, goods_code, spec_id, nowFormat)
  153. return r1 > 0 || r2 > 0
  154. })
  155. }