123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535 |
- package model
- import (
- "encoding/json"
- "fmt"
- "log"
- "regexp"
- "strconv"
- "strings"
- "sync"
- "time"
- IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
- ms "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/model/service"
- "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/type/bxsubscribe"
- "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/util"
- "go.mongodb.org/mongo-driver/bson"
- "github.com/gogf/gf/v2/util/gconv"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/encrypt"
- elastic "app.yhyue.com/moapp/jybase/es"
- P "app.yhyue.com/moapp/jybase/mapping"
- "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/mysql"
- "app.yhyue.com/moapp/jybase/redis"
- "github.com/zeromicro/go-zero/core/logx"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- const (
- PageSize = 100
- AllSubPushCacheSize = 200
- 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}`
- 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}`
- query2 = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","title", "detail"],"from":0,"size":%d}`
- 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}`
- 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}`
- SubFreeFlag = "fType"
- SubVipFlag = "vType"
- MemberFlag = "mType"
- EntnicheFlag = "eType"
- oneDay = 24 * 60 * 60
- )
- var (
- ennicheInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "district", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid", "entid", "deptid", "source", "product"}
- subscribeInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "district", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid", "isvip"}
- memberInsertCollKey = []string{"infoid", "matchkeys", "date", "area", "city", "district", "buyerclass", "toptype", "subtype", "subscopeclass", "budget", "bidamount", "attachment_count", "userid"}
- )
- var aboutDbMsg map[string]*AboutDbMsg = map[string]*AboutDbMsg{
- SubFreeFlag: &AboutDbMsg{"push.pushsubscribe", "subpush"},
- SubVipFlag: &AboutDbMsg{"push.pushsubscribe", "subpush"},
- MemberFlag: &AboutDbMsg{"push.pushmember", "memberpush"},
- EntnicheFlag: &AboutDbMsg{"push.pushentniche", "entnichepush"},
- }
- type AboutDbMsg struct {
- MysqlTable string
- RedisKeyFlag string
- }
- type SubPush struct {
- Date string
- Datas []*bxsubscribe.SubscribeInfo
- Count int64
- }
- type PushCa struct {
- Date int64
- InfoId string
- Visit int
- Index int64
- Keys []string
- MatchWays string
- Type int
- Isvip int
- FileExists bool
- Source int64
- Title string
- SourceAll string
- }
- // 查询参数
- type SubPushQueryParam struct {
- Mgo_bidding mongodb.MongodbSim //
- Bidding string //
- Bidding_back string //
- UserId string //用户id
- PageNum int //页面
- PageSize int //每页数量
- SelectTime string //时间
- Area string //区域
- City string //城市
- Buyerclass string //采购单位行业
- Subtype string //信息类型 二级分类
- Subscopeclass string //信息行业
- Key string //订阅词
- Export bool //导出
- EntId string //企业id
- Price string //价格
- FileExists string //是否有附件;默认全部;1:有附件;-1:无附件
- EntUserId string //商机管理用户id
- DeptId string //商机管理用户部门id
- IsRead string //是否已读
- Source string //信息来源
- Staffs []string //分发人员
- BaseServiceMysql *mysql.Mysql
- NewUserId int64
- IsEnt bool
- SelectInfoIds []string
- BuySubject int64
- UserType string
- PositionType int64
- IsPayUser bool // 是否是付费用户
- NotReturnCount int64
- Item string
- SelectKeys []string //关键词
- District string
- SubPushInactive int // 订阅活跃标记
- }
- // 关键词参数
- type KeyParam struct {
- UserId string //用户id
- EntUserId string //商机管理用户id
- DeptId string //商机管理用户部门id
- NewUserId int64
- IsEnt bool
- VipPower int64
- EntId string //企业id
- MemberPower int64
- PowerSource int64
- UserPower int64
- PositionType int64
- }
- func (spqp *SubPushQueryParam) IsEmpty() bool {
- 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 == ""
- }
- type subscribePush struct {
- ModuleFlag string
- }
- func NewSubscribePush(module ...string) *subscribePush {
- m := ""
- if len(module) > 0 {
- m = module[0]
- }
- return &subscribePush{m}
- }
- // 从pushcache_2_a中取
- func (s *subscribePush) GetTodayCache(userId, userType string) (*SubPush, error) {
- pc_a, err := redis.GetNewBytes("pushcache_2_b", s.todayKey(userId, userType))
- if err != nil {
- return nil, err
- }
- if pc_a == nil {
- return nil, nil
- }
- var p *SubPush
- if err := json.Unmarshal(*pc_a, &p); err != nil {
- return nil, err
- }
- return p, nil
- }
- // 往pushcache_2_a中放
- func (s *subscribePush) PutTodayCache(userId, userType string, pc_a *SubPush) {
- redis.Put("pushcache_2_b", s.todayKey(userId, userType), pc_a, oneDay)
- }
- // 获取redis key
- func (s *subscribePush) todayKey(userId, userType string) string {
- if s.ModuleFlag == EntnicheFlag {
- switch userType {
- case SubVipFlag:
- return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "vip", userId)
- case MemberFlag:
- return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "member", userId)
- case SubFreeFlag:
- return fmt.Sprintf("%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "free", userId)
- }
- }
- return fmt.Sprintf("%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
- }
- func (s *subscribePush) allKey(userId, userType string) string {
- if s.ModuleFlag == EntnicheFlag {
- switch userType {
- case SubVipFlag:
- return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "vip", userId)
- case MemberFlag:
- return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "member", userId)
- case SubFreeFlag:
- return fmt.Sprintf("all_%s_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, "free", userId)
- }
- }
- return fmt.Sprintf("all_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
- }
- // 历史推送记录中单条信息格式化
- func (s *subscribePush) InfoFormat(p *PushCa, info *map[string]interface{}, isPay, isDetail bool) *bxsubscribe.SubscribeInfo {
- area := common.ObjToString((*info)["area"])
- if area == "A" {
- area = "全国"
- }
- industry := common.ObjToString((*info)["s_subscopeclass"])
- scs := strings.Split(industry, ",")
- if len(scs) > 0 {
- industry = scs[0]
- if industry != "" {
- iss := strings.Split(industry, "_")
- if len(iss) > 0 {
- industry = iss[0]
- }
- }
- }
- infotype := common.ObjToString((*info)["subtype"])
- if infotype == "" {
- infotype = common.ObjToString((*info)["toptype"])
- }
- _id := p.InfoId
- if _id == "" {
- _id = common.ObjToString((*info)["_id"])
- }
- isValidFile, _ := (*info)["isValidFile"].(bool)
- detail := common.InterfaceToStr(common.If(isDetail, (*info)["detail"], ""))
- detail = subStrDetail(strings.Join(p.Keys, " "), detail)
- formatInfo := &bxsubscribe.SubscribeInfo{
- XId: encrypt.EncodeArticleId2ByCheck(_id),
- Title: common.InterfaceToStr((*info)["title"]),
- Area: area,
- City: common.InterfaceToStr((*info)["city"]),
- BuyerClass: common.InterfaceToStr((*info)["buyerclass"]),
- Subtype: infotype,
- Industry: industry,
- PublishTime: common.Int64All((*info)["publishtime"]),
- CaIndex: p.Index,
- CaDate: p.Date,
- CaIsvisit: int64(p.Visit),
- CaIsvip: int64(p.Isvip),
- CaType: int64(p.Type),
- MatchKeys: p.Keys,
- Budget: common.Float64All((*info)["budget"]),
- BidAmount: common.Float64All((*info)["bidamount"]),
- Buyer: common.InterfaceToStr((*info)["buyer"]),
- ProjectName: common.InterfaceToStr((*info)["projectname"]),
- Winner: common.InterfaceToStr((*info)["s_winner"]),
- BidOpenTime: common.Int64All((*info)["bidopentime"]),
- CaFileExists: p.FileExists,
- Source: p.Source,
- Site: common.InterfaceToStr((*info)["site"]),
- SpiderCode: common.InterfaceToStr((*info)["spidercode"]),
- Toptype: common.InterfaceToStr((*info)["toptype"]),
- IsValidFile: isValidFile,
- SourceAll: p.SourceAll,
- FiletextSearch: common.If(p.MatchWays == "3", true, false).(bool),
- Detail: detail,
- District: common.InterfaceToStr((*info)["district"]),
- }
- // 免费用户返回精简列表字段
- if !isPay {
- return formatInfo
- }
- winnerList := common.InterfaceToStr((*info)["s_winner"]) //中标企业名称集合
- if winnerList != "" && len(strings.Split(winnerList, ",")) > 0 {
- for wk, wv := range strings.Split(winnerList, ",") {
- var (
- winnerId = ""
- )
- if (*info)["entidlist"] != nil {
- if entIdList := common.ObjArrToStringArr((*info)["entidlist"].([]interface{})); len(entIdList) > wk { //中标企业id集合
- winnerId = entIdList[wk]
- }
- }
- if len([]rune(winnerId)) < 24 { //验证是否真正的有效id
- continue
- }
- formatInfo.WinnerInfo = append(formatInfo.WinnerInfo, &bxsubscribe.WinnerInfo{
- Winner: wv, //中标企业 需要单独处理
- WinnerTel: common.ObjToString((*info)["winnertel"]), //中标企业联系电话
- WinnerPerson: common.ObjToString((*info)["winnerperson"]), //中标企业联系人
- WinnerId: common.If(winnerId != "" && len([]rune(winnerId)) > 12, encrypt.EncodeArticleId2ByCheck(winnerId), "").(string), //中标企业加密id 存在winnerId 异常的情况
- })
- }
- }
- // 付费用户返回详细列表字段
- formatInfo.BuyerTel = common.ObjToString((*info)["buyertel"])
- formatInfo.BuyerPerson = common.ObjToString((*info)["buyerperson"])
- formatInfo.Agency = common.ObjToString((*info)["agency"])
- formatInfo.AgencyPerson = common.ObjToString((*info)["agencyperson"])
- formatInfo.AgencyTel = common.ObjToString((*info)["agencytel"])
- formatInfo.SignendTime = common.Int64All((*info)["signendtime"])
- formatInfo.BidendTime = common.Int64All((*info)["bidendtime"])
- return formatInfo
- }
- func subStrDetail(matchKey, detail string) string {
- matchKeys := []string{}
- for _, v := range regexp.MustCompile(`\s|\+`).Split(matchKey, -1) {
- if v == "" {
- continue
- }
- matchKeys = append(matchKeys, regexp.QuoteMeta(v))
- }
- if len(matchKeys) == 0 {
- return ""
- }
- indexs := regexp.MustCompile(strings.Join(matchKeys, "|")).FindStringIndex(detail)
- if len(indexs) == 0 {
- return ""
- }
- start := string(detail[0:indexs[0]])
- if len([]rune(start)) > 50 {
- start = string([]rune(start)[len([]rune(start))-50:])
- }
- detail = start + string(detail[indexs[0]:])
- if len([]rune(detail)) > 200 {
- detail = string([]rune(detail)[:200])
- }
- return strings.TrimSpace(detail)
- }
- func (s *subscribePush) Datas(spqp *SubPushQueryParam, bsp *ViewCondition) (hasNextPage bool, total int64, result []*bxsubscribe.SubscribeInfo) {
- 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)
- if spqp.UserId == "" {
- return
- }
- if spqp.PageNum < 1 {
- spqp.PageNum = 1
- }
- if spqp.PageSize <= 1 || spqp.PageSize > PageSize {
- if !spqp.Export {
- spqp.PageSize = PageSize
- } else { //数据导出查询20000条限制
- if spqp.PageSize < 1 || spqp.PageSize > 20000 {
- spqp.PageSize = 20000
- }
- }
- }
- starttime, endtime := int64(0), int64(0)
- if len(strings.Split(spqp.SelectTime, "_")) == 2 {
- st := strings.Split(spqp.SelectTime, "_")[0]
- et := strings.Split(spqp.SelectTime, "_")[1]
- if st != "" && et != "" {
- starttime, _ = strconv.ParseInt(st, 0, 64)
- endtime, _ = strconv.ParseInt(et, 0, 64)
- if endtime > 0 {
- etTime := time.Unix(endtime, 0)
- endtime = time.Date(etTime.Year(), etTime.Month(), etTime.Day(), 23, 59, 59, 0, time.Local).Unix()
- }
- }
- }
- //不活跃用户处理
- if spqp.UserType == "fType" && spqp.PositionType == 0 && spqp.SubPushInactive == 1 {
- if spqp.Export {
- spqp.PageSize = 500
- }
- result, total = s.inactiveQuery(spqp, bsp)
- hasNextPage = len(result) >= spqp.PageSize
- return
- }
- nowFormat := date.NowFormat(date.Date_Short_Layout)
- start := (spqp.PageNum - 1) * spqp.PageSize
- end := start + spqp.PageSize
- //时间是今天,没有别的过滤条件--首先读取当前推送缓存数据 其次查询数据库
- if nowFormat == date.FormatDateByInt64(&starttime, date.Date_Short_Layout) && 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 {
- subPush := &SubPush{}
- var err error
- if spqp.IsEnt == false {
- subPush, err = s.GetTodayCache(spqp.UserId, spqp.UserType)
- }
- // 返回信息补正文
- if subPush != nil && len(subPush.Datas) > 0 {
- if strings.Contains(strings.Join(bsp.SelectType, ","), "detail") {
- getDetail(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, subPush)
- } else {
- for _, data := range subPush.Datas {
- data.Detail = "" // 删掉正文
- }
- }
- }
- if err != nil {
- logx.Info(spqp.UserId, "GetTodayCache Error", err)
- }
- if err != nil || subPush == nil || subPush.Date != nowFormat || len(subPush.Datas) == 0 {
- list, countSearch := s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, false,
- common.If(strings.Contains(strings.Join(bsp.SelectType, ","), "detail"), true, false).(bool))
- if len(list) == 0 {
- return
- }
- subPush = &SubPush{
- Date: nowFormat,
- Datas: list,
- Count: countSearch,
- }
- if spqp.IsEnt == false && spqp.NotReturnCount == 0 {
- s.PutTodayCache(spqp.UserId, spqp.UserType, subPush)
- }
- }
- length := len(subPush.Datas)
- if end > length {
- end = length
- }
- if start < length {
- result = subPush.Datas[start:end]
- } else {
- result = subPush.Datas
- }
- total = int64(length)
- //全部,没有过滤条件 之前缓存500
- } else if spqp.IsEmpty() && (spqp.PageNum-1)*spqp.PageSize < 200 && len(spqp.SelectInfoIds) == 0 && !spqp.Export { //全部,没有过滤条件 之前缓存5页*50条=250 非数据导出
- allCache := &SubPush{}
- var err error
- if !spqp.IsEnt {
- allCache, err = s.GetAllCache(spqp.UserId, spqp.UserType)
- }
- if err != nil {
- logx.Info(spqp.UserId, "GetAllCache Error", err)
- }
- // 返回信息补正文
- if allCache != nil && len(allCache.Datas) > 0 {
- if strings.Contains(strings.Join(bsp.SelectType, ","), "detail") {
- getDetail(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, allCache)
- } else {
- for _, data := range allCache.Datas {
- data.Detail = "" // 删掉正文
- }
- }
- }
- if err != nil || allCache == nil || allCache.Date != nowFormat || len(allCache.Datas) == 0 {
- list, countSearch := []*bxsubscribe.SubscribeInfo{}, int64(0)
- if spqp.IsEnt {
- list, countSearch = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true, common.If(strings.Contains(strings.Join(bsp.SelectType, ","), "detail"), true, false).(bool))
- } else {
- spqp.PageNum = 1
- list, countSearch = s.getDatasFromMysql(spqp, starttime, endtime, AllSubPushCacheSize, true, common.If(strings.Contains(strings.Join(bsp.SelectType, ","), "detail"), true, false).(bool))
- }
- if len(list) == 0 {
- return
- }
- allCache = &SubPush{
- Date: nowFormat,
- Datas: list,
- Count: countSearch,
- }
- if spqp.IsEnt == false && spqp.NotReturnCount == 0 {
- s.PutAllCache(spqp.UserId, spqp.UserType, allCache)
- }
- }
- length := len(allCache.Datas)
- if end > length {
- end = length
- }
- if start < length {
- result = allCache.Datas[start:end]
- } else {
- result = allCache.Datas
- }
- total = allCache.Count
- } else {
- if strings.Contains(strings.Join(bsp.SelectType, ","), "detail") {
- result, total = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true, true)
- } else {
- result, total = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true, false)
- }
- }
- if result == nil {
- result = []*bxsubscribe.SubscribeInfo{}
- }
- hasNextPage = total > int64(end)
- return
- }
- func (s *subscribePush) inactiveQuery(spqp *SubPushQueryParam, bsp *ViewCondition) ([]*bxsubscribe.SubscribeInfo, int64) {
- list := &[]map[string]interface{}{}
- count := int64(0)
- if len(spqp.SelectInfoIds) > 0 {
- //通过infoId查数据
- list = elastic.Get("bidding", "bidding", fmt.Sprintf(query, strings.Join(spqp.SelectInfoIds, `","`), len(spqp.SelectInfoIds)))
- count = gconv.Int64(len(*list))
- } else {
- if len(bsp.Keyword) == 0 || spqp.PageNum*spqp.PageSize > 500 {
- return []*bxsubscribe.SubscribeInfo{}, int64(0)
- }
- //通过条件查询
- starttime := int64(0)
- endtime := int64(0)
- if len(strings.Split(spqp.SelectTime, "_")) == 2 {
- st := strings.Split(spqp.SelectTime, "_")[0]
- et := strings.Split(spqp.SelectTime, "_")[1]
- if st == "" {
- starttime = time.Now().AddDate(0, 0, -15).Unix()
- } else {
- starttime, _ = strconv.ParseInt(st, 0, 64)
- if starttime < time.Now().AddDate(0, 0, -15).Unix() {
- starttime = time.Now().AddDate(0, 0, -15).Unix()
- }
- }
- if et == "" {
- endtime = time.Now().Unix()
- } else {
- endtime, _ = strconv.ParseInt(et, 0, 64)
- if endtime < time.Now().AddDate(0, 0, -15).Unix() {
- endtime = time.Now().AddDate(0, 0, -15).Unix()
- }
- }
- } else {
- starttime = time.Now().AddDate(0, 0, -15).Unix()
- endtime = time.Now().Unix()
- }
- qstr := s.getFreeDatasSQL(bsp, starttime, endtime, (spqp.PageNum-1)*spqp.PageSize, spqp.PageSize)
- //list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSort, bidField, 0, 4500, 0, false)
- count, list = elastic.GetWithCount(INDEX, TYPE, "", qstr)
- }
- if count > 500 {
- count = 500
- }
- log.Println(count, list)
- //数据清洗
- size := spqp.PageSize
- if spqp.PageSize == 0 {
- size = len(spqp.SelectInfoIds)
- }
- array := make([]*bxsubscribe.SubscribeInfo, len(*list))
- for i, m := range *list {
- if i == size {
- break
- }
- formatInfo := &bxsubscribe.SubscribeInfo{}
- infoid := common.InterfaceToStr(m["_id"])
- matchkeys := getKeys(common.InterfaceToStr(m["title"]), bsp.Keyword)
- if len(spqp.SelectInfoIds) > 0 {
- formatInfo = &bxsubscribe.SubscribeInfo{XId: encrypt.EncodeArticleId2ByCheck(infoid), MatchKeys: matchkeys}
- } else {
- industry := common.ObjToString(m["s_subscopeclass"])
- scs := strings.Split(industry, ",")
- if len(scs) > 0 {
- industry = scs[0]
- if industry != "" {
- iss := strings.Split(industry, "_")
- if len(iss) > 0 {
- industry = iss[0]
- }
- }
- }
- infotype := common.ObjToString(m["subtype"])
- if infotype == "" {
- infotype = common.ObjToString(m["toptype"])
- }
- area := common.If(m["area"] == nil, "", common.If(common.ObjToString(m["area"]) == "A", "全国", common.ObjToString(m["area"])))
- formatInfo = &bxsubscribe.SubscribeInfo{
- XId: encrypt.EncodeArticleId2ByCheck(infoid),
- Title: common.InterfaceToStr(m["title"]),
- Area: gconv.String(area),
- City: common.InterfaceToStr(m["city"]),
- BuyerClass: common.InterfaceToStr(m["buyerclass"]),
- Subtype: infotype,
- Industry: industry,
- PublishTime: common.Int64All(m["publishtime"]),
- CaDate: gconv.Int64(m["publishtime"]),
- CaIsvisit: 0,
- CaIsvip: 0,
- CaType: 0,
- MatchKeys: matchkeys,
- Budget: common.Float64All(m["budget"]),
- BidAmount: common.Float64All(m["bidamount"]),
- Buyer: common.InterfaceToStr(m["buyer"]),
- ProjectName: common.InterfaceToStr(m["projectname"]),
- Winner: common.InterfaceToStr(m["s_winner"]),
- BidOpenTime: common.Int64All(m["bidopentime"]),
- CaFileExists: m["filetext"] != nil,
- Source: 1,
- Site: common.InterfaceToStr(m["site"]),
- SpiderCode: common.InterfaceToStr(m["spidercode"]),
- Toptype: common.InterfaceToStr(m["toptype"]),
- IsValidFile: m["filetext"] != nil,
- SourceAll: "1",
- District: common.InterfaceToStr(m["district"]),
- }
- }
- array[i] = formatInfo
- }
- return array, count
- }
- var topTypeMap = map[string]string{
- "招标预告": "预告",
- "招标公告": "招标",
- "招标结果": "结果",
- "招标信用信息": "其它",
- }
- func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, endtime int64, size int, isLimit, isDetail bool) (result []*bxsubscribe.SubscribeInfo, count int64) {
- start := time.Now().Unix()
- querys := []string{}
- userStr := " 1=1 "
- var (
- countSql, findSql string
- list []map[string]interface{}
- )
- querys = append(querys, " ")
- if spqp.SelectInfoIds != nil && len(spqp.SelectInfoIds) > 0 {
- var (
- selectInfoIds []string
- )
- for _, sId := range spqp.SelectInfoIds {
- selectInfoIds = append(selectInfoIds, sId)
- if len(selectInfoIds) == 50 {
- 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, "','")))
- logx.Info(spqp.NewUserId, "subscribePush info ids query sql:", findSelectSql)
- list = append(list, *spqp.BaseServiceMysql.SelectBySql(findSelectSql)...)
- selectInfoIds = []string{}
- }
- }
- if len(selectInfoIds) > 0 {
- 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, "','")))
- logx.Info(spqp.NewUserId, "subscribePush info ids query sql:", findSelectSql)
- list = append(list, *spqp.BaseServiceMysql.SelectBySql(findSelectSql)...)
- selectInfoIds = []string{}
- }
- //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, "','")))
- //logx.Info("countSql", countSql)
- //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, "','")))
- ////查询总数
- //count = spqp.BaseServiceMysql.CountBySql(countSql)
- } else {
- //公共查询条件
- //时间
- if starttime > 0 && endtime > 0 {
- querys = append(querys, fmt.Sprintf(" a.date>=%d and a.date<=%d", starttime, endtime))
- } else if starttime > 0 && endtime == 0 {
- querys = append(querys, fmt.Sprintf(" a.date>=%d", starttime))
- } else if starttime == 0 && endtime > 0 {
- querys = append(querys, fmt.Sprintf(" a.date<=%d", endtime))
- } else {
- querys = append(querys, " a.date>0")
- }
- var regionArr = []string{}
- if spqp.Area != "" || spqp.City != "" || spqp.District != "" {
- //城市
- city := []string{}
- for _, v := range strings.Split(spqp.City, ",") {
- if P.BidCodeMapping.City[v] != "" {
- city = append(city, fmt.Sprint(P.BidCodeMapping.City[v]))
- }
- }
- if len(city) > 0 {
- regionArr = append(regionArr, fmt.Sprintf(" a.city in (%s) ", strings.Join(city, ",")))
- }
- //区域
- area := []string{}
- for _, v := range strings.Split(spqp.Area, ",") {
- if P.BidCodeMapping.Area[v] != "" {
- area = append(area, fmt.Sprint(P.BidCodeMapping.Area[v]))
- }
- }
- if len(area) > 0 {
- regionArr = append(regionArr, fmt.Sprintf(" a.area in (%s) ", strings.Join(area, ",")))
- }
- //区域
- district := []string{}
- if spqp.District != "" {
- for _, v := range strings.Split(spqp.District, ",") {
- cityName := strings.Split(v, "_")[0]
- districtName := strings.Split(v, "_")[1]
- if P.BidCodeMapping.District[cityName][districtName] != "" {
- district = append(district, fmt.Sprint(P.BidCodeMapping.District[cityName][districtName]))
- }
- }
- }
- if len(district) > 0 {
- regionArr = append(regionArr, fmt.Sprintf(" a.district in (%s) ", strings.Join(district, ",")))
- }
- if len(regionArr) > 0 {
- querys = append(querys, fmt.Sprintf("(%s)", strings.Join(regionArr, "or")))
- }
- }
- //采购单位行业
- if spqp.Buyerclass != "" {
- buyerclass := []string{}
- for _, v := range strings.Split(spqp.Buyerclass, ",") {
- if fmt.Sprint(P.BidCodeMapping.Buyerclass[v]) != "" {
- buyerclass = append(buyerclass, fmt.Sprint(P.BidCodeMapping.Buyerclass[v]))
- }
- }
- if len(buyerclass) == 1 {
- buyerclass = append(buyerclass, "9999")
- }
- if len(buyerclass) > 0 {
- querys = append(querys, fmt.Sprintf("a.buyerclass in (%s)", strings.Join(buyerclass, ",")))
- }
- }
- //信息类型
- if spqp.Subtype != "" {
- var topType []string
- typeInt := 0
- stype := strings.Split(spqp.Subtype, ",")
- for _, v := range stype {
- if tType := topTypeMap[v]; tType != "" {
- topType = append(topType, tType)
- typeInt += 1
- continue
- }
- }
- var subtype []string
- var toptype []string
- //subtype里都是二级级信息类型
- if typeInt == 0 {
- for _, v := range stype {
- subtype = append(subtype, P.BidCodeMapping.Subtype[v])
- }
- if len(subtype) == 1 {
- subtype = append(subtype, "9999")
- }
- } else if typeInt == len(stype) { // subtype里边 都是一级信息类型
- for _, v := range topType {
- toptype = append(toptype, P.BidCodeMapping.Toptype[v])
- }
- if len(topType) == 1 {
- toptype = append(toptype, "9999")
- }
- } else {
- for _, v := range stype {
- if P.BidCodeMapping.Subtype[v] != "" {
- subtype = append(subtype, P.BidCodeMapping.Subtype[v])
- }
- }
- if len(subtype) == 1 {
- subtype = append(subtype, "9999")
- }
- for _, v := range topType {
- toptype = append(toptype, P.BidCodeMapping.Toptype[v])
- }
- if len(subtype) == 1 {
- toptype = append(toptype, "9999")
- }
- }
- if len(subtype) > 0 && len(toptype) > 0 {
- s1 := fmt.Sprintf("(%s or %s)", fmt.Sprintf("a.subtype in (%s)", strings.Join(subtype, ",")),
- fmt.Sprintf("a.toptype in (%s)", strings.Join(toptype, ",")))
- querys = append(querys, s1)
- } else if len(subtype) > 0 {
- querys = append(querys, fmt.Sprintf("a.subtype in (%s)", strings.Join(subtype, ",")))
- } else if len(toptype) > 0 {
- querys = append(querys, fmt.Sprintf("a.toptype in (%s)", strings.Join(toptype, ",")))
- }
- }
- //信息行业
- if spqp.Subscopeclass != "" {
- find_in_set := []string{}
- for _, v := range strings.Split(spqp.Subscopeclass, ",") {
- find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',a.subscopeclass)", P.BidCodeMapping.Subscopeclass[v]))
- }
- if len(find_in_set) == 1 {
- querys = append(querys, find_in_set[0])
- } else if len(find_in_set) > 1 {
- querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
- }
- }
- //关键词
- find_in_set := []string{}
- if spqp.IsEnt {
- if spqp.Key != "" {
- for _, v := range strings.Split(spqp.Key, ",") {
- find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
- }
- }
- if spqp.Item != "" {
- for _, v := range strings.Split(spqp.Item, ",") {
- find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchitems,' ',','))", v))
- }
- }
- } else {
- if spqp.Key != "" {
- for _, v := range strings.Split(spqp.Key, ",") {
- find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
- }
- }
- if spqp.Item != "" {
- //通过分类名搜索
- keyArr := s.ItemToKey(spqp)
- for _, v := range strings.Split(keyArr, ",") {
- find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(a.matchkeys,' ',','))", v))
- }
- }
- }
- if len(find_in_set) == 1 {
- querys = append(querys, find_in_set[0])
- } else if len(find_in_set) > 1 {
- querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
- }
- //价格- 预算和中标金额
- if spqp.Price != "" && strings.Contains(spqp.Price, "-") {
- minPriceStr, maxPriceStr := strings.Split(spqp.Price, "-")[0], strings.Split(spqp.Price, "-")[1]
- minPrice := common.Int64All(common.Float64All(minPriceStr) * 10000) //换成元
- maxPrice := common.Int64All(common.Float64All(maxPriceStr) * 10000) //换成元
- if minPriceStr != "" && maxPriceStr != "" {
- 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))
- } else if minPriceStr != "" {
- querys = append(querys, fmt.Sprintf("(a.bidamount>=%d or (a.budget>=%d and a.bidamount is null))", minPrice, minPrice))
- } else if maxPriceStr != "" {
- querys = append(querys, fmt.Sprintf("(a.bidamount<=%d or (a.budget<=%d and a.bidamount is null))", maxPrice, maxPrice))
- }
- }
- //附件
- if spqp.FileExists != "" {
- if spqp.FileExists == "1" {
- querys = append(querys, fmt.Sprintf("a.attachment_count is not null"))
- querys = append(querys, fmt.Sprintf("a.attachment_count > 0"))
- } else if spqp.FileExists == "-1" {
- //querys = append(querys, fmt.Sprintf("a.attachment_count is null"))
- querys = append(querys, fmt.Sprintf("(a.attachment_count is null OR a.attachment_count = 0)"))
- }
- }
- // 是否已读
- if spqp.IsRead != "" {
- if spqp.IsRead == "0" {
- querys = append(querys, " a.isvisit IS NULL")
- } else if spqp.IsRead == "1" {
- querys = append(querys, " a.isvisit=1")
- }
- }
- // 信息来源
- if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
- querys = append(querys, " a.source="+spqp.Source)
- }
- if spqp.IsEnt {
- var staffs []string
- userStr += fmt.Sprintf(" and a.entid='%s' ", spqp.EntId)
- //判断是企业管理员还是部门管理员 部门管理员获取所有子部门
- userEnt := EntInfo(common.IntAll(spqp.EntId), common.IntAll(spqp.EntUserId))
- if !(userEnt.Role_admin_system || userEnt.Role_admin_department) {
- result = []*bxsubscribe.SubscribeInfo{}
- return
- }
- if userEnt.Role_admin_department { //
- // 部门管理员 获取所有部门和子部门员工
- users := GetDisUsers(common.IntAll(spqp.EntId), userEnt.Dept.Id)
- if users != nil && len(*users) > 0 {
- for _, v := range *users {
- if len(spqp.Staffs) > 0 {
- for _, vv := range spqp.Staffs {
- if fmt.Sprintf("%d", v.Id) == vv {
- staffs = append(staffs, common.InterfaceToStr(v.Id))
- }
- }
- } else {
- staffs = append(staffs, common.InterfaceToStr(v.Id))
- }
- }
- } else {
- log.Printf("部门管理员为获取到部门员工:%s", spqp.EntUserId)
- result = []*bxsubscribe.SubscribeInfo{}
- return
- }
- } else if len(spqp.Staffs) > 0 {
- staffs = spqp.Staffs
- }
- // 无查询分配人员、是否已读
- if spqp.IsRead == "" && len(staffs) == 0 {
- //查询数量(需要去重)
- countSql = fmt.Sprintf("select count(1) from %s a where %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
- //列表查询
- 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",
- aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
- } else {
- //查询分配人员或是否已读
- staffQuery := " and 1=1 "
- if len(spqp.Staffs) > 0 {
- staffQuery += fmt.Sprintf(" and a.userid in ('%s') ", strings.Join(staffs, "','"))
- } else if len(staffs) > 0 {
- staffQuery += fmt.Sprintf(" and a.userid in (0,'%s') ", strings.Join(staffs, "','"))
- }
- countSql = fmt.Sprintf("select count(1) from %s a where %s %s %s ", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "), staffQuery)
- //列表查询
- 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)
- }
- } else {
- // 是否已读
- if spqp.IsRead != "" {
- if spqp.IsRead == "0" {
- querys = append(querys, " a.isvisit IS NULL")
- } else if spqp.IsRead == "1" {
- querys = append(querys, " a.isvisit=1")
- }
- }
- // 信息来源
- if spqp.Source == "1" || spqp.Source == "2" || spqp.Source == "3" {
- querys = append(querys, " a.source="+spqp.Source)
- }
- if spqp.PositionType == 1 {
- //企业主体是企业的个人查询
- if spqp.UserType == SubVipFlag {
- 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)))
- } else if spqp.UserType == MemberFlag {
- 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)))
- } else if spqp.UserType == EntnicheFlag {
- 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)))
- } else {
- userStr += fmt.Sprintf(" and a.userid='%s' ", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
- }
- } else {
- if s.ModuleFlag == EntnicheFlag {
- //老商机管理个人订阅查询
- userStr += fmt.Sprintf(" and a.userid='%s' and a.product is null", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
- } else {
- //个人订阅查询
- userStr += fmt.Sprintf(" and a.userid='%s'", common.If(s.ModuleFlag == EntnicheFlag, spqp.EntUserId, common.InterfaceToStr(spqp.NewUserId)))
- }
- }
- //查询数量
- countSql = fmt.Sprintf("select count(1) as count from %s a where %s %s", aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
- //列表查询语句
- findSql = "select a.id,a.date,a.infoid,a.matchways,a.isvisit,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.type,a.attachment_count"
- if s.ModuleFlag == EntnicheFlag {
- findSql += ", IF(a.source is NULL,1,a.source) as source "
- }
- if s.ModuleFlag != MemberFlag && s.ModuleFlag != EntnicheFlag {
- findSql += ",a.isvip"
- }
- findSql = fmt.Sprintf("%s from %s a where %s %s "+
- " order by a.date desc,a.id desc", findSql, aboutDbMsg[s.ModuleFlag].MysqlTable, userStr, strings.Join(querys, " and "))
- }
- //查询总数
- logx.Info("countSql", countSql)
- if spqp.NotReturnCount == 0 {
- count = spqp.BaseServiceMysql.CountBySql(countSql)
- }
- log.Println("数据总数查询", count)
- if isLimit {
- findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
- }
- list = *spqp.BaseServiceMysql.SelectBySql(findSql)
- logx.Info(spqp.NewUserId, "subscribePush query sql:", findSql)
- }
- end1 := time.Now().Unix()
- log.Println("mysql查询用时:", end1-start)
- if list != nil && len(list) > 0 {
- pushCas := s.GetJyPushs(list)
- if !spqp.Export {
- result = s.GetInfoByIds(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, pushCas, spqp.IsPayUser, isDetail)
- } else {
- result = s.GetOnlyExportInfo(pushCas)
- }
- } else {
- result = []*bxsubscribe.SubscribeInfo{}
- }
- end2 := time.Now().Unix()
- log.Println("处理数据用时:", end2-end1)
- log.Println("总用时用时:", end2-start)
- return
- }
- // GetOnlyInfoId 获取信息id
- func (s *subscribePush) GetOnlyExportInfo(pushCas []*PushCa) []*bxsubscribe.SubscribeInfo {
- array := make([]*bxsubscribe.SubscribeInfo, len(pushCas))
- for k, v := range pushCas {
- array[k] = &bxsubscribe.SubscribeInfo{XId: encrypt.EncodeArticleId2ByCheck(v.InfoId), MatchKeys: v.Keys}
- }
- return array
- }
- // 根据id取内容
- func (s *subscribePush) GetInfoByIds(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, pushCas []*PushCa, isPay, isDetail bool) []*bxsubscribe.SubscribeInfo {
- array := make([]*bxsubscribe.SubscribeInfo, len(pushCas))
- if len(pushCas) == 0 {
- return array
- }
- m := map[string]bool{}
- ids := []string{}
- for _, v := range pushCas {
- if m[v.InfoId] {
- continue
- }
- m[v.InfoId] = true
- ids = append(ids, v.InfoId)
- }
- infos := getInfosByIds(Mgo_bidding, bidding, bidding_back, isDetail, ids)
- for k, v := range pushCas {
- info := infos[v.InfoId]
- if info == nil {
- info = map[string]interface{}{}
- }
- array[k] = s.InfoFormat(v, &info, isPay, isDetail)
- }
- return array
- }
- func getDetail(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, subPush *SubPush) {
- var ids []string
- for _, data := range subPush.Datas {
- id := encrypt.DecodeArticleId2ByCheck(data.XId)
- ids = append(ids, id[0])
- }
- infos := getInfosByIds(Mgo_bidding, bidding, bidding_back, true, ids)
- for _, data := range subPush.Datas {
- id := encrypt.DecodeArticleId2ByCheck(data.XId)
- if infos[id[0]] == nil {
- continue
- }
- data.Detail = subStrDetail(strings.Join(data.MatchKeys, " "), common.ObjToString(infos[id[0]]["detail"]))
- }
- }
- func getInfosByIds(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, isDetail bool, ids []string) map[string]map[string]interface{} {
- infos := map[string]map[string]interface{}{}
- //redis
- pool := make(chan bool, 3)
- wait := &sync.WaitGroup{}
- lock := &sync.Mutex{}
- es_ids := []string{}
- for _, vv := range ids {
- pool <- true
- wait.Add(1)
- go func(v string) {
- defer func() {
- <-pool
- wait.Done()
- }()
- //剑鱼程序未找到赋值 位置;猜测在推送程序中
- info_i := redis.Get("pushcache_1", fmt.Sprintf("info_%s", v))
- lock.Lock()
- defer lock.Unlock()
- if info_i != nil {
- info_m, _ := info_i.(map[string]interface{})
- info_m["_id"] = v
- infos[v] = info_m
- } else {
- es_ids = append(es_ids, v)
- }
- }(vv)
- }
- wait.Wait()
- //elasticsearch
- if len(es_ids) > 0 {
- list := elastic.Get("bidding", "bidding", fmt.Sprintf(common.If(isDetail, query1, query).(string), strings.Join(es_ids, `","`), len(es_ids)))
- if list != nil {
- for _, v := range *list {
- _id := common.ObjToString(v["_id"])
- infos[_id] = v
- }
- }
- }
- //mongodb bidding
- mgo_ids := []primitive.ObjectID{}
- for _, v := range es_ids {
- if infos[v] == nil {
- _id, _ := primitive.ObjectIDFromHex(v)
- mgo_ids = append(mgo_ids, _id)
- }
- }
- if len(mgo_ids) > 0 {
- 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)
- if ok && *list != nil {
- for _, v := range *list {
- _id := mongodb.BsonIdToSId(v["_id"])
- v["_id"] = _id
- infos[_id] = v
- }
- }
- }
- //mongodb bidding_back
- mgo_back_ids := []primitive.ObjectID{}
- for _, v := range mgo_ids {
- if infos[mongodb.BsonIdToSId(v)] == nil {
- mgo_back_ids = append(mgo_back_ids, v)
- }
- }
- if len(mgo_back_ids) > 0 {
- 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)
- if ok && *list != nil {
- for _, v := range *list {
- _id := mongodb.BsonIdToSId(v["_id"])
- v["_id"] = _id
- infos[_id] = v
- }
- }
- }
- return infos
- }
- // 获取历史推送
- func (s *subscribePush) GetJyPushs(datas []map[string]interface{}) (pushCas []*PushCa) {
- pushCas = []*PushCa{}
- for _, v := range datas {
- keys := []string{}
- if matchkeys := common.ObjToString(v["matchkeys"]); matchkeys != "" {
- keys = strings.Split(matchkeys, " ")
- }
- pushCas = append(pushCas, &PushCa{
- Date: common.Int64All(v["date"]),
- InfoId: common.ObjToString(v["infoid"]),
- Visit: common.IntAll(v["isvisit"]),
- Index: common.Int64All(v["id"]),
- Keys: keys,
- Type: common.IntAll(v["type"]),
- Isvip: common.IntAll(v["isvip"]),
- FileExists: common.IntAll(v["attachment_count"]) > 0,
- Source: common.Int64All(v["source"]),
- SourceAll: common.ObjToString(v["sourceAll"]),
- MatchWays: common.ObjToString(v["matchways"]),
- })
- }
- return
- }
- // 查看全部列表缓存
- func (s *subscribePush) PutAllCache(userId, userType string, datas *SubPush) {
- //log.Println("pushcache_2_a", s.allKey(userId, userType), datas, oneDay)
- redis.Put("pushcache_2_a", s.allKey(userId, userType), datas, oneDay)
- }
- func (s *subscribePush) GetAllCache(userId, userType string) (*SubPush, error) {
- return s.GetCache("pushcache_2_a", s.allKey(userId, userType))
- }
- func (s *subscribePush) GetCache(code, key string) (*SubPush, error) {
- pc_a, err := redis.GetNewBytes(code, key)
- if err != nil {
- return nil, err
- }
- if pc_a == nil {
- return nil, nil
- }
- var p *SubPush
- if err := json.Unmarshal(*pc_a, &p); err != nil {
- return nil, err
- }
- return p, nil
- }
- // 是否收藏
- func (s *subscribePush) MakeCollection(userId string, list []*bxsubscribe.SubscribeInfo) {
- if list == nil || len(list) == 0 {
- return
- }
- param := []interface{}{userId}
- wh := []string{}
- for _, v := range list {
- array := encrypt.DecodeArticleId2ByCheck(v.XId)
- if len(array) == 1 && array[0] != "" {
- param = append(param, array[0])
- wh = append(wh, "?")
- }
- }
- if len(wh) > 0 {
- result := IC.MainMysql.SelectBySql(`select bid from bdcollection where userid=? and bid in (`+strings.Join(wh, ",")+`)`, param...)
- bid_map := map[string]bool{}
- if result != nil {
- for _, v := range *result {
- bid_map[encrypt.EncodeArticleId2ByCheck(common.ObjToString(v["bid"]))] = true
- }
- }
- for _, v := range list {
- if bid_map[v.XId] {
- v.Collection = 1
- }
- }
- }
- }
- // 仅移动端首页使用,历史推送7天信息
- func (s *subscribePush) sevenDayKey(userId string) string {
- return fmt.Sprintf("7day_subpush_%s", userId)
- }
- func (s *subscribePush) PutSevenDayCache(userId string, datas []*bxsubscribe.SubscribeInfo) {
- redis.Put("pushcache_2_a", s.sevenDayKey(userId), SubPush{Datas: datas}, 7*24*60*60)
- }
- // 从pushcache_2_a中取
- func (s *subscribePush) GetSevenDayCache(userId string) ([]*bxsubscribe.SubscribeInfo, error) {
- allPush, _ := s.GetCache("pushcache_2_a", s.sevenDayKey(userId))
- if allPush != nil && allPush.Datas != nil && len(allPush.Datas) > 0 {
- return allPush.Datas, nil
- }
- return nil, nil
- }
- // 历史推送记录中单条信息格式化
- func InfoFormats(info map[string]interface{}, tmp map[string]interface{}) map[string]interface{} {
- area := common.ObjToString(info["area"])
- if area == "A" {
- area = "全国"
- }
- industry := common.ObjToString(info["s_subscopeclass"])
- scs := strings.Split(industry, ",")
- if len(scs) > 0 {
- industry = scs[0]
- if industry != "" {
- iss := strings.Split(industry, "_")
- if len(iss) > 0 {
- industry = iss[0]
- }
- }
- }
- infotype := common.ObjToString(info["subtype"])
- if infotype == "" {
- infotype = common.ObjToString(info["toptype"])
- }
- if infotype == "" {
- infotype = common.ObjToString(info["type"])
- if infotype == "tender" {
- infotype = "招标"
- } else if infotype == "bid" {
- infotype = "中标"
- }
- }
- info["type"] = infotype
- return info
- }
- // UpdateUserPushUnread 更新app未读标识为已读
- func UpdateUserPushUnread(userid string, vt string) {
- if vt == MemberFlag {
- IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_member_apppushunread": 0}})
- } else if vt == EntnicheFlag {
- IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_entniche_apppushunread": 0}})
- } else {
- IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_apppushunread": 0}})
- }
- }
- func (s *subscribePush) SetRead(newUserId, id int64, userId, entUserId, entId string, isEnt bool, userType string) error {
- if id <= 0 && newUserId == 0 {
- return nil
- }
- if newUserId == 0 {
- return nil
- }
- 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)
- if s.ModuleFlag == EntnicheFlag {
- userId = entUserId
- }
- todaySubPush, err := s.GetTodayCache(userId, userType)
- //当天数据处理
- if err == nil && todaySubPush != nil {
- for _, v := range todaySubPush.Datas {
- if v.CaIndex == id {
- v.CaIsvisit = 1
- break
- }
- }
- s.PutTodayCache(userId, userType, todaySubPush)
- }
- //全部数据处理
- allSubPush, err := s.GetAllCache(userId, userType)
- if err == nil && allSubPush != nil {
- for _, v := range allSubPush.Datas {
- if v.CaIndex == id {
- v.CaIsvisit = 1
- break
- }
- }
- s.PutAllCache(userId, userType, allSubPush)
- }
- return nil
- }
- // UserInfo 获取用户信息
- func (s *subscribePush) UserInfo(baseUserId int64) (*map[string]interface{}, int64) {
- 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}`)
- if !ok || user == nil {
- return nil, 0
- }
- return user, common.Int64All((*user)["l_firstpushtime"])
- }
- // GetKeySet 是否有订阅词
- func GetKeySet(t string, u *map[string]interface{}, data []string) (bool, []string) {
- var industry_ = []string{}
- if u != nil {
- if t == SubFreeFlag {
- o_jy, _ := (*u)["o_jy"].(map[string]interface{})
- a_key, _ := o_jy["a_key"].([]interface{})
- return len(a_key) > 0, industry_
- } else {
- var obj map[string]interface{}
- if t == SubVipFlag {
- obj, _ = (*u)["o_vipjy"].(map[string]interface{})
- } else if t == MemberFlag {
- obj, _ = (*u)["o_member_jy"].(map[string]interface{})
- } else if t == EntnicheFlag {
- if len(data) > 0 {
- return true, data
- } else {
- return false, data
- }
- }
- if obj != nil {
- if buyerclassObj, ok := obj["a_buyerclass"].([]interface{}); ok {
- industry_ = common.ObjArrToStringArr(buyerclassObj)
- }
- itmes, _ := obj["a_items"].([]interface{})
- for _, v := range itmes {
- item, _ := v.(map[string]interface{})
- keys, _ := item["a_key"].([]interface{})
- if len(keys) > 0 {
- return true, industry_
- }
- }
- }
- }
- }
- return false, industry_
- }
- const (
- INDEX = "bidding"
- TYPE = "bidding"
- 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"`
- 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"`
- bidTime = `{"range":{"publishtime":{"gt":%d}}}`
- bidSort = `{"publishtime":"desc"}`
- findfields = `"title"`
- )
- // 首次访问推送页面 默认生成推送数据
- // 默认匹配es 15天内数据
- func (s *subscribePush) DefaultDatas(spqp *SubPushQueryParam, bsp *ViewCondition, userType string) (hasNextPage bool, total int64, result []*bxsubscribe.SubscribeInfo) {
- if spqp.UserId == "" {
- return false, 0, nil
- }
- logx.Info("---userId:", spqp.UserId, "newUserId:", spqp.NewUserId)
- t1 := time.Now()
- if len(bsp.Keyword) > 0 {
- logx.Info("--bsp:", bsp)
- //获取查询语句
- qstr := s.getDefaultDatasSQL(bsp)
- // 查询方式包含有正文,返回结果添加正文
- 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)
- logx.Info(time.Since(t1), "list count:", len(*list))
- if list != nil && len(*list) > 0 {
- var mlist []map[string]interface{}
- esM := listHistory(spqp)
- if len(esM) > 0 {
- for _, m := range *list {
- if !esM[common.ObjToString(m["_id"])] {
- mlist = append(mlist, m)
- }
- }
- } else {
- mlist = *list
- }
- logx.Info(time.Since(t1), "mlist count:", len(mlist))
- total = int64(len(mlist))
- result = s.listManager(spqp, mlist, bsp.Keyword)
- if len(result) > PageSize {
- result = result[:PageSize]
- hasNextPage = true
- }
- }
- }
- if spqp.SubPushInactive == -1 && spqp.PositionType == 0 && (userType == "vType" || userType == "fType") {
- IC.Mgo.UpdateById("user", spqp.UserId, bson.M{"$set": bson.M{"subpush_inactive": -2}})
- }
- logx.Info("请求耗时:", time.Since(t1))
- return
- }
- // 历史订阅消息
- func listHistory(spqp *SubPushQueryParam) map[string]bool {
- m := make(map[string]bool)
- sql := "SELECT infoid FROM push.pushsubscribe where userid = ? order by date desc, id desc limit 1000;"
- info := spqp.BaseServiceMysql.SelectBySql(sql, spqp.NewUserId)
- for _, m1 := range *info {
- m[common.ObjToString(m1["infoid"])] = true
- }
- return m
- }
- // 保存推送表
- func (s *subscribePush) listManager(spqp *SubPushQueryParam, list []map[string]interface{}, keyword []ViewKeyWord) (resultList []*bxsubscribe.SubscribeInfo) {
- now := time.Now().Unix()
- length := len(list)
- resultList = make([]*bxsubscribe.SubscribeInfo, length)
- pushInsert := []interface{}{}
- var product interface{}
- if spqp.UserType == SubVipFlag {
- product = 1
- } else if spqp.UserType == MemberFlag {
- product = 2
- }
- for i := length - 1; i >= 0; i-- {
- v := list[i]
- title := strings.Replace(common.ObjToString(v["title"]), " ", "", -1)
- infoid := common.InterfaceToStr(v["_id"])
- matchkeys := getKeys(title, keyword)
- matchkey := strings.Join(matchkeys, " ")
- subscopeclassStr := ""
- 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)))
- city := common.If(P.BidCodeMapping.City[common.ObjToString(v["city"])] == "", nil, P.BidCodeMapping.City[common.ObjToString(v["city"])])
- cityName := common.ObjToString(v["city"])
- district := ""
- districtName := common.ObjToString(v["district"])
- if cityName != "" && districtName != "" {
- district = gconv.String(common.If(P.BidCodeMapping.District[cityName][districtName] == "", nil, P.BidCodeMapping.District[cityName][districtName]))
- }
- subtype := common.If(P.BidCodeMapping.Subtype[common.ObjToString(v["subtype"])] == "", nil, P.BidCodeMapping.Subtype[common.ObjToString(v["subtype"])])
- toptype := common.If(P.BidCodeMapping.Toptype[common.ObjToString(v["toptype"])] == "", nil, P.BidCodeMapping.Toptype[common.ObjToString(v["toptype"])])
- buyerclass := common.If(P.BidCodeMapping.Buyerclass[common.ObjToString(v["buyerclass"])] == "", nil, P.BidCodeMapping.Buyerclass[common.ObjToString(v["buyerclass"])])
- attachment_count := 0
- if v["isValidFile"] != nil {
- isValidFile, _ := v["isValidFile"].(bool)
- attachment_count = common.If(isValidFile, 1, 0).(int)
- }
- bidamount := common.If(v["bidamount"] == nil, nil, v["bidamount"])
- budget := common.If(v["budget"] == nil, nil, v["budget"])
- if s_subscopeclass := common.ObjToString(v["s_subscopeclass"]); s_subscopeclass != "" {
- subscopeclass := []string{}
- for _, v := range strings.Split(s_subscopeclass, ",") {
- if subscopeclass_mapping, ok := P.BidCodeMapping.Subscopeclass[v]; ok {
- subscopeclass = append(subscopeclass, fmt.Sprint(subscopeclass_mapping))
- }
- }
- if len(subscopeclass) > 0 {
- subscopeclassStr = strings.Join(subscopeclass, ",")
- }
- }
- entid := spqp.EntId
- entUserId := spqp.EntUserId
- deptid := spqp.DeptId
- //公共字段处理
- pushInsert = append(pushInsert, common.InterfaceToStr(infoid), matchkey, now, area, city, district, buyerclass, toptype, subtype, subscopeclassStr, budget, bidamount, attachment_count)
- switch s.ModuleFlag {
- case EntnicheFlag:
- pushInsert = append(pushInsert, entUserId, entid, deptid, 1, product)
- case MemberFlag:
- pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId))
- case SubVipFlag:
- pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId), 1)
- case SubFreeFlag:
- pushInsert = append(pushInsert, common.InterfaceToStr(spqp.NewUserId), 0)
- }
- redis.Put("pushcache_2_a", fmt.Sprintf("pushinfo_%s_%s", spqp.UserId, common.ObjToString(v["_id"])), 1, 86400)
- redis.Put("pushcache_2_c", fmt.Sprintf("inactiveChange_%s_%s", spqp.UserId, common.ObjToString(v["_id"])), 1, 86400)
- resultList[i] = s.InfoFormat(&PushCa{
- InfoId: infoid,
- Date: time.Now().Unix(),
- Keys: matchkeys,
- FileExists: v["filetext"] != nil,
- }, &v, spqp.IsPayUser, false)
- }
- id := int64(0)
- switch s.ModuleFlag {
- case EntnicheFlag:
- _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, ennicheInsertCollKey, pushInsert)
- case MemberFlag:
- _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, memberInsertCollKey, pushInsert)
- default:
- _, id = spqp.BaseServiceMysql.InsertIgnoreBatch(aboutDbMsg[s.ModuleFlag].MysqlTable, subscribeInsertCollKey, pushInsert)
- }
- for i := length - 1; i >= 0; i-- {
- resultList[i].CaIndex = id
- id++
- }
- return
- }
- // 获取匹配得关键词
- func getKeys(title string, keywords []ViewKeyWord) (str []string) {
- if len(keywords) > 0 {
- L:
- for _, v := range keywords {
- for _, vk := range v.Keyword {
- if strings.Contains(title, vk) {
- str = append(str, v.Keyword...)
- break L
- }
- }
- for _, va := range v.Appended {
- if strings.Contains(title, va) {
- str = append(str, v.Appended...)
- break L
- }
- }
- }
- }
- return
- }
- // 获取查询语句
- func (s *subscribePush) getFreeDatasSQL(bsp *ViewCondition, startTime, endTime int64, startNumb, size int) (str string) {
- query := `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}},"_source":[%s],"sort": [{"publishtime": "desc"}],"from":%d,"size":%d}`
- query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
- multi_match := `{"multi_match": {"query": %s,"type": "phrase", "fields": [%s]}}`
- query_bool_must_and := `{"bool":{"must":[%s]%s}}`
- query_bool_must_and_district := `{"bool":{"must":[{"terms":{"city":["%s"]}},{"terms":{"district":["%s"]}}]}}`
- publishtime := `{"range":{"publishtime":{"gt":%d,"lt":%d}}}`
- bools := []string{}
- musts := []string{}
- //发布时间最新15天(正式环境需要修改)
- musts = append(musts, fmt.Sprintf(publishtime, startTime, endTime))
- //省份
- areaCity := []string{}
- if len(bsp.Area) > 0 {
- areaquery := `{"terms":{"area":[`
- for k, v := range bsp.Area {
- if k > 0 {
- areaquery += `,`
- }
- areaquery += `"` + v + `"`
- }
- areaquery += `]}}`
- areaCity = append(areaCity, areaquery)
- }
- //城市
- if len(bsp.City) > 0 {
- areaquery := `{"terms":{"city":[`
- for k, v := range bsp.City {
- if k > 0 {
- areaquery += `,`
- }
- areaquery += `"` + v + `"`
- }
- areaquery += `]}}`
- areaCity = append(areaCity, areaquery)
- }
- //区域处理
- if len(bsp.District) > 0 {
- for _, v := range bsp.District {
- cityName := strings.Split(v, "_")[0]
- districtName := strings.Split(v, "_")[1]
- areaCity = append(areaCity, fmt.Sprintf(query_bool_must_and_district, cityName, districtName))
- }
- }
- if len(areaCity) > 0 {
- musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaCity, ",")))
- }
- if len(bsp.Subtype) > 0 {
- subquery := `{"terms":{"subtype":[`
- for k, v := range bsp.Subtype {
- if k > 0 {
- subquery += `,`
- }
- subquery += `"` + v + `"`
- }
- subquery += `]}}`
- musts = append(musts, subquery)
- }
- if len(bsp.Buyerclass) > 0 {
- Buyerclass := `{"terms":{"buyerclass":[`
- for k, v := range bsp.Buyerclass {
- if k > 0 {
- Buyerclass += `,`
- }
- Buyerclass += `"` + v + `"`
- }
- Buyerclass += `]}}`
- musts = append(musts, Buyerclass)
- }
- /**
- 100-500
- 100-
- -500
- */
- if bsp.Amount != "" {
- at := strings.Split(bsp.Amount, "-")
- if len(at) == 2 {
- Bidamount := `{"range":{"bidamount":{` // "gte": %d, "lte": %d}}}
- if at[0] != "" {
- minPrice := common.Int64All(common.Float64All(at[0]) * 10000) //换成元
- if minPrice >= 0 {
- Bidamount += fmt.Sprintf("\"gte\": %d", minPrice)
- }
- }
- if at[1] != "" {
- maxPrice := common.Int64All(common.Float64All(at[1]) * 10000)
- if maxPrice > 0 {
- Bidamount += fmt.Sprintf(",\"lte\": %d", maxPrice)
- }
- }
- Bidamount += `}}}`
- musts = append(musts, Bidamount)
- }
- }
- boolsNum := 0 //should
- if len(bsp.Keyword) > 0 {
- boolsNum = 1
- stype := strings.Join(bsp.SelectType, "\",\"")
- if strings.Contains(stype, "projectname/purchasing") {
- stype = strings.Replace(stype, "projectname/purchasing", "projectname,\",\"purchasing", -1)
- }
- multi_match = fmt.Sprintf(multi_match, "%s", "\""+stype+"\"")
- for _, v := range bsp.Keyword {
- shoulds := []string{}
- must_not := []string{}
- //附加词
- for _, vv := range v.Keyword {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- for _, vv := range v.Appended {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- //排除词
- for _, vv := range v.Exclude {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- must_not = append(must_not, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- //添加
- if len(shoulds) > 0 {
- notStr := ""
- if len(must_not) > 0 {
- notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
- }
- bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr))
- }
- }
- }
- qstr := fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","), boolsNum, bidField, startNumb, bsp.Size)
- logx.Info("----", qstr)
- return qstr
- }
- // 获取查询语句
- func (s *subscribePush) getDefaultDatasSQL(bsp *ViewCondition) (str string) {
- query := `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}}`
- query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
- multi_match := `{"multi_match": {"query": %s,"type": "phrase", "fields": [%s]}}`
- query_bool_must_and := `{"bool":{"must":[%s]%s}}`
- query_bool_must_and_district := `{"bool":{"must":[{"terms":{"city":["%s"]}},{"terms":{"district":["%s"]}}]}}`
- bools := []string{}
- musts := []string{}
- //发布时间最新15天(正式环境需要修改)
- musts = append(musts, fmt.Sprintf(bidTime, time.Now().AddDate(0, 0, -15).Unix()))
- //省份
- areaCity := []string{}
- if len(bsp.Area) > 0 {
- areaquery := `{"terms":{"area":[`
- for k, v := range bsp.Area {
- if k > 0 {
- areaquery += `,`
- }
- areaquery += `"` + v + `"`
- }
- areaquery += `]}}`
- areaCity = append(areaCity, areaquery)
- }
- //城市
- if len(bsp.City) > 0 {
- areaquery := `{"terms":{"city":[`
- for k, v := range bsp.City {
- if k > 0 {
- areaquery += `,`
- }
- areaquery += `"` + v + `"`
- }
- areaquery += `]}}`
- areaCity = append(areaCity, areaquery)
- }
- //区域处理
- if len(bsp.District) > 0 {
- for _, v := range bsp.District {
- cityName := strings.Split(v, "_")[0]
- districtName := strings.Split(v, "_")[1]
- areaCity = append(areaCity, fmt.Sprintf(query_bool_must_and_district, cityName, districtName))
- }
- }
- if len(areaCity) > 0 {
- musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaCity, ",")))
- }
- if len(bsp.Subtype) > 0 {
- subquery := `{"terms":{"subtype":[`
- for k, v := range bsp.Subtype {
- if k > 0 {
- subquery += `,`
- }
- subquery += `"` + v + `"`
- }
- subquery += `]}}`
- musts = append(musts, subquery)
- }
- if len(bsp.Buyerclass) > 0 {
- Buyerclass := `{"terms":{"buyerclass":[`
- for k, v := range bsp.Buyerclass {
- if k > 0 {
- Buyerclass += `,`
- }
- Buyerclass += `"` + v + `"`
- }
- Buyerclass += `]}}`
- musts = append(musts, Buyerclass)
- }
- boolsNum := 0 //should
- if len(bsp.Keyword) > 0 {
- boolsNum = 1
- stype := strings.Join(bsp.SelectType, "\",\"")
- if strings.Contains(stype, "projectname/purchasing") {
- stype = strings.Replace(stype, "projectname/purchasing", "projectname,\",\"purchasing", -1)
- }
- multi_match = fmt.Sprintf(multi_match, "%s", "\""+stype+"\"")
- for _, v := range bsp.Keyword {
- shoulds := []string{}
- must_not := []string{}
- //附加词
- for _, vv := range v.Keyword {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- for _, vv := range v.Appended {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- //排除词
- for _, vv := range v.Exclude {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- must_not = append(must_not, fmt.Sprintf(multi_match, "\""+vv+"\""))
- }
- //添加
- if len(shoulds) > 0 {
- notStr := ""
- if len(must_not) > 0 {
- notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
- }
- bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr))
- }
- }
- }
- qstr := fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","), boolsNum)
- logx.Info("----", qstr)
- return qstr
- }
- /*已选条件--关键词*/
- type ViewKeyWord struct {
- Keyword []string `json:"key"` //关键词
- Appended []string `json:"appendkey"` //附加词
- Exclude []string `json:"notkey"` //排除词
- MatchWay int `json:"matchway"` //匹配模式
- }
- /*已选条件*/
- type ViewCondition struct {
- Area []string //地区-省份
- City []string //地区-城市
- District []string //区县
- Buyerclass []string //采购行业
- Keyword []ViewKeyWord //关键词
- SelectType []string //筛选
- Subtype []string //信息类型
- Size int //数量
- Amount string //金额
- }
- // 获取用户信息
- func (s *subscribePush) GetUserInfo(spqp *SubPushQueryParam) (vc *ViewCondition) {
- var isPayBool = false
- var tmpInfo = struct {
- Items []interface{}
- BuyerClass []interface{}
- SubType []interface{}
- Area map[string]interface{}
- District map[string]interface{}
- SelectType []string
- Amount string
- }{}
- switch s.ModuleFlag {
- case MemberFlag:
- 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}`)
- spqp.SubPushInactive = common.IntAll((*userMap)["subpush_inactive"])
- if !ok || userMap == nil || len(*userMap) == 0 {
- return &ViewCondition{}
- }
- //大会员
- if common.IntAll((*userMap)["i_member_status"]) > 0 {
- isPayBool = true
- if gconv.Int((*userMap)["i_member_sub_status"]) == 1 { // 子账号取主账号订阅设置
- zid := gconv.String((*userMap)["s_member_mainid"])
- userMap, _ = IC.Mgo.FindById("user", zid, `{"o_member_jy":1,"i_member_status":1,"subpush_inactive":1}`)
- }
- o_member_jy, _ := (*userMap)["o_member_jy"].(map[string]interface{})
- tmpInfo.Items, _ = o_member_jy["a_items"].([]interface{})
- tmpInfo.BuyerClass, _ = o_member_jy["a_buyerclass"].([]interface{})
- tmpInfo.SubType, _ = o_member_jy["a_infotype"].([]interface{})
- tmpInfo.Area, _ = o_member_jy["o_area"].(map[string]interface{})
- tmpInfo.District, _ = o_member_jy["o_district"].(map[string]interface{})
- if o_member_jy["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(o_member_jy["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- tmpInfo.Amount = common.ObjToString(o_member_jy["amount"])
- }
- case SubVipFlag:
- userMap, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_vipjy":1,"i_vip_status":1,"subpush_inactive":1}`)
- spqp.SubPushInactive = common.IntAll((*userMap)["subpush_inactive"])
- if !ok || userMap == nil || len(*userMap) == 0 {
- return &ViewCondition{}
- }
- if common.IntAll((*userMap)["i_vip_status"]) > 0 {
- isPayBool = true
- o_vipjy, _ := (*userMap)["o_vipjy"].(map[string]interface{})
- tmpInfo.Items, _ = o_vipjy["a_items"].([]interface{})
- tmpInfo.BuyerClass, _ = o_vipjy["a_buyerclass"].([]interface{})
- tmpInfo.SubType, _ = o_vipjy["a_infotype"].([]interface{})
- tmpInfo.Area, _ = o_vipjy["o_area"].(map[string]interface{})
- tmpInfo.District, _ = o_vipjy["o_district"].(map[string]interface{})
- if o_vipjy["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(o_vipjy["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- tmpInfo.Amount = common.ObjToString(o_vipjy["amount"])
- }
- case EntnicheFlag:
- //商机管理
- entUserId, _ := strconv.Atoi(spqp.EntUserId)
- entId, _ := strconv.Atoi(spqp.EntId)
- entInfo, deptId := (&ms.MatchJob{}).Start(entId, entUserId)
- if entInfo != nil && len(entInfo) > 0 {
- spqp.DeptId = strconv.Itoa(deptId)
- if common.IntAll(entInfo["i_type"]) == 2 {
- if entInfo["a_key"] != nil {
- tmpInfo.Items, _ = entInfo["a_key"].([]interface{})
- //信息类型
- tmpInfo.SubType, _ = entInfo["a_infotype"].([]interface{})
- //省份
- tmpInfo.Area, _ = entInfo["o_area"].(map[string]interface{})
- tmpInfo.District, _ = entInfo["o_district"].(map[string]interface{})
- if entInfo["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(entInfo["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- tmpInfo.Amount = common.ObjToString(entInfo["amount"])
- if common.IntAllDef(entInfo["i_ppstatus"], 0) == 1 && entInfo["o_area_p"] != nil {
- tmpInfo.Area, _ = entInfo["o_area_p"].(map[string]interface{})
- }
- }
- } else {
- isPayBool = true
- tmpInfo.Items, _ = entInfo["a_items"].([]interface{})
- tmpInfo.BuyerClass, _ = entInfo["a_buyerclass"].([]interface{})
- tmpInfo.SubType, _ = entInfo["a_infotype"].([]interface{})
- tmpInfo.Area, _ = entInfo["o_area"].(map[string]interface{})
- tmpInfo.District, _ = entInfo["o_district"].(map[string]interface{})
- if entInfo["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(entInfo["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- tmpInfo.Amount = common.ObjToString(entInfo["amount"])
- }
- }
- if spqp.IsEnt {
- res_, _ := IC.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
- "i_userid": map[string]interface{}{
- "$exists": false,
- },
- "i_entid": common.Int64All(spqp.EntId),
- }, `{"o_entniche":1}`)
- if res_ != nil && len(*res_) > 0 {
- if m, ok := (*res_)["o_entniche"].(map[string]interface{}); ok {
- if m["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(m["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- }
- }
- }
- default:
- userMap, ok := IC.Mgo.FindById("user", spqp.UserId, `{"o_jy":1,"subpush_inactive":1}`)
- if !ok || userMap == nil || len(*userMap) == 0 {
- return &ViewCondition{}
- }
- spqp.SubPushInactive = common.IntAll((*userMap)["subpush_inactive"])
- o_jy, _ := (*userMap)["o_jy"].(map[string]interface{})
- if o_jy["a_key"] != nil {
- tmpInfo.Items, _ = o_jy["a_key"].([]interface{})
- //信息类型
- tmpInfo.SubType, _ = o_jy["a_infotype"].([]interface{})
- //省份
- tmpInfo.Area, _ = o_jy["o_area"].(map[string]interface{})
- tmpInfo.District, _ = o_jy["o_district"].(map[string]interface{})
- if o_jy["i_matchmode"] != nil {
- tmpInfo.SelectType = gconv.SliceStr(o_jy["i_matchmode"])
- } else {
- tmpInfo.SelectType = []string{"title"}
- }
- tmpInfo.Amount = common.ObjToString(o_jy["amount"])
- if common.IntAllDef(o_jy["i_ppstatus"], 0) == 1 && o_jy["o_area_p"] != nil {
- tmpInfo.Area, _ = o_jy["o_area_p"].(map[string]interface{})
- }
- }
- }
- vc = &ViewCondition{
- Buyerclass: common.ObjArrToStringArr(tmpInfo.BuyerClass),
- Subtype: common.ObjArrToStringArr(tmpInfo.SubType),
- Area: getStringArrFromDbResult(tmpInfo.Area, 1),
- City: getStringArrFromDbResult(tmpInfo.Area, 2),
- District: districtHandle(tmpInfo.District),
- SelectType: tmpInfo.SelectType,
- Amount: tmpInfo.Amount,
- }
- //付费用户
- if isPayBool {
- vc.Size = IC.C.DefaulCount.Pay
- vc.Keyword = getKeyWordArrFromDbResult(tmpInfo.Items, "", -1)
- } else {
- vc.Size = IC.C.DefaulCount.Free
- vc.Keyword = getKeyWordArrFromDbResultByFree(tmpInfo.Items, "", -1)
- }
- return
- }
- // 区县处理
- func districtHandle(data map[string]interface{}) []string {
- arr := []string{}
- for k, v := range data {
- for _, v1 := range gconv.SliceAny(v) {
- arr = append(arr, fmt.Sprintf("%s_%s", k, v1))
- }
- }
- return arr
- } // 关键词 附加词 排除词
- func getKeyWordArrFromDbResult(a_items []interface{}, item string, index int) (arr []ViewKeyWord) {
- if a_items == nil {
- return
- }
- for _, v := range a_items {
- vm, _ := v.(map[string]interface{})
- if item != "" && index >= 0 && item != common.ObjToString(vm["s_item"]) {
- continue
- }
- kwsArr := vm["a_key"]
- for i, k := range kwsArr.([]interface{}) {
- if item != "" && index >= 0 && i != index {
- continue
- }
- kw := ViewKeyWord{}
- b, e := json.Marshal(k)
- if e != nil {
- log.Println(e.Error())
- }
- json.Unmarshal(b, &kw)
- if kw.MatchWay == 1 {
- for _, kk := range kw.Keyword {
- arr = append(arr, ViewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- for _, kk := range kw.Appended {
- arr = append(arr, ViewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- } else {
- arr = append(arr, kw)
- }
- }
- }
- return
- }
- // //关键词 附加词 排除词 省份订阅包 - 普通用户
- func getKeyWordArrFromDbResultByFree(a_items []interface{}, item string, index int) (arr []ViewKeyWord) {
- if a_items == nil {
- return
- }
- for _, v := range a_items {
- kw := ViewKeyWord{}
- b, e := json.Marshal(v)
- if e != nil {
- log.Println(e.Error())
- }
- json.Unmarshal(b, &kw)
- if kw.MatchWay == 1 {
- for _, kk := range kw.Keyword {
- arr = append(arr, ViewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- for _, kk := range kw.Appended {
- arr = append(arr, ViewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- } else {
- arr = append(arr, kw)
- }
- }
- return
- }
- // 地区格式化
- func getStringArrFromDbResult(area map[string]interface{}, i int) (arr []string) {
- if area == nil {
- return
- }
- var eareArr []string
- var cityArr []string
- for k, v := range area {
- if len(v.([]interface{})) > 0 {
- cityArr = append(cityArr, common.ObjArrToStringArr(v.([]interface{}))...)
- } else {
- eareArr = append(eareArr, k)
- }
- }
- if i == 1 {
- arr = eareArr
- } else {
- arr = cityArr
- }
- return
- }
- // IsInTsGuide 是否进入向导
- func (s *subscribePush) IsInTsGuide(userid string) bool {
- if userid == "" {
- return false
- }
- data, ok := IC.Mgo.FindById("user", userid, `{"o_jy":1,"i_ts_guide":1,"":1,"i_member_status":1,"i_vip_status":1}`)
- if ok {
- //付费用户无免费订阅,不进入订阅向导页面
- if common.IntAll((*data)["i_member_status"]) > 0 || common.IntAll((*data)["i_vip_status"]) > 0 {
- return false
- }
- o_jy, _ := (*data)["o_jy"].(map[string]interface{})
- i_ts_guide := common.IntAll((*data)["i_ts_guide"])
- if i_ts_guide == 2 || (i_ts_guide == 0 && len(o_jy) == 0) {
- return true
- }
- }
- return false
- }
- func (s *subscribePush) Keys(spqp *KeyParam) (result []*bxsubscribe.KeyItems) {
- keyData := map[string][]interface{}{}
- if spqp.IsEnt {
- //企业关键词查找
- res_, _ := IC.Mgo.Find("entniche_rule", map[string]interface{}{
- "i_userid": map[string]interface{}{
- "$exists": false,
- },
- "i_entid": common.Int64All(spqp.EntId),
- }, "", `{"o_entniche":1}`, false, -1, -1)
- o_entniche := map[string]interface{}{}
- if res_ == nil || len(*res_) == 0 {
- return MapToarr(keyData)
- }
- for _, ruleV := range *res_ {
- o_entniche = common.StructToMapMore(ruleV["o_entniche"])
- if o_entniche["a_items"] != nil {
- a_item := common.ObjArrToMapArr(o_entniche["a_items"].([]interface{}))
- for _, v := range a_item {
- if v["a_key"] != nil {
- a_key_arr := v["a_key"].([]interface{})
- s_item := common.InterfaceToStr(v["s_item"])
- if keyData[s_item] != nil {
- //第一次搜索存在
- //去重
- keyData[s_item] = MergeArray(a_key_arr, keyData[s_item])
- } else {
- //第一次搜索不存在
- keyData[s_item] = a_key_arr
- }
- }
- }
- }
- }
- } else {
- //个人关键词查找
- //fType:普通用户;vType:超级订阅用户;mType:大会员用户;eType:商机管理用户
- types := 2
- typeKey := ""
- switch s.ModuleFlag {
- case SubVipFlag:
- types = 1
- typeKey = "o_vipjy"
- case MemberFlag:
- types = 1
- typeKey = "o_member_jy"
- case EntnicheFlag:
- types = 0
- typeKey = "o_entniche"
- case SubFreeFlag:
- types = 2
- typeKey = "o_jy"
- }
- if spqp.PositionType == 1 {
- typeKey = "o_entniche"
- }
- o_entniche := &map[string]interface{}{}
- if spqp.PositionType == 0 {
- 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}`)
- if mData == nil || len(*mData) == 0 || !ok {
- return nil
- }
- if s.ModuleFlag == MemberFlag {
- sub_status := common.Int64All((*mData)["i_member_sub_status"])
- if sub_status == 1 {
- //大会员子账号处理
- 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}`)
- if mData == nil || len(*mData) == 0 || !ok {
- return nil
- }
- }
- }
- o_entniche = common.ObjToMap((*mData)[typeKey])
- } else {
- res_, _ := IC.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
- "i_userid": common.Int64All(spqp.EntUserId),
- "i_entid": common.Int64All(spqp.EntId),
- "i_type": types,
- }, `{"o_entniche":1}`)
- if res_ != nil {
- o_entniche = common.ObjToMap((*res_)["o_entniche"])
- }
- }
- if s.ModuleFlag == SubVipFlag {
- //从user表中取 o_vipjy.a_items
- if (*o_entniche)["a_items"] != nil {
- a_items := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
- for _, v := range a_items {
- if v["a_key"] != nil {
- a_key_arr := v["a_key"].([]interface{})
- keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
- }
- }
- }
- } else if s.ModuleFlag == MemberFlag {
- //从user表中取 o_member_jy.a_items
- //o_memeberjy := common.ObjToMap((*mData)["o_member_jy"])
- if (*o_entniche)["a_items"] != nil {
- a_items := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
- for _, v := range a_items {
- if v["a_key"] != nil {
- a_key_arr := v["a_key"].([]interface{})
- keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
- }
- }
- }
- } else if s.ModuleFlag == EntnicheFlag {
- res_, _ := IC.Mgo.FindOneByField("entniche_rule", map[string]interface{}{
- "i_userid": common.Int64All(spqp.EntUserId),
- "i_entid": common.Int64All(spqp.EntId),
- "i_type": types,
- }, `{"o_entniche":1}`)
- o_entniche := &map[string]interface{}{}
- if res_ != nil {
- o_entniche = common.ObjToMap((*res_)["o_entniche"])
- }
- if (*o_entniche)["a_items"] != nil {
- a_item := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
- for _, v := range a_item {
- if v["a_key"] != nil {
- a_key_arr := v["a_key"].([]interface{})
- keyData[common.InterfaceToStr(v["s_item"])] = a_key_arr
- }
- }
- }
- } else {
- //免费用户
- a_key, _ := (*o_entniche)["a_key"].([]interface{})
- if len(a_key) > 0 {
- for _, v := range a_key {
- if keyData["未分类"] != nil {
- keyData["未分类"] = append(keyData["未分类"], v)
- } else {
- keyData["未分类"] = []interface{}{v}
- }
- }
- }
- }
- if spqp.PositionType == 1 && s.ModuleFlag != SubFreeFlag {
- //1、mysql entniche_user_rule表中获取自己的分发规则id
- data := IC.MainMysql.SelectBySql(`SELECT rule_id FROM entniche_user_rule a
- INNER JOIN entniche_department b ON a.dept_id = b.id AND a.user_id =?
- ORDER BY b.pid
- LIMIT 1 `, spqp.EntUserId)
- if data == nil || len(*data) == 0 {
- return MapToarr(keyData)
- }
- ruleId := common.ObjToString((*data)[0]["rule_id"])
- //3、entniche_distribute表,查询条件:{_id:ObjectId(1结果中的分发规则id),i_status:0},取 o_entniche.a_items,这个查到的是分类名称,然后到上面1的结果中获取这个分类下的词
- res, _ := IC.Mgo.FindById("entniche_distribute", ruleId, nil)
- if res == nil || len(*res) == 0 {
- return MapToarr(keyData)
- }
- entItem := (*res)["a_items"].([]interface{})
- if (*res)["a_items"] == nil {
- return MapToarr(keyData)
- }
- //2、entniche_rule表, 企业订阅关键词查询条件: {i_entid:123,i_deptid:0},取 o_entniche.a_items
- deptid := common.Int64All((*res)["i_deptid"])
- ruleMap := map[string]interface{}{
- "i_entid": common.Int64All(spqp.EntId),
- }
- if deptid == 0 {
- ruleMap["i_deptid"] = map[string]interface{}{"$exists": false}
- ruleMap["i_userid"] = map[string]interface{}{"$exists": false}
- } else {
- ruleMap["i_deptid"] = deptid
- }
- res_, _ := IC.Mgo.FindOneByField("entniche_rule", ruleMap, `{"o_entniche":1}`)
- o_entniche := &map[string]interface{}{}
- if res_ != nil {
- o_entniche = common.ObjToMap((*res_)["o_entniche"])
- }
- entnicheRule := map[string][]interface{}{}
- if (*o_entniche)["a_items"] != nil {
- a_item := common.ObjArrToMapArr((*o_entniche)["a_items"].([]interface{}))
- for _, v := range a_item {
- if v["a_key"] != nil {
- a_key_arr := v["a_key"].([]interface{})
- entnicheRule[common.InterfaceToStr(v["s_item"])] = a_key_arr
- }
- }
- }
- for _, value := range entItem {
- v := common.InterfaceToStr(value)
- if entnicheRule[v] != nil {
- if keyData[v] != nil {
- //第一次搜索存在
- //去重
- keyData[v] = MergeArray(keyData[v], entnicheRule[v])
- } else {
- //第一次搜索不存在
- keyData[v] = entnicheRule[v]
- }
- }
- }
- }
- }
- return MapToarr(keyData)
- }
- // 关键词处理
- func KeyHandle(a_items map[string]interface{}) []interface{} {
- keyArr := []interface{}{}
- if a_items["a_key"] != nil {
- a_key_arr := common.ObjArrToMapArr(a_items["a_key"].([]interface{}))
- for _, vv := range a_key_arr {
- for _, v := range vv["key"].([]interface{}) {
- keyArr = append(keyArr, v)
- }
- }
- }
- return keyArr
- }
- func MapToarr(data map[string][]interface{}) []*bxsubscribe.KeyItems {
- result := []*bxsubscribe.KeyItems{}
- for k, v := range data {
- keyArr := []*bxsubscribe.Key{}
- for _, m := range common.ObjArrToMapArr(v) {
- keys := &bxsubscribe.Key{}
- if m["notkey"] != nil {
- keys.Notkey = common.ObjArrToStringArr(m["notkey"].([]interface{}))
- }
- if m["key"] != nil {
- keys.Key = common.ObjArrToStringArr(m["key"].([]interface{}))
- }
- if m["matchway"] != nil {
- keys.Matchway = common.Int64All(m["matchway"])
- }
- if m["appendkey"] != nil {
- keys.Appendkey = common.ObjArrToStringArr(m["appendkey"].([]interface{}))
- }
- keyArr = append(keyArr, keys)
- }
- result = append(result, &bxsubscribe.KeyItems{
- AKey: keyArr,
- SItem: k,
- })
- }
- return result
- }
- func MergeArray(dest []interface{}, src []interface{}) (result []interface{}) {
- data := map[string]interface{}{}
- for _, v := range common.ObjArrToMapArr(dest) {
- notkey := ""
- key := ""
- matchway := ""
- appendkey := ""
- if v["notkey"] != nil {
- otkeyArr := common.ObjArrToStringArr(v["notkey"].([]interface{}))
- for _, keyV := range otkeyArr {
- if common.InterfaceToStr(keyV) != "" {
- notkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- if v["key"] != nil {
- keyArr := common.ObjArrToStringArr(v["key"].([]interface{}))
- for _, keyV := range keyArr {
- if common.InterfaceToStr(keyV) != "" {
- key += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- if v["matchway"] != nil {
- matchway = common.InterfaceToStr(v["matchway"])
- }
- if v["appendkey"] != nil {
- appendkeyArr := common.ObjArrToStringArr(v["appendkey"].([]interface{}))
- for _, keyV := range appendkeyArr {
- if common.InterfaceToStr(keyV) != "" {
- appendkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- k := fmt.Sprintf("%s_____%s_____%s_____%s", notkey, key, matchway, appendkey)
- data[k] = v
- }
- for _, v := range common.ObjArrToMapArr(src) {
- notkey := ""
- key := ""
- matchway := ""
- appendkey := ""
- if v["notkey"] != nil {
- otkeyArr := common.ObjArrToStringArr(v["notkey"].([]interface{}))
- for _, keyV := range otkeyArr {
- if common.InterfaceToStr(keyV) != "" {
- notkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- if v["key"] != nil {
- keyArr := common.ObjArrToStringArr(v["key"].([]interface{}))
- for _, keyV := range keyArr {
- if common.InterfaceToStr(keyV) != "" {
- key += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- if v["matchway"] != nil {
- matchway = common.InterfaceToStr(v["matchway"])
- }
- if v["appendkey"] != nil {
- appendkeyArr := common.ObjArrToStringArr(v["appendkey"].([]interface{}))
- for _, keyV := range appendkeyArr {
- if common.InterfaceToStr(keyV) != "" {
- appendkey += fmt.Sprintf(",%s", common.InterfaceToStr(keyV))
- }
- }
- }
- k := fmt.Sprintf("%s_____%s_____%s_____%s", notkey, key, matchway, appendkey)
- data[k] = v
- }
- for _, v := range data {
- result = append(result, v)
- }
- return
- }
- func (s *subscribePush) ItemToKey(in *SubPushQueryParam) string {
- moduleFlag := s.ModuleFlag
- s.ModuleFlag = in.UserType
- spqp := KeyParam{
- UserId: in.UserId,
- EntUserId: in.EntUserId,
- DeptId: in.DeptId,
- NewUserId: in.NewUserId,
- IsEnt: in.IsEnt,
- EntId: in.EntId,
- PositionType: in.PositionType,
- }
- //所有关键词获取
- keyItemsArr := s.Keys(&spqp)
- s.ModuleFlag = moduleFlag
- keyArr := []string{}
- if len(keyItemsArr) > 0 {
- for _, k1 := range keyItemsArr {
- for _, k2 := range strings.Split(in.Item, ",") {
- if k1.SItem == k2 {
- if len(k1.AKey) > 0 {
- for _, k3 := range k1.AKey {
- key := ""
- if len(k3.Key) > 0 {
- key = strings.Join(k3.Key, " ")
- }
- if len(k3.Appendkey) > 0 {
- if key != "" {
- key += " " + strings.Join(k3.Appendkey, " ")
- } else {
- key = strings.Join(k3.Appendkey, " ")
- }
- }
- if k3.Matchway == 1 {
- //模糊处理
- key = strings.ReplaceAll(key, " ", ",")
- } else {
- //精准处理
- key = strings.ReplaceAll(key, " ", "+")
- }
- keyArr = append(keyArr, key)
- }
- }
- }
- }
- }
- }
- return strings.Join(keyArr, ",")
- }
- func (s *SubPushQueryParam) ExportPushFormat() (ids, keyWords []string) {
- if len(s.SelectInfoIds) > 0 {
- for ik, id := range s.SelectInfoIds {
- if xid := util.DecodeId(id); len(xid) > 0 {
- ids = append(ids, xid)
- if len(s.SelectKeys) > ik {
- keyWords = append(keyWords, strings.ReplaceAll(s.SelectKeys[ik], " ", ","))
- } else {
- keyWords = append(keyWords, "")
- }
- }
- }
- }
- return
- }
- // @Author jianghan
- // @Description clickhouse获取推荐的列表数据
- // @Date 2024/3/11
- // 根据用户身份获取数据量:免费50,付费1000
- func (s *subscribePush) SubRecList(userId string, keyword []ViewKeyWord, size int) (hasNextPage bool, total int64, resultList []*bxsubscribe.SubscribeInfo) {
- infos := IC.BaseServiceMysql.SelectBySql(`select autoid from push.sub_recommend_list where userid=? order by create_time desc limit ?`, userId, PageSize)
- ids := []int64{}
- if infos != nil && len(*infos) > 0 {
- for _, v := range *infos {
- ids = append(ids, common.Int64All(v["autoid"]))
- }
- }
- if len(ids) > 0 {
- length := len(ids)
- resultList = []*bxsubscribe.SubscribeInfo{}
- esq := `{"query": {"bool": {"must": [{"terms": {"autoid": [` + strings.Join(gconv.Strings(ids), ",") + ` ]}}]}}}`
- binfo := elastic.GetAllByNgram(INDEX, TYPE, esq, findfields, bidSort, bidField, 0, size, length, false)
- if binfo != nil && len(*binfo) > 0 {
- for n, m := range *binfo {
- if n > PageSize {
- break
- }
- title := strings.Replace(common.ObjToString(m["title"]), " ", "", -1)
- matchkeys := getKeys(title, keyword)
- resultList = append(resultList, s.InfoFormat(&PushCa{
- InfoId: mongodb.BsonIdToSId(m["_id"]),
- Date: time.Now().Unix(),
- Keys: matchkeys,
- FileExists: m["filetext"] != nil,
- }, &m, false, false))
- }
- }
- total = int64(len(resultList))
- }
- return
- }
- // @Author jianghan
- // @Description es获取最新10条数据
- // @Date 2024/3/13
- func (s *subscribePush) GetRecListByEs() (hasNextPage bool, total int64, resultList []*bxsubscribe.SubscribeInfo) {
- resultList = make([]*bxsubscribe.SubscribeInfo, 10)
- if jsonStr := redis.GetStr("pushcache_2_c", "bid_sub_recommend"); jsonStr != "" {
- var data []map[string]interface{}
- _ = json.Unmarshal([]byte(jsonStr), &data)
- for i := 0; i < 10; i++ {
- resultList[i] = s.InfoFormat(&PushCa{
- InfoId: common.ObjToString(data[i]["_id"]),
- Date: time.Now().Unix(),
- Keys: []string{},
- FileExists: data[i]["filetext"] != nil,
- }, &data[i], false, false)
- }
- } else {
- qstr := `{"query": {"bool": {"must": [{"match_all": {}}]}}}`
- list := elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSort, bidField, 0, 10, 10, false)
- redis.Put("pushcache_2_c", "bid_sub_recommend", 1, 86400)
- for i := 0; i < len(*list); i++ {
- resultList[i] = s.InfoFormat(&PushCa{
- InfoId: common.ObjToString((*list)[i]["_id"]),
- Date: time.Now().Unix(),
- Keys: []string{},
- FileExists: (*list)[i]["filetext"] != nil,
- }, &(*list)[i], false, false)
- }
- }
- total = int64(len(resultList))
- if total > PageSize {
- resultList = resultList[:PageSize]
- hasNextPage = true
- }
- return
- }
|