task.go 21 KB

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