labelactionlogic.go 5.8 KB

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