123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706 |
- package p
- import (
- "encoding/json"
- "strings"
- util "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/mongodb"
- "bp.jydev.jianyu360.cn/BaseService/pushpkg/dfa"
- "github.com/donnie4w/go-logger/logger"
- )
- //
- type MgoConf struct {
- Address string
- Size int
- DbName string
- ReplSet string
- UserName string
- Password string
- Collection string
- ExperienceDbName string
- }
- //
- type RedisConf struct {
- Address string
- }
- //
- type MysqlConf struct {
- DbName string
- Address string
- UserName string
- PassWord string
- MaxOpenConns int
- MaxIdleConns int
- }
- //
- type EsConf struct {
- Address string
- Size int
- }
- //
- type MemberService struct {
- IsBuy bool
- Services map[int]*memberService
- }
- //
- type memberService struct {
- Id int
- StartTime string
- }
- //关键词
- type KeySet struct {
- Item string `json:"item"` //分类名称
- Keys []string `json:"key"` //关键词
- NotKeys []string `json:"notkey"` //排除词
- SubTypes []string `json:"infotype"` //信息类型
- Areas []string `json:"area"` //地区
- AppendKeys []string `json:"appendkey"` //附加词
- MatchWay int `json:"matchway"` //匹配模式 0:精准匹配 1:模糊匹配
- }
- //解析订阅词
- type SubSet struct {
- MatchWay int //匹配方式 1-标题 2-正文
- Matchbuyerclass_other int //是否开启其他按钮
- ProjectMatch int //项目关联推送
- Items []string //关键词分类
- Keys []string //过滤后的关键词
- Notkeys []string //排除词
- Key_item map[string]string //关键词对应的分类名称
- Key_notkey map[string]map[string]bool //关键词所对应的排除词
- Key_area map[string]map[string]bool //关键词所对应的信息范围
- Key_subtype map[string]map[string]bool //关键词所对应的信息类型
- OriginalKeys []string //原始关键词
- Areas []string //区域-省份
- Area map[string]interface{} //区域-省份
- Subtype []interface{} //信息类型-二级分类
- Subtypes []string //信息类型-二级分类
- Buyerclass []interface{} //采购单位行业
- Buyerclasss []string //采购单位行业
- IsUpgrade bool //免费用户是否订阅升级
- MaxPushSize int //推送最大条数
- MaxMailSize int //邮件最大条数
- }
- //推送设置
- type PushSet struct {
- Email string //邮箱
- SubSet *PushSetChild //订阅推送设置
- WeekReport *PushSetChild //周报推送设置
- MonthReport *PushSetChild //月报推送设置
- NewprojectForecast *PushSetChild //潜在项目预测推送设置
- EntInfo *PushSetChild //企业情报监控-企业工商变动推送设置
- FollowProject *PushSetChild //项目进度监控推送设置
- FollowEnt *PushSetChild //企业情报监控-企业中标动态推送设置
- }
- type PushSetChild struct {
- RateMode int //推送时间
- Times []string //自定义推送时间
- WxPush int //是否开启微信推送
- AppPush int //是否开启app推送
- MailPush int //是否开启邮箱推送
- }
- //用户基本信息
- type UserInfo struct {
- Id string //mongoid
- BaseUserId int64 //mysql用户id
- S_m_openid string //公众号openid
- A_m_openid string //app微信登录openid
- Phone string //app手机号登录
- Jpushid string //极光推送id
- Opushid string //厂商推送id
- AppPhoneType string //手机型号
- Subscribe int //是否关注
- VipStatus int //超级订阅 1--试用 2--正式
- MemberStatus int //大会员
- NicheStatus int //商机管理
- IsMainAccount bool //是否是主账号
- SonAccountStatus int //子账号状态 0-禁用 1-启用
- MainId string //主账号id
- Entniche *Entniche
- MemberMainid string
- WxTplMsg *WxTplMsg
- SubSet *SubSet
- PushSet *PushSet
- Extend *UserInfoExtend
- }
- /*
- *获取用户信息
- *temp 用户数据
- *tp 1-免费 2-超级订阅 3-大会员
- */
- func NewUserInfo(temp map[string]interface{}, tp int) *UserInfo {
- ui := &UserInfo{
- Id: BsonIdToSId(temp["_id"]),
- BaseUserId: util.Int64All(temp["base_user_id"]),
- S_m_openid: util.ObjToString(temp["s_m_openid"]),
- A_m_openid: util.ObjToString(temp["a_m_openid"]),
- Phone: GetPhone(temp),
- Jpushid: util.ObjToString(temp["s_jpushid"]),
- Opushid: util.ObjToString(temp["s_opushid"]),
- AppPhoneType: util.ObjToString(temp["s_appponetype"]),
- Subscribe: util.IntAllDef(temp["i_ispush"], 1),
- VipStatus: util.IntAll(temp["i_vip_status"]),
- MemberStatus: util.IntAll(temp["i_member_status"]),
- }
- if ui.MemberStatus > 0 {
- ui.IsMainAccount = util.IntAll(temp["i_mainaccount"]) == 1
- ui.SonAccountStatus = util.IntAllDef(temp["i_member_sub_status"], -1)
- ui.MemberMainid = util.ObjToString(temp["s_member_mainid"])
- }
- var obj map[string]interface{}
- if tp == 3 {
- obj, _ = temp["o_member_jy"].(map[string]interface{})
- } else if tp == 2 {
- obj, _ = temp["o_vipjy"].(map[string]interface{})
- } else {
- obj, _ = temp["o_jy"].(map[string]interface{})
- }
- ui.GetSubSet(tp == 1, ui.Id, obj)
- o_pushset, _ := temp["o_pushset"].(map[string]interface{})
- ui.GetPushSet(o_pushset)
- return ui
- }
- //解析用户的推送设置
- func (u *UserInfo) GetPushSet(obj map[string]interface{}) {
- subSet, _ := obj["o_subset"].(map[string]interface{})
- times, _ := subSet["a_times"].([]interface{})
- weekReport, _ := obj["o_week_report"].(map[string]interface{})
- monthReport, _ := obj["o_month_report"].(map[string]interface{})
- newprojectForecast, _ := obj["o_newproject_forecast"].(map[string]interface{})
- entInfo, _ := obj["o_entinfo"].(map[string]interface{})
- followProject, _ := obj["o_follow_project"].(map[string]interface{})
- followEnt, _ := obj["o_follow_ent"].(map[string]interface{})
- u.PushSet = &PushSet{
- Email: strings.TrimSpace(util.ObjToString(obj["s_email"])),
- SubSet: &PushSetChild{
- WxPush: util.IntAllDef(subSet["i_wxpush"], 1),
- AppPush: util.IntAllDef(subSet["i_apppush"], 1),
- MailPush: util.IntAll(subSet["i_mailpush"]),
- RateMode: util.IntAllDef(subSet["i_ratemode"], 5),
- Times: util.ObjArrToStringArr(times),
- },
- WeekReport: &PushSetChild{
- WxPush: util.IntAllDef(weekReport["i_wxpush"], 1),
- AppPush: util.IntAllDef(weekReport["i_apppush"], 1),
- MailPush: util.IntAll(weekReport["i_mailpush"]),
- },
- MonthReport: &PushSetChild{
- WxPush: util.IntAllDef(monthReport["i_wxpush"], 1),
- AppPush: util.IntAllDef(monthReport["i_apppush"], 1),
- MailPush: util.IntAll(monthReport["i_mailpush"]),
- },
- NewprojectForecast: &PushSetChild{
- WxPush: util.IntAllDef(newprojectForecast["i_wxpush"], 1),
- AppPush: util.IntAllDef(newprojectForecast["i_apppush"], 1),
- MailPush: util.IntAll(newprojectForecast["i_mailpush"]),
- },
- EntInfo: &PushSetChild{
- WxPush: util.IntAllDef(entInfo["i_wxpush"], 1),
- AppPush: util.IntAllDef(entInfo["i_apppush"], 1),
- MailPush: util.IntAll(entInfo["i_mailpush"]),
- },
- FollowProject: &PushSetChild{
- WxPush: util.IntAllDef(followProject["i_wxpush"], 1),
- AppPush: util.IntAllDef(followProject["i_apppush"], 1),
- MailPush: util.IntAll(followProject["i_mailpush"]),
- },
- FollowEnt: &PushSetChild{
- WxPush: util.IntAllDef(followEnt["i_wxpush"], 1),
- AppPush: util.IntAllDef(followEnt["i_apppush"], 1),
- MailPush: util.IntAll(followEnt["i_mailpush"]),
- },
- }
- if u.Subscribe == 0 || u.S_m_openid == "" {
- u.PushSet.SubSet.WxPush = -1
- u.PushSet.WeekReport.WxPush = -1
- u.PushSet.MonthReport.WxPush = -1
- u.PushSet.NewprojectForecast.WxPush = -1
- u.PushSet.EntInfo.WxPush = -1
- u.PushSet.FollowProject.WxPush = -1
- u.PushSet.FollowEnt.WxPush = -1
- }
- if u.Jpushid == "" && u.Opushid == "" {
- u.PushSet.SubSet.AppPush = -1
- u.PushSet.WeekReport.AppPush = -1
- u.PushSet.MonthReport.AppPush = -1
- u.PushSet.NewprojectForecast.AppPush = -1
- u.PushSet.EntInfo.AppPush = -1
- u.PushSet.FollowProject.AppPush = -1
- u.PushSet.FollowEnt.AppPush = -1
- }
- mailPush := true
- if (u.S_m_openid == "" || (u.S_m_openid != "" && u.Subscribe == 0)) && u.Phone == "" && u.A_m_openid == "" {
- mailPush = false
- } else if (u.S_m_openid == "" || (u.S_m_openid != "" && u.Subscribe == 0)) && u.Phone == "" && u.A_m_openid != "" && u.Jpushid == "" && u.Opushid == "" {
- mailPush = false
- } else if !MailReg.MatchString(u.PushSet.Email) {
- mailPush = false
- }
- if !mailPush {
- u.PushSet.SubSet.MailPush = -1
- u.PushSet.WeekReport.MailPush = -1
- u.PushSet.MonthReport.MailPush = -1
- u.PushSet.NewprojectForecast.MailPush = -1
- u.PushSet.EntInfo.MailPush = -1
- u.PushSet.FollowProject.MailPush = -1
- u.PushSet.FollowEnt.MailPush = -1
- }
- }
- //解析用户的订阅设置
- func (u *UserInfo) GetSubSet(isFreeUser bool, userId string, obj map[string]interface{}) {
- subSet := &SubSet{
- Keys: []string{},
- Notkeys: []string{},
- Key_item: map[string]string{},
- Key_notkey: map[string]map[string]bool{},
- Key_area: map[string]map[string]bool{},
- Key_subtype: map[string]map[string]bool{},
- OriginalKeys: []string{},
- Areas: []string{},
- Subtypes: []string{},
- Buyerclasss: []string{},
- MatchWay: util.IntAllDef(obj["i_matchway"], 1),
- Matchbuyerclass_other: util.IntAllDef(obj["i_matchbuyerclass_other"], 1),
- ProjectMatch: util.IntAll(obj["i_projectmatch"]),
- MaxPushSize: util.IntAll(obj["i_maxpushsize"]),
- MaxMailSize: util.IntAll(obj["i_maxmailsize"]),
- }
- var keySets []*KeySet
- if isFreeUser {
- var err error
- keySets, err = u.GetKeySets(obj["a_key"])
- if err != nil {
- logger.Error("获取用户关键词错误!", userId, obj["a_key"], err)
- return
- }
- area := obj["o_area"]
- if ppStatus := util.IntAll(obj["i_ppstatus"]); ppStatus == 1 {
- area = obj["o_area_p"]
- }
- if newFree := util.IntAll(obj["i_newfree"]); newFree == 1 && area != nil {
- subSet.IsUpgrade = true
- subSet.Subtype, _ = obj["a_infotype"].([]interface{})
- subSet.Area, _ = area.(map[string]interface{})
- }
- } else {
- items, _ := obj["a_items"].([]interface{})
- for _, v := range items {
- item, _ := v.(map[string]interface{})
- itemName := strings.TrimSpace(util.ObjToString(item["s_item"]))
- subSet.Items = append(subSet.Items, itemName)
- kss, err := u.GetKeySets(item["a_key"])
- if err != nil {
- logger.Error("获取用户关键词错误!", userId, item["a_key"], err)
- continue
- }
- for _, vv := range kss {
- if vv == nil {
- continue
- }
- if vv.MatchWay == 1 {
- for _, vvv := range vv.Keys {
- keySets = append(keySets, &KeySet{
- Item: itemName,
- Keys: []string{vvv},
- NotKeys: vv.NotKeys,
- })
- }
- for _, vvv := range vv.AppendKeys {
- keySets = append(keySets, &KeySet{
- Item: itemName,
- Keys: []string{vvv},
- NotKeys: vv.NotKeys,
- })
- }
- } else {
- vv.Item = itemName
- keySets = append(keySets, vv)
- }
- }
- }
- subSet.Buyerclass, _ = obj["a_buyerclass"].([]interface{})
- subSet.Subtype, _ = obj["a_infotype"].([]interface{})
- subSet.Area, _ = obj["o_area"].(map[string]interface{})
- }
- for _, v := range subSet.Buyerclass {
- s_v, _ := v.(string)
- if s_v == "" {
- continue
- }
- subSet.Buyerclasss = append(subSet.Buyerclasss, s_v)
- }
- for _, v := range subSet.Subtype {
- s_v, _ := v.(string)
- if s_v == "" {
- continue
- }
- subSet.Subtypes = append(subSet.Subtypes, s_v)
- }
- for area, _ := range subSet.Area {
- if area == "" {
- continue
- }
- subSet.Areas = append(subSet.Areas, area)
- }
- ////////////////
- for _, vs := range keySets {
- if vs == nil {
- logger.Error(userId, "关键词设置异常")
- continue
- }
- var vs_keys []string
- for _, vs_v := range [][]string{vs.Keys, vs.AppendKeys} {
- for _, vs_vv := range vs_v {
- vs_vv = strings.TrimSpace(vs_vv)
- if vs_vv == "" {
- continue
- }
- vs_keys = append(vs_keys, vs_vv)
- }
- }
- if len(vs_keys) == 0 {
- continue
- }
- key := strings.Join(vs_keys, "+")
- subSet.OriginalKeys = append(subSet.OriginalKeys, key)
- if KeyFilterReg.MatchString(key) {
- continue
- } else if !KeyRetainReg.MatchString(key) {
- continue
- }
- subSet.Keys = append(subSet.Keys, key)
- subSet.Notkeys = append(subSet.Notkeys, vs.NotKeys...)
- //转大写
- upperKey := strings.ToUpper(key)
- subSet.Key_item[upperKey] = vs.Item
- //建立与排除词的对应关系
- for _, notkey := range vs.NotKeys {
- upperNotkey := strings.ToUpper(notkey)
- if subSet.Key_notkey[upperKey] == nil {
- subSet.Key_notkey[upperKey] = map[string]bool{}
- }
- subSet.Key_notkey[upperKey][upperNotkey] = true
- }
- //免费用户需要进行映射
- if isFreeUser {
- //建立与信息范围的对应关系
- for _, area := range vs.Areas {
- if subSet.Key_area[upperKey] == nil {
- subSet.Key_area[upperKey] = map[string]bool{}
- }
- subSet.Key_area[upperKey][area] = true
- }
- //建立与信息类型的对应关系
- for _, subtype := range vs.SubTypes {
- if subSet.Key_subtype[upperKey] == nil {
- subSet.Key_subtype[upperKey] = map[string]bool{}
- }
- subSet.Key_subtype[upperKey][subtype] = true
- }
- }
- }
- u.SubSet = subSet
- }
- //得到用户的关键词
- func (u *UserInfo) GetKeySets(a_key interface{}) ([]*KeySet, error) {
- var keySets []*KeySet
- if a_key == nil {
- return keySets, nil
- }
- _bs, err := json.Marshal(a_key)
- if err == nil {
- err = json.Unmarshal(_bs, &keySets)
- }
- if err != nil {
- return keySets, err
- }
- for _, v := range keySets {
- if v == nil {
- continue
- }
- keys, appendKeys, notKeys := []string{}, []string{}, []string{}
- for _, vv := range v.Keys {
- keys = append(keys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
- }
- for _, vv := range v.AppendKeys {
- appendKeys = append(appendKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
- }
- for _, vv := range v.NotKeys {
- notKeys = append(notKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
- }
- v.Keys = keys
- v.AppendKeys = appendKeys
- v.NotKeys = notKeys
- }
- return keySets, err
- }
- //
- type Entniche struct {
- EntId int //企业id
- EntName string //企业名称
- DeptId int //部门id
- DisId string //分发id
- UserId int
- Unique string
- OnlyPush int
- IsNew int
- PowerSource int //权益来源
- ProductType string //产品类型
- Mail string //邮箱
- IsDis int //是否是分发
- DisMember int //分发-超级订阅
- DisVip int //分发-大会员
- }
- //用户基本信息-扩展
- type UserInfoExtend struct {
- CreateTime int64 //
- Size int
- }
- func (u *UserInfo) Add(k string, m *map[string]map[*UserInfo]bool) {
- mk := (*m)[k]
- if mk == nil {
- mk = map[*UserInfo]bool{}
- }
- mk[u] = true
- (*m)[k] = mk
- }
- //添加信息类型映射关系
- func (u *UserInfo) AddSbuype(m *map[string]map[*UserInfo]bool) {
- if len(u.SubSet.Subtypes) == 0 {
- u.Add("", m)
- } else {
- for _, v := range u.SubSet.Subtypes {
- u.Add(v, m)
- }
- }
- }
- //添加采购单位类型映射关系
- func (u *UserInfo) AddBuyerclass(m *map[string]map[*UserInfo]bool) {
- //如果有关键词 有采购单位行业,行业加上“其它”
- if len(u.SubSet.Buyerclasss) == 0 {
- u.Add("", m)
- } else {
- if u.SubSet.Matchbuyerclass_other == 1 && len(u.SubSet.Keys) > 0 {
- u.Add("其它", m)
- }
- for _, v := range u.SubSet.Buyerclasss {
- u.Add(v, m)
- }
- }
- }
- //添加区域映射关系
- func (u *UserInfo) AddArea(m *map[string]map[*UserInfo]bool) {
- if len(u.SubSet.Areas) == 0 {
- u.Add("", m)
- } else {
- for _, v := range u.SubSet.Areas {
- u.Add(v, m)
- }
- }
- }
- //添加区域城市映射关系
- func (u *UserInfo) AddAreaCity(area, city *map[string]map[*UserInfo]bool) {
- if len(u.SubSet.Area) == 0 {
- u.Add("", area)
- } else {
- for k, v := range u.SubSet.Area {
- if k == "" {
- continue
- }
- vs, _ := v.([]interface{})
- if len(vs) == 0 {
- u.Add(k, area)
- } else {
- for _, vv := range vs {
- s_vv, _ := vv.(string)
- if s_vv == "" {
- continue
- }
- u.Add(s_vv, city)
- }
- }
- }
- }
- }
- //把用户挂在词下面
- func (u *UserInfo) MakeKeyUser(keys []string, key_user *map[string]*[]*UserInfo) {
- mp := map[string]bool{}
- for _, key := range keys {
- v := strings.ToUpper(key)
- if v == "" || mp[v] {
- continue
- }
- mp[v] = true
- arr := (*key_user)[v]
- if arr == nil {
- arr = &[]*UserInfo{}
- }
- *arr = append(*arr, u)
- (*key_user)[v] = arr
- }
- }
- type WxTplMsg struct {
- Key string
- Area string
- }
- type SortList []*MatchInfo
- func (s SortList) Len() int {
- return len(s)
- }
- func (s SortList) Less(i, j int) bool {
- return util.Int64All((*s[i].Info)["publishtime"]) > util.Int64All((*s[j].Info)["publishtime"])
- }
- func (s SortList) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- type CSortList []map[string]interface{}
- func (s CSortList) Len() int {
- return len(s)
- }
- func (s CSortList) Less(i, j int) bool {
- return util.Int64All(s[i]["publishtime"]) > util.Int64All(s[j]["publishtime"])
- }
- func (s CSortList) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- type MatchInfo struct {
- Info *map[string]interface{}
- Keys []string
- Items []string
- MatchWays []string
- DisId string
- }
- type KeyDfa struct {
- Key *dfa.DFA
- NotKey *dfa.DFA
- Key_user *map[string]*[]*UserInfo
- Notkey_user *map[string]*[]*UserInfo
- }
- //所有用户的关键词和排除词
- func (p *KeyDfa) CreateDaf() {
- //关键词
- p.Key = &dfa.DFA{}
- keys := make([]string, 0)
- for k, _ := range *p.Key_user {
- keys = append(keys, k)
- }
- p.Key.AddWord(keys...)
- //排除关键词
- p.NotKey = &dfa.DFA{}
- notKeys := make([]string, 0)
- for k, _ := range *p.Notkey_user {
- notKeys = append(notKeys, k)
- }
- p.NotKey.AddWord(notKeys...)
- }
- //
- type MatchUser struct {
- Keys []string
- MatchWays []string
- MatchWay map[string]bool
- }
- //
- type RelationProjectUser struct {
- TopTypes []string
- }
- //
- type PushInfo struct {
- Ids []interface{}
- Info map[string]interface{}
- }
- //推送返回结果
- type PushResult struct {
- WxStatus int
- AppStatus int
- MailStatus int
- PushDate int64
- Infos *SortList
- }
- //
- type PushParam struct {
- JpushTitle string
- LastInfoDate int64
- TitleArray []string
- Infos *SortList
- InfosLength int
- MailHtml string
- PushDate int64
- PushCount int
- IsPush bool
- }
- //
- type BiddingInfo struct {
- Id string
- Title string
- ClearTitle string
- HighlightTitle string
- AreaTitle string
- Area string
- Publishtime int64
- PublishtimeDiff string
- PublishtimeYMD string
- Buyerclass string
- Subscopeclass string
- Bidamount interface{}
- Budget interface{}
- Acount string
- Subtype string
- Toptype string
- Infotype string
- }
- //身份信息
- type IdentityInfo struct {
- Name string
- PersonId int64
- UserName string
- AccountId int64
- EntAccountId int64
- PositionId int64
- PositionType int64
- EntId int64
- EntUserId int64
- EntUserName string
- UserId int64
- }
|