newestbiddinglogic.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package logic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. IC "jyBXBase/rpc/init"
  7. "jyBXBase/rpc/model"
  8. "log"
  9. "sort"
  10. "strings"
  11. "time"
  12. MC "app.yhyue.com/moapp/jybase/common"
  13. "app.yhyue.com/moapp/jybase/redis"
  14. "jyBXBase/rpc/internal/svc"
  15. "jyBXBase/rpc/type/bxbase"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type NewestBiddingLogic struct {
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. logx.Logger
  22. }
  23. func NewNewestBiddingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewestBiddingLogic {
  24. return &NewestBiddingLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. // 首页最新招标信息
  31. func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, error) {
  32. t := time.Now()
  33. userType := ""
  34. r := func(in *bxbase.NewestBiddingReq) *bxbase.NewsetBiddingResp {
  35. var res = &bxbase.NewsetBiddingResp{
  36. Data: &bxbase.NewsetBidding{
  37. List: []*bxbase.NewestList{},
  38. },
  39. }
  40. //主体处理(fType:普通用户;vType:超级订阅用户;mType:大会员用户;eType:商机管理用户)
  41. infoCount := IC.MainMysql.CountBySql("select count(id) from entniche_wait_empower where ent_id=? and end_time>NOW() and (product_type like '%VIP订阅%' or product_type like '%大会员%') ", in.EntId)
  42. if infoCount > 0 {
  43. //主体等于企业的
  44. userType = "e"
  45. }
  46. rks := ""
  47. if userType == "e" {
  48. rks = MC.If(in.UserId != "", MC.InterfaceToStr(in.EntUserId), in.City).(string)
  49. } else {
  50. rks = MC.If(in.UserId != "", in.UserId, in.City).(string)
  51. }
  52. redisKey := "p1_indexMessage_new_" + rks
  53. if in.UserId == "" {
  54. redisKey = fmt.Sprintf("%s_%d_%d_%d", redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
  55. }
  56. redisByte, err := redis.GetBytes("other", redisKey)
  57. if err == nil && redisByte != nil && len(*redisByte) > 0 {
  58. err = json.Unmarshal(*redisByte, res.Data)
  59. if err != nil {
  60. res.ErrCode = -1
  61. res.ErrMsg = fmt.Sprintf("读取缓存 序列化异常,err:%s", err.Error())
  62. }
  63. return res
  64. }
  65. //登录用户
  66. if in.UserId != "" {
  67. //优先级 由测试确认 大会员 》 商机管理 》 VIP 》 普通用户 》 搜索历史
  68. //获取订阅信息
  69. userMap, ok := IC.Mgo.FindById("user", in.UserId, `{"o_jy":1,"o_vipjy":1,"i_vip_status":1,"o_member_jy":1,"i_member_status":1,"s_m_phone":1,"s_phone":1}`)
  70. if !ok || userMap == nil || len(*userMap) == 0 {
  71. //查询出错
  72. res.ErrCode = -1
  73. res.ErrMsg = fmt.Errorf("未查询到用户信息").Error()
  74. return res
  75. }
  76. //var isPayUser bool = false
  77. //付费用户如果没有数据 直接返回 需求来源:测试
  78. //vip用户
  79. vipStatus := MC.IntAll((*userMap)["i_vip_status"])
  80. //大会员用户
  81. bigStatus := MC.Int64All((*userMap)["i_member_status"])
  82. if bigStatus > 0 {
  83. o_msgset := MC.ObjToMap((*userMap)["o_member_jy"])
  84. big_items, ok := (*o_msgset)["a_items"].([]interface{})
  85. //大会员推送历史
  86. result := []*bxbase.NewestList{}
  87. if userType == "e" {
  88. result = model.GetNewestInfo(rks, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  89. } else {
  90. result = model.GetNewestInfo(rks, "m", in.NewUserId).GetPushHistory()
  91. }
  92. res.Data.Count = int64(len(result))
  93. if res.Data.Count > 0 {
  94. res.Data.HasSubKeys = ok && len(big_items) > 0
  95. res.Data.List = result
  96. res.Data.SubFlag = "m"
  97. }
  98. return res
  99. }
  100. if phone := MC.If((*userMap)["s_phone"] != nil, MC.ObjToString((*userMap)["s_phone"]), MC.ObjToString((*userMap)["s_m_phone"])).(string); phone != "" && in.EntUserId != "" && in.EntId != "" {
  101. //商机管理
  102. entNicheInfos := IC.MainMysql.SelectBySql(`SELECT i.power_source,u.power FROM entniche_user u LEFT JOIN entniche_info i ON u.ent_id=i.id WHERE u.phone=? and i.status=1 AND i.id = ?`, phone, in.EntId)
  103. if entNicheInfos != nil && len(*entNicheInfos) > 0 {
  104. entNicheInfo := (*entNicheInfos)[0]
  105. //排除商机管理服务
  106. if MC.IntAll(entNicheInfo["power_source"]) != 1 {
  107. // 已分发权限
  108. if MC.IntAll(entNicheInfo["power"]) > 0 {
  109. //商机管理推送历史
  110. result := model.GetNewestInfo(in.EntUserId, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  111. res.Data.Count = int64(len(result))
  112. if res.Data.Count > 0 {
  113. res.Data.List = result
  114. res.Data.SubFlag = "e"
  115. }
  116. return res
  117. }
  118. }
  119. }
  120. }
  121. if vipStatus > 0 {
  122. o_msgset := MC.ObjToMap((*userMap)["o_vipjy"])
  123. vip_items, ok := (*o_msgset)["a_items"].([]interface{})
  124. //vip查询推送历史
  125. result := []*bxbase.NewestList{}
  126. if userType == "e" {
  127. result = model.GetNewestInfo(rks, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  128. } else {
  129. result = model.GetNewestInfo(rks, "v", in.NewUserId).GetPushHistory()
  130. }
  131. res.Data.IsVip = true
  132. res.Data.HasSubKeys = ok && len(vip_items) > 0
  133. res.Data.Count = int64(len(result))
  134. res.Data.List = result
  135. res.Data.SubFlag = "v"
  136. return res
  137. }
  138. //普通用户用户- 有关键词
  139. o_msgset := MC.ObjToMap((*userMap)["o_jy"])
  140. items, ok := (*o_msgset)["a_key"].([]interface{})
  141. if ok && len(items) > 0 {
  142. //普通用户查询推送历史
  143. result := model.GetNewestInfo(rks, "f", in.NewUserId).GetPushHistory()
  144. res.Data.IsVip = false
  145. res.Data.HasSubKeys = ok && len(items) > 0
  146. res.Data.Count = int64(len(result))
  147. res.Data.List = result
  148. res.Data.SubFlag = "f"
  149. return res
  150. }
  151. //搜索历史-关键词
  152. hKeys := redis.GetStr("other", fmt.Sprintf("s_%s", in.UserId))
  153. if hKeys != "" && len(strings.Split(hKeys, ",")) > 0 {
  154. //历史搜索
  155. res.Data.History = strings.Split(hKeys, ",")
  156. //根据订阅词获取查询语句
  157. query := model.NewestQuery("", hKeys)
  158. result := model.NewestES(query)
  159. res.Data.IsVip = false
  160. res.Data.HasSubKeys = false
  161. res.Data.Count = int64(len(result))
  162. res.Data.List = result
  163. return res
  164. }
  165. }
  166. if in.IsSearch == 2 { //定位查询(默认全国)
  167. query := model.NewestQuery(rks, "")
  168. result := model.NewestES(query)
  169. res.Data.IsVip = false
  170. res.Data.HasSubKeys = false
  171. res.Data.Count = int64(len(result))
  172. res.Data.List = result
  173. return res
  174. }
  175. return res
  176. }(in)
  177. if r.Data.Count > 0 {
  178. //排序
  179. sort.Slice(r.Data.List, func(i, j int) bool {
  180. return r.Data.List[i].PublishTime > r.Data.List[j].PublishTime
  181. })
  182. //
  183. rks := ""
  184. if userType == "e" {
  185. rks = MC.If(in.UserId != "", MC.InterfaceToStr(in.EntUserId), in.City).(string)
  186. } else {
  187. rks = MC.If(in.UserId != "", in.UserId, in.City).(string)
  188. }
  189. //rks := MC.If(in.UserId != "", in.UserId, in.City).(string)
  190. b, err := json.Marshal(r.Data)
  191. if err != nil {
  192. r.ErrCode = -1
  193. r.ErrMsg = fmt.Sprintf("保存缓存 序列化异常,err:%s", err.Error())
  194. } else {
  195. redisKey := "p1_indexMessage_new_" + rks
  196. timeOut := 2 * 60 * 60
  197. if in.UserId == "" {
  198. timeOut = 24 * 60 * 60
  199. redisKey = fmt.Sprintf("%s_%d_%d_%d", redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
  200. }
  201. if err = redis.PutBytes("other", redisKey, &b, timeOut); err != nil {
  202. r.ErrCode = -1
  203. r.ErrMsg = fmt.Sprintf("保存缓存 redis 异常,err:%s", err.Error())
  204. }
  205. }
  206. }
  207. model.MakeCollection(in.UserId, r.Data.List)
  208. log.Println("接口耗时:", time.Since(t).Seconds())
  209. return r, nil
  210. }