common.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "app.yhyue.com/moapp/jybase/esv1"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "app.yhyue.com/moapp/jybase/mysql"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. "fmt"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. "jyBXBase/rpc/bxbase"
  12. IC "jyBXBase/rpc/init"
  13. "log"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","subtype","s_winner","buyertel","winnertel","buyerclass"],"from":0,"size":%d}`
  20. mongodb_fields = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "buyerclass":1,"budget":1,"bidamount":1,"winnertel":1,"s_winner":1,"buyertel":1}`
  21. querys = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","title","detail","area","city","publishtime","projectname","buyer","buyerclass","s_winner","bidamount","subtype","toptype","projectcode","buyertel","budget","bidopentime","agency","projectscope","winnerperson","winnertel"],"from":0,"size":%d}}`
  22. )
  23. //是否是付费用户 -bool: true:是 fasle:不是
  24. func Power(userid string) (bool, map[string]interface{}) {
  25. isVip, isMember, isEnt := false, false, false
  26. vipstatus := 0
  27. phone := ""
  28. var registedate int64
  29. data, ok := IC.Mgo.FindById("user", userid, `"i_member_status":1,"i_vip_status":1,"s_m_phone":1,"s_phone":1,"o_vipjy":1,"l_registedate":1`)
  30. if data != nil && len(*data) > 0 && ok {
  31. i_vip_status := common.IntAll((*data)["i_vip_status"])
  32. if i_vip_status > 1 {
  33. vipstatus = 1
  34. isVip = true
  35. }
  36. ovipjy, _ := (*data)["o_vipjy"].(map[string]interface{})
  37. if ovipjy["o_buyset"] != nil {
  38. o_buyset := ovipjy["o_buyset"].(map[string]interface{})
  39. if o_buyset["upgrade"] != nil && isVip {
  40. vipstatus = 2
  41. }
  42. }
  43. if i_member_status := common.IntAllDef((*data)["i_member_status"], 0); i_member_status > 0 {
  44. isMember = true
  45. }
  46. if s_phone, _ := (*data)["s_phone"].(string); s_phone != "" {
  47. phone = s_phone
  48. } else if s_m_phone, _ := (*data)["s_m_phone"].(string); s_m_phone != "" {
  49. phone = s_m_phone
  50. }
  51. if phone != "" {
  52. //已购买企业未过期
  53. if entInfo := *IC.MainMysql.SelectBySql(`SELECT status FROM entniche_info WHERE id IN (SELECT ent_id FROM entniche_user where phone = ? and power =1)`, phone); len(entInfo) > 0 {
  54. for _, v := range entInfo {
  55. if common.IntAll(v["status"]) == 1 {
  56. isEnt = true
  57. break
  58. }
  59. }
  60. }
  61. }
  62. registedate, _ = (*data)["l_registedate"].(int64)
  63. }
  64. return isVip || isEnt || isMember, map[string]interface{}{
  65. "vip": vipstatus,
  66. "member": isMember,
  67. "entniche": isEnt,
  68. "registedate": registedate,
  69. }
  70. }
  71. //招标信息是否被收藏
  72. func IsCollByBids(bids, userid string) []string {
  73. res := []string{}
  74. collBidMap := map[string]bool{}
  75. isCollkey := fmt.Sprintf("isColl_%s", userid)
  76. collStatus := redis.GetInt("other", isCollkey)
  77. if collStatus == 0 {
  78. if count := int(IC.MainMysql.CountBySql(fmt.Sprintf(`select count(1)
  79. from %s where userid = ?`, "bdcollection"), userid)); count > 100 {
  80. collStatus = 2
  81. } else if count == 0 {
  82. collStatus = 0
  83. } else {
  84. collStatus = 1
  85. }
  86. redis.Put("other", isCollkey, collStatus, 259200)
  87. }
  88. if collStatus == 1 { //100条内 取redis
  89. list := GetCollRedis(userid, collStatus)
  90. for _, v := range list {
  91. collBidMap[v] = true
  92. }
  93. } else if collStatus == 2 { //大于100条 取mysql
  94. if labArr := *IC.MainMysql.SelectBySql(fmt.Sprintf("select bid from %s where userid = ?", "bdcollection"), userid); len(labArr) > 0 {
  95. for _, v := range labArr {
  96. bid_id := common.ObjToString(v["bid"])
  97. collBidMap[bid_id] = true
  98. }
  99. }
  100. } else { //0条
  101. return res
  102. }
  103. if bids == "" {
  104. for k, _ := range collBidMap {
  105. res = append(res, k)
  106. }
  107. } else {
  108. for _, v := range strings.Split(bids, ",") {
  109. //招标信息解密
  110. bid := DecodeId(v)
  111. if collBidMap[bid] {
  112. // url.QueryEscape(v)
  113. res = append(res, v)
  114. }
  115. }
  116. }
  117. return res
  118. }
  119. /*
  120. isColl int: 收藏条数的状态 (是否超过100条 超过:2 没超过:1)
  121. return
  122. []string:收藏的id
  123. */
  124. func GetCollRedis(userid string, isColl int) []string {
  125. redisCollKey := fmt.Sprintf("coll_%s", userid) //列表
  126. redisArr := []string{}
  127. if isColl == 1 {
  128. redisArr, _ = redis.Get("other", redisCollKey).([]string)
  129. if len(redisArr) == 0 {
  130. data := IC.MainMysql.SelectBySql(fmt.Sprintf(`select bid from %s where userid ='%s'`, "bdcollection", userid))
  131. if data != nil && len(*data) > 0 {
  132. for _, v := range *data {
  133. redisArr = append(redisArr, common.ObjToString(v["bid"]))
  134. }
  135. redis.Put("other", redisCollKey, redisArr, 259200)
  136. }
  137. }
  138. }
  139. return redisArr
  140. }
  141. //招标信息 bid&userid 唯一
  142. type BidInfo struct {
  143. Bid string `json:"bid"` //招标信息id 加密后
  144. Buyerclass string `json:"buyerclass"` //采购单位类型
  145. Buyerinfo bool `json:"buyerinfo"` //是否有采购单位联系方式
  146. Winnerinfo bool `json:"winnerinfo"` //是否有中标企业联系方式
  147. Userid string `json:"userid"` //用户id
  148. Createdate string `json:"createdate"` //收藏时间
  149. Labelid string `json:"labelid"` //标签ids
  150. }
  151. func FormatColl(bidinfo []string) []BidInfo {
  152. es_ids := []string{}
  153. infos := map[string]interface{}{}
  154. for _, v := range bidinfo {
  155. es_ids = append(es_ids, DecodeId(v))
  156. }
  157. if len(es_ids) > 0 {
  158. list := elastic.Get("bidding", "bidding", fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
  159. if list != nil {
  160. for _, v := range *list {
  161. _id := common.ObjToString(v["_id"])
  162. //中标电话 需要查企业库 和三级页保持一致
  163. winnertel := common.ObjToString(v["winnertel"])
  164. if winnertel == "" && isbid(v["subtype"]) {
  165. v["winnertel"] = getwinnertel(v["s_winner"])
  166. }
  167. infos[_id] = v
  168. }
  169. }
  170. }
  171. var bids []BidInfo
  172. if len(infos) > 0 {
  173. for _, v := range bidinfo {
  174. var bid BidInfo
  175. id := DecodeId(v)
  176. bid.Bid = v
  177. if infos[id] != nil {
  178. infoMap, _ := (infos[id]).(map[string]interface{})
  179. if common.ObjToString(infoMap["winnertel"]) != "" {
  180. bid.Winnerinfo = true
  181. }
  182. if common.ObjToString(infoMap["buyertel"]) != "" {
  183. bid.Buyerinfo = true
  184. }
  185. if common.ObjToString(infoMap["buyerclass"]) != "" {
  186. bid.Buyerclass = common.ObjToString(infoMap["buyerclass"])
  187. }
  188. }
  189. bids = append(bids, bid)
  190. }
  191. }
  192. return bids
  193. }
  194. func isbid(typ interface{}) bool {
  195. if typ != nil {
  196. subtype := common.ObjToString(typ)
  197. if subtype == "中标" || subtype == "合同" || subtype == "成交" {
  198. return true
  199. }
  200. }
  201. return false
  202. }
  203. func getwinnertel(company interface{}) string {
  204. if company != nil {
  205. data, _ := IC.MgoEnt.FindOneByField("winner_enterprise", map[string]interface{}{
  206. "company_name": common.ObjToString(company),
  207. }, map[string]interface{}{"company_phone": 1})
  208. if (*data)["company_phone"] != nil {
  209. return common.ObjToString((*data)["company_phone"])
  210. }
  211. }
  212. return ""
  213. }
  214. //修改redis中的收藏条数;
  215. /* typ:"d"删除 typ:"a"添加
  216. list:需要添加或删除的bid_id
  217. */
  218. func UpdateCollListRedis(typ, userid string, list []string) bool {
  219. bl := false
  220. if len(list) == 0 {
  221. return true
  222. }
  223. Blist := []string{}
  224. m := map[string]bool{}
  225. redisCollKey := fmt.Sprintf("coll_%s", userid)
  226. collTime := 3600 * 2
  227. if 7200 > 0 {
  228. collTime = 7200
  229. }
  230. redisIsCollKey := fmt.Sprintf("isColl_%s", userid) //是否超过100条 超过:2 没超过:1
  231. isCollTime := 3600 * 24 * 3
  232. if 259200 > 0 {
  233. isCollTime = 259200
  234. }
  235. collStatus := redis.GetInt("other", redisIsCollKey)
  236. redisArr := GetCollRedis(userid, collStatus)
  237. if len(redisArr) > 0 {
  238. for _, v := range redisArr {
  239. m[v] = true
  240. }
  241. }
  242. if typ == "a" {
  243. for _, v := range list {
  244. if m[v] {
  245. continue //去重
  246. }
  247. Blist = append(Blist, v)
  248. }
  249. } else if typ == "d" {
  250. for _, v := range list {
  251. delete(m, v)
  252. }
  253. if len(m) > 0 {
  254. for k, _ := range m {
  255. Blist = append(Blist, k)
  256. }
  257. }
  258. }
  259. arr := append(redisArr, Blist...)
  260. status := 1
  261. if len(arr) > 100 {
  262. status = 2
  263. arr = []string{}
  264. } else if len(arr) == 0 {
  265. status = 0
  266. }
  267. bl = redis.Put("other", redisCollKey, arr, collTime) &&
  268. redis.Put("other", redisIsCollKey, status, isCollTime)
  269. return bl
  270. }
  271. var PushMapping = &pushMapping{}
  272. type pushMapping struct {
  273. Area map[string]int
  274. City map[string]int
  275. Toptype map[string]int
  276. Subtype map[string]int
  277. Buyerclass map[string]int
  278. Subscopeclass map[string]int
  279. }
  280. func (p *pushMapping) Init(MainMysql *mysql.Mysql) {
  281. infotype := MainMysql.SelectBySql("select id,type,name from infotype")
  282. p.Toptype = map[string]int{}
  283. p.Subtype = map[string]int{}
  284. p.Buyerclass = map[string]int{}
  285. p.Subscopeclass = map[string]int{}
  286. for _, v := range *infotype {
  287. id := common.IntAll(v["id"])
  288. tp := common.IntAll(v["type"])
  289. name := common.ObjToString(v["name"])
  290. if tp == 1 {
  291. p.Toptype[name] = id
  292. } else if tp == 2 {
  293. p.Subtype[name] = id
  294. } else if tp == 3 {
  295. p.Buyerclass[name] = id
  296. } else if tp == 4 {
  297. p.Subscopeclass[name] = id
  298. }
  299. }
  300. if len(p.Toptype) == 0 {
  301. log.Fatalln("PushMapping Toptype Init Error")
  302. }
  303. if len(p.Subtype) == 0 {
  304. log.Fatalln("PushMapping Subtype Init Error")
  305. }
  306. if len(p.Buyerclass) == 0 {
  307. log.Fatalln("PushMapping Buyerclass Init Error")
  308. }
  309. if len(p.Subscopeclass) == 0 {
  310. log.Fatalln("PushMapping Subscopeclass Init Error")
  311. }
  312. //
  313. p.Area = map[string]int{}
  314. p.City = map[string]int{}
  315. province := MainMysql.SelectBySql("select id,level,name from province")
  316. for _, v := range *province {
  317. id := common.IntAll(v["id"])
  318. level := common.IntAll(v["level"])
  319. name := common.ObjToString(v["name"])
  320. if level == 1 {
  321. p.Area[name] = id
  322. } else if level == 2 {
  323. p.City[name] = id
  324. }
  325. }
  326. if len(p.Area) == 0 {
  327. log.Fatalln("PushMapping Area Init Error")
  328. }
  329. if len(p.City) == 0 {
  330. log.Fatalln("PushMapping City Init Error")
  331. }
  332. }
  333. type InfoList struct {
  334. Id string `json:"_id"`
  335. Title string `json:"title"`
  336. Area string `json:"area"`
  337. Buyerclass string `json:"buyerclass"`
  338. Type string `json:"type"`
  339. S_subscopeclass string `json:"s_subscopeclass"`
  340. Publishtime int64 `json:"publishtime"`
  341. Budget interface{} `json:"budget"`
  342. Bidamount interface{} `json:"bidamount"`
  343. Buyer string `json:"buyer"`
  344. S_winner string `json:"s_winner"`
  345. Bidopentime int64 `json:"bidopentime"`
  346. }
  347. //根据id取内容
  348. func GetInfoById(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, idlist []map[string]interface{}) []*InfoList {
  349. array := make([]*InfoList, len(idlist))
  350. if len(idlist) == 0 {
  351. return array
  352. }
  353. m := map[string]bool{}
  354. ids := []string{}
  355. for _, v := range idlist {
  356. if m[common.ObjToString(v["bid"])] {
  357. continue
  358. }
  359. m[common.ObjToString(v["bid"])] = true
  360. ids = append(ids, common.ObjToString(v["bid"]))
  361. }
  362. infos := map[string]map[string]interface{}{}
  363. //redis
  364. es_ids := []string{}
  365. for _, v := range ids {
  366. info_i := redis.Get("pushcache_1", fmt.Sprintf("info_%s", v))
  367. if info_i != nil {
  368. info_m, _ := info_i.(map[string]interface{})
  369. info_m["_id"] = v
  370. infos[v] = info_m
  371. } else {
  372. es_ids = append(es_ids, v)
  373. }
  374. }
  375. // log.Println(es_ids)
  376. //elasticsearch
  377. if len(es_ids) > 0 {
  378. list := elastic.Get("bidding", "bidding", fmt.Sprintf(querys, strings.Join(es_ids, `","`), len(es_ids)))
  379. if list != nil {
  380. for _, v := range *list {
  381. _id := common.ObjToString(v["_id"])
  382. infos[_id] = v
  383. }
  384. }
  385. }
  386. //mongodb bidding
  387. mgo_ids := []primitive.ObjectID{}
  388. for _, v := range es_ids {
  389. if infos[v] == nil {
  390. _id, _ := primitive.ObjectIDFromHex(v)
  391. mgo_ids = append(mgo_ids, _id)
  392. }
  393. }
  394. if len(mgo_ids) > 0 {
  395. list, ok := Mgo_bidding.Find(bidding, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
  396. if ok && *list != nil {
  397. for _, v := range *list {
  398. _id := mongodb.BsonIdToSId(v["_id"])
  399. v["_id"] = _id
  400. infos[_id] = v
  401. }
  402. }
  403. }
  404. //mongodb bidding_back
  405. mgo_back_ids := []primitive.ObjectID{}
  406. for _, v := range mgo_ids {
  407. if infos[mongodb.BsonIdToSId(v)] == nil {
  408. mgo_back_ids = append(mgo_back_ids, v)
  409. }
  410. }
  411. if len(mgo_back_ids) > 0 {
  412. list, ok := Mgo_bidding.Find(bidding_back, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
  413. if ok && *list != nil {
  414. for _, v := range *list {
  415. _id := mongodb.BsonIdToSId(v["_id"])
  416. v["_id"] = _id
  417. infos[_id] = v
  418. }
  419. }
  420. }
  421. //
  422. for k, v := range idlist {
  423. info := infos[common.ObjToString(v["bid"])]
  424. if info == nil {
  425. info = map[string]interface{}{}
  426. }
  427. array[k] = InfoFormat(common.ObjToString(v["bid"]), &info)
  428. }
  429. return array
  430. }
  431. //列表单条信息格式化
  432. func InfoFormat(p string, info *map[string]interface{}) *InfoList {
  433. area := common.ObjToString((*info)["area"])
  434. if area == "A" {
  435. area = "全国"
  436. }
  437. industry := common.ObjToString((*info)["s_subscopeclass"])
  438. scs := strings.Split(industry, ",")
  439. if len(scs) > 0 {
  440. industry = scs[0]
  441. if industry != "" {
  442. iss := strings.Split(industry, "_")
  443. if len(iss) > 0 {
  444. industry = iss[0]
  445. }
  446. }
  447. }
  448. infotype := common.ObjToString((*info)["subtype"])
  449. if infotype == "" {
  450. infotype = common.ObjToString((*info)["toptype"])
  451. }
  452. if infotype == "" {
  453. infotype = common.ObjToString((*info)["type"])
  454. if infotype == "tender" {
  455. infotype = "招标"
  456. } else if infotype == "bid" {
  457. infotype = "中标"
  458. }
  459. }
  460. _id := p
  461. if _id == "" {
  462. _id = common.ObjToString((*info)["_id"])
  463. }
  464. return &InfoList{
  465. Id: encrypt.EncodeArticleId2ByCheck(_id),
  466. Title: common.ObjToString((*info)["title"]),
  467. Area: area,
  468. Buyerclass: common.ObjToString((*info)["buyerclass"]),
  469. Type: infotype,
  470. S_subscopeclass: industry,
  471. Publishtime: common.Int64All((*info)["publishtime"]),
  472. Budget: (*info)["budget"],
  473. Bidamount: (*info)["bidamount"],
  474. Buyer: common.ObjToString((*info)["buyer"]),
  475. S_winner: common.ObjToString((*info)["s_winner"]),
  476. Bidopentime: common.Int64All((*info)["bidopentime"]),
  477. }
  478. }
  479. //搜藏列表
  480. func CollListSql(c *bxbase.ListReq, isPay bool, userid string) string {
  481. sql := fmt.Sprintf(`select bid from %s where userid ='%s'`, "bdcollection", userid)
  482. limit := 10
  483. //个人标签
  484. if c.Label != "" {
  485. i := 0
  486. sql += ` and `
  487. if labelArr := strings.Split(c.Label, ","); len(labelArr) > 0 {
  488. sql += `(`
  489. for _, v := range labelArr {
  490. i++
  491. labid := encrypt.SE.DecodeString(v)
  492. if i == len(labelArr) {
  493. sql += fmt.Sprintf(`FIND_IN_SET(%s,labelid)`, labid)
  494. } else {
  495. sql += fmt.Sprintf(`FIND_IN_SET(%s,labelid) or `, labid)
  496. }
  497. }
  498. sql += `)`
  499. }
  500. }
  501. now := time.Now()
  502. start, end := "", ""
  503. //收藏时间
  504. if c.SelectTime == "lately-7" { //最近7天
  505. start = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local).Format(Date_Full_Layout))
  506. } else if c.SelectTime == "lately-30" { //最近30天
  507. start = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local).Format(Date_Full_Layout))
  508. } else if c.SelectTime == "thisyear" { //去年
  509. start = fmt.Sprint(time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, time.Local).Format(Date_Full_Layout))
  510. end = fmt.Sprint(time.Date(now.Year()-1, 12, 31, 23, 59, 59, 0, time.Local).Format(Date_Full_Layout))
  511. } else if len(strings.Split(c.SelectTime, "_")) == 2 {
  512. if c.SelectTime != "0_0" {
  513. starttime := strings.Split(c.SelectTime, "_")[0]
  514. startstamp, _ := strconv.Atoi(starttime)
  515. start = time.Unix(int64(startstamp), 0).Format(Date_Full_Layout)
  516. endtime := strings.Split(c.SelectTime, "_")[1]
  517. et, _ := strconv.ParseInt(endtime, 0, 64)
  518. etTime := time.Unix(et, 0)
  519. end = fmt.Sprint(time.Date(etTime.Year(), etTime.Month(), etTime.Day()+1, 0, 0, 0, 0, time.Local).Format(Date_Full_Layout))
  520. }
  521. }
  522. if start != "" && end != "" {
  523. sql += ` and createdate >= '` + start + `' and createdate < '` + end + `'`
  524. } else if start != "" && end == "" {
  525. sql += ` and createdate >= '` + start + `'`
  526. } else if start == "" && end != "" {
  527. sql += ` and createdate < '` + end + `'`
  528. }
  529. if isPay {
  530. //采购单位 用,分隔开
  531. if c.Buyerclass != "" {
  532. i := 0
  533. sql += ` and buyerclass in (`
  534. if buyClassArr := strings.Split(c.Buyerclass, ","); len(buyClassArr) > 0 {
  535. for _, v := range buyClassArr {
  536. i++
  537. buyerclassid := fmt.Sprint(PushMapping.Buyerclass[v])
  538. if i == len(buyClassArr) {
  539. sql += buyerclassid + `)`
  540. } else {
  541. sql += buyerclassid + ","
  542. }
  543. }
  544. }
  545. }
  546. //是否存在采购单位电话
  547. if c.BuyerPhone == 1 {
  548. sql += ` and buyerinfo = 1`
  549. } else if c.BuyerPhone == -1 {
  550. sql += ` and buyerinfo = 0`
  551. }
  552. //是否存在中标单位电话
  553. if c.WinnerPhone == 1 {
  554. sql += ` and winnerinfo = 1`
  555. } else if c.WinnerPhone == -1 {
  556. sql += ` and winnerinfo = 0`
  557. }
  558. limit = 5000
  559. }
  560. sql += fmt.Sprintf(` order by createdate desc limit %v`, limit)
  561. return sql
  562. }