bidding.go 17 KB

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