task.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/util/gconv"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. type Transaction struct {
  13. Project_Id string `bson:"project_id"`
  14. Project_Name string `bson:"project_name"`
  15. Project_Budget float64 `bson:"project_budget"`
  16. Project_Bidamount float64 `bson:"project_bidamount"`
  17. Project_Money float64 `bson:"project_money"`
  18. Business_Type string `bson:"business_type"`
  19. Project_Bidstatus int `bson:"project_bidstatus"`
  20. Info_Id string `bson:"info_id"`
  21. Info_Ids []string `bson:"info_ids"`
  22. Information_Id string `bson:"information_id"`
  23. BuyerClass string `bson:"buyerclass"`
  24. Buyer string `bson:"buyer"`
  25. Buyer_Id string `bson:"buyer_id"`
  26. Winner []string `bson:"winner"`
  27. Winner_Id []string `bson:"winner_id"`
  28. Agency string `bson:"agency"`
  29. Agency_Id string `bson:"agency_id"`
  30. Property_Form []string `bson:"property_form"`
  31. SubClass []string `bson:"subclass"`
  32. MultiPackage int `bson:"multipackage"`
  33. Area string `bson:"area"`
  34. City string `bson:"city"`
  35. District string `bson:"district"`
  36. ZbTime int64 `bson:"zbtime"`
  37. JgTime int64 `bson:"jgtime"`
  38. StartTime int64 `bson:"starttime"`
  39. EndTime int64 `bson:"endtime"`
  40. Create_Time int64 `bson:"create_time"`
  41. Update_Time int64 `bson:"update_time"`
  42. //
  43. From string `bson:"from"`
  44. }
  45. func IncTransactionDataFromBidAndPro() {
  46. IncTransactionDataFromBid() //bidding
  47. IncTransactionDataFromPro() //project
  48. return
  49. IncTransactionDataMgoToCkhAndEs() //mongodb迁移至clickhouse
  50. }
  51. // IncTransactionDataFromBid 增量bidding
  52. func IncTransactionDataFromBid() {
  53. endTime := GetTime(-1) //前一天凌晨
  54. fmt.Println("开始执行增量采购意向、拟建信息", BidStartTime, endTime)
  55. if BidStartTime >= endTime {
  56. fmt.Println("增量bidding采购意向、拟建查询异常:", BidStartTime, endTime)
  57. return
  58. }
  59. query := map[string]interface{}{
  60. "comeintime": map[string]interface{}{
  61. "$gte": BidStartTime,
  62. "$lt": endTime,
  63. },
  64. }
  65. fmt.Println("增量bidding采购意向query:", query)
  66. sess := MgoB.GetMgoConn()
  67. defer MgoB.DestoryMongoConn(sess)
  68. ch := make(chan bool, 5)
  69. wg := &sync.WaitGroup{}
  70. lock := &sync.Mutex{}
  71. fields := map[string]interface{}{
  72. "projectname": 1,
  73. "budget": 1,
  74. "bidamount": 1,
  75. "buyer": 1,
  76. "s_winner": 1,
  77. "agency": 1,
  78. "property_form": 1,
  79. "multipackage": 1,
  80. "area": 1,
  81. "city": 1,
  82. "district": 1,
  83. "buyerclass": 1,
  84. //
  85. "owner": 1,
  86. "s_topscopeclass": 1,
  87. "publishtime": 1,
  88. "toptype": 1,
  89. "extracttype": 1,
  90. "tag_subinformation": 1,
  91. "tag_subinformation_ai": 1,
  92. "tag_topinformation": 1,
  93. "tag_topinformation_ai": 1,
  94. }
  95. arr := []map[string]interface{}{}
  96. it := sess.DB(MgoB.DbName).C("bidding").Find(&query).Select(&fields).Iter()
  97. n := 0
  98. count := 0
  99. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  100. ch <- true
  101. wg.Add(1)
  102. go func(tmp map[string]interface{}) {
  103. defer func() {
  104. <-ch
  105. wg.Done()
  106. }()
  107. if gconv.Int(tmp["extracttype"]) == -1 { //重复数据过滤
  108. return
  109. }
  110. toptype := gconv.String(tmp["toptype"])
  111. tag_topinformation := gconv.String(tmp["tag_topinformation"])
  112. tag_topinformation_ai := gconv.String(tmp["tag_topinformation_ai"])
  113. var business_type string
  114. var project_bidstatus int
  115. if toptype == "采购意向" { //采购意向数据
  116. if !strings.Contains(tag_topinformation, "物业") && !strings.Contains(tag_topinformation_ai, "物业") {
  117. return
  118. }
  119. business_type = "采购意向"
  120. project_bidstatus = 3
  121. } else if toptype == "拟建" {
  122. s_topscopeclass := gconv.String(tmp["s_topscopeclass"])
  123. if !strings.Contains(s_topscopeclass, "建筑工程") || strings.Contains(tag_topinformation, "物业") || strings.Contains(tag_topinformation_ai, "物业") {
  124. return
  125. }
  126. business_type = "新增项目"
  127. project_bidstatus = 4
  128. } else {
  129. return
  130. }
  131. result := DealTransactionForBid(tmp, business_type, project_bidstatus)
  132. lock.Lock()
  133. if len(result) > 0 {
  134. arr = append(arr, result)
  135. count++
  136. }
  137. if len(arr) > 50 {
  138. MgoPro.SaveBulk("projectset_wy", arr...)
  139. arr = []map[string]interface{}{}
  140. }
  141. lock.Unlock()
  142. }(tmp)
  143. if n%1000 == 0 {
  144. fmt.Println("current:", n)
  145. }
  146. tmp = map[string]interface{}{}
  147. }
  148. wg.Wait()
  149. if len(arr) > 0 {
  150. MgoPro.SaveBulk("projectset_wy", arr...)
  151. arr = []map[string]interface{}{}
  152. }
  153. fmt.Println("执行增量采购意向、拟建信息完毕", BidStartTime, endTime, count)
  154. BidStartTime = endTime //替换
  155. }
  156. // DealTransactionForBid bidding采购意向、拟建数据处理
  157. func DealTransactionForBid(tmp map[string]interface{}, business_type string, project_bidstatus int) map[string]interface{} {
  158. //基本信息封装
  159. id := mongodb.BsonIdToSId(tmp["_id"])
  160. buyerclass := gconv.String(tmp["buyerclass"])
  161. buyer := gconv.String(tmp["buyer"])
  162. if buyer == "" {
  163. buyer = gconv.String(tmp["owner"])
  164. }
  165. winner := gconv.String(tmp["s_winner"])
  166. agency := gconv.String(tmp["agency"])
  167. property_form := []string{}
  168. if tmp["property_form"] != nil {
  169. property_form = gconv.Strings(tmp["property_form"])
  170. }
  171. bidamount := gconv.Float64(tmp["bidamount"])
  172. budget := gconv.Float64(tmp["budget"])
  173. money := bidamount
  174. if money <= 0 {
  175. money = budget
  176. }
  177. //物业分类
  178. subclass := []string{}
  179. if tag_subinformation := tmp["tag_subinformation"]; tag_subinformation != nil {
  180. subclass = gconv.Strings(tag_subinformation)
  181. } else if tag_subinformation_ai := tmp["tag_subinformation_ai"]; tag_subinformation_ai != nil {
  182. subclass = gconv.Strings(tag_subinformation_ai)
  183. }
  184. //TODO 情报库信息(待补充)
  185. info := Infomation{}
  186. //if project_bidstatus == 4 {//补充情报信息
  187. // //bidId = "65fbf3f566cf0db42a2a99d2"
  188. // info = FindInfomationData(id) //情报信息查询
  189. // information_id = info.Id
  190. // starttime = info.Starttime
  191. // endtime = info.Endtime
  192. //}
  193. //TODO 查询法人库信息(待补充)
  194. winners := []string{}
  195. if winner != "" {
  196. winners = strings.Split(winner, ",")
  197. }
  198. buyer_id, agency_id, winner_ids := FindEntInfoData(id, buyer, agency, winners)
  199. //物业信息
  200. t := &Transaction{
  201. Project_Id: id,
  202. Project_Name: gconv.String(tmp["projectname"]),
  203. Project_Budget: budget,
  204. Project_Bidamount: bidamount,
  205. Project_Money: money,
  206. Business_Type: business_type,
  207. Project_Bidstatus: project_bidstatus,
  208. Info_Id: id,
  209. Info_Ids: []string{id},
  210. Information_Id: info.Id,
  211. BuyerClass: buyerclass,
  212. Buyer: buyer,
  213. Winner: winners,
  214. Agency: agency,
  215. Buyer_Id: buyer_id,
  216. Winner_Id: winner_ids,
  217. Agency_Id: agency_id,
  218. Property_Form: property_form,
  219. SubClass: subclass,
  220. MultiPackage: gconv.Int(tmp["multipackage"]),
  221. Area: gconv.String(tmp["area"]),
  222. City: gconv.String(tmp["city"]),
  223. District: gconv.String(tmp["district"]),
  224. ZbTime: gconv.Int64(tmp["publishtime"]),
  225. JgTime: int64(0),
  226. StartTime: info.Starttime,
  227. EndTime: info.Endtime,
  228. Create_Time: time.Now().Unix(),
  229. Update_Time: time.Now().Unix(),
  230. //
  231. From: "bidding",
  232. }
  233. result := map[string]interface{}{}
  234. infomation, _ := bson.Marshal(t)
  235. bson.Unmarshal(infomation, &result)
  236. return result
  237. }
  238. // IncTransactionDataFromProject 增量project
  239. func IncTransactionDataFromPro() {
  240. endTime := GetTime(-1) //前一天凌晨
  241. fmt.Println("开始执行增量项目信息", ProStartTime, endTime)
  242. if ProStartTime >= endTime {
  243. fmt.Println("增量项目信息查询异常:", ProStartTime, endTime)
  244. return
  245. }
  246. query := map[string]interface{}{
  247. "pici": map[string]interface{}{
  248. "$gte": ProStartTime,
  249. "$lt": endTime,
  250. },
  251. }
  252. fmt.Println("增量项目查询query:", query)
  253. sess := MgoPro.GetMgoConn()
  254. defer MgoPro.DestoryMongoConn(sess)
  255. ch := make(chan bool, 5)
  256. wg := &sync.WaitGroup{}
  257. lock := &sync.Mutex{}
  258. fields := map[string]interface{}{
  259. "projectname": 1,
  260. "budget": 1,
  261. "bidamount": 1,
  262. "buyer": 1,
  263. "s_winner": 1,
  264. "agency": 1,
  265. "property_form": 1,
  266. "multipackage": 1,
  267. "area": 1,
  268. "city": 1,
  269. "district": 1,
  270. "zbtime": 1,
  271. "jgtime": 1,
  272. "bidstatus": 1,
  273. "buyerclass": 1,
  274. //
  275. "firsttime": 1,
  276. "pici": 1,
  277. "ids": 1,
  278. "sourceinfoid": 1,
  279. "tag_subinformation": 1,
  280. "tag_subinformation_ai": 1,
  281. "tag_topinformation": 1,
  282. "tag_topinformation_ai": 1,
  283. }
  284. arr := [][]map[string]interface{}{}
  285. it := sess.DB(MgoPro.DbName).C("projectset_20230904").Find(&query).Select(&fields).Iter()
  286. n := 0
  287. count := 0
  288. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  289. ch <- true
  290. wg.Add(1)
  291. go func(tmp map[string]interface{}) {
  292. defer func() {
  293. <-ch
  294. wg.Done()
  295. }()
  296. if tmp["tag_topinformation"] == nil && tmp["tag_topinformation_ai"] == nil { //无效数据过滤
  297. return
  298. }
  299. result := DealTransactionForPro(tmp)
  300. lock.Lock()
  301. if len(result) > 0 {
  302. count++
  303. update := []map[string]interface{}{
  304. {"project_id": tmp["_id"]},
  305. {"$set": result},
  306. }
  307. arr = append(arr, update)
  308. }
  309. if len(arr) > 50 {
  310. MgoPro.UpSertBulk("projectset_wy_back", arr...)
  311. arr = [][]map[string]interface{}{}
  312. }
  313. lock.Unlock()
  314. }(tmp)
  315. if n%1000 == 0 {
  316. fmt.Println("current:", n)
  317. }
  318. tmp = map[string]interface{}{}
  319. }
  320. wg.Wait()
  321. if len(arr) > 0 {
  322. MgoPro.UpSertBulk("projectset_wy_back", arr...)
  323. arr = [][]map[string]interface{}{}
  324. }
  325. fmt.Println("执行增量项目信息完毕", ProStartTime, endTime, count)
  326. ProStartTime = endTime //替换
  327. }
  328. // DealTransactionForPro project数据处理
  329. func DealTransactionForPro(data map[string]interface{}) map[string]interface{} {
  330. //基本信息封装
  331. id := mongodb.BsonIdToSId(data["_id"])
  332. buyerclass := gconv.String(data["buyerclass"])
  333. buyer := gconv.String(data["buyer"])
  334. winner := gconv.String(data["s_winner"])
  335. agency := gconv.String(data["agency"])
  336. zbtime := gconv.Int64(data["zbtime"])
  337. if zbtime == 0 {
  338. zbtime = gconv.Int64(data["firsttime"])
  339. }
  340. property_form := []string{}
  341. if data["property_form"] != nil {
  342. property_form = gconv.Strings(data["property_form"])
  343. }
  344. bidamount := gconv.Float64(data["bidamount"])
  345. budget := gconv.Float64(data["budget"])
  346. money := bidamount
  347. if money <= 0 {
  348. money = budget
  349. }
  350. //物业分类
  351. subclass := []string{}
  352. if tag_subinformation := data["tag_subinformation"]; tag_subinformation != nil {
  353. subclass = gconv.Strings(tag_subinformation)
  354. } else if tag_subinformation_ai := data["tag_subinformation_ai"]; tag_subinformation_ai != nil {
  355. subclass = gconv.Strings(tag_subinformation_ai)
  356. }
  357. //项目状态、商机类型
  358. business_type := ""
  359. project_bidstatus := 2
  360. bidstatus := gconv.String(data["bidstatus"])
  361. if bidstatus == "中标" || bidstatus == "成交" || bidstatus == "合同" {
  362. project_bidstatus = 1
  363. business_type = "存量项目"
  364. } else if bidstatus == "废标" || bidstatus == "流标" {
  365. project_bidstatus = 0
  366. } else if bidstatus == "拟建" {
  367. project_bidstatus = 4
  368. } else if bidstatus == "招标" {
  369. business_type = "招标项目"
  370. }
  371. //查询情报信息
  372. //bidId = "65fbf3f566cf0db42a2a99d2"
  373. ids := gconv.Strings(data["ids"])
  374. info := Infomation{}
  375. //info := FindInfomationData(ids...) //情报信息查询
  376. //TODO 查询法人库信息(待补充)
  377. winners := []string{}
  378. if winner != "" {
  379. winners = strings.Split(winner, ",")
  380. }
  381. buyer_id, agency_id, winner_ids := FindEntInfoData(id, buyer, agency, winners)
  382. //物业信息
  383. t := &Transaction{
  384. Project_Id: id,
  385. Project_Name: gconv.String(data["projectname"]),
  386. Project_Budget: budget,
  387. Project_Bidamount: bidamount,
  388. Project_Money: money,
  389. Business_Type: business_type,
  390. Project_Bidstatus: project_bidstatus,
  391. Info_Id: gconv.String(data["sourceinfoid"]),
  392. Info_Ids: ids,
  393. Information_Id: info.Id,
  394. BuyerClass: buyerclass,
  395. Buyer: buyer,
  396. Winner: winners,
  397. Agency: agency,
  398. Buyer_Id: buyer_id,
  399. Winner_Id: winner_ids,
  400. Agency_Id: agency_id,
  401. Property_Form: property_form,
  402. SubClass: subclass,
  403. MultiPackage: gconv.Int(data["multipackage"]),
  404. Area: gconv.String(data["area"]),
  405. City: gconv.String(data["city"]),
  406. District: gconv.String(data["district"]),
  407. ZbTime: zbtime,
  408. JgTime: gconv.Int64(data["jgtime"]),
  409. StartTime: info.Starttime,
  410. EndTime: info.Endtime,
  411. Create_Time: time.Now().Unix(),
  412. Update_Time: time.Now().Unix(),
  413. //
  414. From: "project",
  415. }
  416. result := map[string]interface{}{}
  417. infomation, _ := bson.Marshal(t)
  418. bson.Unmarshal(infomation, &result)
  419. return result
  420. }
  421. // IncTransactionDataMgoToCkhAndEs 数据迁移
  422. func IncTransactionDataMgoToCkhAndEs() {
  423. /*
  424. 数据根据update_time查询
  425. 1、采购意向数据(from=bidding)只插入
  426. 2、项目信息先查,有则更新,无则插入
  427. */
  428. fmt.Println("开始执行迁移...")
  429. sess := MgoPro.GetMgoConn()
  430. defer MgoPro.DestoryMongoConn(sess)
  431. ch := make(chan bool, 2)
  432. wg := &sync.WaitGroup{}
  433. query := map[string]interface{}{
  434. "update_time": map[string]interface{}{
  435. "$gte": GetTime(0),
  436. },
  437. }
  438. it := sess.DB(MgoPro.DbName).C("projectset_wy").Find(&query).Iter()
  439. n := 0
  440. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  441. ch <- true
  442. wg.Add(1)
  443. go func(tmp map[string]interface{}) {
  444. defer func() {
  445. <-ch
  446. wg.Done()
  447. }()
  448. from := gconv.String(tmp["from"])
  449. delete(tmp, "from") //无用字段删除
  450. delete(tmp, "_id") //无用字段删除
  451. if !SaveDataToEs(tmp) { //保存、更新es
  452. fmt.Println("数据保存es失败,项目project_id", tmp["project_id"])
  453. }
  454. if from == "bidding" { //采购意向、拟建,插入
  455. SaveDataToClickHouse(tmp)
  456. } else { //项目信息,更新,插入
  457. UpdateOrSaveDataToClickHouse(tmp)
  458. }
  459. }(tmp)
  460. if n%100 == 0 {
  461. fmt.Println("current:", n)
  462. }
  463. tmp = map[string]interface{}{}
  464. }
  465. wg.Wait()
  466. fmt.Println("迁移结束...")
  467. }
  468. type Infomation struct {
  469. Id string
  470. Starttime int64
  471. Endtime int64
  472. }
  473. // FindInfomationData 情报信息查询
  474. func FindInfomationData(ids ...string) (info Infomation) {
  475. for _, id := range ids {
  476. query := fmt.Sprintf(`SELECT id,starttime,endtime FROM %s WHERE datajson_id = ?`, Config.ClickHouse.DataBase+".information")
  477. rows, err := CkhTool.Query(context.Background(), query, id)
  478. if err != nil {
  479. continue
  480. }
  481. for rows.Next() {
  482. info = Infomation{}
  483. if err := rows.Scan(&info.Id, &info.Starttime, &info.Endtime); err != nil {
  484. fmt.Println("查询情报信息异常:", id, err)
  485. }
  486. if info.Id != "" {
  487. return
  488. }
  489. //break //目前只有一条结果
  490. }
  491. }
  492. return
  493. }
  494. // FindEntInfoData 法人信息查询
  495. func FindEntInfoData(bid, buyer, agency string, winners []string) (buyer_id, agency_id string, winner_ids []string) {
  496. winner_ids = []string{}
  497. winnerMap := map[string]bool{} //记录所有中标单位
  498. values := []interface{}{}
  499. placeholders := []string{}
  500. if buyer != "" {
  501. placeholders = append(placeholders, "?")
  502. values = append(values, buyer)
  503. }
  504. if len(winners) > 0 {
  505. for _, w := range winners {
  506. winnerMap[w] = true
  507. placeholders = append(placeholders, "?")
  508. values = append(values, w)
  509. }
  510. }
  511. if agency != "" {
  512. placeholders = append(placeholders, "?")
  513. values = append(values, agency)
  514. }
  515. if len(values) == 0 {
  516. return
  517. }
  518. query := fmt.Sprintf(`SELECT id,company_name FROM %s WHERE company_name IN (%s)`, Config.ClickHouse.DataBase+".ent_info", strings.Join(placeholders, ","))
  519. rows, err := CkhTool.Query(context.Background(), query, values...)
  520. if err != nil {
  521. return
  522. }
  523. for rows.Next() {
  524. var id, company_name string
  525. if err := rows.Scan(&id, &company_name); err == nil {
  526. if company_name == buyer {
  527. buyer_id = id
  528. } else if company_name == agency {
  529. agency_id = id
  530. } else if winnerMap[company_name] {
  531. winner_ids = append(winner_ids, id)
  532. }
  533. } else {
  534. fmt.Println("查询法人信息异常:", err, bid)
  535. }
  536. }
  537. return
  538. }
  539. // UpdateOrSaveDataToClickHouse 判断clickhouse更新or保存
  540. func UpdateOrSaveDataToClickHouse(data map[string]interface{}) (err error) {
  541. project_id := gconv.String(data["project_id"])
  542. count := FindClickHouseByProjectId(project_id) //查询
  543. if count > 0 { //更新
  544. delete(data, "create_time") //不更新创建时间
  545. delete(data, "project_id") //不更新项目id(主键)
  546. err = UpdateDataToClickHouse(data, map[string]interface{}{"project_id": project_id})
  547. if err != nil {
  548. fmt.Println("clickhouse更新失败", project_id, data)
  549. }
  550. } else { //插入
  551. err = SaveDataToClickHouse(data)
  552. if err != nil {
  553. fmt.Println("clickhouse保存失败", project_id, data)
  554. }
  555. }
  556. return
  557. }
  558. // SaveDataToClickHouse 数据保存clickhouse
  559. func SaveDataToClickHouse(data map[string]interface{}) error {
  560. fields, placeholders := []string{}, []string{}
  561. values := []interface{}{}
  562. for k, v := range data {
  563. fields = append(fields, k)
  564. values = append(values, v)
  565. placeholders = append(placeholders, "?")
  566. }
  567. query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", Config.ClickHouse.DataBase+".transaction_info", strings.Join(fields, ","), strings.Join(placeholders, ","))
  568. return CkhTool.Exec(context.Background(), query, values...)
  569. }
  570. // FindClickHouseByProjectId 根据条件count clickhouse
  571. func FindClickHouseByProjectId(project_id string) int {
  572. query := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE project_id = ?`, Config.ClickHouse.DataBase+".transaction_info")
  573. row := CkhTool.QueryRow(context.Background(), query, project_id)
  574. var count uint64
  575. row.Scan(&count)
  576. return gconv.Int(count)
  577. }
  578. // UpdateDataToClickHouse 数据更新clickhouse
  579. func UpdateDataToClickHouse(data, querys map[string]interface{}) error {
  580. sets := []string{}
  581. values := []interface{}{}
  582. for k, v := range data {
  583. sets = append(sets, fmt.Sprintf("%s=?", k))
  584. values = append(values, v)
  585. }
  586. qs := []string{}
  587. for k, v := range querys {
  588. qs = append(qs, fmt.Sprintf("%s=?", k))
  589. values = append(values, v)
  590. }
  591. query := fmt.Sprintf("ALTER TABLE %s UPDATE %s WHERE %s", Config.ClickHouse.DataBase+".transaction_info", strings.Join(sets, ","), strings.Join(qs, ","))
  592. //query := `ALTER TABLE information.transaction_info UPDATE update_time = ? WHERE project_id = '5c9ee78ca5cb26b9b7fd0b57'`
  593. return CkhTool.Exec(context.Background(), query, values...)
  594. }
  595. // SaveDataToEs es存储
  596. func SaveDataToEs(data map[string]interface{}) bool {
  597. tmp := map[string]interface{}{}
  598. for k, v := range data {
  599. if k == "project_id" {
  600. k = "_id"
  601. } else if k == "winner" || k == "winner_id" { //winner和winner_id无值不进es
  602. if len(gconv.Strings(v)) == 0 {
  603. continue
  604. }
  605. }
  606. tmp[k] = v
  607. }
  608. err, result := Es.GetById(Config.Es.Index, gconv.String(tmp["_id"]))
  609. if err == nil && len(result) > 0 { //存在,更新
  610. tmp["create_time"] = result["create_time"] //不更新create_time
  611. }
  612. return Es.Save(Config.Es.Index, tmp)
  613. }
  614. func FindEntInfoData2(bid, buyer, agency string, winners []string) (buyer_id, agency_id string, winner_ids []string) {
  615. query := fmt.Sprintf(`SELECT id FROM %s WHERE company_name = ?`, Config.ClickHouse.DataBase+".ent_info")
  616. if buyer != "" {
  617. buyer_id = GetClickHouseData(bid, query, buyer)
  618. }
  619. if agency != "" {
  620. agency_id = GetClickHouseData(bid, query, agency)
  621. }
  622. if len(winners) > 0 {
  623. for _, w := range winners {
  624. winner_id := GetClickHouseData(bid, query, w)
  625. if winner_id != "" {
  626. winner_ids = append(winner_ids, winner_id)
  627. }
  628. }
  629. }
  630. return
  631. }
  632. func GetClickHouseData(bid, query, value string) string {
  633. rows, err := CkhTool.Query(context.Background(), query, value)
  634. if err != nil {
  635. return ""
  636. }
  637. for rows.Next() {
  638. var id string
  639. if err := rows.Scan(&id); err == nil {
  640. return id
  641. } else {
  642. fmt.Println("查询情报信息异常:", err, bid)
  643. }
  644. }
  645. return ""
  646. }
  647. /*// SaveTransactionData 保存增量物业信息
  648. func SaveTransactionData() {
  649. fmt.Println("save projectset_wy...")
  650. savearr := make([]map[string]interface{}, 100)
  651. indexdb := 0
  652. for {
  653. select {
  654. case v := <-TransactionSaveCache:
  655. savearr[indexdb] = v
  656. indexdb++
  657. if indexdb == 100 {
  658. Transaction_Ch <- true
  659. go func(tmp []map[string]interface{}) {
  660. defer func() {
  661. <-Transaction_Ch
  662. }()
  663. MgoPro.SaveBulk("projectset_wy", tmp...)
  664. }(savearr)
  665. savearr = make([]map[string]interface{}, 100)
  666. indexdb = 0
  667. }
  668. case <-time.After(30 * time.Second):
  669. if indexdb > 0 {
  670. Transaction_Ch <- true
  671. go func(tmp []map[string]interface{}) {
  672. defer func() {
  673. <-Transaction_Ch
  674. }()
  675. MgoPro.SaveBulk("projectset_wy", tmp...)
  676. }(savearr[:indexdb])
  677. savearr = make([]map[string]interface{}, 100)
  678. indexdb = 0
  679. }
  680. }
  681. }
  682. }*/