public.go 22 KB

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