public.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. "site": 1,
  443. "agencyperson": 1,
  444. "agencytel": 1,
  445. "winnerperson": 1,
  446. "winnertel": 1,
  447. "signendtime": 1,
  448. "bidendtime": 1,
  449. "entidlist": 1,
  450. }).Iter()
  451. index := 0
  452. for temp := make(map[string]interface{}); it.Next(&temp); {
  453. _id := BsonIdToSId(temp["_id"])
  454. if publishtime := util.Int64All(temp["publishtime"]); startTime-publishtime > 7*86400 {
  455. logger.Info(_id, "发布时间大于7天,不参与匹配", startTime, publishtime)
  456. continue
  457. }
  458. temp["_id"] = _id
  459. title, _ := temp["title"].(string)
  460. title = strings.ReplaceAll(title, "\n", "")
  461. temp["title"] = title
  462. if util.ObjToString(temp["area"]) == "A" {
  463. temp["area"] = "全国"
  464. }
  465. temp["attachment_count"] = GetAttachmentCount(temp)
  466. delete(temp, "projectinfo")
  467. res = append(res, temp)
  468. if redisCache {
  469. //信息缓存3天
  470. info := map[string]interface{}{}
  471. for _, v := range SaveBiddingField {
  472. if v == "_id" || temp[v] == nil {
  473. continue
  474. }
  475. info[v] = temp[v]
  476. }
  477. redis.Put(Pushcache_1, "info_"+_id, info, 259200)
  478. }
  479. temp = make(map[string]interface{})
  480. index++
  481. if index%500 == 0 {
  482. logger.Info("加载", coll, "数据:", index)
  483. }
  484. }
  485. logger.Info(coll, "数据已经加载结束。。。", index)
  486. return &res, endTime
  487. }
  488. //
  489. func IsVipUser(vipStatus int) bool {
  490. if vipStatus == 1 || vipStatus == 2 {
  491. return true
  492. }
  493. return false
  494. }
  495. //
  496. func ToSortList(list interface{}) *SortList {
  497. sl := make(SortList, 0)
  498. if list == nil {
  499. return &sl
  500. }
  501. b, err := json.Marshal(list)
  502. if err != nil {
  503. return &sl
  504. }
  505. err = json.Unmarshal(b, &sl)
  506. if err != nil {
  507. return &sl
  508. }
  509. sort.Sort(sl)
  510. return &sl
  511. }
  512. //第一个参数是老数据,第二个参数是新进数据
  513. func MergeSortList(o, n interface{}, maxPushSize int) *SortList {
  514. of, oo := o.(*SortList)
  515. if !oo {
  516. of = ToSortList(o)
  517. }
  518. nf, no := n.(*SortList)
  519. if !no {
  520. nf = ToSortList(n)
  521. }
  522. idMap := map[string]bool{}
  523. for _, v := range *nf {
  524. idMap[util.ObjToString((*v.Info)["_id"])] = true
  525. }
  526. for _, v := range *of {
  527. if idMap[util.ObjToString((*v.Info)["_id"])] {
  528. continue
  529. }
  530. *nf = append(*nf, v)
  531. }
  532. sort.Sort(nf)
  533. if len(*nf) > maxPushSize {
  534. *nf = (*nf)[:maxPushSize]
  535. }
  536. return nf
  537. }
  538. //获取招标信息附件数量
  539. func GetAttachmentCount(temp map[string]interface{}) int {
  540. isValidFile, _ := temp["isValidFile"].(bool)
  541. if isValidFile {
  542. return 1
  543. }
  544. return 0
  545. }
  546. //获取招标信息附件数量
  547. func GetAttachmentCountById(mgo *MongodbSim, dbName, coll, _id string) int {
  548. sess := mgo.GetMgoConn()
  549. defer mgo.DestoryMongoConn(sess)
  550. temp := map[string]interface{}{}
  551. sess.DB(dbName).C(coll).Find(map[string]interface{}{
  552. "_id": StringTOBsonId(_id),
  553. }).Select(map[string]interface{}{
  554. "isValidFile": 1,
  555. }).One(&temp)
  556. if temp != nil {
  557. return GetAttachmentCount(temp)
  558. }
  559. return -1
  560. }
  561. //
  562. func NewBiddingInfo(info *map[string]interface{}, keys []string) *BiddingInfo {
  563. bi := &BiddingInfo{}
  564. bi.Title, _ = (*info)["title"].(string)
  565. bi.ClearTitle = TitleClearRe.ReplaceAllString(strings.Replace(bi.Title, "\n", "", -1), "$1")
  566. bi.Area, _ = (*info)["area"].(string)
  567. bi.AreaTitle = fmt.Sprintf("[%s]%s", bi.Area, bi.ClearTitle)
  568. bi.Publishtime = util.Int64All((*info)["publishtime"])
  569. bi.PublishtimeYMD = FormatDateByInt64(&bi.Publishtime, Date_Short_Layout)
  570. bi.PublishtimeDiff = util.TimeDiff(time.Unix(bi.Publishtime, 0))
  571. bi.Buyerclass, _ = (*info)["buyerclass"].(string)
  572. bi.Subscopeclass = GetSubScopeClass((*info)["s_subscopeclass"])
  573. bi.Bidamount = (*info)["bidamount"]
  574. bi.Budget = (*info)["budget"]
  575. if bi.Bidamount != nil {
  576. bi.Acount = ConversionMoney(bi.Bidamount)
  577. } else if bi.Budget != nil {
  578. bi.Acount = ConversionMoney(bi.Budget)
  579. }
  580. bi.Id, _ = (*info)["_id"].(string)
  581. bi.Subtype, _ = (*info)["subtype"].(string)
  582. bi.Toptype, _ = (*info)["toptype"].(string)
  583. bi.Infotype = bi.Subtype
  584. if bi.Infotype == "" {
  585. bi.Infotype = bi.Toptype
  586. }
  587. bi.HighlightTitle = bi.ClearTitle
  588. for _, kw := range keys {
  589. kws := strings.Split(kw, "+")
  590. n := 0
  591. otitle := bi.HighlightTitle
  592. for _, kwn := range kws {
  593. ot := strings.Replace(otitle, kwn, "<span class='keys'>"+kwn+"</span>", 1)
  594. if ot != bi.HighlightTitle {
  595. n++
  596. otitle = ot
  597. } else {
  598. break
  599. }
  600. }
  601. if n == len(kws) {
  602. bi.HighlightTitle = otitle
  603. break
  604. }
  605. }
  606. return bi
  607. }
  608. //
  609. func SortListSplit(list *SortList, f func(v interface{})) {
  610. l := len(*list)
  611. if l == 0 {
  612. return
  613. }
  614. i := Mgo_ListSize
  615. for {
  616. if l > i {
  617. arr := (*list)[i-Mgo_ListSize : i]
  618. f(&arr)
  619. } else if l > i-Mgo_ListSize {
  620. arr := (*list)[i-Mgo_ListSize:]
  621. f(&arr)
  622. break
  623. }
  624. i += Mgo_ListSize
  625. }
  626. }
  627. //获取企业授权超级订阅/大会员的用户
  628. func LoadEntProductUsers(m *Mysql, testUserIds []int) (map[string]*UserInfo, map[int]*UserInfo, map[int]*UserInfo, []*UserInfo) {
  629. logger.Info("开始加载企业授权用户。。。")
  630. phoneMap := map[string]*UserInfo{}
  631. userMap := map[int]*UserInfo{}
  632. entMap := map[int]*UserInfo{}
  633. all := []*UserInfo{}
  634. 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
  635. 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)
  636. inner join entniche_user c on (`
  637. if len(testUserIds) > 0 {
  638. array := []string{}
  639. for _, v := range testUserIds {
  640. array = append(array, fmt.Sprint(v))
  641. }
  642. query += `c.id in (` + strings.Join(array, ",") + `) and `
  643. }
  644. query += `b.ent_user_id=c.id)
  645. inner join entniche_info d on (d.id=a.ent_id)`
  646. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  647. for _, v := range *l {
  648. phone, _ := v["phone"].(string)
  649. if phone == "" {
  650. continue
  651. }
  652. u := &UserInfo{
  653. Entniche: &Entniche{
  654. EntId: util.IntAll(v["ent_id"]),
  655. EntName: util.ObjToString(v["ent_name"]),
  656. UserId: util.IntAll(v["ent_user_id"]),
  657. ProductType: util.ObjToString(v["product_type"]),
  658. PowerSource: util.IntAll(v["power_source"]),
  659. IsNew: util.IntAll(v["isNew"]),
  660. },
  661. Phone: phone,
  662. }
  663. if strings.Contains(u.Entniche.ProductType, Ent_EmpowerMember) {
  664. u.MemberStatus = 1
  665. } else if strings.Contains(u.Entniche.ProductType, Ent_EmpowerVip) {
  666. u.VipStatus = 1
  667. } else {
  668. continue
  669. }
  670. phoneMap[phone] = u
  671. userMap[u.Entniche.UserId] = u
  672. all = append(all, u)
  673. entMap[u.Entniche.EntId] = u
  674. logger.Info("加载企业授权用户", u.Entniche.EntName, u.Entniche.EntId, u.Entniche.UserId, u.Entniche.ProductType, u.Phone)
  675. }
  676. return true
  677. }, query, NowFormat(Date_Full_Layout))
  678. logger.Info("企业授权用户加载结束。。。", len(phoneMap), len(userMap), len(entMap), len(all))
  679. return phoneMap, userMap, entMap, all
  680. }
  681. //加载商机管理用户
  682. func LoadEntnicheUsers(m *Mysql) map[string]bool {
  683. logger.Info("开始加载新版商机管理用户。。。")
  684. r := map[string]bool{}
  685. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  686. for _, v := range *l {
  687. phone := util.ObjToString(v["phone"])
  688. if phone != "" {
  689. r[phone] = true
  690. logger.Info("加载商机管理用户", phone)
  691. }
  692. }
  693. return true
  694. }, `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)`)
  695. logger.Info("商机管理用户加载结束。。。", len(r))
  696. return r
  697. }
  698. //
  699. func GetWxTplMsg(subSet *SubSet) (string, string) {
  700. keyword := strings.Join(subSet.Keys, " ")
  701. if len([]rune(keyword)) > 10 {
  702. keyword = string([]rune(keyword)[:10]) + "..."
  703. }
  704. area := strings.Join(subSet.Areas, " ")
  705. if len([]rune(area)) > 10 {
  706. area = string([]rune(area)[:10]) + "..."
  707. }
  708. return keyword, area
  709. }
  710. //
  711. func GetPhone(u map[string]interface{}) string {
  712. phone := util.ObjToString(u["s_phone"])
  713. if phone == "" {
  714. phone = util.ObjToString(u["s_m_phone"])
  715. }
  716. return phone
  717. }
  718. //
  719. func GetAllByEntUserId(mgo *MongodbSim, msl *Mysql, entUserId int) *map[string]interface{} {
  720. entUsers := msl.SelectBySql(`select phone from entniche_user where id=?`, entUserId)
  721. if entUsers == nil || len(*entUsers) == 0 {
  722. logger.Info("entniche_user表中没有找到该企业用户", entUserId)
  723. return nil
  724. }
  725. phone, _ := (*entUsers)[0]["phone"].(string)
  726. if phone == "" {
  727. return nil
  728. }
  729. return getEntPushSet(mgo, msl, entUserId, phone)
  730. }
  731. //
  732. func GetAllByEntPositionId(mgo *MongodbSim, tidb, msl *Mysql, positionId int) *map[string]interface{} {
  733. 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)
  734. if position == nil || len(*position) == 0 {
  735. logger.Info("无效的职位id", position)
  736. return nil
  737. }
  738. entId := util.Int64All((*position)[0]["ent_id"])
  739. if entId == 0 {
  740. logger.Info("该职位id没有找到对应的企业id", position)
  741. return nil
  742. }
  743. phone, _ := (*position)[0]["phone"].(string)
  744. if phone == "" {
  745. logger.Info("该职位id没有找到对应的手机号", position)
  746. return nil
  747. }
  748. entUsers := msl.SelectBySql(`select id from entniche_user where phone=? and ent_id=?`, phone, entId)
  749. if entUsers == nil || len(*entUsers) == 0 {
  750. logger.Info("entniche_user表中没有找到该企业用户", phone, entId)
  751. return nil
  752. }
  753. return getEntPushSet(mgo, msl, util.IntAll((*entUsers)[0]["id"]), phone)
  754. }
  755. //
  756. func getEntPushSet(mgo *MongodbSim, msl *Mysql, entUserId int, phone string) *map[string]interface{} {
  757. users, ok := mgo.Find(Mgo_User, map[string]interface{}{
  758. "$or": []map[string]interface{}{
  759. map[string]interface{}{
  760. "s_phone": phone,
  761. },
  762. map[string]interface{}{
  763. "s_m_phone": phone,
  764. },
  765. },
  766. }, map[string]interface{}{
  767. "_id": 0,
  768. "s_m_openid": 1,
  769. "i_ispush": 1,
  770. "s_jpushid": 1,
  771. "s_opushid": 1,
  772. "s_appponetype": 1,
  773. "i_applystatus": 1,
  774. }, `{"s_phone":-1}`, false, -1, -1)
  775. if !ok || users == nil || len(*users) == 0 {
  776. logger.Info("user表中没有找到该企业用户", entUserId)
  777. return nil
  778. }
  779. user := map[string]interface{}{}
  780. for _, v := range *users {
  781. s_m_openid := util.ObjToString(v["s_m_openid"])
  782. i_ispush := util.IntAll(v["i_ispush"])
  783. s_jpushid := util.ObjToString(v["s_jpushid"])
  784. s_opushid := util.ObjToString(v["s_opushid"])
  785. s_appponetype := util.ObjToString(v["s_appponetype"])
  786. if user["s_m_openid"] == nil && user["i_ispush"] == nil && s_m_openid != "" && i_ispush == 1 {
  787. user["s_m_openid"] = s_m_openid
  788. user["i_ispush"] = i_ispush
  789. user["i_applystatus"] = v["i_applystatus"]
  790. }
  791. if user["s_jpushid"] == nil && user["s_opushid"] == nil && user["s_appponetype"] == nil && s_appponetype != "" && (s_jpushid != "" || s_opushid != "") {
  792. user["s_jpushid"] = s_jpushid
  793. user["s_opushid"] = s_opushid
  794. user["s_appponetype"] = s_appponetype
  795. }
  796. }
  797. entniche_user, ok := mgo.FindOneByField(Mgo_Ent_User, map[string]interface{}{
  798. "i_userid": entUserId,
  799. }, `{"_id":0,"i_member_status":1,"i_vip_status":1,"o_follow_project":1,"o_follow_ent":1}`)
  800. if ok && entniche_user != nil && len(*entniche_user) > 0 {
  801. for k, v := range *entniche_user {
  802. user[k] = v
  803. }
  804. }
  805. entniche_rule, ok := mgo.Find("entniche_rule", map[string]interface{}{
  806. "i_userid": entUserId,
  807. }, nil, `{"_id":0,"o_entniche":1,"i_type":1}`, false, -1, -1)
  808. if ok && entniche_rule != nil {
  809. for _, v := range *entniche_rule {
  810. i_type := util.IntAll(v["i_type"])
  811. if i_type == 0 {
  812. user["o_entniche"] = v["o_entniche"]
  813. } else if i_type == 1 {
  814. user["o_vipjy"] = v["o_entniche"]
  815. user["o_member_jy"] = v["o_entniche"]
  816. } else if i_type == 2 {
  817. user["o_jy"] = v["o_entniche"]
  818. }
  819. }
  820. }
  821. return &user
  822. }
  823. //
  824. func GetEntUserSubset(mgo *MongodbSim, entUserId, tp int) map[string]interface{} {
  825. temp, ok := mgo.FindOneByField("entniche_rule", map[string]interface{}{
  826. "i_userid": entUserId,
  827. "i_type": tp,
  828. }, `{"o_entniche":1}`)
  829. if ok && temp != nil && len(*temp) > 0 {
  830. o_msgset, _ := (*temp)["o_entniche"].(map[string]interface{})
  831. return o_msgset
  832. }
  833. return nil
  834. }