push.go 60 KB

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