public.go 23 KB

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