public.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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/mongodb"
  15. . "app.yhyue.com/moapp/jybase/mysql"
  16. "app.yhyue.com/moapp/jybase/redis"
  17. "github.com/donnie4w/go-logger/logger"
  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(mysql *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 := mysql.SelectBySql(`SELECT a.l_starttime,b.id from bigmember_service_user a
  176. 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...)
  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. "buyerclass": 1,
  238. "jsondata": 1,
  239. "budget": 1,
  240. "bidamount": 1,
  241. "isValidFile": 1,
  242. "site": 1,
  243. "agencyperson": 1,
  244. "agencytel": 1,
  245. "winnerperson": 1,
  246. "winnertel": 1,
  247. "signendtime": 1,
  248. "bidendtime": 1,
  249. "entidlist": 1,
  250. }).Iter()
  251. index := 0
  252. for temp := make(map[string]interface{}); it.Next(&temp); {
  253. _id := BsonIdToSId(temp["_id"])
  254. if publishtime := util.Int64All(temp["publishtime"]); startTime-publishtime > 7*86400 {
  255. logger.Info(_id, "发布时间大于7天,不参与匹配", startTime, publishtime)
  256. continue
  257. }
  258. temp["_id"] = _id
  259. title, _ := temp["title"].(string)
  260. title = strings.ReplaceAll(title, "\n", "")
  261. temp["title"] = title
  262. if util.ObjToString(temp["area"]) == "A" {
  263. temp["area"] = "全国"
  264. }
  265. temp["attachment_count"] = GetAttachmentCount(temp)
  266. delete(temp, "projectinfo")
  267. res = append(res, temp)
  268. if redisCache {
  269. //信息缓存3天
  270. info := map[string]interface{}{}
  271. for _, v := range SaveBiddingField {
  272. if v == "_id" || temp[v] == nil {
  273. continue
  274. }
  275. info[v] = temp[v]
  276. }
  277. redis.Put(Pushcache_1, "info_"+_id, info, 259200)
  278. }
  279. temp = make(map[string]interface{})
  280. index++
  281. if index%500 == 0 {
  282. logger.Info("加载", coll, "数据:", index)
  283. }
  284. }
  285. logger.Info(coll, "数据已经加载结束。。。", index)
  286. return &res, endTime
  287. }
  288. //
  289. func ToSortList(list interface{}) *SortList {
  290. sl := make(SortList, 0)
  291. if list == nil {
  292. return &sl
  293. }
  294. b, err := json.Marshal(list)
  295. if err != nil {
  296. return &sl
  297. }
  298. err = json.Unmarshal(b, &sl)
  299. if err != nil {
  300. return &sl
  301. }
  302. sort.Sort(sl)
  303. return &sl
  304. }
  305. //第一个参数是老数据,第二个参数是新进数据
  306. func MergeSortList(o, n interface{}, maxPushSize int) *SortList {
  307. of, oo := o.(*SortList)
  308. if !oo {
  309. of = ToSortList(o)
  310. }
  311. nf, no := n.(*SortList)
  312. if !no {
  313. nf = ToSortList(n)
  314. }
  315. idMap := map[string]bool{}
  316. for _, v := range *nf {
  317. idMap[util.ObjToString((*v.Info)["_id"])] = true
  318. }
  319. for _, v := range *of {
  320. if idMap[util.ObjToString((*v.Info)["_id"])] {
  321. continue
  322. }
  323. *nf = append(*nf, v)
  324. }
  325. sort.Sort(nf)
  326. if len(*nf) > maxPushSize {
  327. *nf = (*nf)[:maxPushSize]
  328. }
  329. return nf
  330. }
  331. //获取招标信息附件数量
  332. func GetAttachmentCount(temp map[string]interface{}) int {
  333. isValidFile, _ := temp["isValidFile"].(bool)
  334. if isValidFile {
  335. return 1
  336. }
  337. return 0
  338. }
  339. //获取招标信息附件数量
  340. func GetAttachmentCountById(mgo *MongodbSim, dbName, coll, _id string) int {
  341. sess := mgo.GetMgoConn()
  342. defer mgo.DestoryMongoConn(sess)
  343. temp := map[string]interface{}{}
  344. sess.DB(dbName).C(coll).Find(map[string]interface{}{
  345. "_id": StringTOBsonId(_id),
  346. }).Select(map[string]interface{}{
  347. "isValidFile": 1,
  348. }).One(&temp)
  349. if temp != nil {
  350. return GetAttachmentCount(temp)
  351. }
  352. return -1
  353. }
  354. //
  355. func NewBiddingInfo(info *map[string]interface{}, keys []string) *BiddingInfo {
  356. bi := &BiddingInfo{}
  357. bi.Title, _ = (*info)["title"].(string)
  358. bi.ClearTitle = TitleClearRe.ReplaceAllString(strings.Replace(bi.Title, "\n", "", -1), "$1")
  359. bi.Area, _ = (*info)["area"].(string)
  360. bi.AreaTitle = fmt.Sprintf("[%s]%s", bi.Area, bi.ClearTitle)
  361. bi.Publishtime = util.Int64All((*info)["publishtime"])
  362. bi.PublishtimeYMD = FormatDateByInt64(&bi.Publishtime, Date_Short_Layout)
  363. bi.PublishtimeDiff = util.TimeDiff(time.Unix(bi.Publishtime, 0))
  364. bi.Buyerclass, _ = (*info)["buyerclass"].(string)
  365. bi.Subscopeclass = GetSubScopeClass((*info)["s_subscopeclass"])
  366. bi.Bidamount = (*info)["bidamount"]
  367. bi.Budget = (*info)["budget"]
  368. if bi.Bidamount != nil {
  369. bi.Acount = ConversionMoney(bi.Bidamount)
  370. } else if bi.Budget != nil {
  371. bi.Acount = ConversionMoney(bi.Budget)
  372. }
  373. bi.Id, _ = (*info)["_id"].(string)
  374. bi.Subtype, _ = (*info)["subtype"].(string)
  375. bi.Toptype, _ = (*info)["toptype"].(string)
  376. bi.Infotype = bi.Subtype
  377. if bi.Infotype == "" {
  378. bi.Infotype = bi.Toptype
  379. }
  380. bi.HighlightTitle = bi.ClearTitle
  381. for _, kw := range keys {
  382. kws := strings.Split(kw, "+")
  383. n := 0
  384. otitle := bi.HighlightTitle
  385. for _, kwn := range kws {
  386. ot := strings.Replace(otitle, kwn, "<span class='keys'>"+kwn+"</span>", 1)
  387. if ot != bi.HighlightTitle {
  388. n++
  389. otitle = ot
  390. } else {
  391. break
  392. }
  393. }
  394. if n == len(kws) {
  395. bi.HighlightTitle = otitle
  396. break
  397. }
  398. }
  399. return bi
  400. }
  401. //
  402. func SortListSplit(list *SortList, f func(v interface{})) {
  403. l := len(*list)
  404. if l == 0 {
  405. return
  406. }
  407. i := Mgo_ListSize
  408. for {
  409. if l > i {
  410. arr := (*list)[i-Mgo_ListSize : i]
  411. f(&arr)
  412. } else if l > i-Mgo_ListSize {
  413. arr := (*list)[i-Mgo_ListSize:]
  414. f(&arr)
  415. break
  416. }
  417. i += Mgo_ListSize
  418. }
  419. }
  420. //获取企业授权超级订阅/大会员的用户
  421. func LoadEntProductUsers(m *Mysql, testUserIds []int) (map[string]*UserInfo, map[int]*UserInfo, map[int]*UserInfo, []*UserInfo) {
  422. logger.Info("开始加载企业授权用户。。。")
  423. phoneMap := map[string]*UserInfo{}
  424. userMap := map[int]*UserInfo{}
  425. entMap := map[int]*UserInfo{}
  426. all := []*UserInfo{}
  427. 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
  428. 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)
  429. inner join entniche_user c on (`
  430. if len(testUserIds) > 0 {
  431. array := []string{}
  432. for _, v := range testUserIds {
  433. array = append(array, fmt.Sprint(v))
  434. }
  435. query += `c.id in (` + strings.Join(array, ",") + `) and `
  436. }
  437. query += `b.ent_user_id=c.id)
  438. inner join entniche_info d on (d.id=a.ent_id)`
  439. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  440. for _, v := range *l {
  441. phone, _ := v["phone"].(string)
  442. if phone == "" {
  443. continue
  444. }
  445. u := &UserInfo{
  446. Entniche: &Entniche{
  447. EntId: util.IntAll(v["ent_id"]),
  448. EntName: util.ObjToString(v["ent_name"]),
  449. UserId: util.IntAll(v["ent_user_id"]),
  450. ProductType: util.ObjToString(v["product_type"]),
  451. PowerSource: util.IntAll(v["power_source"]),
  452. IsNew: util.IntAll(v["isNew"]),
  453. },
  454. Phone: phone,
  455. }
  456. if strings.Contains(u.Entniche.ProductType, Ent_EmpowerMember) {
  457. u.MemberStatus = 1
  458. } else if strings.Contains(u.Entniche.ProductType, Ent_EmpowerVip) {
  459. u.VipStatus = 1
  460. } else {
  461. continue
  462. }
  463. phoneMap[phone] = u
  464. userMap[u.Entniche.UserId] = u
  465. all = append(all, u)
  466. entMap[u.Entniche.EntId] = u
  467. logger.Info("加载企业授权用户", u.Entniche.EntName, u.Entniche.EntId, u.Entniche.UserId, u.Entniche.ProductType, u.Phone)
  468. }
  469. return true
  470. }, query, NowFormat(Date_Full_Layout))
  471. logger.Info("企业授权用户加载结束。。。", len(phoneMap), len(userMap), len(entMap), len(all))
  472. return phoneMap, userMap, entMap, all
  473. }
  474. //加载商机管理用户
  475. func LoadEntnicheUsers(m *Mysql) map[string]bool {
  476. logger.Info("开始加载新版商机管理用户。。。")
  477. r := map[string]bool{}
  478. m.SelectByBath(200, func(l *[]map[string]interface{}) bool {
  479. for _, v := range *l {
  480. phone := util.ObjToString(v["phone"])
  481. if phone != "" {
  482. r[phone] = true
  483. logger.Info("加载商机管理用户", phone)
  484. }
  485. }
  486. return true
  487. }, `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)`)
  488. logger.Info("商机管理用户加载结束。。。", len(r))
  489. return r
  490. }
  491. //
  492. func GetWxTplMsg(subSet *SubSet) (string, string) {
  493. keyword := strings.Join(subSet.Keys, " ")
  494. if len([]rune(keyword)) > 10 {
  495. keyword = string([]rune(keyword)[:10]) + "..."
  496. }
  497. area := strings.Join(subSet.Areas, " ")
  498. if len([]rune(area)) > 10 {
  499. area = string([]rune(area)[:10]) + "..."
  500. }
  501. return keyword, area
  502. }
  503. //
  504. func GetPhone(u map[string]interface{}) string {
  505. phone := util.ObjToString(u["s_phone"])
  506. if phone == "" {
  507. phone = util.ObjToString(u["s_m_phone"])
  508. }
  509. return phone
  510. }
  511. //
  512. func GetAllByEntUserId(mgo *MongodbSim, msl *Mysql, entUserId int) *map[string]interface{} {
  513. entUsers := msl.SelectBySql(`select phone from entniche_user where id=?`, entUserId)
  514. if entUsers == nil || len(*entUsers) == 0 {
  515. logger.Info("entniche_user表中没有找到该企业用户", entUserId)
  516. return nil
  517. }
  518. phone, _ := (*entUsers)[0]["phone"].(string)
  519. if phone == "" {
  520. return nil
  521. }
  522. return getEntPushSet(mgo, msl, entUserId, phone)
  523. }
  524. //
  525. func GetAllByEntPositionId(mgo *MongodbSim, tidb, msl *Mysql, positionId int) *map[string]interface{} {
  526. 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)
  527. if position == nil || len(*position) == 0 {
  528. logger.Info("无效的职位id", position)
  529. return nil
  530. }
  531. entId := util.Int64All((*position)[0]["ent_id"])
  532. if entId == 0 {
  533. logger.Info("该职位id没有找到对应的企业id", position)
  534. return nil
  535. }
  536. phone, _ := (*position)[0]["phone"].(string)
  537. if phone == "" {
  538. logger.Info("该职位id没有找到对应的手机号", position)
  539. return nil
  540. }
  541. entUsers := msl.SelectBySql(`select id from entniche_user where phone=? and ent_id=?`, phone, entId)
  542. if entUsers == nil || len(*entUsers) == 0 {
  543. logger.Info("entniche_user表中没有找到该企业用户", phone, entId)
  544. return nil
  545. }
  546. return getEntPushSet(mgo, msl, util.IntAll((*entUsers)[0]["id"]), phone)
  547. }
  548. //
  549. func getEntPushSet(mgo *MongodbSim, msl *Mysql, entUserId int, phone string) *map[string]interface{} {
  550. users, ok := mgo.Find(Mgo_User, map[string]interface{}{
  551. "$or": []map[string]interface{}{
  552. map[string]interface{}{
  553. "s_phone": phone,
  554. },
  555. map[string]interface{}{
  556. "s_m_phone": phone,
  557. },
  558. },
  559. }, `{"s_phone":-1}`, map[string]interface{}{
  560. "_id": 1,
  561. "s_m_openid": 1,
  562. "a_m_openid": 1,
  563. "s_phone": 1,
  564. "s_m_phone": 1,
  565. "i_ispush": 1,
  566. "s_jpushid": 1,
  567. "s_opushid": 1,
  568. "s_appponetype": 1,
  569. "base_user_id": 1,
  570. }, false, -1, -1)
  571. if !ok || users == nil || len(*users) == 0 {
  572. logger.Info("user表中没有找到该企业用户", entUserId, phone)
  573. return nil
  574. }
  575. user := map[string]interface{}{}
  576. for _, v := range *users {
  577. if user["_id"] == nil {
  578. user["_id"] = v["_id"]
  579. user["base_user_id"] = v["base_user_id"]
  580. }
  581. if user["s_phone"] == nil && util.ObjToString(v["s_phone"]) != "" {
  582. user["s_phone"] = v["s_phone"]
  583. }
  584. if user["s_m_phone"] == nil && util.ObjToString(v["s_m_phone"]) != "" {
  585. user["s_m_phone"] = v["s_m_phone"]
  586. }
  587. if user["a_m_openid"] == nil && util.ObjToString(v["a_m_openid"]) != "" {
  588. user["a_m_openid"] = v["a_m_openid"]
  589. }
  590. s_m_openid := util.ObjToString(v["s_m_openid"])
  591. i_ispush := util.IntAll(v["i_ispush"])
  592. s_jpushid := util.ObjToString(v["s_jpushid"])
  593. s_opushid := util.ObjToString(v["s_opushid"])
  594. s_appponetype := util.ObjToString(v["s_appponetype"])
  595. if user["s_m_openid"] == nil && user["i_ispush"] == nil && s_m_openid != "" && i_ispush == 1 {
  596. user["s_m_openid"] = s_m_openid
  597. user["i_ispush"] = i_ispush
  598. }
  599. if user["s_jpushid"] == nil && user["s_opushid"] == nil && user["s_appponetype"] == nil && s_appponetype != "" && (s_jpushid != "" || s_opushid != "") {
  600. user["s_jpushid"] = s_jpushid
  601. user["s_opushid"] = s_opushid
  602. user["s_appponetype"] = s_appponetype
  603. }
  604. }
  605. entniche_user, ok := mgo.FindOneByField(Mgo_Ent_User, map[string]interface{}{
  606. "i_userid": entUserId,
  607. }, `{"_id":0,"i_member_status":1,"i_vip_status":1,"o_pushset":1}`)
  608. if ok && entniche_user != nil && len(*entniche_user) > 0 {
  609. for k, v := range *entniche_user {
  610. user[k] = v
  611. }
  612. }
  613. entniche_rule, ok := mgo.Find("entniche_rule", map[string]interface{}{
  614. "i_userid": entUserId,
  615. }, nil, `{"_id":0,"o_entniche":1,"i_type":1}`, false, -1, -1)
  616. if ok && entniche_rule != nil {
  617. for _, v := range *entniche_rule {
  618. i_type := util.IntAll(v["i_type"])
  619. if i_type == 0 {
  620. user["o_entniche"] = v["o_entniche"]
  621. } else if i_type == 1 {
  622. user["o_vipjy"] = v["o_entniche"]
  623. user["o_member_jy"] = v["o_entniche"]
  624. } else if i_type == 2 {
  625. user["o_jy"] = v["o_entniche"]
  626. }
  627. }
  628. }
  629. return &user
  630. }
  631. //
  632. func GetEntUserSubset(mgo *MongodbSim, entUserId, tp int) map[string]interface{} {
  633. temp, ok := mgo.FindOneByField("entniche_rule", map[string]interface{}{
  634. "i_userid": entUserId,
  635. "i_type": tp,
  636. }, `{"o_entniche":1}`)
  637. if ok && temp != nil && len(*temp) > 0 {
  638. o_msgset, _ := (*temp)["o_entniche"].(map[string]interface{})
  639. return o_msgset
  640. }
  641. return nil
  642. }
  643. //根据企业员工id获取身份信息
  644. func IdentityByEntUserId(base, main *Mysql, entUserId int64) *IdentityInfo {
  645. ents := main.SelectBySql(`SELECT a.id,a.name,a.phone,a.ent_id,b.name as ent_name from entniche_user a inner join entniche_info b on (a.id=? and a.ent_id=b.id) limit 1`, entUserId)
  646. if ents == nil || len(*ents) == 0 {
  647. return nil
  648. }
  649. if entId := Int64All((*ents)[0]["ent_id"]); entId > 0 {
  650. list := base.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_user d
  651. inner join base_position a on (d.phone=? and a.ent_id=? and a.type=1 and d.id=a.user_id)
  652. inner join base_account c on (c.ent_id=? and c.type=1 and a.account_id=c.id)
  653. inner join 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)
  654. if list != nil && len(*list) == 1 {
  655. identity := &IdentityInfo{
  656. PersonId: Int64All((*list)[0]["person_id"]),
  657. UserName: ObjToString((*list)[0]["person_name"]),
  658. AccountId: Int64All((*list)[0]["account_id"]),
  659. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  660. PositionId: Int64All((*list)[0]["position_id"]),
  661. PositionType: Int64All((*list)[0]["position_type"]),
  662. UserId: Int64All((*list)[0]["user_id"]),
  663. EntId: entId,
  664. EntUserId: Int64All((*ents)[0]["id"]),
  665. EntUserName: ObjToString((*ents)[0]["name"]),
  666. Name: ObjToString((*ents)[0]["ent_name"]),
  667. }
  668. return identity
  669. }
  670. }
  671. return nil
  672. }
  673. //格式化小时
  674. func HourFormat(hour int) string {
  675. if hour < 10 {
  676. return fmt.Sprintf("0%d:00", hour)
  677. } else {
  678. return fmt.Sprintf("%d:00", hour)
  679. }
  680. }
  681. //数组中的小时是否结束
  682. func TimesIsOver(times []string, hour int) (bool, time.Time, time.Time) {
  683. now := time.Now()
  684. if len(times) == 0 {
  685. return false, now, now
  686. }
  687. sort.Strings(times)
  688. lastHour := util.IntAll(strings.Split(times[len(times)-1], ":"))
  689. //跨天
  690. if lastHour == 23 && hour == 0 {
  691. start := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
  692. end := time.Date(now.Year(), now.Month(), now.Day()-1, 23, 59, 59, 0, time.Local)
  693. return true, start, end
  694. } else {
  695. start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  696. return hour > lastHour, start, now
  697. }
  698. }
  699. //监听超时
  700. func MonitorTimeOut(pool chan bool, timeoutWarn string, f func()) bool {
  701. select {
  702. case <-time.After(1 * time.Minute):
  703. go func() {
  704. f()
  705. if timeoutWarn != "" {
  706. if _, err := http.Get(timeoutWarn); err != nil {
  707. logger.Error("发送告警邮件错误", err)
  708. }
  709. }
  710. }()
  711. return false
  712. case pool <- true:
  713. }
  714. return true
  715. }