push.go 62 KB

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