public.go 24 KB

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