cache.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package cache
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/encrypt"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. )
  10. const (
  11. threeDay = 172800
  12. oneDay = 86400
  13. AllSubPushCacheSize = 200
  14. )
  15. type SubPushList struct {
  16. Id string `json:"_id"`
  17. Title string `json:"title"`
  18. Area string `json:"area"`
  19. BuyerClass string `json:"buyerClass"`
  20. Subtype string `json:"subtype"`
  21. Industry string `json:"industry"`
  22. PublishTime int64 `json:"publishTime"`
  23. Ca_index int64 `json:"ca_index"`
  24. Ca_date int64 `json:"ca_date"`
  25. Ca_isvisit int `json:"ca_isvisit"`
  26. Ca_isvip int `json:"ca_isvip"`
  27. Ca_type int `json:"ca_type"`
  28. Ca_fileExists bool `json:"ca_fileExists"`
  29. MatchKeys []string `json:"matchKeys"`
  30. Budget interface{} `json:"budget"`
  31. BidAmount interface{} `json:"bidAmount"`
  32. Collection int `json:"collection"`
  33. Buyer string `json:"buyer"`
  34. ProjectName string `json:"projectName"`
  35. Winner string `json:"winner"`
  36. BidOpenTime int64 `json:"bidOpenTime"`
  37. Source int64 `json:"source"`
  38. WinnerInfo []*WinnerInfo `json:"winnerInfo"`
  39. Site string `json:"site"`
  40. BuyerTel string `json:"buyerTel"`
  41. BuyerPerson string `json:"buyerPerson"`
  42. Agency string `json:"agency"`
  43. AgencyPerson string `json:"agencyPerson"`
  44. AgencyTel string `json:"agencyTel"`
  45. SignendTime int64 `json:"signendTime"`
  46. BidendTime int64 `json:"bidendTime"`
  47. }
  48. //
  49. type WinnerInfo struct {
  50. Winner string `json:"winner"`
  51. WinnerTel string `json:"winnerTel"`
  52. WinnerPerson string `json:"winnerPerson"`
  53. WinnerId string `json:"winnerId"`
  54. }
  55. type SubPush struct {
  56. Date string
  57. Datas []*SubPushList
  58. Count int64
  59. }
  60. type PushCa struct {
  61. Date int64
  62. InfoId string
  63. Visit int
  64. Keys []string
  65. Type int
  66. Isvip int
  67. FileExists bool
  68. Source int64
  69. Info *map[string]interface{}
  70. }
  71. type Cache struct {
  72. Prefix string
  73. UserId string
  74. Ymd string
  75. }
  76. func NewEntnicheCache(userId, ymd string) *Cache {
  77. return &Cache{
  78. Prefix: "entnichepush",
  79. UserId: userId,
  80. Ymd: ymd,
  81. }
  82. }
  83. func NewSubscribeCache(userId, ymd string) *Cache {
  84. return &Cache{
  85. Prefix: "subpush",
  86. UserId: userId,
  87. Ymd: ymd,
  88. }
  89. }
  90. func NewMemberCache(userId, ymd string) *Cache {
  91. return &Cache{
  92. Prefix: "memberpush",
  93. UserId: userId,
  94. Ymd: ymd,
  95. }
  96. }
  97. //
  98. func NewSubPushList(pushCas []*PushCa) []*SubPushList {
  99. subPushList := []*SubPushList{}
  100. for _, p := range pushCas {
  101. info := p.Info
  102. area := ObjToString((*info)["area"])
  103. if area == "A" {
  104. area = "全国"
  105. }
  106. industry := ObjToString((*info)["s_subscopeclass"])
  107. scs := strings.Split(industry, ",")
  108. if len(scs) > 0 {
  109. industry = scs[0]
  110. if industry != "" {
  111. iss := strings.Split(industry, "_")
  112. if len(iss) > 0 {
  113. industry = iss[0]
  114. }
  115. }
  116. }
  117. infotype := ObjToString((*info)["subtype"])
  118. if infotype == "" {
  119. infotype = ObjToString((*info)["toptype"])
  120. }
  121. _id := p.InfoId
  122. if _id == "" {
  123. _id = ObjToString((*info)["_id"])
  124. }
  125. winner := ObjToString((*info)["s_winner"])
  126. winnerInfo := []*WinnerInfo{}
  127. if winner != "" && len(strings.Split(winner, ",")) > 0 {
  128. for wk, wv := range strings.Split(winner, ",") {
  129. var winnerId = ""
  130. eil, _ := (*info)["entidlist"].([]interface{})
  131. if entIdList := ObjArrToStringArr(eil); len(entIdList) > wk { //中标企业id集合
  132. winnerId = entIdList[wk]
  133. }
  134. winnerInfo = append(winnerInfo, &WinnerInfo{
  135. Winner: wv, //中标企业 需要单独处理
  136. WinnerTel: ObjToString((*info)["winnertel"]), //中标企业联系电话
  137. WinnerPerson: ObjToString((*info)["winnerperson"]), //中标企业联系人
  138. WinnerId: If(winnerId != "" && len([]rune(winnerId)) > 12, EncodeArticleId2ByCheck(winnerId), "").(string), //中标企业加密id 存在winnerId 异常的情况
  139. })
  140. }
  141. }
  142. subPushList = append(subPushList, &SubPushList{
  143. Id: EncodeArticleId2ByCheck(_id),
  144. Title: ObjToString((*info)["title"]),
  145. Area: area,
  146. BuyerClass: ObjToString((*info)["buyerclass"]),
  147. Subtype: infotype,
  148. Industry: industry,
  149. PublishTime: Int64All((*info)["publishtime"]),
  150. Ca_date: p.Date,
  151. Ca_isvisit: p.Visit,
  152. Ca_isvip: p.Isvip,
  153. Ca_type: p.Type,
  154. Ca_fileExists: p.FileExists,
  155. MatchKeys: p.Keys,
  156. Budget: (*info)["budget"],
  157. BidAmount: (*info)["bidamount"],
  158. Buyer: ObjToString((*info)["buyer"]),
  159. ProjectName: ObjToString((*info)["projectname"]),
  160. Winner: winner,
  161. BidOpenTime: Int64All((*info)["bidopentime"]),
  162. Source: p.Source,
  163. Site: ObjToString((*info)["site"]),
  164. BuyerTel: ObjToString((*info)["buyerTel"]),
  165. BuyerPerson: ObjToString((*info)["buyerPerson"]),
  166. Agency: ObjToString((*info)["agency"]),
  167. AgencyPerson: ObjToString((*info)["agencyPerson"]),
  168. AgencyTel: ObjToString((*info)["agencyTel"]),
  169. SignendTime: Int64All((*info)["signendTime"]),
  170. BidendTime: Int64All((*info)["bidendTime"]),
  171. WinnerInfo: winnerInfo,
  172. })
  173. }
  174. return subPushList
  175. }
  176. //从pushcache_2_b中取
  177. func (c *Cache) GetTodayCache(userId string) (*SubPush, error) {
  178. pc_a, err := redis.GetNewBytes("pushcache_2_b", c.todayKey(userId))
  179. if err != nil {
  180. return nil, err
  181. }
  182. if pc_a == nil {
  183. return nil, nil
  184. }
  185. var p *SubPush
  186. if err := json.Unmarshal(*pc_a, &p); err != nil {
  187. return nil, err
  188. }
  189. return p, nil
  190. }
  191. //往pushcache_2_b中放
  192. func (c *Cache) PutTodayCache(userId string, pc_a *SubPush) {
  193. redis.Put("pushcache_2_b", c.todayKey(userId), pc_a, oneDay)
  194. }
  195. //获取redis key
  196. func (c *Cache) todayKey(userId string) string {
  197. return fmt.Sprintf("%s_%s", c.Prefix, userId)
  198. }
  199. func (c *Cache) allKey(userId string) string {
  200. return fmt.Sprintf("all_%s_%s", c.Prefix, userId)
  201. }
  202. //查看全部列表缓存
  203. func (c *Cache) PutAllCache(userId string, datas *SubPush) {
  204. redis.Put("pushcache_2_a", c.allKey(userId), datas, oneDay)
  205. }
  206. func (c *Cache) GetAllCache(userId string) (*SubPush, error) {
  207. return c.GetCache("pushcache_2_a", c.allKey(userId))
  208. }
  209. func (c *Cache) GetCache(code, key string) (*SubPush, error) {
  210. pc_a, err := redis.GetNewBytes(code, key)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if pc_a == nil {
  215. return nil, nil
  216. }
  217. var p *SubPush
  218. if err := json.Unmarshal(*pc_a, &p); err != nil {
  219. return nil, err
  220. }
  221. return p, nil
  222. }
  223. func (c *Cache) DoCache(subPushList []*SubPushList) {
  224. subPush_datas := []*SubPushList{}
  225. for i := len(subPushList) - 1; i >= 0; i-- {
  226. subPush_datas = append(subPush_datas, subPushList[i])
  227. }
  228. //更新redis
  229. subPush, err := c.GetTodayCache(c.UserId)
  230. if err == nil && subPush != nil && len(subPush.Datas) > 0 {
  231. if subPush.Date != c.Ymd {
  232. subPush = &SubPush{
  233. Date: c.Ymd,
  234. Datas: subPush_datas,
  235. Count: int64(len(subPush_datas)),
  236. }
  237. } else {
  238. subPush.Datas = append(subPush_datas, subPush.Datas...)
  239. subPush.Count = subPush.Count + int64(len(subPush_datas))
  240. }
  241. c.PutTodayCache(c.UserId, subPush)
  242. }
  243. //全部列表缓存
  244. allCache, err := c.GetAllCache(c.UserId)
  245. if err == nil && allCache != nil && len(allCache.Datas) > 0 {
  246. allCache.Datas = append(subPush_datas, allCache.Datas...)
  247. if len(allCache.Datas) > AllSubPushCacheSize {
  248. allCache.Datas = allCache.Datas[:AllSubPushCacheSize]
  249. }
  250. allCache.Count = allCache.Count + int64(len(subPush_datas))
  251. c.PutAllCache(c.UserId, allCache)
  252. }
  253. }