labelactionlogic.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. se "app.yhyue.com/moapp/jybase/encrypt"
  5. "context"
  6. "fmt"
  7. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXBase/rpc/init"
  8. util2 "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXBase/rpc/util"
  9. "log"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXBase/rpc/bxbase"
  14. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXBase/rpc/internal/svc"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type LabelActionLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewLabelActionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LabelActionLogic {
  23. return &LabelActionLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. // 标签新增或删除
  30. func (l *LabelActionLogic) LabelAction(in *bxbase.LabelActionReq) (*bxbase.LabelActionRes, error) {
  31. //var resp bxbase.LabelActionRes
  32. var (
  33. //insertCollKey = []string{"userid", "bid", "buyerclass", "buyerinfo", "winnerinfo", "createdate"}
  34. insertCollKey2 = []string{"userid", "bid", "labelid", "buyerclass", "buyerinfo", "winnerinfo", "createdate"}
  35. )
  36. ibool, ok, msg := true, true, ""
  37. redisArr := []string{}
  38. if in.Laction == "D" && in.Lids != "" {
  39. in.Lids = strings.Split(in.Lids, ",")[0]
  40. labid := se.SE.DecodeString(in.Lids)
  41. //收藏信息解绑标签成功
  42. deleteMap := map[string]interface{}{
  43. "userid": in.UserId,
  44. "id": se.SE.DecodeString(in.Lids),
  45. }
  46. labBid := map[string]interface{}{}
  47. //查询出该标签下的三级页
  48. rdata := IC.MainMysql.SelectBySql(fmt.Sprintf(`select labelid,bid from %s where
  49. FIND_IN_SET(?,labelid) and userid = ?`, "bdcollection"), labid, in.UserId)
  50. if rdata != nil && len(*rdata) > 0 {
  51. for _, v := range *rdata {
  52. bid_id := common.ObjToString(v["bid"])
  53. label_id := labid
  54. labBid[bid_id] = label_id
  55. }
  56. }
  57. //解绑标签
  58. index := 0
  59. for k, v := range labBid {
  60. if IC.MainMysql.UpdateOrDeleteBySql(`update bdcollection set labelid =
  61. (select new.lab from
  62. (SELECT TRIM(BOTH ',' FROM REPLACE(CONCAT(',',labelid,','),CONCAT(',',?,','),',')) lab
  63. FROM bdcollection WHERE bid = ? and userid =? ) new
  64. )
  65. WHERE bid = ? and userid =?`, v, k, in.UserId, k, in.UserId) > -1 {
  66. index++
  67. }
  68. }
  69. if index == len(labBid) {
  70. ibool = IC.MainMysql.Delete("bdlabel", deleteMap)
  71. }
  72. } else {
  73. //标签名称不为空 标签id为空 == 新增标签;如果bids 收藏的招标信息id不为空则再进行绑定
  74. if in.Lname != "" && in.Lids == "" {
  75. //是否有重名
  76. if labArr := IC.MainMysql.SelectBySql(fmt.Sprintf("select * from %s where labelname = ? and userid = ?", "bdlabel"), in.Lname, in.UserId); labArr != nil && len(*labArr) == 1 {
  77. res := (*labArr)[0]
  78. in.Lids = se.SE.EncodeString(common.ObjToString(res["id"]))
  79. ibool = true
  80. } else { //新增标签
  81. insertMap := map[string]interface{}{
  82. "userid": in.UserId,
  83. "labelname": in.Lname,
  84. "createdate": time.Now().Format("2006-01-02 15:04:05"),
  85. }
  86. if lid := IC.MainMysql.Insert("bdlabel", insertMap); lid > 0 {
  87. in.Lids = se.SE.EncodeString(strconv.FormatInt(lid, 10))
  88. ibool = true
  89. } else {
  90. log.Println("新增标签失败 - name:", in.Lname)
  91. }
  92. }
  93. }
  94. //收藏招标信息
  95. if in.Lids != "" && len(in.Binfo) > 0 {
  96. //获取标签
  97. var lids = ""
  98. for _, lv := range strings.Split(in.Lids, ",") {
  99. if lids != "" {
  100. lids += "," + se.SE.DecodeString(lv)
  101. continue
  102. }
  103. lids += se.SE.DecodeString(lv)
  104. }
  105. //更新需要的参数
  106. updatesql := fmt.Sprintf("update %s set labelid=? where userid = ? and ", "bdcollection") //更新语句
  107. updateValue := []interface{}{lids, in.UserId}
  108. //新增需要的参数
  109. insertValue := []interface{}{}
  110. collMap := map[string]bool{} //获取用户收藏的列表
  111. for _, v := range util2.IsCollByBids("", in.UserId) {
  112. collMap[v] = true
  113. }
  114. //maxCount := config.BidCollConfig.FreeUserCollLimit
  115. maxCount := 50
  116. isPay, _ := util2.Power(in.UserId)
  117. if isPay {
  118. //maxCount = config.BidCollConfig.PayUserCollLimit
  119. maxCount = 50
  120. }
  121. //已收藏的次数
  122. collCount := len(collMap)
  123. var i = 0
  124. info := util2.FormatColl(in.Binfo)
  125. for _, bd := range info {
  126. if collMap[util2.DecodeId(bd.Bid)] {
  127. //更新
  128. if updatesql == fmt.Sprintf("update %s set labelid=? where userid = ? and ", "bdcollection") {
  129. updatesql += `( bid =? `
  130. } else {
  131. updatesql += `or bid =? `
  132. }
  133. updateValue = append(updateValue, util2.DecodeId(bd.Bid))
  134. i++
  135. } else {
  136. if collCount >= maxCount {
  137. ok, msg = false, common.If(isPay, "付费用户收藏已达上限", "免费用户收藏已达上限").(string)
  138. log.Printf("userid :%s,收藏失败 id : %s ,已收藏数量:%v ,上限数量:%v", in.UserId, util2.DecodeId(bd.Bid), collCount, maxCount)
  139. } else {
  140. i++
  141. //新增并绑定标签 "userid", "bid", "labelid", "buyerclass", "buyerinfo", "winnerinfo", "createdate"
  142. insertValue = append(insertValue, in.UserId, util2.DecodeId(bd.Bid), lids, util2.PushMapping.Buyerclass[bd.Buyerclass], bd.Buyerinfo, bd.Winnerinfo, time.Now().Format("2006-01-02 15:04:05"))
  143. }
  144. }
  145. }
  146. if len(updateValue) > 2 {
  147. updatesql += `)`
  148. log.Println("updatesql:", updatesql)
  149. _, errs := IC.MainMysql.ExecBySql(updatesql, updateValue...)
  150. if errs != nil {
  151. log.Println("更新、绑定标签失败", in.UserId, updateValue)
  152. }
  153. }
  154. if len(insertValue) > 0 {
  155. x, _ := IC.MainMysql.InsertBatch("bdcollection", insertCollKey2, insertValue)
  156. if x < 0 {
  157. log.Printf("userid:%s收藏失败", in.UserId)
  158. }
  159. }
  160. ibool = i == len(in.Binfo)
  161. }
  162. }
  163. if bl := util2.UpdateCollListRedis("a", in.UserId, redisArr); !bl {
  164. log.Println("更新redis失败", in.UserId)
  165. }
  166. sta, _ := common.If(ok, ibool, ok).(bool)
  167. return &bxbase.LabelActionRes{
  168. ErrMsg: msg,
  169. Status: sta,
  170. }, nil
  171. }