newestbiddinglogic.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, i := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, int) {
  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. //未登录用户查询当天缓存
  55. redisKey = fmt.Sprintf("%s_%d_%d_%d", redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
  56. }
  57. redisByte, err := redis.GetBytes("other", redisKey)
  58. if err == nil && redisByte != nil && len(*redisByte) > 0 {
  59. err = json.Unmarshal(*redisByte, res.Data)
  60. if err != nil {
  61. res.ErrCode = -1
  62. res.ErrMsg = fmt.Sprintf("读取缓存 序列化异常,err:%s", err.Error())
  63. }
  64. return res, 1
  65. }
  66. //登录用户
  67. if in.UserId != "" {
  68. //优先级 由测试确认 大会员 》 商机管理 》 VIP 》 普通用户 》 搜索历史
  69. //获取订阅信息
  70. 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}`)
  71. if !ok || userMap == nil || len(*userMap) == 0 {
  72. //查询出错
  73. res.ErrCode = -1
  74. res.ErrMsg = fmt.Errorf("未查询到用户信息").Error()
  75. return res, -1
  76. }
  77. //var isPayUser bool = false
  78. //付费用户如果没有数据 直接返回 需求来源:测试
  79. //vip用户
  80. vipStatus := MC.IntAll((*userMap)["i_vip_status"])
  81. //大会员用户
  82. bigStatus := MC.Int64All((*userMap)["i_member_status"])
  83. if bigStatus > 0 {
  84. o_msgset := MC.ObjToMap((*userMap)["o_member_jy"])
  85. big_items, ok := (*o_msgset)["a_items"].([]interface{})
  86. //大会员推送历史
  87. result := []*bxbase.NewestList{}
  88. if userType == "e" {
  89. result = model.GetNewestInfo(rks, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  90. } else {
  91. result = model.GetNewestInfo(rks, "m", in.NewUserId).GetPushHistory()
  92. }
  93. res.Data.Count = int64(len(result))
  94. if res.Data.Count > 0 {
  95. res.Data.HasSubKeys = ok && len(big_items) > 0
  96. res.Data.List = result
  97. res.Data.SubFlag = "m"
  98. }
  99. return res, 0
  100. }
  101. 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 != "" {
  102. //商机管理
  103. 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)
  104. if entNicheInfos != nil && len(*entNicheInfos) > 0 {
  105. entNicheInfo := (*entNicheInfos)[0]
  106. //排除商机管理服务
  107. if MC.IntAll(entNicheInfo["power_source"]) != 1 {
  108. // 已分发权限
  109. if MC.IntAll(entNicheInfo["power"]) > 0 {
  110. //商机管理推送历史
  111. result := model.GetNewestInfo(in.EntUserId, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  112. res.Data.Count = int64(len(result))
  113. if res.Data.Count > 0 {
  114. res.Data.List = result
  115. res.Data.SubFlag = "e"
  116. }
  117. return res, 0
  118. }
  119. }
  120. }
  121. }
  122. if vipStatus > 0 {
  123. o_msgset := MC.ObjToMap((*userMap)["o_vipjy"])
  124. vip_items, ok := (*o_msgset)["a_items"].([]interface{})
  125. //vip查询推送历史
  126. result := []*bxbase.NewestList{}
  127. if userType == "e" {
  128. result = model.GetNewestInfo(rks, "e", MC.Int64All(in.EntUserId)).GetPushHistory()
  129. } else {
  130. result = model.GetNewestInfo(rks, "v", in.NewUserId).GetPushHistory()
  131. }
  132. res.Data.IsVip = true
  133. res.Data.HasSubKeys = ok && len(vip_items) > 0
  134. res.Data.Count = int64(len(result))
  135. res.Data.List = result
  136. res.Data.SubFlag = "v"
  137. return res, 0
  138. }
  139. //普通用户用户- 有关键词
  140. o_msgset := MC.ObjToMap((*userMap)["o_jy"])
  141. items, ok := (*o_msgset)["a_key"].([]interface{})
  142. if ok && len(items) > 0 {
  143. //普通用户查询推送历史
  144. result := model.GetNewestInfo(rks, "f", in.NewUserId).GetPushHistory()
  145. res.Data.IsVip = false
  146. res.Data.HasSubKeys = ok && len(items) > 0
  147. res.Data.Count = int64(len(result))
  148. res.Data.List = result
  149. res.Data.SubFlag = "f"
  150. return res, 0
  151. }
  152. //搜索历史-关键词
  153. hKeys := redis.GetStr("other", fmt.Sprintf("s_%s", in.UserId))
  154. if hKeys != "" && len(strings.Split(hKeys, ",")) > 0 {
  155. //历史搜索
  156. res.Data.History = strings.Split(hKeys, ",")
  157. //根据订阅词获取查询语句
  158. query := model.NewestQuery("", hKeys)
  159. result := model.NewestES(query)
  160. res.Data.IsVip = false
  161. res.Data.HasSubKeys = false
  162. res.Data.Count = int64(len(result))
  163. res.Data.List = result
  164. return res, 0
  165. }
  166. }
  167. if in.IsSearch == 2 { //定位查询(默认全国)
  168. query := model.NewestQuery(rks, "")
  169. result := model.NewestES(query)
  170. res.Data.IsVip = false
  171. res.Data.HasSubKeys = false
  172. res.Data.Count = int64(len(result))
  173. res.Data.List = result
  174. return res, 0
  175. }
  176. return res, 0
  177. }(in)
  178. if r.Data.Count > 0 {
  179. //排序
  180. sort.Slice(r.Data.List, func(i, j int) bool {
  181. return r.Data.List[i].PublishTime > r.Data.List[j].PublishTime
  182. })
  183. //-1:程序异常 0 不存在缓存数据 1 存在缓存数据 新数据 存入缓存
  184. if i == 0 {
  185. rks := ""
  186. if userType == "e" {
  187. rks = MC.If(in.UserId != "", MC.InterfaceToStr(in.EntUserId), in.City).(string)
  188. } else {
  189. rks = MC.If(in.UserId != "", in.UserId, in.City).(string)
  190. }
  191. //rks := MC.If(in.UserId != "", in.UserId, in.City).(string)
  192. b, err := json.Marshal(r.Data)
  193. if err != nil {
  194. r.ErrCode = -1
  195. r.ErrMsg = fmt.Sprintf("保存缓存 序列化异常,err:%s", err.Error())
  196. } else {
  197. redisKey := "p1_indexMessage_new_" + rks
  198. timeOut := 2 * 60 * 60
  199. if in.UserId == "" {
  200. //未登录用户缓存一天
  201. timeOut = 24 * 60 * 60
  202. redisKey = fmt.Sprintf("%s_%d_%d_%d", redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
  203. }
  204. if err = redis.PutBytes("other", redisKey, &b, timeOut); err != nil {
  205. r.ErrCode = -1
  206. r.ErrMsg = fmt.Sprintf("保存缓存 redis 异常,err:%s", err.Error())
  207. }
  208. }
  209. }
  210. }
  211. model.MakeCollection(in.UserId, r.Data.List)
  212. log.Println("接口耗时:", time.Since(t).Seconds())
  213. return r, nil
  214. }