newestbiddinglogic.go 7.5 KB

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