push.go 68 KB

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