newestbiddinglogic.go 7.6 KB

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