push.go 63 KB

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