push.go 88 KB

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