task.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. //情报信息查询
  185. info := FindInfomationData(id)
  186. //法人信息
  187. winners := []string{}
  188. if winner != "" {
  189. winners = strings.Split(winner, ",")
  190. }
  191. buyer_id, agency_id, winner_ids := FindEntInfoData(id, buyer, agency, winners)
  192. //物业信息
  193. t := &Transaction{
  194. Project_Id: id,
  195. Project_Name: gconv.String(tmp["projectname"]),
  196. Project_Budget: budget,
  197. Project_Bidamount: bidamount,
  198. Project_Money: money,
  199. Business_Type: business_type,
  200. Project_Bidstatus: project_bidstatus,
  201. Info_Id: id,
  202. Info_Ids: []string{id},
  203. Information_Id: info.Id,
  204. BuyerClass: buyerclass,
  205. Buyer: buyer,
  206. Winner: winners,
  207. Agency: agency,
  208. Buyer_Id: buyer_id,
  209. Winner_Id: winner_ids,
  210. Agency_Id: agency_id,
  211. Property_Form: property_form,
  212. SubClass: subclass,
  213. MultiPackage: gconv.Int(tmp["multipackage"]),
  214. Area: gconv.String(tmp["area"]),
  215. City: gconv.String(tmp["city"]),
  216. District: gconv.String(tmp["district"]),
  217. ZbTime: gconv.Int64(tmp["publishtime"]),
  218. JgTime: int64(0),
  219. StartTime: info.Starttime,
  220. EndTime: info.Endtime,
  221. Create_Time: time.Now().Unix(),
  222. Update_Time: time.Now().Unix(),
  223. //
  224. From: "bidding",
  225. }
  226. result := map[string]interface{}{}
  227. infomation, _ := bson.Marshal(t)
  228. bson.Unmarshal(infomation, &result)
  229. return result
  230. }
  231. // IncTransactionDataFromProject 增量project
  232. func IncTransactionDataFromPro() {
  233. endTime := GetTime(-1) //前一天凌晨
  234. fmt.Println("开始执行增量项目信息", ProStartTime, endTime)
  235. if ProStartTime >= endTime {
  236. fmt.Println("增量项目信息查询异常:", ProStartTime, endTime)
  237. return
  238. }
  239. query := map[string]interface{}{
  240. "pici": map[string]interface{}{
  241. "$gte": ProStartTime,
  242. "$lt": endTime,
  243. },
  244. }
  245. fmt.Println("增量项目查询query:", query)
  246. sess := MgoPro.GetMgoConn()
  247. defer MgoPro.DestoryMongoConn(sess)
  248. ch := make(chan bool, 5)
  249. wg := &sync.WaitGroup{}
  250. lock := &sync.Mutex{}
  251. fields := map[string]interface{}{
  252. "projectname": 1,
  253. "budget": 1,
  254. "bidamount": 1,
  255. "buyer": 1,
  256. "s_winner": 1,
  257. "agency": 1,
  258. "property_form": 1,
  259. "multipackage": 1,
  260. "area": 1,
  261. "city": 1,
  262. "district": 1,
  263. "zbtime": 1,
  264. "jgtime": 1,
  265. "bidstatus": 1,
  266. "buyerclass": 1,
  267. //
  268. "firsttime": 1,
  269. "pici": 1,
  270. "ids": 1,
  271. "sourceinfoid": 1,
  272. "tag_subinformation": 1,
  273. "tag_subinformation_ai": 1,
  274. "tag_topinformation": 1,
  275. "tag_topinformation_ai": 1,
  276. }
  277. arr := [][]map[string]interface{}{}
  278. it := sess.DB(MgoPro.DbName).C("projectset_20230904").Find(&query).Select(&fields).Iter()
  279. n := 0
  280. count := 0
  281. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  282. ch <- true
  283. wg.Add(1)
  284. go func(tmp map[string]interface{}) {
  285. defer func() {
  286. <-ch
  287. wg.Done()
  288. }()
  289. if tmp["tag_topinformation"] == nil && tmp["tag_topinformation_ai"] == nil { //无效数据过滤
  290. return
  291. }
  292. result := DealTransactionForPro(tmp)
  293. lock.Lock()
  294. if len(result) > 0 {
  295. count++
  296. update := []map[string]interface{}{
  297. {"project_id": mongodb.BsonIdToSId(tmp["_id"])},
  298. {"$set": result},
  299. }
  300. arr = append(arr, update)
  301. }
  302. if len(arr) > 50 {
  303. MgoPro.UpSertBulk("projectset_wy_back", arr...)
  304. arr = [][]map[string]interface{}{}
  305. }
  306. lock.Unlock()
  307. }(tmp)
  308. if n%1000 == 0 {
  309. fmt.Println("current:", n)
  310. }
  311. tmp = map[string]interface{}{}
  312. }
  313. wg.Wait()
  314. if len(arr) > 0 {
  315. MgoPro.UpSertBulk("projectset_wy_back", arr...)
  316. arr = [][]map[string]interface{}{}
  317. }
  318. fmt.Println("执行增量项目信息完毕", ProStartTime, endTime, count)
  319. ProStartTime = endTime //替换
  320. }
  321. // DealTransactionForPro project数据处理
  322. func DealTransactionForPro(data map[string]interface{}) map[string]interface{} {
  323. //基本信息封装
  324. id := mongodb.BsonIdToSId(data["_id"])
  325. buyerclass := gconv.String(data["buyerclass"])
  326. buyer := gconv.String(data["buyer"])
  327. winner := gconv.String(data["s_winner"])
  328. agency := gconv.String(data["agency"])
  329. zbtime := gconv.Int64(data["zbtime"])
  330. if zbtime == 0 {
  331. zbtime = gconv.Int64(data["firsttime"])
  332. }
  333. property_form := []string{}
  334. if data["property_form"] != nil {
  335. property_form = gconv.Strings(data["property_form"])
  336. }
  337. bidamount := gconv.Float64(data["bidamount"])
  338. budget := gconv.Float64(data["budget"])
  339. money := bidamount
  340. if money <= 0 {
  341. money = budget
  342. }
  343. //物业分类
  344. subclass := []string{}
  345. if tag_subinformation := data["tag_subinformation"]; tag_subinformation != nil {
  346. subclass = gconv.Strings(tag_subinformation)
  347. } else if tag_subinformation_ai := data["tag_subinformation_ai"]; tag_subinformation_ai != nil {
  348. subclass = gconv.Strings(tag_subinformation_ai)
  349. }
  350. //项目状态、商机类型
  351. business_type := ""
  352. project_bidstatus := 2
  353. bidstatus := gconv.String(data["bidstatus"])
  354. if bidstatus == "中标" || bidstatus == "成交" || bidstatus == "合同" {
  355. project_bidstatus = 1
  356. business_type = "合约到期项目"
  357. } else if bidstatus == "废标" || bidstatus == "流标" {
  358. project_bidstatus = 0
  359. } else if bidstatus == "拟建" {
  360. project_bidstatus = 4
  361. } else if bidstatus == "招标" {
  362. business_type = "招标项目"
  363. }
  364. //查询情报信息
  365. ids := gconv.Strings(data["ids"])
  366. info := FindInfomationData(ids...) //情报信息查询
  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. }*/