bidding.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package main
  2. import (
  3. "data_tidb/config"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  9. "reflect"
  10. "regexp"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. var (
  18. regLetter = regexp.MustCompile("[a-z]*")
  19. )
  20. func doBiddingTask(gtid, lteid string, mapInfo map[string]interface{}) {
  21. sess := MongoB.GetMgoConn()
  22. defer MongoB.DestoryMongoConn(sess)
  23. ch := make(chan bool, 10)
  24. wg := &sync.WaitGroup{}
  25. stype := util.ObjToString(mapInfo["stype"])
  26. q := map[string]interface{}{"_id": map[string]interface{}{"$gt": mongodb.StringTOBsonId(gtid),
  27. "$lte": mongodb.StringTOBsonId(lteid)}}
  28. query := sess.DB(config.Conf.DB.MongoB.Dbname).C("bidding").Find(q).Sort("_id").Iter()
  29. count := 0
  30. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  31. if count%1000 == 0 {
  32. log.Info(fmt.Sprintf("current --- %d", count))
  33. }
  34. ch <- true
  35. wg.Add(1)
  36. go func(tmp map[string]interface{}) {
  37. defer func() {
  38. <-ch
  39. wg.Done()
  40. }()
  41. if util.IntAll(tmp["dataprocess"]) != 8 {
  42. return
  43. }
  44. if stype == "bidding_history" && tmp["history_updatetime"] == nil {
  45. return
  46. }
  47. taskBase(tmp)
  48. taskTags(tmp)
  49. taskExpand(tmp)
  50. taskAtts(tmp)
  51. taskInfoformat(tmp)
  52. taskIntent(tmp)
  53. taskWinner(tmp)
  54. taskPackage(tmp)
  55. taskPur(tmp)
  56. }(tmp)
  57. tmp = make(map[string]interface{})
  58. }
  59. wg.Wait()
  60. log.Info(fmt.Sprintf("over --- %d", count))
  61. }
  62. func taskB() {
  63. sess := MongoB.GetMgoConn()
  64. defer MongoB.DestoryMongoConn(sess)
  65. ch := make(chan bool, 10)
  66. wg := &sync.WaitGroup{}
  67. q := map[string]interface{}{"_id": map[string]interface{}{"$gt": mongodb.StringTOBsonId("132d42d667a6b0a2861eef92")}}
  68. query := sess.DB(config.Conf.DB.MongoB.Dbname).C("bidding").Find(q).Sort("_id").Iter()
  69. count := 0
  70. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  71. if count%10000 == 0 {
  72. log.Info(fmt.Sprintf("current --- %d", count))
  73. }
  74. ch <- true
  75. wg.Add(1)
  76. go func(tmp map[string]interface{}) {
  77. defer func() {
  78. <-ch
  79. wg.Done()
  80. }()
  81. if util.IntAll(tmp["extracttype"]) != -1 {
  82. //taskBase(tmp) //基础标讯数据
  83. //taskTags(tmp) //标签信息
  84. //taskExpand(tmp) //扩展数据
  85. taskDetail(tmp) //正文信息
  86. //taskAtts(tmp) //附件信息
  87. //taskInfoformat(tmp) //拟建
  88. //taskIntent(tmp) //采购意向
  89. //taskWinner(tmp)
  90. //taskPackage(tmp)
  91. //taskPur(tmp)
  92. }
  93. }(tmp)
  94. tmp = make(map[string]interface{})
  95. }
  96. wg.Wait()
  97. log.Info(fmt.Sprintf("is over --- %d", count))
  98. }
  99. // @Description 基本信息
  100. // @Author J 2022/9/22 11:12
  101. func taskBase(tmp map[string]interface{}) {
  102. saveM := make(map[string]interface{})
  103. var errf []string // 异常字段
  104. for _, nf := range BaseField {
  105. f := nf[2:]
  106. if f == "info_id" {
  107. saveM[nf] = mongodb.BsonIdToSId(tmp["_id"])
  108. } else if f == "area_code" {
  109. if tmp["area"] != nil {
  110. saveM[nf] = AreaCode[util.ObjToString(tmp["area"])]
  111. }
  112. } else if f == "city_code" {
  113. if tmp["area"] != nil && tmp["city"] != nil {
  114. c := util.ObjToString(tmp["area"]) + "," + util.ObjToString(tmp["city"])
  115. saveM[nf] = AreaCode[c]
  116. }
  117. } else if f == "district_code" {
  118. if tmp["area"] != nil && tmp["city"] != nil && tmp["district"] != nil {
  119. c := util.ObjToString(tmp["area"]) + "," + util.ObjToString(tmp["city"]) + "," + util.ObjToString(tmp["district"])
  120. saveM[nf] = AreaCode[c]
  121. }
  122. } else if f == "toptype_code" {
  123. if obj := util.ObjToString(tmp["toptype"]); obj != "" {
  124. saveM[nf] = TopTypeCode[obj]
  125. }
  126. } else if f == "subtype_code" {
  127. if obj := util.ObjToString(tmp["subtype"]); obj != "" {
  128. saveM[nf] = SubTypeCode[obj]
  129. }
  130. } else if f == "buyerclass_code" {
  131. if obj := util.ObjToString(tmp["buyerclass"]); obj != "" {
  132. saveM[nf] = BuyerCode[obj]
  133. }
  134. } else if f == "createtime" || f == "updatetime" {
  135. saveM[nf] = time.Now().Format(util.Date_Full_Layout)
  136. } else if f == "comeintime" || f == "publishtime" || f == "bidopentime" || f == "bidendtime" {
  137. if tmp[f] != nil && util.IntAll(tmp[f]) > 0 {
  138. t := util.Int64All(tmp[f])
  139. saveM[nf] = util.FormatDateByInt64(&t, util.Date_Full_Layout)
  140. }
  141. } else if f == "multipackage" || f == "isValidFile" {
  142. if tmp[f] == nil {
  143. saveM[nf] = 0
  144. } else {
  145. saveM[nf] = tmp[f]
  146. }
  147. } else if f == "buyer_id" {
  148. if b := util.ObjToString(tmp["buyer"]); b != "" {
  149. if code := getNameId(b); code != "" {
  150. saveM[nf] = code
  151. }
  152. }
  153. } else if f == "agency_id" {
  154. if b := util.ObjToString(tmp["agency"]); b != "" {
  155. if code := getNameId(b); code != "" {
  156. saveM[nf] = code
  157. }
  158. }
  159. } else {
  160. if tmp[f] != nil {
  161. if BaseVMap[f] != nil {
  162. var b bool
  163. saveM[nf], b = verifyF(f, tmp[f], BaseVMap[f])
  164. if b { // 保存异常字段数据
  165. errf = append(errf, f)
  166. }
  167. } else {
  168. saveM[nf] = tmp[f]
  169. }
  170. }
  171. }
  172. }
  173. saveBasePool <- saveM
  174. if len(errf) > 0 {
  175. saveErrPool <- map[string]interface{}{"infoid": mongodb.BsonIdToSId(tmp["_id"]), "f": strings.Join(errf, ",")}
  176. }
  177. }
  178. func getNameId(name string) string {
  179. info := MysqlTool.FindOne("dws_f_ent_baseinfo", map[string]interface{}{"name": name}, "name_id", "")
  180. if info != nil && (*info)["name_id"] != nil {
  181. return util.ObjToString((*info)["name_id"])
  182. } else {
  183. return ""
  184. }
  185. }
  186. // @Description 扩展信息
  187. // @Author J 2022/9/22 11:13
  188. func taskExpand(tmp map[string]interface{}) {
  189. saveM := make(map[string]interface{})
  190. var errf []string // 异常字段
  191. for _, nf := range ExpandField {
  192. f := nf[2:]
  193. if f == "info_id" {
  194. saveM[nf] = mongodb.BsonIdToSId(tmp["_id"])
  195. } else if f == "project_startdate" || f == "project_completedate" || f == "signstarttime" || f == "bidendtime" || f == "bidstarttime" || f == "docstarttime" ||
  196. f == "docendtime" || f == "signaturedate" || f == "signendtime" {
  197. if tmp[f] != nil && util.IntAll(tmp[f]) > 0 {
  198. t := util.Int64All(tmp[f])
  199. saveM[nf] = util.FormatDateByInt64(&t, util.Date_Full_Layout)
  200. }
  201. } else if f == "createtime" || f == "updatetime" {
  202. saveM[nf] = time.Now().Format(util.Date_Full_Layout)
  203. } else if f == "bidway" {
  204. if util.ObjToString(tmp[f]) == "电子投标" {
  205. saveM[nf] = 1
  206. } else if util.ObjToString(tmp[f]) == "纸质投标" {
  207. saveM[nf] = 0
  208. }
  209. } else if f == "review_experts" { //评标专家
  210. if tmp[f] != nil {
  211. if reflect.TypeOf(tmp[f]).String() == "string" {
  212. saveM[nf] = tmp[f]
  213. } else if reflect.TypeOf(tmp[f]).String() == "[]interface {}" {
  214. if arr, ok := tmp[f].([]interface{}); ok {
  215. saveM[nf] = strings.Join(util.ObjArrToStringArr(arr), ",")
  216. }
  217. }
  218. }
  219. } else if f == "bid_guarantee" || f == "contract_guarantee" {
  220. if tmp[f] != nil {
  221. if tmp[f].(bool) {
  222. saveM[nf] = 1
  223. } else {
  224. saveM[nf] = 0
  225. }
  226. }
  227. } else if f == "agencyfee" {
  228. if tmp[f] != nil {
  229. if reflect.TypeOf(tmp[f]).String() == "string" {
  230. v2, err := strconv.ParseFloat(strings.ReplaceAll(util.ObjToString(tmp[f]), "%", ""), 64)
  231. if err != nil {
  232. v, _ := decimal.NewFromFloat(v2).Div(decimal.NewFromFloat(float64(100))).Float64()
  233. saveM[nf] = v
  234. }
  235. } else {
  236. saveM[nf], _ = util.FormatFloat(util.Float64All(tmp[f]), 4)
  237. }
  238. }
  239. } else if f == "project_duration" {
  240. if tmp[f] != nil {
  241. saveM[nf] = util.IntAll(tmp[f])
  242. }
  243. } else {
  244. if tmp[f] != nil {
  245. if ExpandVMap[f] != nil {
  246. var b bool
  247. saveM[nf], b = verifyF(f, tmp[f], ExpandVMap[f])
  248. if b { // 保存异常字段数据
  249. errf = append(errf, f)
  250. }
  251. } else {
  252. saveM[nf] = tmp[f]
  253. }
  254. }
  255. }
  256. }
  257. saveExpandPool <- saveM
  258. if len(errf) > 0 {
  259. saveErrPool <- map[string]interface{}{"infoid": mongodb.BsonIdToSId(tmp["_id"]), "f": strings.Join(errf, ",")}
  260. }
  261. }
  262. func taskDetail(tmp map[string]interface{}) {
  263. }
  264. // @Description 标签记录
  265. // @Author J 2022/9/22 11:13
  266. func taskTags(tmp map[string]interface{}) {
  267. id := mongodb.BsonIdToSId(tmp["_id"])
  268. if topArr, ok := tmp["topscopeclass"].([]interface{}); ok {
  269. for _, i2 := range topArr {
  270. tclass := regLetter.ReplaceAllString(util.ObjToString(i2), "") // 去除字母
  271. code := TopScopeCode[tclass]
  272. saveTagPool <- map[string]interface{}{"infoid": id, "labelcode": "1", "labelvalues": code, "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  273. //MysqlTool.Insert("bid_tags", map[string]interface{}{"infoid": id, "labelcode": "1", "labelvalues": code, "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)})
  274. }
  275. }
  276. if subArr, ok := tmp["subscopeclass"].([]interface{}); ok {
  277. for _, i2 := range subArr {
  278. sc := strings.Split(util.ObjToString(i2), "_")
  279. if len(sc) > 1 {
  280. code := SubScopeCode[sc[1]]
  281. saveTagPool <- map[string]interface{}{"infoid": id, "labelcode": "2", "labelvalues": code, "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  282. }
  283. //MysqlTool.Insert("bid_tags", map[string]interface{}{"infoid": id, "labelcode": "2", "labelvalues": code, "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)})
  284. }
  285. }
  286. if tArr, ok := tmp["certificate_class"].([]interface{}); ok {
  287. for _, i2 := range tArr {
  288. if util.ObjToString(i2) == "ISO" {
  289. saveTagPool <- map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "01", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  290. //MysqlTool.Insert("bid_tags", map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "01", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)})
  291. } else if util.ObjToString(i2) == "AAA" {
  292. saveTagPool <- map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "02", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  293. //MysqlTool.Insert("bid_tags", map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "02", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)})
  294. } else if util.ObjToString(i2) == "ISO,AAA" {
  295. saveTagPool <- map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "03", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  296. //MysqlTool.Insert("bid_tags", map[string]interface{}{"infoid": id, "labelcode": "3", "labelvalues": "03", "labelweight": 1, "createtime": time.Now().Format(util.Date_Full_Layout)})
  297. }
  298. }
  299. }
  300. }
  301. // @Description 附件
  302. // @Author J 2022/9/22 11:13
  303. func taskAtts(tmp map[string]interface{}) {
  304. id := mongodb.BsonIdToSId(tmp["_id"])
  305. if tmp["projectinfo"] != nil {
  306. if pinfo, o := tmp["projectinfo"].(map[string]interface{}); o {
  307. if attsMap, ok := pinfo["attachments"].(map[string]interface{}); ok {
  308. for _, attr := range attsMap {
  309. if at, ok := attr.(map[string]interface{}); ok {
  310. if util.ObjToString(at["fid"]) != "" {
  311. ftype := ""
  312. for _, s := range FileTypeArr {
  313. ft := strings.ToLower(util.ObjToString(tmp["ftype"]))
  314. if strings.Contains(ft, s) {
  315. ftype = s
  316. break
  317. }
  318. }
  319. saveAttrPool <- map[string]interface{}{"infoid": id, "org_url": at["org_url"], "size": at["size"], "fid": at["fid"],
  320. "filename": at["filename"], "ftype": ftype, "file_type": 0, "createtime": time.Now().Format(util.Date_Full_Layout)}
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. if attachTxt, o := tmp["attach_text"].(map[string]interface{}); o {
  328. if len(attachTxt) > 0 {
  329. for _, at := range attachTxt {
  330. at1 := at.(map[string]interface{})
  331. if len(at1) > 0 {
  332. for k, v := range at1 {
  333. if reflect.TypeOf(v).String() == "string" {
  334. if util.ObjToString(at1["attach_url"]) != "" {
  335. saveAttrPool <- map[string]interface{}{"infoid": id, "fid": at1["attach_url"], "filename": at1["file_name"], "file_type": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  336. }
  337. break
  338. } else {
  339. if at2, ok := at1[k].(map[string]interface{}); ok {
  340. if util.ObjToString(at2["attach_url"]) != "" {
  341. saveAttrPool <- map[string]interface{}{"infoid": id, "fid": at2["attach_url"], "filename": at2["file_name"], "file_type": 1, "createtime": time.Now().Format(util.Date_Full_Layout)}
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. // @Description 拟建
  352. // @Author J 2022/9/22 15:56
  353. func taskInfoformat(tmp map[string]interface{}) {
  354. if util.IntAll(tmp["infoformat"]) != 2 && tmp["projectinfo"] != nil {
  355. return
  356. }
  357. if info, ok := tmp["projectinfo"].(map[string]interface{}); ok {
  358. delete(info, "attachments")
  359. if len(info) > 0 {
  360. saveM := make(map[string]interface{})
  361. for _, f := range IfmField {
  362. if f == "infoid" {
  363. saveM[f] = mongodb.BsonIdToSId(tmp["_id"])
  364. } else if f == "createtime" || f == "updatetime" {
  365. saveM[f] = time.Now().Format(util.Date_Full_Layout)
  366. } else if f == "approvetime" {
  367. if info[f] != nil && util.IntAll(tmp[f]) > 0 {
  368. saveM[f] = info[f]
  369. }
  370. } else {
  371. if info[f] != nil {
  372. saveM[f] = info[f]
  373. }
  374. }
  375. }
  376. saveIfmPool <- saveM
  377. }
  378. }
  379. }
  380. // @Description 采购意向
  381. // @Author J 2022/9/22 16:27
  382. func taskIntent(tmp map[string]interface{}) {
  383. if arr, ok := tmp["procurementlist"].([]interface{}); ok {
  384. for _, p := range arr {
  385. p1 := p.(map[string]interface{})
  386. saveM := make(map[string]interface{})
  387. for _, f := range IntentField {
  388. if f == "infoid" {
  389. saveM[f] = mongodb.BsonIdToSId(tmp["_id"])
  390. } else if f == "createtime" || f == "updatetime" {
  391. saveM[f] = time.Now().Format(util.Date_Full_Layout)
  392. } else if f == "buyer_id" {
  393. if b := util.ObjToString(tmp["buyer"]); b != "" {
  394. //if code := redis.GetStr("qyxy_id", b); code != "" {
  395. // saveM[f] = code
  396. //}
  397. if code := getNameId(b); code != "" {
  398. saveM[f] = code
  399. }
  400. }
  401. } else {
  402. if p1[f] != nil {
  403. saveM[f] = p1[f]
  404. }
  405. }
  406. }
  407. saveIntentPool <- saveM
  408. }
  409. }
  410. }
  411. // @Description 中标单位
  412. // @Author J 2022/9/27 10:58
  413. func taskWinner(tmp map[string]interface{}) {
  414. if wod, ok := tmp["winnerorder"].([]interface{}); ok {
  415. for _, w := range wod {
  416. w1 := w.(map[string]interface{})
  417. if w1["sort"] != nil {
  418. saveM := make(map[string]interface{})
  419. for _, f := range WinnerField {
  420. if f == "infoid" {
  421. saveM[f] = mongodb.BsonIdToSId(tmp["_id"])
  422. } else if f == "createtime" || f == "updatetime" {
  423. saveM[f] = time.Now().Format(util.Date_Full_Layout)
  424. } else if f == "winnersort" {
  425. saveM[f] = util.IntAll(w1["sort"])
  426. } else if f == "winner_id" {
  427. if b := util.ObjToString(w1["entname"]); b != "" {
  428. saveM["winner"] = b
  429. //if code := redis.GetStr("qyxy_id", b); code != "" {
  430. // saveM[f] = code
  431. //}
  432. if code := getNameId(b); code != "" {
  433. saveM[f] = code
  434. }
  435. }
  436. } else if f == "package_id" {
  437. }
  438. }
  439. saveWinnerPool <- saveM
  440. }
  441. }
  442. }
  443. warr := strings.Split(util.ObjToString(tmp["s_winner"]), ",")
  444. if BinarySearch(warr, util.ObjToString(tmp["winner"])) == -1 {
  445. warr = append(warr, util.ObjToString(tmp["winner"]))
  446. }
  447. for _, s := range warr {
  448. saveM := make(map[string]interface{})
  449. for _, f := range WinnerField {
  450. if f == "infoid" {
  451. saveM[f] = mongodb.BsonIdToSId(tmp["_id"])
  452. } else if f == "createtime" || f == "updatetime" {
  453. saveM[f] = time.Now().Format(util.Date_Full_Layout)
  454. } else if f == "winnersort" {
  455. saveM[f] = 0
  456. } else if f == "winner_id" {
  457. if s != "" {
  458. saveM["winner"] = s
  459. //if code := redis.GetStr("qyxy_id", s); code != "" {
  460. // saveM[f] = code
  461. //}
  462. if code := getNameId(s); code != "" {
  463. saveM[f] = code
  464. }
  465. }
  466. }
  467. }
  468. saveWinnerPool <- saveM
  469. }
  470. }
  471. func BinarySearch(s []string, k string) int {
  472. sort.Strings(s)
  473. lo, hi := 0, len(s)-1
  474. for lo <= hi {
  475. m := (lo + hi) >> 1
  476. if s[m] < k {
  477. lo = m + 1
  478. } else if s[m] > k {
  479. hi = m - 1
  480. } else {
  481. return m
  482. }
  483. }
  484. return -1
  485. }
  486. func taskPackage(tmp map[string]interface{}) {
  487. }
  488. // @Description 标的物
  489. // @Author J 2022/9/29 16:48
  490. func taskPur(tmp map[string]interface{}) {
  491. if plist, ok := tmp["purchasinglist"].([]interface{}); ok {
  492. for _, p := range plist {
  493. saveM := make(map[string]interface{})
  494. p1 := p.(map[string]interface{})
  495. for _, f := range PurField {
  496. if f == "infoid" {
  497. saveM[f] = mongodb.BsonIdToSId(tmp["_id"])
  498. } else if f == "unitprice" || f == "totalprice" {
  499. if p1[f] != nil {
  500. if reflect.TypeOf(p1[f]).String() == "string" {
  501. } else {
  502. if util.Float64All(p1[f]) <= 10000000000 {
  503. saveM[f], _ = util.FormatFloat(util.Float64All(p1[f]), 4)
  504. }
  505. }
  506. }
  507. } else {
  508. if p1[f] != nil {
  509. if reflect.TypeOf(p1[f]).String() == "string" {
  510. if f == "item" || f == "itemname" || f == "brandname" {
  511. if len(util.ObjToString(p1[f])) <= 500 {
  512. saveM[f] = p1[f]
  513. }
  514. } else {
  515. saveM[f] = p1[f]
  516. }
  517. } else {
  518. saveM[f] = p1[f]
  519. }
  520. }
  521. }
  522. }
  523. savePurPool <- saveM
  524. }
  525. }
  526. }