push.go 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. IC "jyBXSubscribe/rpc/init"
  6. ms "jyBXSubscribe/rpc/model/service"
  7. "jyBXSubscribe/rpc/type/bxsubscribe"
  8. "log"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "app.yhyue.com/moapp/jybase/common"
  14. "app.yhyue.com/moapp/jybase/date"
  15. "app.yhyue.com/moapp/jybase/encrypt"
  16. elastic "app.yhyue.com/moapp/jybase/es"
  17. P "app.yhyue.com/moapp/jybase/mapping"
  18. "app.yhyue.com/moapp/jybase/mongodb"
  19. "app.yhyue.com/moapp/jybase/mysql"
  20. "app.yhyue.com/moapp/jybase/redis"
  21. "github.com/zeromicro/go-zero/core/logx"
  22. "go.mongodb.org/mongo-driver/bson/primitive"
  23. )
  24. const (
  25. pageSize = 100
  26. AllSubPushCacheSize = 200
  27. query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area","city", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site","buyertel","buyerperson","agency","agencyperson","agencytel","winnerperson","winnertel","signendtime","bidendtime","entidlist","isValidFile"],"from":0,"size":%d}`
  28. mongodb_fields = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "city":1,"buyerclass":1,"budget":1,"bidamount":1,"s_winner":1,"bidopentime":1,"buyer":1,"projectname":1,"filetext":1,"spidercode":1,"site":1,"buyertel":1,"buyerperson":1,"agency":1,"agencyperson":1,"agencytel":1,"winnerperson":1,"winnertel":1,"signendtime":1,"bidendtime":1,"entidlist":1,"isValidFile":1}`
  29. SubFreeFlag = "fType"
  30. SubVipFlag = "vType"
  31. MemberFlag = "mType"
  32. EntnicheFlag = "eType"
  33. oneDay = 24 * 60 * 60
  34. )
  35. var (
  36. ennicheInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid", "entid", "deptid", "source", "product"}
  37. subscribeInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid", "isvip"}
  38. memberInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid"}
  39. )
  40. var aboutDbMsg map[string]*AboutDbMsg = map[string]*AboutDbMsg{
  41. SubFreeFlag: &AboutDbMsg{"push.pushsubscribe", "subpush"},
  42. SubVipFlag: &AboutDbMsg{"push.pushsubscribe", "subpush"},
  43. MemberFlag: &AboutDbMsg{"push.pushmember", "memberpush"},
  44. EntnicheFlag: &AboutDbMsg{"push.pushentniche", "entnichepush"},
  45. }
  46. type AboutDbMsg struct {
  47. MysqlTable string
  48. RedisKeyFlag string
  49. }
  50. type SubPush struct {
  51. Date string
  52. Datas []*bxsubscribe.SubscribeInfo
  53. Count int64
  54. }
  55. type PushCa struct {
  56. Date int64
  57. InfoId string
  58. Visit int
  59. Index int64
  60. Keys []string
  61. Type int
  62. Isvip int
  63. FileExists bool
  64. Source int64
  65. Title string
  66. }
  67. // 查询参数
  68. type SubPushQueryParam struct {
  69. Mgo_bidding mongodb.MongodbSim //
  70. Bidding string //
  71. Bidding_back string //
  72. UserId string //用户id
  73. PageNum int //页面
  74. PageSize int //每页数量
  75. SelectTime string //时间
  76. Area string //区域
  77. City string //城市
  78. Buyerclass string //采购单位行业
  79. Subtype string //信息类型 二级分类
  80. Subscopeclass string //信息行业
  81. Key string //订阅词
  82. Export bool //导出
  83. EntId string //企业id
  84. Price string //价格
  85. FileExists string //是否有附件;默认全部;1:有附件;-1:无附件
  86. EntUserId string //商机管理用户id
  87. DeptId string //商机管理用户部门id
  88. IsRead string //是否已读
  89. Source string //信息来源
  90. Staffs []string //分发人员
  91. BaseServiceMysql *mysql.Mysql
  92. NewUserId int64
  93. IsEnt bool
  94. SelectInfoIds []string
  95. BuySubject int64
  96. UserType string
  97. PositionType int64
  98. IsPayUser bool // 是否是付费用户
  99. NotReturnCount int64
  100. Item string
  101. }
  102. // 关键词参数
  103. type KeyParam struct {
  104. UserId string //用户id
  105. EntUserId string //商机管理用户id
  106. DeptId string //商机管理用户部门id
  107. NewUserId int64
  108. IsEnt bool
  109. VipPower int64
  110. EntId string //企业id
  111. MemberPower int64
  112. PowerSource int64
  113. UserPower int64
  114. PositionType int64
  115. }
  116. func (spqp *SubPushQueryParam) IsEmpty() bool {
  117. return (spqp.SelectTime == "" || spqp.SelectTime == "all") && spqp.Area == "" && spqp.City == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == "" && spqp.Item == "" && spqp.Price == "" && spqp.FileExists == "" && len(spqp.Staffs) == 0 && spqp.Source == "" && spqp.IsRead == ""
  118. }
  119. type subscribePush struct {
  120. ModuleFlag string
  121. }
  122. func NewSubscribePush(module ...string) *subscribePush {
  123. m := ""
  124. if len(module) > 0 {
  125. m = module[0]
  126. }
  127. return &subscribePush{m}
  128. }
  129. // 从pushcache_2_a中取
  130. func (s *subscribePush) GetTodayCache(userId, userType string) (*SubPush, error) {
  131. pc_a, err := redis.GetNewBytes("pushcache_2_b", s.todayKey(userId, userType))
  132. if err != nil {
  133. return nil, err
  134. }
  135. if pc_a == nil {
  136. return nil, nil
  137. }
  138. var p *SubPush
  139. if err := json.Unmarshal(*pc_a, &p); err != nil {
  140. return nil, err
  141. }
  142. return p, nil
  143. }
  144. // 往pushcache_2_a中放
  145. func (s *subscribePush) PutTodayCache(userId, userType string, pc_a *SubPush) {
  146. redis.Put("pushcache_2_b", s.todayKey(userId, userType), pc_a, oneDay)
  147. }
  148. // 获取redis key
  149. func (s *subscribePush) todayKey(userId, userType string) string {
  150. if s.ModuleFlag == EntnicheFlag {
  151. switch userType {
  152. case SubVipFlag:
  153. return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "vip", userId)
  154. case MemberFlag:
  155. return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "member", userId)
  156. case SubFreeFlag:
  157. return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "free", userId)
  158. }
  159. }
  160. return fmt.Sprintf("%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
  161. }
  162. func (s *subscribePush) allKey(userId, userType string) string {
  163. if s.ModuleFlag == EntnicheFlag {
  164. switch userType {
  165. case SubVipFlag:
  166. return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "vip", userId)
  167. case MemberFlag:
  168. return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "member", userId)
  169. case SubFreeFlag:
  170. return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "free", userId)
  171. }
  172. }
  173. return fmt.Sprintf("all_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
  174. }
  175. // 历史推送记录中单条信息格式化
  176. func (s *subscribePush) InfoFormat(p *PushCa, info *map[string]interface{}, isPay bool) *bxsubscribe.SubscribeInfo {
  177. area := common.ObjToString((*info)["area"])
  178. if area == "A" {
  179. area = "全国"
  180. }
  181. industry := common.ObjToString((*info)["s_subscopeclass"])
  182. scs := strings.Split(industry, ",")
  183. if len(scs) > 0 {
  184. industry = scs[0]
  185. if industry != "" {
  186. iss := strings.Split(industry, "_")
  187. if len(iss) > 0 {
  188. industry = iss[0]
  189. }
  190. }
  191. }
  192. infotype := common.ObjToString((*info)["subtype"])
  193. if infotype == "" {
  194. infotype = common.ObjToString((*info)["toptype"])
  195. }
  196. _id := p.InfoId
  197. if _id == "" {
  198. _id = common.ObjToString((*info)["_id"])
  199. }
  200. isValidFile, _ := (*info)["isValidFile"].(bool)
  201. formatInfo := &bxsubscribe.SubscribeInfo{
  202. XId: encrypt.EncodeArticleId2ByCheck(_id),
  203. Title: common.InterfaceToStr((*info)["title"]),
  204. Area: area,
  205. City: common.InterfaceToStr((*info)["city"]),
  206. BuyerClass: common.InterfaceToStr((*info)["buyerclass"]),
  207. Subtype: infotype,
  208. Industry: industry,
  209. PublishTime: common.Int64All((*info)["publishtime"]),
  210. CaIndex: p.Index,
  211. CaDate: p.Date,
  212. CaIsvisit: int64(p.Visit),
  213. CaIsvip: int64(p.Isvip),
  214. CaType: int64(p.Type),
  215. MatchKeys: p.Keys,
  216. Budget: common.Float64All((*info)["budget"]),
  217. BidAmount: common.Float64All((*info)["bidamount"]),
  218. Buyer: common.InterfaceToStr((*info)["buyer"]),
  219. ProjectName: common.InterfaceToStr((*info)["projectname"]),
  220. Winner: common.InterfaceToStr((*info)["s_winner"]),
  221. BidOpenTime: common.Int64All((*info)["bidopentime"]),
  222. CaFileExists: p.FileExists,
  223. Source: p.Source,
  224. Site: common.InterfaceToStr((*info)["site"]),
  225. SpiderCode: common.InterfaceToStr((*info)["spidercode"]),
  226. Toptype: common.InterfaceToStr((*info)["toptype"]),
  227. IsValidFile: isValidFile,
  228. }
  229. // 免费用户返回精简列表字段
  230. if !isPay {
  231. return formatInfo
  232. }
  233. winnerList := common.InterfaceToStr((*info)["s_winner"]) //中标企业名称集合
  234. if winnerList != "" && len(strings.Split(winnerList, ",")) > 0 {
  235. for wk, wv := range strings.Split(winnerList, ",") {
  236. var (
  237. winnerId = ""
  238. )
  239. if (*info)["entidlist"] != nil {
  240. if entIdList := common.ObjArrToStringArr((*info)["entidlist"].([]interface{})); len(entIdList) > wk { //中标企业id集合
  241. winnerId = entIdList[wk]
  242. }
  243. }
  244. formatInfo.WinnerInfo = append(formatInfo.WinnerInfo, &bxsubscribe.WinnerInfo{
  245. Winner: wv, //中标企业 需要单独处理
  246. WinnerTel: common.ObjToString((*info)["winnertel"]), //中标企业联系电话
  247. WinnerPerson: common.ObjToString((*info)["winnerperson"]), //中标企业联系人
  248. WinnerId: common.If(winnerId != "" && len([]rune(winnerId)) > 12, encrypt.EncodeArticleId2ByCheck(winnerId), "").(string), //中标企业加密id 存在winnerId 异常的情况
  249. })
  250. }
  251. }
  252. // 付费用户返回详细列表字段
  253. formatInfo.BuyerTel = common.ObjToString((*info)["buyertel"])
  254. formatInfo.BuyerPerson = common.ObjToString((*info)["buyerperson"])
  255. formatInfo.Agency = common.ObjToString((*info)["agency"])
  256. formatInfo.AgencyPerson = common.ObjToString((*info)["agencyperson"])
  257. formatInfo.AgencyTel = common.ObjToString((*info)["agencytel"])
  258. formatInfo.SignendTime = common.Int64All((*info)["signendtime"])
  259. formatInfo.BidendTime = common.Int64All((*info)["bidendtime"])
  260. return formatInfo
  261. }
  262. func (s *subscribePush) Datas(spqp *SubPushQueryParam) (hasNextPage bool, total int64, result []*bxsubscribe.SubscribeInfo) {
  263. logx.Info(spqp.UserId, spqp.NewUserId, s.ModuleFlag, "subscribePush query param:", "SelectTime:", spqp.SelectTime, "Area:", spqp.Area, "City:", spqp.City, "Subtype:", spqp.Subtype, "Subscopeclass:", spqp.Subscopeclass, "Buyerclass:", spqp.Buyerclass, "Key:", spqp.Key, "PageNum:", spqp.PageNum, "Price:", spqp.Price, "FileExists:", spqp.FileExists)
  264. if spqp.UserId == "" {
  265. return
  266. }
  267. if spqp.PageNum < 1 {
  268. spqp.PageNum = 1
  269. }
  270. if spqp.PageSize < 1 || spqp.PageSize > pageSize {
  271. if !spqp.Export {
  272. spqp.PageSize = pageSize
  273. } else { //数据导出查询20000条限制
  274. if spqp.PageSize < 1 || spqp.PageSize > 20000 {
  275. spqp.PageSize = 20000
  276. }
  277. }
  278. }
  279. starttime, endtime := int64(0), int64(0)
  280. if len(strings.Split(spqp.SelectTime, "_")) == 2 {
  281. st := strings.Split(spqp.SelectTime, "_")[0]
  282. et := strings.Split(spqp.SelectTime, "_")[1]
  283. if st != "" && et != "" {
  284. starttime, _ = strconv.ParseInt(st, 0, 64)
  285. endtime, _ = strconv.ParseInt(et, 0, 64)
  286. if endtime > 0 {
  287. etTime := time.Unix(endtime, 0)
  288. endtime = time.Date(etTime.Year(), etTime.Month(), etTime.Day(), 23, 59, 59, 0, time.Local).Unix()
  289. }
  290. }
  291. }
  292. nowFormat := date.NowFormat(date.Date_Short_Layout)
  293. start := (spqp.PageNum - 1) * spqp.PageSize
  294. end := start + spqp.PageSize
  295. //时间是今天,没有别的过滤条件--首先读取当前推送缓存数据 其次查询数据库
  296. if nowFormat == date.FormatDateByInt64(&starttime, date.Date_Short_Layout) && spqp.Area == "" && spqp.City == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == "" && spqp.Item == "" && spqp.Price == "" && spqp.FileExists == "" && len(spqp.Staffs) == 0 && spqp.Source == "" && spqp.IsRead == "" && len(spqp.SelectInfoIds) == 0 {
  297. subPush := &SubPush{}
  298. var err error
  299. if spqp.IsEnt == false {
  300. subPush, err = s.GetTodayCache(spqp.UserId, spqp.UserType)
  301. }
  302. if err != nil {
  303. logx.Info(spqp.UserId, "GetTodayCache Error", err)
  304. }
  305. if err != nil || subPush == nil || subPush.Date != nowFormat || len(subPush.Datas) == 0 {
  306. list, countSearch := s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, false)
  307. subPush = &SubPush{
  308. Date: nowFormat,
  309. Datas: list,
  310. Count: countSearch,
  311. }
  312. if spqp.IsEnt == false && spqp.NotReturnCount == 0 {
  313. s.PutTodayCache(spqp.UserId, spqp.UserType, subPush)
  314. }
  315. }
  316. length := len(subPush.Datas)
  317. if end > length {
  318. end = length
  319. }
  320. if start < length {
  321. result = subPush.Datas[start:end]
  322. } else {
  323. result = subPush.Datas
  324. }
  325. total = int64(length)
  326. //全部,没有过滤条件 之前缓存500
  327. } else if spqp.IsEmpty() && (spqp.PageNum-1)*spqp.PageSize < 200 && len(spqp.SelectInfoIds) == 0 && !spqp.Export { //全部,没有过滤条件 之前缓存5页*50条=250 非数据导出
  328. allCache := &SubPush{}
  329. var err error
  330. if !spqp.IsEnt {
  331. allCache, err = s.GetAllCache(spqp.UserId, spqp.UserType)
  332. }
  333. if err != nil {
  334. logx.Info(spqp.UserId, "GetAllCache Error", err)
  335. }
  336. if err != nil || allCache == nil || allCache.Date != nowFormat || len(allCache.Datas) == 0 {
  337. list, countSearch := []*bxsubscribe.SubscribeInfo{}, int64(0)
  338. if spqp.IsEnt {
  339. list, countSearch = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true)
  340. } else {
  341. spqp.PageNum = 1
  342. list, countSearch = s.getDatasFromMysql(spqp, starttime, endtime, AllSubPushCacheSize, true)
  343. }
  344. allCache = &SubPush{
  345. Date: nowFormat,
  346. Datas: list,
  347. Count: countSearch,
  348. }
  349. if spqp.IsEnt == false && spqp.NotReturnCount == 0 {
  350. s.PutAllCache(spqp.UserId, spqp.UserType, allCache)
  351. }
  352. }
  353. length := len(allCache.Datas)
  354. if end > length {
  355. end = length
  356. }
  357. if start < length {
  358. result = allCache.Datas[start:end]
  359. } else {
  360. result = allCache.Datas
  361. }
  362. total = allCache.Count
  363. } else {
  364. result, total = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true)
  365. }
  366. if result == nil {
  367. result = []*bxsubscribe.SubscribeInfo{}
  368. }
  369. hasNextPage = len(result) >= spqp.PageSize
  370. return
  371. }
  372. func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, endtime int64, size int, isLimit bool) (result []*bxsubscribe.SubscribeInfo, count int64) {
  373. start := time.Now().Unix()
  374. querys := []string{}
  375. userStr := " 1=1 "
  376. var (
  377. countSql, findSql string
  378. )
  379. querys = append(querys, " ")
  380. if spqp.SelectInfoIds != nil && len(spqp.SelectInfoIds) > 0 {
  381. countSql = fmt.Sprintf("select count(1) as count from %s a where %s", aboutDbMsg[s.ModuleFlag].MysqlTable, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
  382. logx.Info("countSql", countSql)
  383. findSql = fmt.Sprintf("select a.id,a.date,a.infoid,a.isvisit,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.type,a.attachment_count from %s a where %s order by a.date desc,a.id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, fmt.Sprintf("a.id in ('%s')", strings.Join(spqp.SelectInfoIds, "','")))
  384. //查询总数
  385. count = spqp.BaseServiceMysql.CountBySql(countSql)
  386. } else {
  387. //公共查询条件
  388. //时间
  389. if starttime > 0 && endtime > 0 {
  390. querys = append(querys, fmt.Sprintf(" a.date>=%d and a.date<=%d", starttime, endtime))
  391. } else if starttime > 0 && endtime == 0 {
  392. querys = append(querys, fmt.Sprintf(" a.date>=%d", starttime))
  393. } else if starttime == 0 && endtime > 0 {
  394. querys = append(querys, fmt.Sprintf(" a.date<=%d", endtime))
  395. }
  396. if spqp.Area != "" || spqp.City != "" {
  397. var sqlAreaCity = ""
  398. //城市
  399. city := []string{}
  400. for _, v := range strings.Split(spqp.City, ",") {
  401. if P.BidCodeMapping.City[v] != "" {
  402. city = append(city, fmt.Sprint(P.BidCodeMapping.City[v]))
  403. } else {
  404. city = append(city, "-1")
  405. }
  406. }
  407. if len(city) == 1 {
  408. city = append(city, "9999")
  409. }
  410. if len(city) > 0 {
  411. sqlAreaCity = fmt.Sprintf("city in (%s)", strings.Join(city, ","))
  412. }
  413. //区域
  414. var sqlArea = ""
  415. area := []string{}
  416. for _, v := range strings.Split(spqp.Area, ",") {
  417. if P.BidCodeMapping.Area[v] != "" {
  418. area = append(area, fmt.Sprint(P.BidCodeMapping.Area[v]))
  419. } else {
  420. area = append(area, "-1")
  421. }
  422. }
  423. if len(area) == 1 {
  424. area = append(area, "9999")
  425. }
  426. if len(area) > 0 {
  427. sqlArea = fmt.Sprintf("a.area in (%s)", strings.Join(area, ","))
  428. }
  429. if sqlAreaCity != "" && sqlArea != "" {
  430. sqlAreaCity = "( " + sqlAreaCity + " or " + sqlArea + " )"
  431. } else if sqlAreaCity == "" && sqlArea != "" {
  432. sqlAreaCity = sqlArea
  433. }
  434. if sqlAreaCity != "" {
  435. querys = append(querys, sqlAreaCity)
  436. }
  437. }
  438. //采购单位行业
  439. if spqp.Buyerclass != "" {
  440. buyerclass := []string{}
  441. for _, v := range strings.Split(spqp.Buyerclass, ",") {
  442. if fmt.Sprint(P.BidCodeMapping.Buyerclass[v]) != "" {
  443. buyerclass = append(buyerclass, fmt.Sprint(P.BidCodeMapping.Buyerclass[v]))
  444. }
  445. }
  446. if len(buyerclass) == 1 {
  447. buyerclass = append(buyerclass, "9999")
  448. }
  449. if len(buyerclass) > 0 {
  450. querys = append(querys, fmt.Sprintf("a.buyerclass in (%s)", strings.Join(buyerclass, ",")))
  451. }
  452. }
  453. //信息类型
  454. if spqp.Subtype != "" {
  455. subtype := []string{}
  456. for _, v := range strings.Split(spqp.Subtype, ",") {
  457. subtype = append(subtype, P.BidCodeMapping.Subtype[v])
  458. }
  459. if len(subtype) == 1 {
  460. subtype = append(subtype, "9999")
  461. }
  462. if len(subtype) > 0 {
  463. querys = append(querys, fmt.Sprintf("subtype in (%s)", strings.Join(subtype, ",")))
  464. }
  465. }
  466. //信息行业
  467. if spqp.Subscopeclass != "" {
  468. find_in_set := []string{}
  469. for _, v := range strings.Split(spqp.Subscopeclass, ",") {
  470. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',a.subscopeclass)", P.BidCodeMapping.Subscopeclass[v]))
  471. }
  472. if len(find_in_set) == 1 {
  473. querys = append(querys, find_in_set[0])
  474. } else if len(find_in_set) > 1 {
  475. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
  476. }
  477. }
  478. //关键词
  479. find_in_set := []string{}
  480. if spqp.IsEnt {
  481. if spqp.Key != "" {
  482. for _, v := range strings.Split(spqp.Key, ",") {
  483. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
  484. }
  485. }
  486. if spqp.Item != "" {
  487. for _, v := range strings.Split(spqp.Item, ",") {
  488. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchitems,' ',','))", v))
  489. }
  490. }
  491. } else {
  492. if spqp.Key != "" {
  493. for _, v := range strings.Split(spqp.Key, ",") {
  494. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
  495. }
  496. }
  497. if spqp.Item != "" {
  498. //通过分类名搜索
  499. keyArr := s.ItemToKey(spqp)
  500. for _, v := range strings.Split(keyArr, ",") {
  501. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
  502. }
  503. }
  504. }
  505. if len(find_in_set) == 1 {
  506. querys = append(querys, find_in_set[0])
  507. } else if len(find_in_set) > 1 {
  508. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
  509. }
  510. //价格- 预算和中标金额
  511. if spqp.Price != "" && strings.Contains(spqp.Price, "-") {
  512. minPriceStr, maxPriceStr := strings.Split(spqp.Price, "-")[0], strings.Split(spqp.Price, "-")[1]
  513. minPrice := common.Int64All(common.Float64All(minPriceStr) * 10000) //换成元
  514. maxPrice := common.Int64All(common.Float64All(maxPriceStr) * 10000) //换成元
  515. if minPriceStr != "" && maxPriceStr != "" {
  516. querys = append(querys, fmt.Sprintf("((a.bidamount>=%d and a.bidamount<=%d) or (a.budget>=%d and a.budget<=%d and a.bidamount is null))", minPrice, maxPrice, minPrice, maxPrice))
  517. } else if minPriceStr != "" {
  518. querys = append(querys, fmt.Sprintf("(a.bidamount>=%d or (a.budget>=%d and a.bidamount is null))", minPrice, minPrice))
  519. } else if maxPriceStr != "" {
  520. querys = append(querys, fmt.Sprintf("(a.bidamount<=%d or (a.budget<=%d and a.bidamount is null))", maxPrice, maxPrice))
  521. }
  522. }
  523. //附件
  524. if spqp.FileExists != "" {
  525. if spqp.FileExists == "1" {
  526. querys = append(querys, fmt.Sprintf("a.attachment_count is not null"))
  527. } else if spqp.FileExists == "-1" {
  528. querys = append(querys, fmt.Sprintf("a.attachment_count is null"))
  529. }
  530. }
  531. // 是否已读
  532. if spqp.IsRead != "" {
  533. if spqp.IsRead == "0" {
  534. querys = append(querys, " a.isvisit IS NULL")
  535. } else if spqp.IsRead == "1" {
  536. querys = append(querys, " a.isvisit=1")
  537. }
  538. }
  539. // 信息来源
  540. if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
  541. querys = append(querys, " a.source="+spqp.Source)
  542. }
  543. if spqp.IsEnt {
  544. var staffs []string
  545. userStr += fmt.Sprintf(" and a.entid='%s' ", spqp.EntId)
  546. //判断是企业管理员还是部门管理员 部门管理员获取所有子部门
  547. userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
  548. if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
  549. result = []*bxsubscribe.SubscribeInfo{}
  550. return
  551. }
  552. if userEnt.Role_admin_department { //
  553. // 部门管理员 获取所有部门和子部门员工
  554. users := GetDisUsers(common.IntAll(spqp.EntId), userEnt.Dept.Id)
  555. if users != nil && len(*users) > 0 {
  556. for _, v := range *users {
  557. if len(spqp.Staffs) > 0 {
  558. for _, vv := range spqp.Staffs {
  559. if fmt.Sprintf("%d", v.Id) == vv {
  560. staffs = append(staffs, common.InterfaceToStr(v.Id))
  561. }
  562. }
  563. } else {
  564. staffs = append(staffs, common.InterfaceToStr(v.Id))
  565. }
  566. }
  567. } else {
  568. log.Printf("部门管理员为获取到部门员工:%s", spqp.EntUserId)
  569. result = []*bxsubscribe.SubscribeInfo{}
  570. return
  571. }
  572. } else if len(spqp.Staffs) > 0 {
  573. staffs = spqp.Staffs
  574. }
  575. // 无查询分配人员、是否已读
  576. if spqp.IsRead == "" && len(staffs) == 0 {
  577. //查询数量(需要去重)
  578. countSql = fmt.Sprintf("select count(1) from %s a where %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
  579. //列表查询
  580. findSql = fmt.Sprintf("select a.id,a.infoid,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.attachment_count, IF(a.source is NULL,1,a.source) as source from %s a where %s %s order by a.date desc,a.id desc",
  581. aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
  582. } else {
  583. //查询分配人员或是否已读
  584. staffQuery := " and 1=1 "
  585. if len(spqp.Staffs) > 0 {
  586. staffQuery += fmt.Sprintf(" and a.userid in ('%s') ", strings.Join(staffs, "','"))
  587. } else if len(staffs) > 0 {
  588. staffQuery += fmt.Sprintf(" and a.userid in (0,'%s') ", strings.Join(staffs, "','"))
  589. }
  590. countSql = fmt.Sprintf("select count(1) from %s a where %s %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "), staffQuery)
  591. //列表查询
  592. findSql = fmt.Sprintf("select a.id,a.infoid,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.attachment_count,IF(a.source is NULL,1,a.source) as source from %s a where %s %s %s order by a.date desc,a.id desc ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "), staffQuery)
  593. }
  594. } else {
  595. // 是否已读
  596. if spqp.IsRead != "" {
  597. if spqp.IsRead == "0" {
  598. querys = append(querys, " a.isvisit IS NULL")
  599. } else if spqp.IsRead == "1" {
  600. querys = append(querys, " a.isvisit=1")
  601. }
  602. }
  603. // 信息来源
  604. if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
  605. querys = append(querys, " a.source="+spqp.Source)
  606. }
  607. if spqp.PositionType == 1 {
  608. //企业主体是企业的个人查询
  609. if spqp.UserType == SubVipFlag {
  610. userStr += fmt.Sprintf(" and a.userid='%s' and ( a.product=1 or a.product=3 or a.source>1 ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  611. } else if spqp.UserType == MemberFlag {
  612. userStr += fmt.Sprintf(" and a.userid='%s' and ( a.product=2 or a.product=3 or a.source>1 ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  613. } else if spqp.UserType == EntnicheFlag {
  614. userStr += fmt.Sprintf(" and a.userid='%s' and ( a.product is null or a.product=3 or a.source>1 ) ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  615. } else {
  616. userStr += fmt.Sprintf(" and a.userid='%s' ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  617. }
  618. } else {
  619. if s.ModuleFlag == EntnicheFlag {
  620. //老商机管理个人订阅查询
  621. userStr += fmt.Sprintf(" and a.userid='%s' and a.product is null", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  622. } else {
  623. //个人订阅查询
  624. userStr += fmt.Sprintf(" and a.userid='%s'", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
  625. }
  626. }
  627. //查询数量
  628. countSql = fmt.Sprintf("select count(1) as count from %s a where %s %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
  629. //列表查询语句
  630. findSql = "select a.id,a.date,a.infoid,a.isvisit,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.type,a.attachment_count"
  631. if s.ModuleFlag == EntnicheFlag {
  632. findSql += ", IF(a.source is NULL,1,a.source) as source "
  633. }
  634. if s.ModuleFlag != MemberFlag && s.ModuleFlag != EntnicheFlag {
  635. findSql += ",a.isvip"
  636. }
  637. findSql = fmt.Sprintf("%s from %s a where %s %s "+
  638. " order by a.date desc,a.id desc", findSql, aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
  639. }
  640. //查询总数
  641. logx.Info("countSql", countSql)
  642. if spqp.NotReturnCount == 0 {
  643. count = spqp.BaseServiceMysql.CountBySql(countSql)
  644. }
  645. log.Println("数据总数查询", count)
  646. }
  647. if isLimit {
  648. findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
  649. }
  650. logx.Info(spqp.NewUserId, "subscribePush query sql:", findSql)
  651. list := spqp.BaseServiceMysql.SelectBySql(findSql)
  652. end1 := time.Now().Unix()
  653. log.Println("mysql查询用时:", end1-start)
  654. if list != nil && len(*list) > 0 {
  655. pushCas := s.GetJyPushs(*list)
  656. if !spqp.Export {
  657. result = s.GetInfoByIds(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, pushCas, spqp.IsPayUser)
  658. } else {
  659. result = s.GetOnlyExportInfo(pushCas)
  660. }
  661. } else {
  662. result = []*bxsubscribe.SubscribeInfo{}
  663. }
  664. end2 := time.Now().Unix()
  665. log.Println("处理数据用时:", end2-end1)
  666. log.Println("总用时用时:", end2-start)
  667. return
  668. }
  669. // GetOnlyInfoId 获取信息id
  670. func (s *subscribePush) GetOnlyExportInfo(pushCas []*PushCa) []*bxsubscribe.SubscribeInfo {
  671. array := make([]*bxsubscribe.SubscribeInfo, len(pushCas))
  672. for k, v := range pushCas {
  673. array[k] = &bxsubscribe.SubscribeInfo{XId: encrypt.EncodeArticleId2ByCheck(v.InfoId), MatchKeys: v.Keys}
  674. }
  675. return array
  676. }
  677. // 根据id取内容
  678. func (s *subscribePush) GetInfoByIds(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, pushCas []*PushCa, isPay bool) []*bxsubscribe.SubscribeInfo {
  679. array := make([]*bxsubscribe.SubscribeInfo, len(pushCas))
  680. if len(pushCas) == 0 {
  681. return array
  682. }
  683. m := map[string]bool{}
  684. ids := []string{}
  685. for _, v := range pushCas {
  686. if m[v.InfoId] {
  687. continue
  688. }
  689. m[v.InfoId] = true
  690. ids = append(ids, v.InfoId)
  691. }
  692. infos := map[string]map[string]interface{}{}
  693. //redis
  694. pool := make(chan bool, 3)
  695. wait := &sync.WaitGroup{}
  696. lock := &sync.Mutex{}
  697. es_ids := []string{}
  698. for _, vv := range ids {
  699. pool <- true
  700. wait.Add(1)
  701. go func(v string) {
  702. defer func() {
  703. <-pool
  704. wait.Done()
  705. }()
  706. //剑鱼程序未找到赋值 位置;猜测在推送程序中
  707. info_i := redis.Get("pushcache_1", fmt.Sprintf("info_%s", v))
  708. lock.Lock()
  709. defer lock.Unlock()
  710. if info_i != nil {
  711. info_m, _ := info_i.(map[string]interface{})
  712. info_m["_id"] = v
  713. infos[v] = info_m
  714. } else {
  715. es_ids = append(es_ids, v)
  716. }
  717. }(vv)
  718. }
  719. wait.Wait()
  720. //elasticsearch
  721. if len(es_ids) > 0 {
  722. list := elastic.Get("bidding", "bidding", fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
  723. if list != nil {
  724. for _, v := range *list {
  725. _id := common.ObjToString(v["_id"])
  726. infos[_id] = v
  727. }
  728. }
  729. }
  730. //mongodb bidding
  731. mgo_ids := []primitive.ObjectID{}
  732. for _, v := range es_ids {
  733. if infos[v] == nil {
  734. _id, _ := primitive.ObjectIDFromHex(v)
  735. mgo_ids = append(mgo_ids, _id)
  736. }
  737. }
  738. if len(mgo_ids) > 0 {
  739. list, ok := Mgo_bidding.Find(bidding, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
  740. if ok && *list != nil {
  741. for _, v := range *list {
  742. _id := mongodb.BsonIdToSId(v["_id"])
  743. v["_id"] = _id
  744. infos[_id] = v
  745. }
  746. }
  747. }
  748. //mongodb bidding_back
  749. mgo_back_ids := []primitive.ObjectID{}
  750. for _, v := range mgo_ids {
  751. if infos[mongodb.BsonIdToSId(v)] == nil {
  752. mgo_back_ids = append(mgo_back_ids, v)
  753. }
  754. }
  755. if len(mgo_back_ids) > 0 {
  756. list, ok := Mgo_bidding.Find(bidding_back, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
  757. if ok && *list != nil {
  758. for _, v := range *list {
  759. _id := mongodb.BsonIdToSId(v["_id"])
  760. v["_id"] = _id
  761. infos[_id] = v
  762. }
  763. }
  764. }
  765. for k, v := range pushCas {
  766. info := infos[v.InfoId]
  767. if info == nil {
  768. info = map[string]interface{}{}
  769. }
  770. array[k] = s.InfoFormat(v, &info, isPay)
  771. }
  772. return array
  773. }
  774. // 获取历史推送
  775. func (s *subscribePush) GetJyPushs(datas []map[string]interface{}) (pushCas []*PushCa) {
  776. pushCas = []*PushCa{}
  777. for _, v := range datas {
  778. keys := []string{}
  779. if matchkeys := common.ObjToString(v["matchkeys"]); matchkeys != "" {
  780. keys = strings.Split(matchkeys, " ")
  781. }
  782. pushCas = append(pushCas, &PushCa{
  783. Date: common.Int64All(v["date"]),
  784. InfoId: common.ObjToString(v["infoid"]),
  785. Visit: common.IntAll(v["isvisit"]),
  786. Index: common.Int64All(v["id"]),
  787. Keys: keys,
  788. Type: common.IntAll(v["type"]),
  789. Isvip: common.IntAll(v["isvip"]),
  790. FileExists: common.IntAll(v["attachment_count"]) > 0,
  791. Source: common.Int64All(v["source"]),
  792. })
  793. }
  794. return
  795. }
  796. // 查看全部列表缓存
  797. func (s *subscribePush) PutAllCache(userId, userType string, datas *SubPush) {
  798. log.Println("pushcache_2_a", s.allKey(userId, userType), datas, oneDay)
  799. redis.Put("pushcache_2_a", s.allKey(userId, userType), datas, oneDay)
  800. }
  801. func (s *subscribePush) GetAllCache(userId, userType string) (*SubPush, error) {
  802. return s.GetCache("pushcache_2_a", s.allKey(userId, userType))
  803. }
  804. func (s *subscribePush) GetCache(code, key string) (*SubPush, error) {
  805. pc_a, err := redis.GetNewBytes(code, key)
  806. if err != nil {
  807. return nil, err
  808. }
  809. if pc_a == nil {
  810. return nil, nil
  811. }
  812. var p *SubPush
  813. if err := json.Unmarshal(*pc_a, &p); err != nil {
  814. return nil, err
  815. }
  816. return p, nil
  817. }
  818. // 是否收藏
  819. func (s *subscribePush) MakeCollection(userId string, list []*bxsubscribe.SubscribeInfo) {
  820. if list == nil || len(list) == 0 {
  821. return
  822. }
  823. param := []interface{}{userId}
  824. wh := []string{}
  825. for _, v := range list {
  826. array := encrypt.DecodeArticleId2ByCheck(v.XId)
  827. if len(array) == 1 && array[0] != "" {
  828. param = append(param, array[0])
  829. wh = append(wh, "?")
  830. }
  831. }
  832. if len(wh) > 0 {
  833. result := IC.MainMysql.SelectBySql(`select bid from bdcollection where userid=? and bid in (`+strings.Join(wh, ",")+`)`, param...)
  834. bid_map := map[string]bool{}
  835. if result != nil {
  836. for _, v := range *result {
  837. bid_map[encrypt.EncodeArticleId2ByCheck(common.ObjToString(v["bid"]))] = true
  838. }
  839. }
  840. for _, v := range list {
  841. if bid_map[v.XId] {
  842. v.Collection = 1
  843. }
  844. }
  845. }
  846. }
  847. // 仅移动端首页使用,历史推送7天信息
  848. func (s *subscribePush) sevenDayKey(userId string) string {
  849. return fmt.Sprintf("7day_subpush_%s", userId)
  850. }
  851. func (s *subscribePush) PutSevenDayCache(userId string, datas []*bxsubscribe.SubscribeInfo) {
  852. redis.Put("pushcache_2_a", s.sevenDayKey(userId), SubPush{Datas: datas}, 7*24*60*60)
  853. }
  854. // 从pushcache_2_a中取
  855. func (s *subscribePush) GetSevenDayCache(userId string) ([]*bxsubscribe.SubscribeInfo, error) {
  856. allPush, _ := s.GetCache("pushcache_2_a", s.sevenDayKey(userId))
  857. if allPush != nil && allPush.Datas != nil && len(allPush.Datas) > 0 {
  858. return allPush.Datas, nil
  859. }
  860. return nil, nil
  861. }
  862. // 历史推送记录中单条信息格式化
  863. func InfoFormats(info map[string]interface{}, tmp map[string]interface{}) map[string]interface{} {
  864. area := common.ObjToString(info["area"])
  865. if area == "A" {
  866. area = "全国"
  867. }
  868. industry := common.ObjToString(info["s_subscopeclass"])
  869. scs := strings.Split(industry, ",")
  870. if len(scs) > 0 {
  871. industry = scs[0]
  872. if industry != "" {
  873. iss := strings.Split(industry, "_")
  874. if len(iss) > 0 {
  875. industry = iss[0]
  876. }
  877. }
  878. }
  879. infotype := common.ObjToString(info["subtype"])
  880. if infotype == "" {
  881. infotype = common.ObjToString(info["toptype"])
  882. }
  883. if infotype == "" {
  884. infotype = common.ObjToString(info["type"])
  885. if infotype == "tender" {
  886. infotype = "招标"
  887. } else if infotype == "bid" {
  888. infotype = "中标"
  889. }
  890. }
  891. info["type"] = infotype
  892. return info
  893. }
  894. // UpdateUserPushUnread 更新app未读标识为已读
  895. func UpdateUserPushUnread(userid string, vt string) {
  896. if vt == MemberFlag {
  897. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_member_apppushunread": 0}})
  898. } else if vt == EntnicheFlag {
  899. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_entniche_apppushunread": 0}})
  900. } else {
  901. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_apppushunread": 0}})
  902. }
  903. }
  904. func (s *subscribePush) SetRead(newUserId, id int64, userId, entUserId, entId string, isEnt bool, userType string) error {
  905. if id <= 0 && newUserId == 0 {
  906. return nil
  907. }
  908. if newUserId == 0 {
  909. return nil
  910. }
  911. IC.BaseServiceMysql.UpdateOrDeleteBySql(fmt.Sprintf("update %s set isvisit=1,visittime=now() where userid=? and id=?", aboutDbMsg[s.ModuleFlag].MysqlTable), common.If(s.ModuleFlag == EntnicheFlag, entUserId, newUserId), id)
  912. if s.ModuleFlag == EntnicheFlag {
  913. userId = entUserId
  914. }
  915. todaySubPush, err := s.GetTodayCache(userId, userType)
  916. //当天数据处理
  917. if err == nil && todaySubPush != nil {
  918. for _, v := range todaySubPush.Datas {
  919. if v.CaIndex == id {
  920. v.CaIsvisit = 1
  921. break
  922. }
  923. }
  924. s.PutTodayCache(userId, userType, todaySubPush)
  925. }
  926. //全部数据处理
  927. allSubPush, err := s.GetAllCache(userId, userType)
  928. if err == nil && allSubPush != nil {
  929. for _, v := range allSubPush.Datas {
  930. if v.CaIndex == id {
  931. v.CaIsvisit = 1
  932. break
  933. }
  934. }
  935. s.PutAllCache(userId, userType, allSubPush)
  936. }
  937. return nil
  938. }
  939. // UserInfo 获取用户信息
  940. func (s *subscribePush) UserInfo(baseUserId int64) (*map[string]interface{}, int64) {
  941. user, ok := IC.Mgo.FindOneByField("user", map[string]interface{}{"base_user_id": baseUserId}, `{"i_ts_guide":1,"s_m_openid":1,"a_m_openid":1,"s_phone":1,"a_mergeorder":1,"o_jy":1,"l_firstpushtime":1,"i_vip_status":1,"l_vip_endtime":1,"o_vipjy":1,"i_member_status":1,"o_member_jy":1}`)
  942. if !ok || user == nil {
  943. return nil, 0
  944. }
  945. return user, common.Int64All((*user)["l_firstpushtime"])
  946. }
  947. // GetKeySet 是否有订阅词
  948. func GetKeySet(t string, u *map[string]interface{}, data []string) (bool, []string) {
  949. var industry_ = []string{}
  950. if u != nil {
  951. if t == SubFreeFlag {
  952. o_jy, _ := (*u)["o_jy"].(map[string]interface{})
  953. a_key, _ := o_jy["a_key"].([]interface{})
  954. return len(a_key) > 0, industry_
  955. } else {
  956. var obj map[string]interface{}
  957. if t == SubVipFlag {
  958. obj, _ = (*u)["o_vipjy"].(map[string]interface{})
  959. } else if t == MemberFlag {
  960. obj, _ = (*u)["o_member_jy"].(map[string]interface{})
  961. } else if t == EntnicheFlag {
  962. if len(data) > 0 {
  963. return true, data
  964. } else {
  965. return false, data
  966. }
  967. }
  968. if obj != nil {
  969. if buyerclassObj, ok := obj["a_buyerclass"].([]interface{}); ok {
  970. industry_ = common.ObjArrToStringArr(buyerclassObj)
  971. }
  972. itmes, _ := obj["a_items"].([]interface{})
  973. for _, v := range itmes {
  974. item, _ := v.(map[string]interface{})
  975. keys, _ := item["a_key"].([]interface{})
  976. if len(keys) > 0 {
  977. return true, industry_
  978. }
  979. }
  980. }
  981. }
  982. }
  983. return false, industry_
  984. }
  985. const (
  986. INDEX = "bidding"
  987. TYPE = "bidding"
  988. bidField = `"_id","title","publishtime","toptype","subtype","type","area","city","s_subscopeclass","buyerclass","budget","bidamount","filetext","spidercode","site","buyer","bidopentime","buyertel","buyerperson","agency","agencyperson","agencytel","s_winner","winnerperson","winnertel","signendtime","bidendtime","entidlist"`
  989. bidTime = `{"range":{"publishtime":{"gt":%d}}}`
  990. bidSort = `{"publishtime":"desc"}`
  991. findfields = `"title"`
  992. )
  993. // 首次访问推送页面 默认生成推送数据
  994. // 默认匹配es 7天内数据
  995. func (s *subscribePush) DefaultDatas(spqp *SubPushQueryParam) (hasNextPage bool, total int64, result []*bxsubscribe.SubscribeInfo) {
  996. if spqp.UserId == "" {
  997. return false, 0, nil
  998. }
  999. t1 := time.Now()
  1000. logx.Info("userId:", spqp.UserId, "newUserId:", spqp.NewUserId)
  1001. //用户信息
  1002. bsp := s.getUserInfo(spqp)
  1003. logx.Info("获取用户信息耗时:", time.Since(t1))
  1004. t2 := time.Now()
  1005. if len(bsp.Keyword) > 0 {
  1006. logx.Info(time.Since(t1), "--bsp:", bsp)
  1007. //获取查询语句
  1008. qstr := s.getDefaultDatasSQL(bsp)
  1009. list := elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSort, bidField, 0, bsp.Size, 0, false)
  1010. //logx.Info(time.Since(t1), "count:", len(*list))
  1011. if list != nil && len(*list) > 0 {
  1012. total = int64(len(*list))
  1013. result = s.listManager(spqp, *list, bsp.Keyword)
  1014. if len(result) > pageSize {
  1015. result = result[:pageSize]
  1016. hasNextPage = true
  1017. }
  1018. }
  1019. }
  1020. logx.Info("请求耗时:", time.Since(t2))
  1021. return
  1022. }
  1023. // 保存推送表
  1024. func (s *subscribePush) listManager(spqp *SubPushQueryParam, list []map[string]interface{}, keyword []ViewKeyWord) (resultList []*bxsubscribe.SubscribeInfo) {
  1025. now := time.Now().Unix()
  1026. length := len(list)
  1027. resultList = make([]*bxsubscribe.SubscribeInfo, length)
  1028. pushInsert := []interface{}{}
  1029. var product interface{}
  1030. if spqp.UserType == SubVipFlag {
  1031. product = 1
  1032. } else if spqp.UserType == MemberFlag {
  1033. product = 2
  1034. }
  1035. for i := length - 1; i >= 0; i-- {
  1036. v := list[i]
  1037. title := strings.Replace(common.ObjToString(v["title"]), " ", "", -1)
  1038. infoid := common.InterfaceToStr(v["_id"])
  1039. matchkeys := getKeys(title, keyword)
  1040. matchkey := strings.Join(matchkeys, " ")
  1041. subscopeclassStr := ""
  1042. area := common.If(v["area"] == nil, nil, common.If(common.ObjToString(v["area"]) == "A", 0, common.If(P.BidCodeMapping.Area[common.ObjToString(v["area"])] != "", P.BidCodeMapping.Area[common.ObjToString(v["area"])], 0)))
  1043. city := common.If(P.BidCodeMapping.City[common.ObjToString(v["city"])] != "", nil, P.BidCodeMapping.City[common.ObjToString(v["city"])])
  1044. subtype := common.If(P.BidCodeMapping.Subtype[common.ObjToString(v["subtype"])] != "", nil, P.BidCodeMapping.Subtype[common.ObjToString(v["subtype"])])
  1045. toptype := common.If(P.BidCodeMapping.Toptype[common.ObjToString(v["toptype"])] != "", nil, P.BidCodeMapping.Toptype[common.ObjToString(v["toptype"])])
  1046. buyerclass := common.If(P.BidCodeMapping.Buyerclass[common.ObjToString(v["buyerclass"])] != "", nil, P.BidCodeMapping.Buyerclass[common.ObjToString(v["buyerclass"])])
  1047. attachment_count := 0
  1048. if v["isValidFile"] != nil {
  1049. isValidFile, _ := v["isValidFile"].(bool)
  1050. attachment_count = common.If(isValidFile, 1, 0).(int)
  1051. }
  1052. bidamount := common.If(v["bidamount"] == nil, nil, v["bidamount"])
  1053. budget := common.If(v["budget"] == nil, nil, v["budget"])
  1054. if s_subscopeclass := common.ObjToString(v["s_subscopeclass"]); s_subscopeclass != "" {
  1055. subscopeclass := []string{}
  1056. for _, v := range strings.Split(s_subscopeclass, ",") {
  1057. if subscopeclass_mapping, ok := P.BidCodeMapping.Subscopeclass[v]; ok {
  1058. subscopeclass = append(subscopeclass, fmt.Sprint(subscopeclass_mapping))
  1059. }
  1060. }
  1061. if len(subscopeclass) > 0 {
  1062. subscopeclassStr = strings.Join(subscopeclass, ",")
  1063. }
  1064. }
  1065. redisKey := fmt.Sprintf("pushinfo_%s_%s", spqp.UserId, common.ObjToString(v["_id"]))
  1066. entid := spqp.EntId
  1067. entUserId := spqp.EntUserId
  1068. deptid := spqp.DeptId
  1069. //公共字段处理
  1070. pushInsert = append(pushInsert, common.InterfaceToStr(infoid), matchkey, now, area, city, buyerclass, toptype, subtype, subscopeclassStr, budget, bidamount, attachment_count)
  1071. switch s.ModuleFlag {
  1072. case EntnicheFlag:
  1073. pushInsert = append(pushInsert, entUserId, entid, deptid, 1, product)
  1074. case MemberFlag:
  1075. pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId))
  1076. case SubVipFlag:
  1077. pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId), 1)
  1078. case SubFreeFlag:
  1079. pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId), 0)
  1080. }
  1081. redis.Put("pushcache_2_a", redisKey, 1, 86400)
  1082. resultList[i] = s.InfoFormat(&PushCa{
  1083. InfoId: infoid,
  1084. Date: time.Now().Unix(),
  1085. Keys: matchkeys,
  1086. FileExists: v["filetext"] != nil,
  1087. }, &v, spqp.IsPayUser)
  1088. }
  1089. id := int64(0)
  1090. switch s.ModuleFlag {
  1091. case EntnicheFlag:
  1092. _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, ennicheInsertCollKey, pushInsert)
  1093. case MemberFlag:
  1094. _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, memberInsertCollKey, pushInsert)
  1095. default:
  1096. _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, subscribeInsertCollKey, pushInsert)
  1097. }
  1098. for i := length - 1; i >= 0; i-- {
  1099. resultList[i].CaIndex = id
  1100. id++
  1101. }
  1102. return
  1103. }
  1104. // 获取匹配得关键词
  1105. func getKeys(title string, keywords []ViewKeyWord) (str []string) {
  1106. if len(keywords) > 0 {
  1107. L:
  1108. for _, v := range keywords {
  1109. for _, vk := range v.Keyword {
  1110. if strings.Contains(title, vk) {
  1111. str = append(str, v.Keyword...)
  1112. break L
  1113. }
  1114. }
  1115. for _, va := range v.Appended {
  1116. if strings.Contains(title, va) {
  1117. str = append(str, v.Appended...)
  1118. break L
  1119. }
  1120. }
  1121. }
  1122. }
  1123. return
  1124. }
  1125. // 获取查询语句
  1126. func (s *subscribePush) getDefaultDatasSQL(bsp *ViewCondition) (str string) {
  1127. query := `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}}`
  1128. query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
  1129. multi_match := `{"multi_match": {"query": %s,"type": "phrase", "fields": [%s]}}`
  1130. query_bool_must_and := `{"bool":{"must":[%s]%s}}`
  1131. bools := []string{}
  1132. musts := []string{}
  1133. //发布时间最新7天(正式环境需要修改)
  1134. musts = append(musts, fmt.Sprintf(bidTime, time.Now().AddDate(0, 0, -7).Unix()))
  1135. //省份
  1136. areaCity := []string{}
  1137. if len(bsp.Area) > 0 {
  1138. areaquery := `{"terms":{"area":[`
  1139. for k, v := range bsp.Area {
  1140. if k > 0 {
  1141. areaquery += `,`
  1142. }
  1143. areaquery += `"` + v + `"`
  1144. }
  1145. areaquery += `]}}`
  1146. areaCity = append(areaCity, areaquery)
  1147. }
  1148. //城市
  1149. if len(bsp.City) > 0 {
  1150. areaquery := `{"terms":{"city":[`
  1151. for k, v := range bsp.City {
  1152. if k > 0 {
  1153. areaquery += `,`
  1154. }
  1155. areaquery += `"` + v + `"`
  1156. }
  1157. areaquery += `]}}`
  1158. areaCity = append(areaCity, areaquery)
  1159. }
  1160. if len(areaCity) > 0 {
  1161. musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaCity, ",")))
  1162. }
  1163. if len(bsp.Subtype) > 0 {
  1164. subquery := `{"terms":{"subtype":[`
  1165. for k, v := range bsp.Subtype {
  1166. if k > 0 {
  1167. subquery += `,`
  1168. }
  1169. subquery += `"` + v + `"`
  1170. }
  1171. subquery += `]}}`
  1172. musts = append(musts, subquery)
  1173. }
  1174. if len(bsp.Buyerclass) > 0 {
  1175. Buyerclass := `{"terms":{"buyerclass":[`
  1176. for k, v := range bsp.Buyerclass {
  1177. if k > 0 {
  1178. Buyerclass += `,`
  1179. }
  1180. Buyerclass += `"` + v + `"`
  1181. }
  1182. Buyerclass += `]}}`
  1183. musts = append(musts, Buyerclass)
  1184. }
  1185. boolsNum := 0 //should
  1186. if len(bsp.Keyword) > 0 {
  1187. boolsNum = 1
  1188. if bsp.SelectType == "" || bsp.SelectType == "2" {
  1189. bsp.SelectType = "detail\", \"title"
  1190. } else {
  1191. bsp.SelectType = "title"
  1192. }
  1193. multi_match = fmt.Sprintf(multi_match, "%s", "\""+bsp.SelectType+"\"")
  1194. for _, v := range bsp.Keyword {
  1195. shoulds := []string{}
  1196. must_not := []string{}
  1197. //附加词
  1198. for _, vv := range v.Keyword {
  1199. vv = strings.TrimSpace(vv)
  1200. if vv == "" {
  1201. continue
  1202. }
  1203. shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
  1204. }
  1205. for _, vv := range v.Appended {
  1206. vv = strings.TrimSpace(vv)
  1207. if vv == "" {
  1208. continue
  1209. }
  1210. shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
  1211. }
  1212. //排除词
  1213. for _, vv := range v.Exclude {
  1214. vv = strings.TrimSpace(vv)
  1215. if vv == "" {
  1216. continue
  1217. }
  1218. must_not = append(must_not, fmt.Sprintf(multi_match, "\""+vv+"\""))
  1219. }
  1220. //添加
  1221. if len(shoulds) > 0 {
  1222. notStr := ""
  1223. if len(must_not) > 0 {
  1224. notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
  1225. }
  1226. bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr))
  1227. }
  1228. }
  1229. }
  1230. qstr := fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","), boolsNum)
  1231. logx.Info("----", qstr)
  1232. return qstr
  1233. }
  1234. /*已选条件--关键词*/
  1235. type ViewKeyWord struct {
  1236. Keyword []string `json:"key"` //关键词
  1237. Appended []string `json:"appendkey"` //附加词
  1238. Exclude []string `json:"notkey"` //排除词
  1239. MatchWay int `json:"matchway"` //匹配模式
  1240. }
  1241. /*已选条件*/
  1242. type ViewCondition struct {
  1243. Area []string //地区-省份
  1244. City []string //地区-城市
  1245. Buyerclass []string //采购行业
  1246. Keyword []ViewKeyWord //关键词
  1247. SelectType string //筛选(正文 or 标题)
  1248. Subtype []string //信息类型
  1249. Size int //数量
  1250. }
  1251. // 获取用户信息
  1252. func (s *subscribePush) getUserInfo(spqp *SubPushQueryParam) (vc *ViewCondition) {
  1253. var isPayBool = false
  1254. var tmpInfo = struct {
  1255. Items []interface{}
  1256. BuyerClass []interface{}
  1257. SubType []interface{}
  1258. Area map[string]interface{}
  1259. SelectType string
  1260. }{}
  1261. switch s.ModuleFlag {
  1262. case MemberFlag:
  1263. userMap, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_member_jy":1,"i_member_status":1}`)
  1264. if !ok || userMap == nil || len(*userMap) == 0 {
  1265. return &ViewCondition{}
  1266. }
  1267. //大会员
  1268. if common.IntAll((*userMap)["i_member_status"]) > 0 {
  1269. isPayBool = true
  1270. o_member_jy, _ := (*userMap)["o_member_jy"].(map[string]interface{})
  1271. tmpInfo.Items, _ = o_member_jy["a_items"].([]interface{})
  1272. tmpInfo.BuyerClass, _ = o_member_jy["a_buyerclass"].([]interface{})
  1273. tmpInfo.SubType, _ = o_member_jy["a_infotype"].([]interface{})
  1274. tmpInfo.Area, _ = o_member_jy["o_area"].(map[string]interface{})
  1275. }
  1276. case SubVipFlag:
  1277. userMap, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_vipjy":1,"i_vip_status":1}`)
  1278. if !ok || userMap == nil || len(*userMap) == 0 {
  1279. return &ViewCondition{}
  1280. }
  1281. if common.IntAll((*userMap)["i_vip_status"]) > 0 {
  1282. isPayBool = true
  1283. o_vipjy, _ := (*userMap)["o_vipjy"].(map[string]interface{})
  1284. tmpInfo.Items, _ = o_vipjy["a_items"].([]interface{})
  1285. tmpInfo.BuyerClass, _ = o_vipjy["a_buyerclass"].([]interface{})
  1286. tmpInfo.SubType, _ = o_vipjy["a_infotype"].([]interface{})
  1287. tmpInfo.Area, _ = o_vipjy["o_area"].(map[string]interface{})
  1288. }
  1289. case EntnicheFlag:
  1290. //商机管理
  1291. entUserId, _ := strconv.Atoi(spqp.EntUserId)
  1292. entId, _ := strconv.Atoi(spqp.EntId)
  1293. entInfo, deptId := (&ms.MatchJob{}).Start(entId, entUserId)
  1294. if entInfo != nil && len(entInfo) > 0 {
  1295. spqp.DeptId = strconv.Itoa(deptId)
  1296. if common.IntAll(entInfo["i_type"]) == 2 {
  1297. if entInfo["a_key"] != nil {
  1298. tmpInfo.Items, _ = entInfo["a_key"].([]interface{})
  1299. //信息类型
  1300. tmpInfo.SubType, _ = entInfo["a_infotype"].([]interface{})
  1301. //省份
  1302. tmpInfo.Area, _ = entInfo["o_area"].(map[string]interface{})
  1303. if common.IntAllDef(entInfo["i_ppstatus"], 0) == 1 && entInfo["o_area_p"] != nil {
  1304. tmpInfo.Area, _ = entInfo["o_area_p"].(map[string]interface{})
  1305. }
  1306. }
  1307. } else {
  1308. isPayBool = true
  1309. tmpInfo.Items, _ = entInfo["a_items"].([]interface{})
  1310. tmpInfo.BuyerClass, _ = entInfo["a_buyerclass"].([]interface{})
  1311. tmpInfo.SubType, _ = entInfo["a_infotype"].([]interface{})
  1312. tmpInfo.Area, _ = entInfo["o_area"].(map[string]interface{})
  1313. }
  1314. }
  1315. default:
  1316. userMap, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_jy":1}`)
  1317. if !ok || userMap == nil || len(*userMap) == 0 {
  1318. return &ViewCondition{}
  1319. }
  1320. o_jy, _ := (*userMap)["o_jy"].(map[string]interface{})
  1321. if o_jy["a_key"] != nil {
  1322. tmpInfo.Items, _ = o_jy["a_key"].([]interface{})
  1323. //信息类型
  1324. tmpInfo.SubType, _ = o_jy["a_infotype"].([]interface{})
  1325. //省份
  1326. tmpInfo.Area, _ = o_jy["o_area"].(map[string]interface{})
  1327. if common.IntAllDef(o_jy["i_ppstatus"], 0) == 1 && o_jy["o_area_p"] != nil {
  1328. tmpInfo.Area, _ = o_jy["o_area_p"].(map[string]interface{})
  1329. }
  1330. }
  1331. }
  1332. vc = &ViewCondition{
  1333. Buyerclass: common.ObjArrToStringArr(tmpInfo.BuyerClass),
  1334. Subtype: common.ObjArrToStringArr(tmpInfo.SubType),
  1335. Area: getStringArrFromDbResult(tmpInfo.Area, 1),
  1336. City: getStringArrFromDbResult(tmpInfo.Area, 2),
  1337. SelectType: "1",
  1338. }
  1339. //付费用户
  1340. if isPayBool {
  1341. vc.Size = IC.C.DefaulCount.Pay
  1342. vc.Keyword = getKeyWordArrFromDbResult(tmpInfo.Items, "", -1)
  1343. } else {
  1344. vc.Size = IC.C.DefaulCount.Free
  1345. vc.Keyword = getKeyWordArrFromDbResultByFree(tmpInfo.Items, "", -1)
  1346. //vc.Keyword = getKeyWordArrFromDbResult(tmpInfo.Items, "", -1)
  1347. }
  1348. return
  1349. }
  1350. // 关键词 附加词 排除词
  1351. func getKeyWordArrFromDbResult(a_items []interface{}, item string, index int) (arr []ViewKeyWord) {
  1352. if a_items == nil {
  1353. return
  1354. }
  1355. for _, v := range a_items {
  1356. vm, _ := v.(map[string]interface{})
  1357. if item != "" && index >= 0 && item != common.ObjToString(vm["s_item"]) {
  1358. continue
  1359. }
  1360. kwsArr := vm["a_key"]
  1361. for i, k := range kwsArr.([]interface{}) {
  1362. if item != "" && index >= 0 && i != index {
  1363. continue
  1364. }
  1365. kw := ViewKeyWord{}
  1366. b, e := json.Marshal(k)
  1367. if e != nil {
  1368. log.Println(e.Error())
  1369. }
  1370. json.Unmarshal(b, &kw)
  1371. if kw.MatchWay == 1 {
  1372. for _, kk := range kw.Keyword {
  1373. arr = append(arr, ViewKeyWord{
  1374. Keyword: []string{kk},
  1375. Exclude: kw.Exclude,
  1376. })
  1377. }
  1378. for _, kk := range kw.Appended {
  1379. arr = append(arr, ViewKeyWord{
  1380. Keyword: []string{kk},
  1381. Exclude: kw.Exclude,
  1382. })
  1383. }
  1384. } else {
  1385. arr = append(arr, kw)
  1386. }
  1387. }
  1388. }
  1389. return
  1390. }
  1391. // //关键词 附加词 排除词 省份订阅包 - 普通用户
  1392. func getKeyWordArrFromDbResultByFree(a_items []interface{}, item string, index int) (arr []ViewKeyWord) {
  1393. if a_items == nil {
  1394. return
  1395. }
  1396. for _, v := range a_items {
  1397. kw := ViewKeyWord{}
  1398. b, e := json.Marshal(v)
  1399. if e != nil {
  1400. log.Println(e.Error())
  1401. }
  1402. json.Unmarshal(b, &kw)
  1403. if kw.MatchWay == 1 {
  1404. for _, kk := range kw.Keyword {
  1405. arr = append(arr, ViewKeyWord{
  1406. Keyword: []string{kk},
  1407. Exclude: kw.Exclude,
  1408. })
  1409. }
  1410. for _, kk := range kw.Appended {
  1411. arr = append(arr, ViewKeyWord{
  1412. Keyword: []string{kk},
  1413. Exclude: kw.Exclude,
  1414. })
  1415. }
  1416. } else {
  1417. arr = append(arr, kw)
  1418. }
  1419. }
  1420. return
  1421. }
  1422. // 地区格式化
  1423. func getStringArrFromDbResult(area map[string]interface{}, i int) (arr []string) {
  1424. if area == nil {
  1425. return
  1426. }
  1427. var eareArr []string
  1428. var cityArr []string
  1429. for k, v := range area {
  1430. if len(v.([]interface{})) > 0 {
  1431. cityArr = append(cityArr, common.ObjArrToStringArr(v.([]interface{}))...)
  1432. } else {
  1433. eareArr = append(eareArr, k)
  1434. }
  1435. }
  1436. if i == 1 {
  1437. arr = eareArr
  1438. } else {
  1439. arr = cityArr
  1440. }
  1441. return
  1442. }
  1443. // IsInTsGuide 是否进入向导
  1444. func (s *subscribePush) IsInTsGuide(userid string) bool {
  1445. if userid == "" {
  1446. return false
  1447. }
  1448. data, ok := IC.Mgo.FindById("user", userid, `{"o_jy":1,"i_ts_guide":1,"":1,"i_member_status":1,"i_vip_status":1}`)
  1449. if ok {
  1450. //付费用户无免费订阅,不进入订阅向导页面
  1451. if common.IntAll((*data)["i_member_status"]) > 0 || common.IntAll((*data)["i_vip_status"]) > 0 {
  1452. return false
  1453. }
  1454. o_jy, _ := (*data)["o_jy"].(map[string]interface{})
  1455. i_ts_guide := common.IntAll((*data)["i_ts_guide"])
  1456. if i_ts_guide == 2 || (i_ts_guide == 0 && len(o_jy) == 0) {
  1457. return true
  1458. }
  1459. }
  1460. return false
  1461. }
  1462. func (s *subscribePush) Keys(spqp *KeyParam) (result []*bxsubscribe.KeyItems) {
  1463. keyData := map[string][]interface{}{}
  1464. if spqp.IsEnt {
  1465. //企业关键词查找
  1466. res_, _ := IC.Mgo.Find("entniche_rule", map[string]interface{}{
  1467. "i_userid": map[string]interface{}{
  1468. "$exists": false,
  1469. },
  1470. "i_entid": common.Int64All(spqp.EntId),
  1471. }, "", `{"o_entniche":1}`, false, -1, -1)
  1472. o_entniche := map[string]interface{}{}
  1473. if res_ == nil || len(*res_) == 0 {
  1474. return MapToarr(keyData)
  1475. }
  1476. for _, ruleV := range *res_ {
  1477. o_entniche = common.StructToMapMore(ruleV["o_entniche"])
  1478. if o_entniche["a_items"] != nil {
  1479. a_item := common.ObjArrToMapArr(o_entniche["a_items"].([]interface{}))
  1480. for _, v := range a_item {
  1481. if v["a_key"] != nil {
  1482. a_key_arr := v["a_key"].([]interface{})
  1483. s_item := common.InterfaceToStr(v["s_item"])
  1484. if keyData[s_item] != nil {
  1485. //第一次搜索存在
  1486. //去重
  1487. keyData[s_item] = MergeArray(a_key_arr, keyData[s_item])
  1488. } else {
  1489. //第一次搜索不存在
  1490. keyData[s_item] = a_key_arr
  1491. }
  1492. }
  1493. }
  1494. }
  1495. }
  1496. } else {
  1497. //个人关键词查找
  1498. //fType:普通用户;vType:超级订阅用户;mType:大会员用户;eType:商机管理用户
  1499. types := 2
  1500. typeKey := ""
  1501. switch s.ModuleFlag {
  1502. case SubVipFlag:
  1503. types = 1
  1504. typeKey = "o_vipjy"
  1505. case MemberFlag:
  1506. types = 1
  1507. typeKey = "o_member_jy"
  1508. case EntnicheFlag:
  1509. types = 0
  1510. typeKey = "o_entniche"
  1511. case SubFreeFlag:
  1512. types = 2
  1513. typeKey = "o_jy"
  1514. }
  1515. if spqp.PositionType == 1 {
  1516. typeKey = "o_entniche"
  1517. }
  1518. o_entniche := &map[string]interface{}{}
  1519. if spqp.PositionType == 0 {
  1520. mData, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_jy":1,"o_vipjy":1,"o_member_jy":1,""i_member_sub_status":1,"s_member_mainid":1}`)
  1521. if mData == nil || len(*mData) == 0 || !ok {
  1522. return nil
  1523. }
  1524. if s.ModuleFlag == MemberFlag {
  1525. sub_status := common.Int64All((*mData)["i_member_sub_status"])
  1526. if sub_status == 1 {
  1527. //大会员子账号处理
  1528. mData, ok = IC.Mgo.FindById("user", common.InterfaceToStr((*mData)["s_member_mainid"]), `{"o_jy":1,"o_vipjy":1,"o_member_jy":1,""i_member_sub_status":1,"s_member_mainid":1}`)
  1529. if mData == nil || len(*mData) == 0 || !ok {
  1530. return nil
  1531. }
  1532. }
  1533. }
  1534. o_entniche = common.ObjToMap((*mData)[typeKey])
  1535. } else {
  1536. res_, _ := IC.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
  1537. "i_userid": common.Int64All(spqp.EntUserId),
  1538. "i_entid": common.Int64All(spqp.EntId),
  1539. "i_type": types,
  1540. }, `{"o_entniche":1}`)
  1541. if res_ != nil {
  1542. o_entniche = common.ObjToMap((*res_)["o_entniche"])
  1543. }
  1544. }
  1545. if s.ModuleFlag == SubVipFlag {
  1546. //从user表中取 o_vipjy.a_items
  1547. if (*o_entniche)["a_items"] != nil {
  1548. a_items := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
  1549. for _, v := range a_items {
  1550. if v["a_key"] != nil {
  1551. a_key_arr := v["a_key"].([]interface{})
  1552. keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
  1553. }
  1554. }
  1555. }
  1556. } else if s.ModuleFlag == MemberFlag {
  1557. //从user表中取 o_member_jy.a_items
  1558. //o_memeberjy := common.ObjToMap((*mData)["o_member_jy"])
  1559. if (*o_entniche)["a_items"] != nil {
  1560. a_items := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
  1561. for _, v := range a_items {
  1562. if v["a_key"] != nil {
  1563. a_key_arr := v["a_key"].([]interface{})
  1564. keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
  1565. }
  1566. }
  1567. }
  1568. } else if s.ModuleFlag == EntnicheFlag {
  1569. res_, _ := IC.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
  1570. "i_userid": common.Int64All(spqp.EntUserId),
  1571. "i_entid": common.Int64All(spqp.EntId),
  1572. "i_type": types,
  1573. }, `{"o_entniche":1}`)
  1574. o_entniche := &map[string]interface{}{}
  1575. if res_ != nil {
  1576. o_entniche = common.ObjToMap((*res_)["o_entniche"])
  1577. }
  1578. if (*o_entniche)["a_items"] != nil {
  1579. a_item := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
  1580. for _, v := range a_item {
  1581. if v["a_key"] != nil {
  1582. a_key_arr := v["a_key"].([]interface{})
  1583. keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
  1584. }
  1585. }
  1586. }
  1587. } else {
  1588. //免费用户
  1589. a_key, _ := (*o_entniche)["a_key"].([]interface{})
  1590. if len(a_key) > 0 {
  1591. for _, v := range a_key {
  1592. if keyData["未分类"] != nil {
  1593. keyData["未分类"] = append(keyData["未分类"], v)
  1594. } else {
  1595. keyData["未分类"] = []interface{}{v}
  1596. }
  1597. }
  1598. }
  1599. }
  1600. if spqp.PositionType == 1 && s.ModuleFlag != SubFreeFlag {
  1601. //1、mysql entniche_user_rule表中获取自己的分发规则id
  1602. data := IC.MainMysql.SelectBySql(`SELECT rule_id FROM entniche_user_rule a
  1603. INNER JOIN entniche_department b ON a.dept_id = b.id AND a.user_id =?
  1604. ORDER BY b.pid
  1605. LIMIT 1 `, spqp.EntUserId)
  1606. if data == nil || len(*data) == 0 {
  1607. return MapToarr(keyData)
  1608. }
  1609. ruleId := common.ObjToString((*data)[0]["rule_id"])
  1610. //3、entniche_distribute表,查询条件:{_id:ObjectId(1结果中的分发规则id),i_status:0},取 o_entniche.a_items,这个查到的是分类名称,然后到上面1的结果中获取这个分类下的词
  1611. res, _ := IC.Mgo.FindById("entniche_distribute", ruleId, nil)
  1612. if res == nil || len(*res) == 0 {
  1613. return MapToarr(keyData)
  1614. }
  1615. entItem := (*res)["a_items"].([]interface{})
  1616. if (*res)["a_items"] == nil {
  1617. return MapToarr(keyData)
  1618. }
  1619. //2、entniche_rule表, 企业订阅关键词查询条件: {i_entid:123,i_deptid:0},取 o_entniche.a_items
  1620. deptid := common.Int64All((*res)["i_deptid"])
  1621. ruleMap := map[string]interface{}{
  1622. "i_entid": common.Int64All(spqp.EntId),
  1623. }
  1624. if deptid == 0 {
  1625. ruleMap["i_deptid"] = map[string]interface{}{"$exists": false}
  1626. ruleMap["i_userid"] = map[string]interface{}{"$exists": false}
  1627. } else {
  1628. ruleMap["i_deptid"] = deptid
  1629. }
  1630. res_, _ := IC.Mgo.FindOneByField("entniche_rule", ruleMap, `{"o_entniche":1}`)
  1631. o_entniche := &map[string]interface{}{}
  1632. if res_ != nil {
  1633. o_entniche = common.ObjToMap((*res_)["o_entniche"])
  1634. }
  1635. entnicheRule := map[string][]interface{}{}
  1636. if (*o_entniche)["a_items"] != nil {
  1637. a_item := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
  1638. for _, v := range a_item {
  1639. if v["a_key"] != nil {
  1640. a_key_arr := v["a_key"].([]interface{})
  1641. entnicheRule[common.InterfaceToStr(v["s_item"])] = a_key_arr
  1642. }
  1643. }
  1644. }
  1645. for _, value := range entItem {
  1646. v := common.InterfaceToStr(value)
  1647. if entnicheRule[v] != nil {
  1648. if keyData[v] != nil {
  1649. //第一次搜索存在
  1650. //去重
  1651. keyData[v] = MergeArray(keyData[v], entnicheRule[v])
  1652. } else {
  1653. //第一次搜索不存在
  1654. keyData[v] = entnicheRule[v]
  1655. }
  1656. }
  1657. }
  1658. }
  1659. }
  1660. return MapToarr(keyData)
  1661. }
  1662. // 关键词处理
  1663. func KeyHandle(a_items map[string]interface{}) []interface{} {
  1664. keyArr := []interface{}{}
  1665. if a_items["a_key"] != nil {
  1666. a_key_arr := common.ObjArrToMapArr(a_items["a_key"].([]interface{}))
  1667. for _, vv := range a_key_arr {
  1668. for _, v := range vv["key"].([]interface{}) {
  1669. keyArr = append(keyArr, v)
  1670. }
  1671. }
  1672. }
  1673. return keyArr
  1674. }
  1675. func MapToarr(data map[string][]interface{}) []*bxsubscribe.KeyItems {
  1676. result := []*bxsubscribe.KeyItems{}
  1677. for k, v := range data {
  1678. keyArr := []*bxsubscribe.Key{}
  1679. for _, m := range common.ObjArrToMapArr(v) {
  1680. keys := &bxsubscribe.Key{}
  1681. if m["notkey"] != nil {
  1682. keys.Notkey = common.ObjArrToStringArr(m["notkey"].([]interface{}))
  1683. }
  1684. if m["key"] != nil {
  1685. keys.Key = common.ObjArrToStringArr(m["key"].([]interface{}))
  1686. }
  1687. if m["matchway"] != nil {
  1688. keys.Matchway = common.Int64All(m["matchway"])
  1689. }
  1690. if m["appendkey"] != nil {
  1691. keys.Appendkey = common.ObjArrToStringArr(m["appendkey"].([]interface{}))
  1692. }
  1693. keyArr = append(keyArr, keys)
  1694. }
  1695. result = append(result, &bxsubscribe.KeyItems{
  1696. AKey: keyArr,
  1697. SItem: k,
  1698. })
  1699. }
  1700. return result
  1701. }
  1702. func MergeArray(dest []interface{}, src []interface{}) (result []interface{}) {
  1703. data := map[string]interface{}{}
  1704. for _, v := range common.ObjArrToMapArr(dest) {
  1705. notkey := ""
  1706. key := ""
  1707. matchway := ""
  1708. appendkey := ""
  1709. if v["notkey"] != nil {
  1710. otkeyArr := common.ObjArrToStringArr(v["notkey"].([]interface{}))
  1711. for _, keyV := range otkeyArr {
  1712. if common.InterfaceToStr(keyV) != "" {
  1713. notkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1714. }
  1715. }
  1716. }
  1717. if v["key"] != nil {
  1718. keyArr := common.ObjArrToStringArr(v["key"].([]interface{}))
  1719. for _, keyV := range keyArr {
  1720. if common.InterfaceToStr(keyV) != "" {
  1721. key += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1722. }
  1723. }
  1724. }
  1725. if v["matchway"] != nil {
  1726. matchway = common.InterfaceToStr(v["matchway"])
  1727. }
  1728. if v["appendkey"] != nil {
  1729. appendkeyArr := common.ObjArrToStringArr(v["appendkey"].([]interface{}))
  1730. for _, keyV := range appendkeyArr {
  1731. if common.InterfaceToStr(keyV) != "" {
  1732. appendkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1733. }
  1734. }
  1735. }
  1736. k := fmt.Sprintf("%s_____%s_____%s_____%s", notkey, key, matchway, appendkey)
  1737. data[k] = v
  1738. }
  1739. for _, v := range common.ObjArrToMapArr(src) {
  1740. notkey := ""
  1741. key := ""
  1742. matchway := ""
  1743. appendkey := ""
  1744. if v["notkey"] != nil {
  1745. otkeyArr := common.ObjArrToStringArr(v["notkey"].([]interface{}))
  1746. for _, keyV := range otkeyArr {
  1747. if common.InterfaceToStr(keyV) != "" {
  1748. notkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1749. }
  1750. }
  1751. }
  1752. if v["key"] != nil {
  1753. keyArr := common.ObjArrToStringArr(v["key"].([]interface{}))
  1754. for _, keyV := range keyArr {
  1755. if common.InterfaceToStr(keyV) != "" {
  1756. key += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1757. }
  1758. }
  1759. }
  1760. if v["matchway"] != nil {
  1761. matchway = common.InterfaceToStr(v["matchway"])
  1762. }
  1763. if v["appendkey"] != nil {
  1764. appendkeyArr := common.ObjArrToStringArr(v["appendkey"].([]interface{}))
  1765. for _, keyV := range appendkeyArr {
  1766. if common.InterfaceToStr(keyV) != "" {
  1767. appendkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
  1768. }
  1769. }
  1770. }
  1771. k := fmt.Sprintf("%s_____%s_____%s_____%s", notkey, key, matchway, appendkey)
  1772. data[k] = v
  1773. }
  1774. for _, v := range data {
  1775. result = append(result, v)
  1776. }
  1777. return
  1778. }
  1779. func (s *subscribePush) ItemToKey(in *SubPushQueryParam) string {
  1780. moduleFlag := s.ModuleFlag
  1781. s.ModuleFlag = in.UserType
  1782. spqp := KeyParam{
  1783. UserId: in.UserId,
  1784. EntUserId: in.EntUserId,
  1785. DeptId: in.DeptId,
  1786. NewUserId: in.NewUserId,
  1787. IsEnt: in.IsEnt,
  1788. EntId: in.EntId,
  1789. PositionType: in.PositionType,
  1790. }
  1791. //所有关键词获取
  1792. keyItemsArr := s.Keys(&spqp)
  1793. s.ModuleFlag = moduleFlag
  1794. keyArr := []string{}
  1795. if len(keyItemsArr) > 0 {
  1796. for _, k1 := range keyItemsArr {
  1797. for _, k2 := range strings.Split(in.Item, ",") {
  1798. if k1.SItem == k2 {
  1799. if len(k1.AKey) > 0 {
  1800. for _, k3 := range k1.AKey {
  1801. key := ""
  1802. if len(k3.Key) > 0 {
  1803. key = strings.Join(k3.Key, " ")
  1804. }
  1805. if len(k3.Appendkey) > 0 {
  1806. if key != "" {
  1807. key += " " + strings.Join(k3.Appendkey, " ")
  1808. } else {
  1809. key = strings.Join(k3.Appendkey, " ")
  1810. }
  1811. }
  1812. if k3.Matchway == 1 {
  1813. //模糊处理
  1814. key = strings.ReplaceAll(key, " ", ",")
  1815. } else {
  1816. //精准处理
  1817. key = strings.ReplaceAll(key, " ", "+")
  1818. }
  1819. keyArr = append(keyArr, key)
  1820. }
  1821. }
  1822. }
  1823. }
  1824. }
  1825. }
  1826. return strings.Join(keyArr, ",")
  1827. }