push.go 62 KB

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