base_power.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Power_ofid int64 //权益所属id;个人id/企业id/部门id/员工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, user_id, ent_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 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)
  30. return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
  31. }
  32. //查找我的权益
  33. func (b *base_power) FindMyPower(appid, function_code string, user_id, ent_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 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)
  36. return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
  37. }
  38. //开通权益
  39. 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 {
  40. return Mysql_BaseService.ExecTx("开通权益", func(tx *sql.Tx) bool {
  41. ok1, ok2 := true, true
  42. //企业互斥的功能失效掉
  43. if ent_id > 0 {
  44. ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  45. 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)
  46. inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code)
  47. set a.status=0`, appid, ent_id, appid, goods_code, goods_spec_id, appid) > -1
  48. }
  49. //用户互斥的功能失效掉
  50. if user_id > 0 {
  51. ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  52. 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)
  53. inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code)
  54. set a.status=0`, appid, user_id, appid, goods_code, goods_spec_id, appid) > -1
  55. }
  56. power_values, empower_values, wait_empower_values := []interface{}{}, []interface{}{}, []interface{}{}
  57. for _, bgs := range *bgss {
  58. if bgs.Base_goods_spec_powers == nil {
  59. continue
  60. }
  61. for _, v := range bgs.Base_goods_spec_powers {
  62. if v.Base_function == nil {
  63. continue
  64. } else if v.Base_function.Haspower == 0 {
  65. continue
  66. }
  67. power_values = append(power_values, appid, v.Id, v.Function_code, v.Power_type)
  68. if v.Power_type == 1 {
  69. power_values = append(power_values, user_id)
  70. } else if v.Power_type == 2 {
  71. power_values = append(power_values, ent_id)
  72. } else {
  73. logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的power_type", v.Power_type)
  74. continue
  75. }
  76. now := time.Now()
  77. end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  78. if strings.HasSuffix(v.Cycle, "d") {
  79. cycle := IntAll(strings.TrimSuffix(v.Cycle, "d"))
  80. end = end.AddDate(0, 0, cycle)
  81. } else if strings.HasSuffix(v.Cycle, "m") {
  82. cycle := IntAll(strings.TrimSuffix(v.Cycle, "m"))
  83. end = end.AddDate(0, cycle, 0)
  84. } else if strings.HasSuffix(v.Cycle, "y") {
  85. cycle := IntAll(strings.TrimSuffix(v.Cycle, "y"))
  86. end = end.AddDate(cycle, 0, 0)
  87. } else {
  88. logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的cycle", v.Cycle)
  89. continue
  90. }
  91. start_time, end_time := NowFormat(Date_Full_Layout), FormatDate(&end, Date_Full_Layout)
  92. power_values = append(power_values, start_time, end_time)
  93. use_count := buy_num
  94. if bgs.Calculation_type == 2 {
  95. use_count = buy_num * v.Use_count
  96. }
  97. power_values = append(power_values, use_count, v.Limit_strategy, v.Strategy_count, 1, NowFormat(Date_Full_Layout))
  98. //
  99. if v.Power_type == 2 && v.Power_count == -1 && Base_ent_empower.Count(appid, v.Function_code, ent_id) == 0 {
  100. empower_values = append(empower_values, appid, ent_id, 0, v.Function_code, NowFormat(Date_Full_Layout))
  101. } else if v.Power_type == 2 && v.Power_count > 0 {
  102. 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))
  103. }
  104. }
  105. }
  106. if len(power_values) == 0 {
  107. logx.Error(appid, goods_code, goods_spec_id, user_id, ent_id, "没有找到需要开通的权益")
  108. return false
  109. }
  110. 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)
  111. ok3 := true
  112. if len(empower_values) > 0 {
  113. v3, v4 := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, empower_values)
  114. ok3 = v3 > 0 && v4 > 0
  115. }
  116. ok4 := true
  117. if len(wait_empower_values) > 0 {
  118. 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)
  119. ok4 = v5 > 0 && v6 > 0
  120. }
  121. return ok1 && ok2 && v1 > 0 && v2 > 0 && ok3 && ok4
  122. })
  123. }
  124. //取消权益
  125. func (b *base_power) CancelPower(appid, goods_code string, spec_id, user_id, ent_id int64) bool {
  126. return Mysql_BaseService.ExecTx("取消权益", func(tx *sql.Tx) bool {
  127. ok1, ok2 := false, false
  128. if user_id > 0 {
  129. ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  130. 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)
  131. set a.status=-1`, appid, user_id, appid, goods_code, spec_id) > 0
  132. }
  133. if ent_id > 0 {
  134. ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b
  135. 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)
  136. set a.status=-1`, appid, ent_id, appid, goods_code, spec_id) > 0
  137. }
  138. return ok1 || ok2
  139. })
  140. }