public.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. package p
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. . "app.yhyue.com/moapp/jybase/common"
  11. util "app.yhyue.com/moapp/jybase/common"
  12. . "app.yhyue.com/moapp/jybase/date"
  13. . "app.yhyue.com/moapp/jybase/mongodb"
  14. . "app.yhyue.com/moapp/jybase/mysql"
  15. "app.yhyue.com/moapp/jybase/redis"
  16. "github.com/donnie4w/go-logger/logger"
  17. )
  18. //金额转化 金额:0-万元以下单位为元 ,万元以上至亿元以下单位为万元 ,亿元以上单位为亿元。保留 小数点后 2 位,不进行四舍五入。
  19. func ConversionMoney(i_money interface{}) string {
  20. m := ""
  21. if reflect.TypeOf(i_money).Name() == "float64" {
  22. m = strconv.FormatFloat(Float64All(i_money), 'f', -1, 64)
  23. } else {
  24. m = ObjToString(i_money)
  25. }
  26. if m == "" {
  27. return m
  28. }
  29. m_arr := strings.Split(m, ".")
  30. m_1 := m_arr[0]
  31. len_m1 := len([]rune(m_1))
  32. if len_m1 >= 9 {
  33. m = m_1[0:len_m1-8] + "." + m_1[len_m1-8:len_m1-6] + "亿元"
  34. } else if len_m1 >= 5 {
  35. m = m_1[0:len_m1-4] + "." + m_1[len_m1-4:len_m1-2] + "万元"
  36. } else {
  37. if len(m_arr) == 1 {
  38. return m + ".00元"
  39. }
  40. m_2 := m_arr[1]
  41. if len([]rune(m_2)) > 1 {
  42. m_2 = m_2[0:2]
  43. } else {
  44. m_2 = m_2[0:1] + "0"
  45. }
  46. m = m_1 + "." + m_2 + "元"
  47. }
  48. return m
  49. }
  50. //微信模板消息 remark
  51. func WxTplRemark(titles []string, lastTime int64, hasLen int) string {
  52. tip := ""
  53. second := time.Now().Unix() - lastTime
  54. if second > 0 {
  55. if second < 61 {
  56. tip = fmt.Sprintf("%d秒前发布的:\n", second)
  57. } else {
  58. second = second / 60
  59. if second < 121 {
  60. if second < 1 {
  61. second = 1
  62. }
  63. tip = fmt.Sprintf("%d分钟前发布的:\n", second)
  64. }
  65. }
  66. }
  67. lastTip := ""
  68. if len(titles) > 1 {
  69. lastTip = fmt.Sprintf("...(共%d条)", len(titles))
  70. }
  71. reLen := 199 - hasLen - len([]rune(tip))
  72. lastTipLen := len([]rune(lastTip))
  73. wxTplMsgTitle := ""
  74. bshow := false
  75. for n := 1; n < len(titles)+1; n++ {
  76. curTitle := titles[n-1]
  77. tmptitle := wxTplMsgTitle + fmt.Sprintf("%d %s\n", n, curTitle)
  78. ch := reLen - len([]rune(tmptitle))
  79. if ch < lastTipLen { //加上后大于后辍,则没有完全显示
  80. if ch == 0 && n == len(titles) {
  81. wxTplMsgTitle = tmptitle
  82. bshow = true
  83. } else {
  84. ch_1 := reLen - len([]rune(wxTplMsgTitle)) - lastTipLen
  85. if ch_1 > 8 {
  86. curLen := len([]rune(curTitle))
  87. if ch_1 > curLen {
  88. ch_1 = curLen
  89. }
  90. wxTplMsgTitle += fmt.Sprintf("%d %s\n", n, string([]rune(curTitle)[:ch_1-3]))
  91. }
  92. }
  93. } else {
  94. wxTplMsgTitle = tmptitle
  95. if n == len(titles) {
  96. bshow = true
  97. }
  98. }
  99. }
  100. if bshow {
  101. lastTip = ""
  102. }
  103. return tip + wxTplMsgTitle + lastTip
  104. }
  105. //获取信息行业
  106. func GetSubScopeClass(subscopeclass interface{}) string {
  107. industry := ""
  108. if subscopeclass != nil {
  109. k2sub := strings.Split(ObjToString(subscopeclass), ",")
  110. if len(k2sub) > 0 {
  111. industry = k2sub[0]
  112. if industry != "" {
  113. ss := strings.Split(industry, "_")
  114. if len(ss) > 1 {
  115. industry = ss[0]
  116. }
  117. }
  118. }
  119. }
  120. return industry
  121. }
  122. //控制一分钟最大推送数,均匀调度
  123. func LimitMaxOneMinutePush(pushPoll *chan bool, maxOneMinute int) {
  124. max := int(maxOneMinute / 60)
  125. *pushPoll = make(chan bool, max)
  126. go func() {
  127. for {
  128. time.Sleep(time.Second)
  129. for i := 0; i < max; i++ {
  130. select {
  131. case *pushPoll <- true:
  132. default:
  133. continue
  134. }
  135. }
  136. }
  137. }()
  138. }
  139. //
  140. func UpdateUserIsPush(Mgo *MongodbSim, userId string, err error) {
  141. if err == nil {
  142. return
  143. }
  144. if strings.Contains(err.Error(), "[43004]") {
  145. Mgo.UpdateById("user", userId, map[string]interface{}{
  146. "$set": map[string]interface{}{
  147. "i_ispush": 0,
  148. },
  149. })
  150. }
  151. }
  152. //查找我的子账号
  153. func MySonAccounts(Mgo *MongodbSim, coll, userId string, field map[string]interface{}) *[]map[string]interface{} {
  154. users, _ := Mgo.Find(coll, map[string]interface{}{
  155. "i_member_sub_status": 1,
  156. "s_member_mainid": userId,
  157. "i_member_status": map[string]interface{}{"$gt": 0},
  158. }, nil, field, false, -1, -1)
  159. if users == nil {
  160. users = &[]map[string]interface{}{}
  161. }
  162. return users
  163. }
  164. //是否购买此服务
  165. func HasService(mysql *Mysql, userId string, params ...int) *MemberService {
  166. args := []interface{}{}
  167. ws := []string{}
  168. for _, v := range params {
  169. args = append(args, v)
  170. ws = append(ws, "?")
  171. }
  172. args = append(args, args...)
  173. args = append(args, userId, NowFormat(Date_Full_Layout))
  174. list := mysql.SelectBySql(`SELECT a.l_starttime,b.id from bigmember_service_user a
  175. INNER JOIN bigmember_service b on (((b.id in (`+strings.Join(ws, ",")+`) and a.s_serviceid=b.id) or (b.i_pid in (`+strings.Join(ws, ",")+`) and a.s_serviceid=b.i_pid)) and b.i_status=0 and a.s_userid=? and a.i_status=0 and a.l_endtime>?)`, args...)
  176. ms := &MemberService{
  177. Services: map[int]*memberService{},
  178. }
  179. if list != nil {
  180. for _, v := range *list {
  181. ms.IsBuy = true
  182. id := util.IntAll(v["id"])
  183. ms.Services[id] = &memberService{
  184. Id: id,
  185. StartTime: util.ObjToString(v["l_starttime"]),
  186. }
  187. }
  188. }
  189. return ms
  190. }
  191. //
  192. func GetInfoTitle(info *map[string]interface{}) string {
  193. title, _ := (*info)["title"].(string)
  194. jsondata, _ := (*info)["jsondata"].(map[string]interface{})
  195. if jsondata != nil {
  196. goods, _ := jsondata["goods"].(string)
  197. title += goods
  198. }
  199. title = strings.ToUpper(title)
  200. return title
  201. }
  202. //得到用户的关键词
  203. func GetKeySets(a_key interface{}) ([]*KeySet, error) {
  204. var keySets []*KeySet
  205. if a_key == nil {
  206. return keySets, nil
  207. }
  208. _bs, err := json.Marshal(a_key)
  209. if err == nil {
  210. err = json.Unmarshal(_bs, &keySets)
  211. }
  212. for _, v := range keySets {
  213. keys, appendKeys, notKeys := []string{}, []string{}, []string{}
  214. for _, vv := range v.Keys {
  215. keys = append(keys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  216. }
  217. for _, vv := range v.AppendKeys {
  218. appendKeys = append(appendKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  219. }
  220. for _, vv := range v.NotKeys {
  221. notKeys = append(notKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  222. }
  223. v.Keys = keys
  224. v.AppendKeys = appendKeys
  225. v.NotKeys = notKeys
  226. }
  227. return keySets, err
  228. }
  229. //解析用户的订阅词
  230. func GetSubSet(isFreeUser bool, userId string, obj map[string]interface{}) *SubSet {
  231. mailpush := util.IntAll(obj["i_mailpush"])
  232. email := strings.TrimSpace(util.ObjToString(obj["s_email"]))
  233. if mailpush == 1 && !MailReg.MatchString(email) {
  234. mailpush = -1
  235. }
  236. subSet := &SubSet{
  237. Keys: []string{},
  238. Notkeys: []string{},
  239. Key_item: map[string]string{},
  240. Key_notkey: map[string]map[string]bool{},
  241. Key_area: map[string]map[string]bool{},
  242. Key_subtype: map[string]map[string]bool{},
  243. OriginalKeys: []string{},
  244. Areas: []string{},
  245. Subtypes: []string{},
  246. Buyerclasss: []string{},
  247. WxPush: util.IntAllDef(obj["i_wxpush"], 1),
  248. AppPush: util.IntAllDef(obj["i_apppush"], 1),
  249. MailPush: mailpush,
  250. Email: email,
  251. MatchWay: util.IntAllDef(obj["i_matchway"], 1),
  252. Matchbuyerclass_other: util.IntAllDef(obj["i_matchbuyerclass_other"], 1),
  253. ProjectMatch: util.IntAll(obj["i_projectmatch"]),
  254. RateMode: util.IntAllDef(obj["i_ratemode"], 2),
  255. MaxPushSize: util.IntAll(obj["i_maxpushsize"]),
  256. MaxMailSize: util.IntAll(obj["i_maxmailsize"]),
  257. }
  258. var keySets []*KeySet
  259. if isFreeUser {
  260. var err error
  261. keySets, err = GetKeySets(obj["a_key"])
  262. if err != nil {
  263. logger.Error("获取用户关键词错误!", userId, obj["a_key"], err)
  264. return subSet
  265. }
  266. area := obj["o_area"]
  267. if ppStatus := util.IntAll(obj["i_ppstatus"]); ppStatus == 1 {
  268. area = obj["o_area_p"]
  269. }
  270. if newFree := util.IntAll(obj["i_newfree"]); newFree == 1 && area != nil {
  271. subSet.IsUpgrade = true
  272. subSet.Subtype, _ = obj["a_infotype"].([]interface{})
  273. subSet.Area, _ = area.(map[string]interface{})
  274. }
  275. } else {
  276. items, _ := obj["a_items"].([]interface{})
  277. for _, v := range items {
  278. item, _ := v.(map[string]interface{})
  279. itemName := strings.TrimSpace(util.ObjToString(item["s_item"]))
  280. subSet.Items = append(subSet.Items, itemName)
  281. kss, err := GetKeySets(item["a_key"])
  282. if err != nil {
  283. logger.Error("获取用户关键词错误!", userId, item["a_key"], err)
  284. continue
  285. }
  286. for _, vv := range kss {
  287. if vv == nil {
  288. continue
  289. }
  290. if vv.MatchWay == 1 {
  291. for _, vvv := range vv.Keys {
  292. keySets = append(keySets, &KeySet{
  293. Item: itemName,
  294. Keys: []string{vvv},
  295. NotKeys: vv.NotKeys,
  296. })
  297. }
  298. for _, vvv := range vv.AppendKeys {
  299. keySets = append(keySets, &KeySet{
  300. Item: itemName,
  301. Keys: []string{vvv},
  302. NotKeys: vv.NotKeys,
  303. })
  304. }
  305. } else {
  306. vv.Item = itemName
  307. keySets = append(keySets, vv)
  308. }
  309. }
  310. }
  311. subSet.Buyerclass, _ = obj["a_buyerclass"].([]interface{})
  312. subSet.Subtype, _ = obj["a_infotype"].([]interface{})
  313. subSet.Area, _ = obj["o_area"].(map[string]interface{})
  314. }
  315. for _, v := range subSet.Buyerclass {
  316. s_v, _ := v.(string)
  317. if s_v == "" {
  318. continue
  319. }
  320. subSet.Buyerclasss = append(subSet.Buyerclasss, s_v)
  321. }
  322. for _, v := range subSet.Subtype {
  323. s_v, _ := v.(string)
  324. if s_v == "" {
  325. continue
  326. }
  327. subSet.Subtypes = append(subSet.Subtypes, s_v)
  328. }
  329. for area, _ := range subSet.Area {
  330. if area == "" {
  331. continue
  332. }
  333. subSet.Areas = append(subSet.Areas, area)
  334. }
  335. ////////////////
  336. for _, vs := range keySets {
  337. if vs == nil {
  338. logger.Error(userId, "关键词设置异常")
  339. continue
  340. }
  341. var vs_keys []string
  342. for _, vs_v := range [][]string{vs.Keys, vs.AppendKeys} {
  343. for _, vs_vv := range vs_v {
  344. vs_vv = strings.TrimSpace(vs_vv)
  345. if vs_vv == "" {
  346. continue
  347. }
  348. vs_keys = append(vs_keys, vs_vv)
  349. }
  350. }
  351. if len(vs_keys) == 0 {
  352. continue
  353. }
  354. key := strings.Join(vs_keys, "+")
  355. subSet.OriginalKeys = append(subSet.OriginalKeys, key)
  356. if KeyFilterReg.MatchString(key) {
  357. continue
  358. } else if !KeyRetainReg.MatchString(key) {
  359. continue
  360. }
  361. subSet.Keys = append(subSet.Keys, key)
  362. subSet.Notkeys = append(subSet.Notkeys, vs.NotKeys...)
  363. //转大写
  364. upperKey := strings.ToUpper(key)
  365. subSet.Key_item[upperKey] = vs.Item
  366. //建立与排除词的对应关系
  367. for _, notkey := range vs.NotKeys {
  368. upperNotkey := strings.ToUpper(notkey)
  369. if subSet.Key_notkey[upperKey] == nil {
  370. subSet.Key_notkey[upperKey] = map[string]bool{}
  371. }
  372. subSet.Key_notkey[upperKey][upperNotkey] = true
  373. }
  374. //免费用户需要进行映射
  375. if isFreeUser {
  376. //建立与信息范围的对应关系
  377. for _, area := range vs.Areas {
  378. if subSet.Key_area[upperKey] == nil {
  379. subSet.Key_area[upperKey] = map[string]bool{}
  380. }
  381. subSet.Key_area[upperKey][area] = true
  382. }
  383. //建立与信息类型的对应关系
  384. for _, subtype := range vs.SubTypes {
  385. if subSet.Key_subtype[upperKey] == nil {
  386. subSet.Key_subtype[upperKey] = map[string]bool{}
  387. }
  388. subSet.Key_subtype[upperKey][subtype] = true
  389. }
  390. }
  391. }
  392. return subSet
  393. }
  394. //加载数据到内存中
  395. func LoadBidding(mgo *MongodbSim, dbName, coll string, startTime int64, redisCache bool) (*[]map[string]interface{}, int64) {
  396. defer util.Catch()
  397. endTime := time.Now().Unix()
  398. query := map[string]interface{}{
  399. "pici": map[string]interface{}{
  400. "$gte": startTime,
  401. "$lt": endTime,
  402. },
  403. }
  404. // query = map[string]interface{}{
  405. // "_id": map[string]interface{}{
  406. // "$in": ToObjectIds([]string{"5b7bd6b9a5cb26b9b7449fd7", "5b7bd6b9a5cb26b9b7449fe4", "626c4b63923488e172579d81", "5b7bd6b9a5cb26b9b7449fde"}),
  407. // },
  408. // }
  409. logger.Info("开始加载", coll, "数据", query)
  410. var res []map[string]interface{}
  411. sess := mgo.GetMgoConn()
  412. defer mgo.DestoryMongoConn(sess)
  413. it := sess.DB(dbName).C(coll).Find(query).Select(map[string]interface{}{
  414. "title": 1,
  415. "detail": 1,
  416. "projectname": 1,
  417. "projectcode": 1,
  418. "buyer": 1,
  419. "buyerperson": 1,
  420. "buyertel": 1,
  421. "s_winner": 1,
  422. "agency": 1,
  423. "bidopentime": 1,
  424. "projectscope": 1,
  425. "publishtime": 1,
  426. "toptype": 1,
  427. "subtype": 1,
  428. "area": 1,
  429. "s_subscopeclass": 1,
  430. "city": 1,
  431. "buyerclass": 1,
  432. "jsondata": 1,
  433. "budget": 1,
  434. "bidamount": 1,
  435. "isValidFile": 1,
  436. }).Iter()
  437. index := 0
  438. for temp := make(map[string]interface{}); it.Next(&temp); {
  439. _id := BsonIdToSId(temp["_id"])
  440. if publishtime := util.Int64All(temp["publishtime"]); startTime-publishtime > 7*86400 {
  441. logger.Info(_id, "发布时间大于7天,不参与匹配", startTime, publishtime)
  442. continue
  443. }
  444. temp["_id"] = _id
  445. title, _ := temp["title"].(string)
  446. title = strings.ReplaceAll(title, "\n", "")
  447. temp["title"] = title
  448. if util.ObjToString(temp["area"]) == "A" {
  449. temp["area"] = "全国"
  450. }
  451. temp["attachment_count"] = GetAttachmentCount(temp)
  452. delete(temp, "projectinfo")
  453. res = append(res, temp)
  454. if redisCache {
  455. //信息缓存3天
  456. info := map[string]interface{}{}
  457. for _, v := range SaveBiddingField {
  458. if v == "_id" || temp[v] == nil {
  459. continue
  460. }
  461. info[v] = temp[v]
  462. }
  463. redis.Put(Pushcache_1, "info_"+_id, info, 259200)
  464. }
  465. temp = make(map[string]interface{})
  466. index++
  467. if index%500 == 0 {
  468. logger.Info("加载", coll, "数据:", index)
  469. }
  470. }
  471. logger.Info(coll, "数据已经加载结束。。。", index)
  472. return &res, endTime
  473. }
  474. //
  475. func IsVipUser(vipStatus int) bool {
  476. if vipStatus == 1 || vipStatus == 2 {
  477. return true
  478. }
  479. return false
  480. }
  481. //
  482. func ToSortList(list interface{}) *SortList {
  483. sl := make(SortList, 0)
  484. if list == nil {
  485. return &sl
  486. }
  487. b, err := json.Marshal(list)
  488. if err != nil {
  489. return &sl
  490. }
  491. err = json.Unmarshal(b, &sl)
  492. if err != nil {
  493. return &sl
  494. }
  495. sort.Sort(sl)
  496. return &sl
  497. }
  498. //第一个参数是老数据,第二个参数是新进数据
  499. func MergeSortList(o, n interface{}, maxPushSize int) *SortList {
  500. of, oo := o.(*SortList)
  501. if !oo {
  502. of = ToSortList(o)
  503. }
  504. nf, no := n.(*SortList)
  505. if !no {
  506. nf = ToSortList(n)
  507. }
  508. idMap := map[string]bool{}
  509. for _, v := range *nf {
  510. idMap[util.ObjToString((*v.Info)["_id"])] = true
  511. }
  512. for _, v := range *of {
  513. if idMap[util.ObjToString((*v.Info)["_id"])] {
  514. continue
  515. }
  516. *nf = append(*nf, v)
  517. }
  518. sort.Sort(nf)
  519. if len(*nf) > maxPushSize {
  520. *nf = (*nf)[:maxPushSize]
  521. }
  522. return nf
  523. }
  524. //获取招标信息附件数量
  525. func GetAttachmentCount(temp map[string]interface{}) int {
  526. isValidFile, _ := temp["isValidFile"].(bool)
  527. if isValidFile {
  528. return 1
  529. }
  530. return 0
  531. }
  532. //获取招标信息附件数量
  533. func GetAttachmentCountById(mgo *MongodbSim, dbName, coll, _id string) int {
  534. sess := mgo.GetMgoConn()
  535. defer mgo.DestoryMongoConn(sess)
  536. temp := map[string]interface{}{}
  537. sess.DB(dbName).C(coll).Find(map[string]interface{}{
  538. "_id": StringTOBsonId(_id),
  539. }).Select(map[string]interface{}{
  540. "isValidFile": 1,
  541. }).One(&temp)
  542. if temp != nil {
  543. return GetAttachmentCount(temp)
  544. }
  545. return -1
  546. }
  547. //
  548. func NewBiddingInfo(info *map[string]interface{}, keys []string) *BiddingInfo {
  549. bi := &BiddingInfo{}
  550. bi.Title, _ = (*info)["title"].(string)
  551. bi.ClearTitle = TitleClearRe.ReplaceAllString(strings.Replace(bi.Title, "\n", "", -1), "$1")
  552. bi.Area, _ = (*info)["area"].(string)
  553. bi.AreaTitle = fmt.Sprintf("[%s]%s", bi.Area, bi.ClearTitle)
  554. bi.Publishtime = util.Int64All((*info)["publishtime"])
  555. bi.PublishtimeYMD = FormatDateByInt64(&bi.Publishtime, Date_Short_Layout)
  556. bi.PublishtimeDiff = util.TimeDiff(time.Unix(bi.Publishtime, 0))
  557. bi.Buyerclass, _ = (*info)["buyerclass"].(string)
  558. bi.Subscopeclass = GetSubScopeClass((*info)["s_subscopeclass"])
  559. bi.Bidamount = (*info)["bidamount"]
  560. bi.Budget = (*info)["budget"]
  561. if bi.Bidamount != nil {
  562. bi.Acount = ConversionMoney(bi.Bidamount)
  563. } else if bi.Budget != nil {
  564. bi.Acount = ConversionMoney(bi.Budget)
  565. }
  566. bi.Id, _ = (*info)["_id"].(string)
  567. bi.Subtype, _ = (*info)["subtype"].(string)
  568. bi.Toptype, _ = (*info)["toptype"].(string)
  569. bi.Infotype = bi.Subtype
  570. if bi.Infotype == "" {
  571. bi.Infotype = bi.Toptype
  572. }
  573. bi.HighlightTitle = bi.ClearTitle
  574. for _, kw := range keys {
  575. kws := strings.Split(kw, "+")
  576. n := 0
  577. otitle := bi.HighlightTitle
  578. for _, kwn := range kws {
  579. ot := strings.Replace(otitle, kwn, "<span class='keys'>"+kwn+"</span>", 1)
  580. if ot != bi.HighlightTitle {
  581. n++
  582. otitle = ot
  583. } else {
  584. break
  585. }
  586. }
  587. if n == len(kws) {
  588. bi.HighlightTitle = otitle
  589. break
  590. }
  591. }
  592. return bi
  593. }
  594. //
  595. func SortListSplit(list *SortList, f func(v interface{})) {
  596. l := len(*list)
  597. if l == 0 {
  598. return
  599. }
  600. i := Mgo_ListSize
  601. for {
  602. if l > i {
  603. arr := (*list)[i-Mgo_ListSize : i]
  604. f(&arr)
  605. } else if l > i-Mgo_ListSize {
  606. arr := (*list)[i-Mgo_ListSize:]
  607. f(&arr)
  608. break
  609. }
  610. i += Mgo_ListSize
  611. }
  612. }
  613. //获取企业授权超级订阅/大会员的用户
  614. func LoadEntProductUsers(m *Mysql, testUserIds []int) (map[string]*UserInfo, map[int]*UserInfo, map[int]*UserInfo, []*UserInfo) {
  615. logger.Info("开始加载企业授权用户。。。")
  616. phoneMap := map[string]*UserInfo{}
  617. userMap := map[int]*UserInfo{}
  618. entMap := map[int]*UserInfo{}
  619. all := []*UserInfo{}
  620. query := `SELECT DISTINCT a.ent_id,IF(instr(a.product_type,'` + Ent_EmpowerMember + `')>0,'` + Ent_EmpowerMember + `','` + Ent_EmpowerVip + `') as product_type,c.phone,b.ent_user_id,d.name as ent_name,d.power_source,d.isNew from entniche_wait_empower a
  621. inner join entniche_power b on (a.end_time>? and b.status=1 and (a.product_type like '%` + Ent_EmpowerVip + `%' or a.product_type like '%` + Ent_EmpowerMember + `%') and a.id=b.wait_empower_id)
  622. inner join entniche_user c on (`
  623. if len(testUserIds) > 0 {
  624. array := []string{}
  625. for _, v := range testUserIds {
  626. array = append(array, fmt.Sprint(v))
  627. }
  628. query += `c.id in (` + strings.Join(array, ",") + `) and `
  629. }
  630. query += `b.ent_user_id=c.id)
  631. inner join entniche_info d on (d.id=a.ent_id)`
  632. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  633. for _, v := range *l {
  634. phone, _ := v["phone"].(string)
  635. if phone == "" {
  636. continue
  637. }
  638. u := &UserInfo{
  639. Entniche: &Entniche{
  640. EntId: util.IntAll(v["ent_id"]),
  641. EntName: util.ObjToString(v["ent_name"]),
  642. UserId: util.IntAll(v["ent_user_id"]),
  643. ProductType: util.ObjToString(v["product_type"]),
  644. PowerSource: util.IntAll(v["power_source"]),
  645. IsNew: util.IntAll(v["isNew"]),
  646. },
  647. Phone: phone,
  648. }
  649. if strings.Contains(u.Entniche.ProductType, Ent_EmpowerMember) {
  650. u.MemberStatus = 1
  651. } else if strings.Contains(u.Entniche.ProductType, Ent_EmpowerVip) {
  652. u.VipStatus = 1
  653. } else {
  654. continue
  655. }
  656. phoneMap[phone] = u
  657. userMap[u.Entniche.UserId] = u
  658. all = append(all, u)
  659. entMap[u.Entniche.EntId] = u
  660. logger.Info("加载企业授权用户", u.Entniche.EntName, u.Entniche.EntId, u.Entniche.UserId, u.Entniche.ProductType, u.Phone)
  661. }
  662. return true
  663. }, query, NowFormat(Date_Full_Layout))
  664. logger.Info("企业授权用户加载结束。。。", len(phoneMap), len(userMap), len(entMap), len(all))
  665. return phoneMap, userMap, entMap, all
  666. }
  667. //加载商机管理用户
  668. func LoadEntnicheUsers(m *Mysql) map[string]bool {
  669. logger.Info("开始加载新版商机管理用户。。。")
  670. r := map[string]bool{}
  671. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  672. for _, v := range *l {
  673. phone := util.ObjToString(v["phone"])
  674. if phone != "" {
  675. r[phone] = true
  676. logger.Info("加载商机管理用户", phone)
  677. }
  678. }
  679. return true
  680. }, `SELECT b.phone from entniche_info a INNER JOIN entniche_user b on (a.status=1 and a.power_source is null and b.power=1 and a.id=b.ent_id)`)
  681. logger.Info("商机管理用户加载结束。。。", len(r))
  682. return r
  683. }
  684. //
  685. func GetWxTplMsg(subSet *SubSet) (string, string) {
  686. keyword := strings.Join(subSet.Keys, " ")
  687. if len([]rune(keyword)) > 10 {
  688. keyword = string([]rune(keyword)[:10]) + "..."
  689. }
  690. area := strings.Join(subSet.Areas, " ")
  691. if len([]rune(area)) > 10 {
  692. area = string([]rune(area)[:10]) + "..."
  693. }
  694. return keyword, area
  695. }
  696. //
  697. func GetPhone(u map[string]interface{}) string {
  698. phone := util.ObjToString(u["s_phone"])
  699. if phone == "" {
  700. phone = util.ObjToString(u["s_m_phone"])
  701. }
  702. return phone
  703. }
  704. //
  705. func GetAllByEntUserId(mgo *MongodbSim, msl *Mysql, entUserId int) *map[string]interface{} {
  706. entUsers := msl.SelectBySql(`select phone from entniche_user where id=?`, entUserId)
  707. if entUsers == nil || len(*entUsers) == 0 {
  708. logger.Info("entniche_user表中没有找到该企业用户", entUserId)
  709. return nil
  710. }
  711. phone, _ := (*entUsers)[0]["phone"].(string)
  712. if phone == "" {
  713. return nil
  714. }
  715. return getEntPushSet(mgo, msl, entUserId, phone)
  716. }
  717. //
  718. func GetAllByEntPositionId(mgo *MongodbSim, tidb, msl *Mysql, positionId int) *map[string]interface{} {
  719. position := tidb.SelectBySql(`select a.ent_id,b.phone from base_position a inner join base_user b on (a.id=? and a.user_id=b.id)`, positionId)
  720. if position == nil || len(*position) == 0 {
  721. logger.Info("无效的职位id", position)
  722. return nil
  723. }
  724. entId := util.Int64All((*position)[0]["ent_id"])
  725. if entId == 0 {
  726. logger.Info("该职位id没有找到对应的企业id", position)
  727. return nil
  728. }
  729. phone, _ := (*position)[0]["phone"].(string)
  730. if phone == "" {
  731. logger.Info("该职位id没有找到对应的手机号", position)
  732. return nil
  733. }
  734. entUsers := msl.SelectBySql(`select id from entniche_user where phone=? and ent_id=?`, phone, entId)
  735. if entUsers == nil || len(*entUsers) == 0 {
  736. logger.Info("entniche_user表中没有找到该企业用户", phone, entId)
  737. return nil
  738. }
  739. return getEntPushSet(mgo, msl, util.IntAll((*entUsers)[0]["id"]), phone)
  740. }
  741. //
  742. func getEntPushSet(mgo *MongodbSim, msl *Mysql, entUserId int, phone string) *map[string]interface{} {
  743. users, ok := mgo.Find(Mgo_User, map[string]interface{}{
  744. "$or": []map[string]interface{}{
  745. map[string]interface{}{
  746. "s_phone": phone,
  747. },
  748. map[string]interface{}{
  749. "s_m_phone": phone,
  750. },
  751. },
  752. }, map[string]interface{}{
  753. "_id": 0,
  754. "s_m_openid": 1,
  755. "i_ispush": 1,
  756. "s_jpushid": 1,
  757. "s_opushid": 1,
  758. "s_appponetype": 1,
  759. "i_applystatus": 1,
  760. }, `{"s_phone":-1}`, false, -1, -1)
  761. if !ok || users == nil || len(*users) == 0 {
  762. logger.Info("user表中没有找到该企业用户", entUserId)
  763. return nil
  764. }
  765. user := map[string]interface{}{}
  766. for _, v := range *users {
  767. s_m_openid := util.ObjToString(v["s_m_openid"])
  768. i_ispush := util.IntAll(v["i_ispush"])
  769. s_jpushid := util.ObjToString(v["s_jpushid"])
  770. s_opushid := util.ObjToString(v["s_opushid"])
  771. s_appponetype := util.ObjToString(v["s_appponetype"])
  772. if user["s_m_openid"] == nil && user["i_ispush"] == nil && s_m_openid != "" && i_ispush == 1 {
  773. user["s_m_openid"] = s_m_openid
  774. user["i_ispush"] = i_ispush
  775. user["i_applystatus"] = v["i_applystatus"]
  776. }
  777. if user["s_jpushid"] == nil && user["s_opushid"] == nil && user["s_appponetype"] == nil && s_appponetype != "" && (s_jpushid != "" || s_opushid != "") {
  778. user["s_jpushid"] = s_jpushid
  779. user["s_opushid"] = s_opushid
  780. user["s_appponetype"] = s_appponetype
  781. }
  782. }
  783. entniche_user, ok := mgo.FindOneByField(Mgo_Ent_User, map[string]interface{}{
  784. "i_userid": entUserId,
  785. }, `{"_id":0,"i_member_status":1,"i_vip_status":1}`)
  786. if ok && entniche_user != nil && len(*entniche_user) > 0 {
  787. for k, v := range *entniche_user {
  788. user[k] = v
  789. }
  790. }
  791. entniche_rule, ok := mgo.Find("entniche_rule", map[string]interface{}{
  792. "i_userid": entUserId,
  793. }, nil, `{"_id":0,"o_entniche":1}`, false, -1, -1)
  794. if ok && entniche_rule != nil {
  795. for _, v := range *entniche_rule {
  796. i_type := util.IntAll(v["i_type"])
  797. if i_type == 0 {
  798. user["o_entniche"] = v["o_entniche"]
  799. } else if i_type == 1 {
  800. user["o_vipjy"] = v["o_entniche"]
  801. user["o_member_jy"] = v["o_entniche"]
  802. } else if i_type == 2 {
  803. user["o_jy"] = v["o_entniche"]
  804. }
  805. }
  806. }
  807. return &user
  808. }
  809. //
  810. func GetEntUserSubset(mgo *MongodbSim, entUserId, tp int) map[string]interface{} {
  811. temp, ok := mgo.FindOneByField("entniche_rule", map[string]interface{}{
  812. "i_userid": entUserId,
  813. "i_type": tp,
  814. }, `{"o_entniche":1}`)
  815. if ok && temp != nil && len(*temp) > 0 {
  816. o_msgset, _ := (*temp)["o_entniche"].(map[string]interface{})
  817. return o_msgset
  818. }
  819. return nil
  820. }