common.go 18 KB

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