newestbiddinglogic.go 7.9 KB

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