cache.go 8.1 KB

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