public.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. if err != nil {
  213. return keySets, err
  214. }
  215. for _, v := range keySets {
  216. if v == nil {
  217. continue
  218. }
  219. keys, appendKeys, notKeys := []string{}, []string{}, []string{}
  220. for _, vv := range v.Keys {
  221. keys = append(keys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  222. }
  223. for _, vv := range v.AppendKeys {
  224. appendKeys = append(appendKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  225. }
  226. for _, vv := range v.NotKeys {
  227. notKeys = append(notKeys, SpaceReg.Split(strings.TrimSpace(vv), -1)...)
  228. }
  229. v.Keys = keys
  230. v.AppendKeys = appendKeys
  231. v.NotKeys = notKeys
  232. }
  233. return keySets, err
  234. }
  235. //解析用户的订阅词
  236. func GetSubSet(isFreeUser bool, userId string, obj map[string]interface{}) *SubSet {
  237. mailpush := util.IntAll(obj["i_mailpush"])
  238. email := strings.TrimSpace(util.ObjToString(obj["s_email"]))
  239. if mailpush == 1 && !MailReg.MatchString(email) {
  240. mailpush = -1
  241. }
  242. subSet := &SubSet{
  243. Keys: []string{},
  244. Notkeys: []string{},
  245. Key_item: map[string]string{},
  246. Key_notkey: map[string]map[string]bool{},
  247. Key_area: map[string]map[string]bool{},
  248. Key_subtype: map[string]map[string]bool{},
  249. OriginalKeys: []string{},
  250. Areas: []string{},
  251. Subtypes: []string{},
  252. Buyerclasss: []string{},
  253. WxPush: util.IntAllDef(obj["i_wxpush"], 1),
  254. AppPush: util.IntAllDef(obj["i_apppush"], 1),
  255. MailPush: mailpush,
  256. Email: email,
  257. MatchWay: util.IntAllDef(obj["i_matchway"], 1),
  258. Matchbuyerclass_other: util.IntAllDef(obj["i_matchbuyerclass_other"], 1),
  259. ProjectMatch: util.IntAll(obj["i_projectmatch"]),
  260. RateMode: util.IntAllDef(obj["i_ratemode"], 2),
  261. MaxPushSize: util.IntAll(obj["i_maxpushsize"]),
  262. MaxMailSize: util.IntAll(obj["i_maxmailsize"]),
  263. }
  264. var keySets []*KeySet
  265. if isFreeUser {
  266. var err error
  267. keySets, err = GetKeySets(obj["a_key"])
  268. if err != nil {
  269. logger.Error("获取用户关键词错误!", userId, obj["a_key"], err)
  270. return subSet
  271. }
  272. area := obj["o_area"]
  273. if ppStatus := util.IntAll(obj["i_ppstatus"]); ppStatus == 1 {
  274. area = obj["o_area_p"]
  275. }
  276. if newFree := util.IntAll(obj["i_newfree"]); newFree == 1 && area != nil {
  277. subSet.IsUpgrade = true
  278. subSet.Subtype, _ = obj["a_infotype"].([]interface{})
  279. subSet.Area, _ = area.(map[string]interface{})
  280. }
  281. } else {
  282. items, _ := obj["a_items"].([]interface{})
  283. for _, v := range items {
  284. item, _ := v.(map[string]interface{})
  285. itemName := strings.TrimSpace(util.ObjToString(item["s_item"]))
  286. subSet.Items = append(subSet.Items, itemName)
  287. kss, err := GetKeySets(item["a_key"])
  288. if err != nil {
  289. logger.Error("获取用户关键词错误!", userId, item["a_key"], err)
  290. continue
  291. }
  292. for _, vv := range kss {
  293. if vv == nil {
  294. continue
  295. }
  296. if vv.MatchWay == 1 {
  297. for _, vvv := range vv.Keys {
  298. keySets = append(keySets, &KeySet{
  299. Item: itemName,
  300. Keys: []string{vvv},
  301. NotKeys: vv.NotKeys,
  302. })
  303. }
  304. for _, vvv := range vv.AppendKeys {
  305. keySets = append(keySets, &KeySet{
  306. Item: itemName,
  307. Keys: []string{vvv},
  308. NotKeys: vv.NotKeys,
  309. })
  310. }
  311. } else {
  312. vv.Item = itemName
  313. keySets = append(keySets, vv)
  314. }
  315. }
  316. }
  317. subSet.Buyerclass, _ = obj["a_buyerclass"].([]interface{})
  318. subSet.Subtype, _ = obj["a_infotype"].([]interface{})
  319. subSet.Area, _ = obj["o_area"].(map[string]interface{})
  320. }
  321. for _, v := range subSet.Buyerclass {
  322. s_v, _ := v.(string)
  323. if s_v == "" {
  324. continue
  325. }
  326. subSet.Buyerclasss = append(subSet.Buyerclasss, s_v)
  327. }
  328. for _, v := range subSet.Subtype {
  329. s_v, _ := v.(string)
  330. if s_v == "" {
  331. continue
  332. }
  333. subSet.Subtypes = append(subSet.Subtypes, s_v)
  334. }
  335. for area, _ := range subSet.Area {
  336. if area == "" {
  337. continue
  338. }
  339. subSet.Areas = append(subSet.Areas, area)
  340. }
  341. ////////////////
  342. for _, vs := range keySets {
  343. if vs == nil {
  344. logger.Error(userId, "关键词设置异常")
  345. continue
  346. }
  347. var vs_keys []string
  348. for _, vs_v := range [][]string{vs.Keys, vs.AppendKeys} {
  349. for _, vs_vv := range vs_v {
  350. vs_vv = strings.TrimSpace(vs_vv)
  351. if vs_vv == "" {
  352. continue
  353. }
  354. vs_keys = append(vs_keys, vs_vv)
  355. }
  356. }
  357. if len(vs_keys) == 0 {
  358. continue
  359. }
  360. key := strings.Join(vs_keys, "+")
  361. subSet.OriginalKeys = append(subSet.OriginalKeys, key)
  362. if KeyFilterReg.MatchString(key) {
  363. continue
  364. } else if !KeyRetainReg.MatchString(key) {
  365. continue
  366. }
  367. subSet.Keys = append(subSet.Keys, key)
  368. subSet.Notkeys = append(subSet.Notkeys, vs.NotKeys...)
  369. //转大写
  370. upperKey := strings.ToUpper(key)
  371. subSet.Key_item[upperKey] = vs.Item
  372. //建立与排除词的对应关系
  373. for _, notkey := range vs.NotKeys {
  374. upperNotkey := strings.ToUpper(notkey)
  375. if subSet.Key_notkey[upperKey] == nil {
  376. subSet.Key_notkey[upperKey] = map[string]bool{}
  377. }
  378. subSet.Key_notkey[upperKey][upperNotkey] = true
  379. }
  380. //免费用户需要进行映射
  381. if isFreeUser {
  382. //建立与信息范围的对应关系
  383. for _, area := range vs.Areas {
  384. if subSet.Key_area[upperKey] == nil {
  385. subSet.Key_area[upperKey] = map[string]bool{}
  386. }
  387. subSet.Key_area[upperKey][area] = true
  388. }
  389. //建立与信息类型的对应关系
  390. for _, subtype := range vs.SubTypes {
  391. if subSet.Key_subtype[upperKey] == nil {
  392. subSet.Key_subtype[upperKey] = map[string]bool{}
  393. }
  394. subSet.Key_subtype[upperKey][subtype] = true
  395. }
  396. }
  397. }
  398. return subSet
  399. }
  400. //加载数据到内存中
  401. func LoadBidding(mgo *MongodbSim, dbName, coll string, startTime int64, redisCache bool) (*[]map[string]interface{}, int64) {
  402. defer util.Catch()
  403. endTime := time.Now().Unix()
  404. query := map[string]interface{}{
  405. "pici": map[string]interface{}{
  406. "$gte": startTime,
  407. "$lt": endTime,
  408. },
  409. }
  410. // query = map[string]interface{}{
  411. // "_id": map[string]interface{}{
  412. // "$in": ToObjectIds([]string{"5b7bd6b9a5cb26b9b7449fd7", "5b7bd6b9a5cb26b9b7449fe4", "626c4b63923488e172579d81", "5b7bd6b9a5cb26b9b7449fde"}),
  413. // },
  414. // }
  415. logger.Info("开始加载", coll, "数据", query)
  416. var res []map[string]interface{}
  417. sess := mgo.GetMgoConn()
  418. defer mgo.DestoryMongoConn(sess)
  419. it := sess.DB(dbName).C(coll).Find(query).Select(map[string]interface{}{
  420. "title": 1,
  421. "detail": 1,
  422. "projectname": 1,
  423. "projectcode": 1,
  424. "buyer": 1,
  425. "buyerperson": 1,
  426. "buyertel": 1,
  427. "s_winner": 1,
  428. "agency": 1,
  429. "bidopentime": 1,
  430. "projectscope": 1,
  431. "publishtime": 1,
  432. "toptype": 1,
  433. "subtype": 1,
  434. "area": 1,
  435. "s_subscopeclass": 1,
  436. "city": 1,
  437. "buyerclass": 1,
  438. "jsondata": 1,
  439. "budget": 1,
  440. "bidamount": 1,
  441. "isValidFile": 1,
  442. }).Iter()
  443. index := 0
  444. for temp := make(map[string]interface{}); it.Next(&temp); {
  445. _id := BsonIdToSId(temp["_id"])
  446. if publishtime := util.Int64All(temp["publishtime"]); startTime-publishtime > 7*86400 {
  447. logger.Info(_id, "发布时间大于7天,不参与匹配", startTime, publishtime)
  448. continue
  449. }
  450. temp["_id"] = _id
  451. title, _ := temp["title"].(string)
  452. title = strings.ReplaceAll(title, "\n", "")
  453. temp["title"] = title
  454. if util.ObjToString(temp["area"]) == "A" {
  455. temp["area"] = "全国"
  456. }
  457. temp["attachment_count"] = GetAttachmentCount(temp)
  458. delete(temp, "projectinfo")
  459. res = append(res, temp)
  460. if redisCache {
  461. //信息缓存3天
  462. info := map[string]interface{}{}
  463. for _, v := range SaveBiddingField {
  464. if v == "_id" || temp[v] == nil {
  465. continue
  466. }
  467. info[v] = temp[v]
  468. }
  469. redis.Put(Pushcache_1, "info_"+_id, info, 259200)
  470. }
  471. temp = make(map[string]interface{})
  472. index++
  473. if index%500 == 0 {
  474. logger.Info("加载", coll, "数据:", index)
  475. }
  476. }
  477. logger.Info(coll, "数据已经加载结束。。。", index)
  478. return &res, endTime
  479. }
  480. //
  481. func IsVipUser(vipStatus int) bool {
  482. if vipStatus == 1 || vipStatus == 2 {
  483. return true
  484. }
  485. return false
  486. }
  487. //
  488. func ToSortList(list interface{}) *SortList {
  489. sl := make(SortList, 0)
  490. if list == nil {
  491. return &sl
  492. }
  493. b, err := json.Marshal(list)
  494. if err != nil {
  495. return &sl
  496. }
  497. err = json.Unmarshal(b, &sl)
  498. if err != nil {
  499. return &sl
  500. }
  501. sort.Sort(sl)
  502. return &sl
  503. }
  504. //第一个参数是老数据,第二个参数是新进数据
  505. func MergeSortList(o, n interface{}, maxPushSize int) *SortList {
  506. of, oo := o.(*SortList)
  507. if !oo {
  508. of = ToSortList(o)
  509. }
  510. nf, no := n.(*SortList)
  511. if !no {
  512. nf = ToSortList(n)
  513. }
  514. idMap := map[string]bool{}
  515. for _, v := range *nf {
  516. idMap[util.ObjToString((*v.Info)["_id"])] = true
  517. }
  518. for _, v := range *of {
  519. if idMap[util.ObjToString((*v.Info)["_id"])] {
  520. continue
  521. }
  522. *nf = append(*nf, v)
  523. }
  524. sort.Sort(nf)
  525. if len(*nf) > maxPushSize {
  526. *nf = (*nf)[:maxPushSize]
  527. }
  528. return nf
  529. }
  530. //获取招标信息附件数量
  531. func GetAttachmentCount(temp map[string]interface{}) int {
  532. isValidFile, _ := temp["isValidFile"].(bool)
  533. if isValidFile {
  534. return 1
  535. }
  536. return 0
  537. }
  538. //获取招标信息附件数量
  539. func GetAttachmentCountById(mgo *MongodbSim, dbName, coll, _id string) int {
  540. sess := mgo.GetMgoConn()
  541. defer mgo.DestoryMongoConn(sess)
  542. temp := map[string]interface{}{}
  543. sess.DB(dbName).C(coll).Find(map[string]interface{}{
  544. "_id": StringTOBsonId(_id),
  545. }).Select(map[string]interface{}{
  546. "isValidFile": 1,
  547. }).One(&temp)
  548. if temp != nil {
  549. return GetAttachmentCount(temp)
  550. }
  551. return -1
  552. }
  553. //
  554. func NewBiddingInfo(info *map[string]interface{}, keys []string) *BiddingInfo {
  555. bi := &BiddingInfo{}
  556. bi.Title, _ = (*info)["title"].(string)
  557. bi.ClearTitle = TitleClearRe.ReplaceAllString(strings.Replace(bi.Title, "\n", "", -1), "$1")
  558. bi.Area, _ = (*info)["area"].(string)
  559. bi.AreaTitle = fmt.Sprintf("[%s]%s", bi.Area, bi.ClearTitle)
  560. bi.Publishtime = util.Int64All((*info)["publishtime"])
  561. bi.PublishtimeYMD = FormatDateByInt64(&bi.Publishtime, Date_Short_Layout)
  562. bi.PublishtimeDiff = util.TimeDiff(time.Unix(bi.Publishtime, 0))
  563. bi.Buyerclass, _ = (*info)["buyerclass"].(string)
  564. bi.Subscopeclass = GetSubScopeClass((*info)["s_subscopeclass"])
  565. bi.Bidamount = (*info)["bidamount"]
  566. bi.Budget = (*info)["budget"]
  567. if bi.Bidamount != nil {
  568. bi.Acount = ConversionMoney(bi.Bidamount)
  569. } else if bi.Budget != nil {
  570. bi.Acount = ConversionMoney(bi.Budget)
  571. }
  572. bi.Id, _ = (*info)["_id"].(string)
  573. bi.Subtype, _ = (*info)["subtype"].(string)
  574. bi.Toptype, _ = (*info)["toptype"].(string)
  575. bi.Infotype = bi.Subtype
  576. if bi.Infotype == "" {
  577. bi.Infotype = bi.Toptype
  578. }
  579. bi.HighlightTitle = bi.ClearTitle
  580. for _, kw := range keys {
  581. kws := strings.Split(kw, "+")
  582. n := 0
  583. otitle := bi.HighlightTitle
  584. for _, kwn := range kws {
  585. ot := strings.Replace(otitle, kwn, "<span class='keys'>"+kwn+"</span>", 1)
  586. if ot != bi.HighlightTitle {
  587. n++
  588. otitle = ot
  589. } else {
  590. break
  591. }
  592. }
  593. if n == len(kws) {
  594. bi.HighlightTitle = otitle
  595. break
  596. }
  597. }
  598. return bi
  599. }
  600. //
  601. func SortListSplit(list *SortList, f func(v interface{})) {
  602. l := len(*list)
  603. if l == 0 {
  604. return
  605. }
  606. i := Mgo_ListSize
  607. for {
  608. if l > i {
  609. arr := (*list)[i-Mgo_ListSize : i]
  610. f(&arr)
  611. } else if l > i-Mgo_ListSize {
  612. arr := (*list)[i-Mgo_ListSize:]
  613. f(&arr)
  614. break
  615. }
  616. i += Mgo_ListSize
  617. }
  618. }
  619. //获取企业授权超级订阅/大会员的用户
  620. func LoadEntProductUsers(m *Mysql, testUserIds []int) (map[string]*UserInfo, map[int]*UserInfo, map[int]*UserInfo, []*UserInfo) {
  621. logger.Info("开始加载企业授权用户。。。")
  622. phoneMap := map[string]*UserInfo{}
  623. userMap := map[int]*UserInfo{}
  624. entMap := map[int]*UserInfo{}
  625. all := []*UserInfo{}
  626. 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
  627. 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)
  628. inner join entniche_user c on (`
  629. if len(testUserIds) > 0 {
  630. array := []string{}
  631. for _, v := range testUserIds {
  632. array = append(array, fmt.Sprint(v))
  633. }
  634. query += `c.id in (` + strings.Join(array, ",") + `) and `
  635. }
  636. query += `b.ent_user_id=c.id)
  637. inner join entniche_info d on (d.id=a.ent_id)`
  638. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  639. for _, v := range *l {
  640. phone, _ := v["phone"].(string)
  641. if phone == "" {
  642. continue
  643. }
  644. u := &UserInfo{
  645. Entniche: &Entniche{
  646. EntId: util.IntAll(v["ent_id"]),
  647. EntName: util.ObjToString(v["ent_name"]),
  648. UserId: util.IntAll(v["ent_user_id"]),
  649. ProductType: util.ObjToString(v["product_type"]),
  650. PowerSource: util.IntAll(v["power_source"]),
  651. IsNew: util.IntAll(v["isNew"]),
  652. },
  653. Phone: phone,
  654. }
  655. if strings.Contains(u.Entniche.ProductType, Ent_EmpowerMember) {
  656. u.MemberStatus = 1
  657. } else if strings.Contains(u.Entniche.ProductType, Ent_EmpowerVip) {
  658. u.VipStatus = 1
  659. } else {
  660. continue
  661. }
  662. phoneMap[phone] = u
  663. userMap[u.Entniche.UserId] = u
  664. all = append(all, u)
  665. entMap[u.Entniche.EntId] = u
  666. logger.Info("加载企业授权用户", u.Entniche.EntName, u.Entniche.EntId, u.Entniche.UserId, u.Entniche.ProductType, u.Phone)
  667. }
  668. return true
  669. }, query, NowFormat(Date_Full_Layout))
  670. logger.Info("企业授权用户加载结束。。。", len(phoneMap), len(userMap), len(entMap), len(all))
  671. return phoneMap, userMap, entMap, all
  672. }
  673. //加载商机管理用户
  674. func LoadEntnicheUsers(m *Mysql) map[string]bool {
  675. logger.Info("开始加载新版商机管理用户。。。")
  676. r := map[string]bool{}
  677. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  678. for _, v := range *l {
  679. phone := util.ObjToString(v["phone"])
  680. if phone != "" {
  681. r[phone] = true
  682. logger.Info("加载商机管理用户", phone)
  683. }
  684. }
  685. return true
  686. }, `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)`)
  687. logger.Info("商机管理用户加载结束。。。", len(r))
  688. return r
  689. }
  690. //
  691. func GetWxTplMsg(subSet *SubSet) (string, string) {
  692. keyword := strings.Join(subSet.Keys, " ")
  693. if len([]rune(keyword)) > 10 {
  694. keyword = string([]rune(keyword)[:10]) + "..."
  695. }
  696. area := strings.Join(subSet.Areas, " ")
  697. if len([]rune(area)) > 10 {
  698. area = string([]rune(area)[:10]) + "..."
  699. }
  700. return keyword, area
  701. }
  702. //
  703. func GetPhone(u map[string]interface{}) string {
  704. phone := util.ObjToString(u["s_phone"])
  705. if phone == "" {
  706. phone = util.ObjToString(u["s_m_phone"])
  707. }
  708. return phone
  709. }
  710. //
  711. func GetAllByEntUserId(mgo *MongodbSim, msl *Mysql, entUserId int) *map[string]interface{} {
  712. entUsers := msl.SelectBySql(`select phone from entniche_user where id=?`, entUserId)
  713. if entUsers == nil || len(*entUsers) == 0 {
  714. logger.Info("entniche_user表中没有找到该企业用户", entUserId)
  715. return nil
  716. }
  717. phone, _ := (*entUsers)[0]["phone"].(string)
  718. if phone == "" {
  719. return nil
  720. }
  721. return getEntPushSet(mgo, msl, entUserId, phone)
  722. }
  723. //
  724. func GetAllByEntPositionId(mgo *MongodbSim, tidb, msl *Mysql, positionId int) *map[string]interface{} {
  725. 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)
  726. if position == nil || len(*position) == 0 {
  727. logger.Info("无效的职位id", position)
  728. return nil
  729. }
  730. entId := util.Int64All((*position)[0]["ent_id"])
  731. if entId == 0 {
  732. logger.Info("该职位id没有找到对应的企业id", position)
  733. return nil
  734. }
  735. phone, _ := (*position)[0]["phone"].(string)
  736. if phone == "" {
  737. logger.Info("该职位id没有找到对应的手机号", position)
  738. return nil
  739. }
  740. entUsers := msl.SelectBySql(`select id from entniche_user where phone=? and ent_id=?`, phone, entId)
  741. if entUsers == nil || len(*entUsers) == 0 {
  742. logger.Info("entniche_user表中没有找到该企业用户", phone, entId)
  743. return nil
  744. }
  745. return getEntPushSet(mgo, msl, util.IntAll((*entUsers)[0]["id"]), phone)
  746. }
  747. //
  748. func getEntPushSet(mgo *MongodbSim, msl *Mysql, entUserId int, phone string) *map[string]interface{} {
  749. users, ok := mgo.Find(Mgo_User, map[string]interface{}{
  750. "$or": []map[string]interface{}{
  751. map[string]interface{}{
  752. "s_phone": phone,
  753. },
  754. map[string]interface{}{
  755. "s_m_phone": phone,
  756. },
  757. },
  758. }, map[string]interface{}{
  759. "_id": 0,
  760. "s_m_openid": 1,
  761. "i_ispush": 1,
  762. "s_jpushid": 1,
  763. "s_opushid": 1,
  764. "s_appponetype": 1,
  765. "i_applystatus": 1,
  766. }, `{"s_phone":-1}`, false, -1, -1)
  767. if !ok || users == nil || len(*users) == 0 {
  768. logger.Info("user表中没有找到该企业用户", entUserId)
  769. return nil
  770. }
  771. user := map[string]interface{}{}
  772. for _, v := range *users {
  773. s_m_openid := util.ObjToString(v["s_m_openid"])
  774. i_ispush := util.IntAll(v["i_ispush"])
  775. s_jpushid := util.ObjToString(v["s_jpushid"])
  776. s_opushid := util.ObjToString(v["s_opushid"])
  777. s_appponetype := util.ObjToString(v["s_appponetype"])
  778. if user["s_m_openid"] == nil && user["i_ispush"] == nil && s_m_openid != "" && i_ispush == 1 {
  779. user["s_m_openid"] = s_m_openid
  780. user["i_ispush"] = i_ispush
  781. user["i_applystatus"] = v["i_applystatus"]
  782. }
  783. if user["s_jpushid"] == nil && user["s_opushid"] == nil && user["s_appponetype"] == nil && s_appponetype != "" && (s_jpushid != "" || s_opushid != "") {
  784. user["s_jpushid"] = s_jpushid
  785. user["s_opushid"] = s_opushid
  786. user["s_appponetype"] = s_appponetype
  787. }
  788. }
  789. entniche_user, ok := mgo.FindOneByField(Mgo_Ent_User, map[string]interface{}{
  790. "i_userid": entUserId,
  791. }, `{"_id":0,"i_member_status":1,"i_vip_status":1,"o_follow_project":1,"o_follow_ent":1}`)
  792. if ok && entniche_user != nil && len(*entniche_user) > 0 {
  793. for k, v := range *entniche_user {
  794. user[k] = v
  795. }
  796. }
  797. entniche_rule, ok := mgo.Find("entniche_rule", map[string]interface{}{
  798. "i_userid": entUserId,
  799. }, nil, `{"_id":0,"o_entniche":1,"i_type":1}`, false, -1, -1)
  800. if ok && entniche_rule != nil {
  801. for _, v := range *entniche_rule {
  802. i_type := util.IntAll(v["i_type"])
  803. if i_type == 0 {
  804. user["o_entniche"] = v["o_entniche"]
  805. } else if i_type == 1 {
  806. user["o_vipjy"] = v["o_entniche"]
  807. user["o_member_jy"] = v["o_entniche"]
  808. } else if i_type == 2 {
  809. user["o_jy"] = v["o_entniche"]
  810. }
  811. }
  812. }
  813. return &user
  814. }
  815. //
  816. func GetEntUserSubset(mgo *MongodbSim, entUserId, tp int) map[string]interface{} {
  817. temp, ok := mgo.FindOneByField("entniche_rule", map[string]interface{}{
  818. "i_userid": entUserId,
  819. "i_type": tp,
  820. }, `{"o_entniche":1}`)
  821. if ok && temp != nil && len(*temp) > 0 {
  822. o_msgset, _ := (*temp)["o_entniche"].(map[string]interface{})
  823. return o_msgset
  824. }
  825. return nil
  826. }