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