task.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. Buyer string `bson:"buyer"`
  24. Buyer_Id string `bson:"buyer_id"`
  25. Winner []string `bson:"winner"`
  26. Winner_Id []string `bson:"winner_id"`
  27. Agency string `bson:"agency"`
  28. Agency_Id string `bson:"agency_id"`
  29. Property_Form []string `bson:"property_form"`
  30. SubClass []string `bson:"subclass"`
  31. MultiPackage int `bson:"multipackage"`
  32. Area string `bson:"area"`
  33. City string `bson:"city"`
  34. District string `bson:"district"`
  35. ZbTime int64 `bson:"zbtime"`
  36. JgTime int64 `bson:"jgtime"`
  37. StartTime int64 `bson:"starttime"`
  38. EndTime int64 `bson:"endtime"`
  39. Create_Time int64 `bson:"create_time"`
  40. Update_Time int64 `bson:"update_time"`
  41. //
  42. From string `bson:"from"`
  43. }
  44. func IncTransactionDataFromBidAndPro() {
  45. IncTransactionDataFromBid() //bidding
  46. IncTransactionDataFromPro() //project
  47. return
  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. //
  83. "s_topscopeclass": 1,
  84. "publishtime": 1,
  85. "toptype": 1,
  86. "extracttype": 1,
  87. "tag_subinformation": 1,
  88. "tag_subinformation_ai": 1,
  89. "tag_topinformation": 1,
  90. "tag_topinformation_ai": 1,
  91. }
  92. arr := []map[string]interface{}{}
  93. it := sess.DB(MgoB.DbName).C("bidding").Find(&query).Select(&fields).Iter()
  94. n := 0
  95. count := 0
  96. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  97. ch <- true
  98. wg.Add(1)
  99. go func(tmp map[string]interface{}) {
  100. defer func() {
  101. <-ch
  102. wg.Done()
  103. }()
  104. if gconv.Int(tmp["extracttype"]) == -1 { //重复数据过滤
  105. return
  106. }
  107. toptype := gconv.String(tmp["toptype"])
  108. tag_topinformation := gconv.String(tmp["tag_topinformation"])
  109. tag_topinformation_ai := gconv.String(tmp["tag_topinformation_ai"])
  110. var business_type string
  111. var project_bidstatus int
  112. if toptype == "采购意向" { //采购意向数据
  113. if !strings.Contains(tag_topinformation, "物业") && !strings.Contains(tag_topinformation_ai, "物业") {
  114. return
  115. }
  116. business_type = "采购意向"
  117. project_bidstatus = 3
  118. } else if toptype == "拟建" {
  119. if strings.Contains(tag_topinformation, "物业") || strings.Contains(tag_topinformation_ai, "物业") {
  120. return
  121. }
  122. business_type = "新增项目"
  123. project_bidstatus = 4
  124. } else {
  125. return
  126. }
  127. result := DealTransactionForBid(tmp, business_type, project_bidstatus)
  128. lock.Lock()
  129. if len(result) > 0 {
  130. arr = append(arr, result)
  131. count++
  132. }
  133. if len(arr) > 50 {
  134. MgoPro.SaveBulk("projectset_wy", arr...)
  135. arr = []map[string]interface{}{}
  136. }
  137. lock.Unlock()
  138. }(tmp)
  139. if n%1000 == 0 {
  140. fmt.Println("current:", n)
  141. }
  142. tmp = map[string]interface{}{}
  143. }
  144. wg.Wait()
  145. if len(arr) > 0 {
  146. MgoPro.SaveBulk("projectset_wy", arr...)
  147. arr = []map[string]interface{}{}
  148. }
  149. fmt.Println("执行增量采购意向、拟建信息完毕", BidStartTime, endTime, count)
  150. BidStartTime = endTime //替换
  151. }
  152. // DealTransactionForBid bidding采购意向数据处理
  153. func DealTransactionForBid(tmp map[string]interface{}, business_type string, project_bidstatus int) map[string]interface{} {
  154. //基本信息封装
  155. id := mongodb.BsonIdToSId(tmp["_id"])
  156. buyer := gconv.String(tmp["buyer"])
  157. winner := gconv.String(tmp["s_winner"])
  158. agency := gconv.String(tmp["agency"])
  159. property_form := []string{}
  160. if tmp["property_form"] != nil {
  161. property_form = gconv.Strings(tmp["property_form"])
  162. }
  163. bidamount := gconv.Float64(tmp["bidamount"])
  164. budget := gconv.Float64(tmp["budget"])
  165. money := bidamount
  166. if money <= 0 {
  167. money = budget
  168. }
  169. //物业分类
  170. subclass := []string{}
  171. if tag_subinformation := tmp["tag_subinformation"]; tag_subinformation != nil {
  172. subclass = gconv.Strings(tag_subinformation)
  173. } else if tag_subinformation_ai := tmp["tag_subinformation_ai"]; tag_subinformation_ai != nil {
  174. subclass = gconv.Strings(tag_subinformation_ai)
  175. }
  176. //TODO 情报库信息(待补充)
  177. information_id := ""
  178. starttime, endtime := int64(0), int64(0)
  179. //if project_bidstatus == 4 {//补充情报信息
  180. // //bidId = "65fbf3f566cf0db42a2a99d2"
  181. // info := FindInfomationData(id) //情报信息查询
  182. // information_id = info.Id
  183. // starttime = info.Starttime
  184. // endtime = info.Endtime
  185. //}
  186. //TODO 查询法人库信息(待补充)
  187. winners := []string{}
  188. if winner != "" {
  189. winners = strings.Split(winner, ",")
  190. }
  191. buyer_id, agency_id := "", ""
  192. winner_ids := []string{}
  193. //buyer_id, agency_id, winner_ids := FindEntInfoData(id, buyer, agency, winners)
  194. //物业信息
  195. t := &Transaction{
  196. Project_Id: id,
  197. Project_Name: gconv.String(tmp["projectname"]),
  198. Project_Budget: budget,
  199. Project_Bidamount: bidamount,
  200. Project_Money: money,
  201. Business_Type: business_type,
  202. Project_Bidstatus: project_bidstatus,
  203. Info_Id: id,
  204. Info_Ids: []string{id},
  205. Information_Id: information_id,
  206. Buyer: buyer,
  207. Winner: winners,
  208. Agency: agency,
  209. Buyer_Id: buyer_id,
  210. Winner_Id: winner_ids,
  211. Agency_Id: agency_id,
  212. Property_Form: property_form,
  213. SubClass: subclass,
  214. MultiPackage: gconv.Int(tmp["multipackage"]),
  215. Area: gconv.String(tmp["area"]),
  216. City: gconv.String(tmp["city"]),
  217. District: gconv.String(tmp["district"]),
  218. ZbTime: gconv.Int64(tmp["publishtime"]),
  219. JgTime: int64(0),
  220. StartTime: starttime,
  221. EndTime: endtime,
  222. Create_Time: time.Now().Unix(),
  223. Update_Time: time.Now().Unix(),
  224. //
  225. From: "bidding",
  226. }
  227. result := map[string]interface{}{}
  228. infomation, _ := bson.Marshal(t)
  229. bson.Unmarshal(infomation, &result)
  230. return result
  231. }
  232. // IncTransactionDataFromProject 增量project
  233. func IncTransactionDataFromPro() {
  234. endTime := GetTime(-1) //前一天凌晨
  235. fmt.Println("开始执行增量项目信息", ProStartTime, endTime)
  236. if ProStartTime >= endTime {
  237. fmt.Println("增量项目信息查询异常:", ProStartTime, endTime)
  238. return
  239. }
  240. query := map[string]interface{}{
  241. "pici": map[string]interface{}{
  242. "$gte": ProStartTime,
  243. "$lt": endTime,
  244. },
  245. }
  246. fmt.Println("增量项目查询query:", query)
  247. sess := MgoPro.GetMgoConn()
  248. defer MgoPro.DestoryMongoConn(sess)
  249. ch := make(chan bool, 5)
  250. wg := &sync.WaitGroup{}
  251. lock := &sync.Mutex{}
  252. fields := map[string]interface{}{
  253. "projectname": 1,
  254. "budget": 1,
  255. "bidamount": 1,
  256. "buyer": 1,
  257. "s_winner": 1,
  258. "agency": 1,
  259. "property_form": 1,
  260. "multipackage": 1,
  261. "area": 1,
  262. "city": 1,
  263. "district": 1,
  264. "zbtime": 1,
  265. "jgtime": 1,
  266. "bidstatus": 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": 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. 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. business_type = "招标项目"
  360. }
  361. //查询情报信息
  362. //bidId = "65fbf3f566cf0db42a2a99d2"
  363. ids := gconv.Strings(data["ids"])
  364. info := FindInfomationData(ids...) //情报信息查询
  365. //TODO 查询法人库信息(待补充)
  366. winners := []string{}
  367. if winner != "" {
  368. winners = strings.Split(winner, ",")
  369. }
  370. buyer_id, agency_id := "", ""
  371. winner_ids := []string{}
  372. //buyer_id, agency_id, winner_ids = FindEntInfoData(id, buyer, agency, winners)
  373. //物业信息
  374. t := &Transaction{
  375. Project_Id: id,
  376. Project_Name: gconv.String(data["projectname"]),
  377. Project_Budget: budget,
  378. Project_Bidamount: bidamount,
  379. Project_Money: money,
  380. Business_Type: business_type,
  381. Project_Bidstatus: project_bidstatus,
  382. Info_Id: gconv.String(data["sourceinfoid"]),
  383. Info_Ids: ids,
  384. Information_Id: info.Id,
  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, 2)
  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. }
  592. tmp[k] = v
  593. }
  594. err, result := Es.GetById(Config.Es.Index, gconv.String(tmp["_id"]))
  595. if err == nil && len(result) > 0 { //存在,更新
  596. tmp["create_time"] = result["create_time"] //不更新create_time
  597. }
  598. return Es.Save(Config.Es.Index, tmp)
  599. }
  600. func FindEntInfoData2(bid, buyer, agency string, winners []string) (buyer_id, agency_id string, winner_ids []string) {
  601. query := fmt.Sprintf(`SELECT id FROM %s WHERE company_name = ?`, Config.ClickHouse.DataBase+".ent_info")
  602. if buyer != "" {
  603. buyer_id = GetClickHouseData(bid, query, buyer)
  604. }
  605. if agency != "" {
  606. agency_id = GetClickHouseData(bid, query, agency)
  607. }
  608. if len(winners) > 0 {
  609. for _, w := range winners {
  610. winner_id := GetClickHouseData(bid, query, w)
  611. if winner_id != "" {
  612. winner_ids = append(winner_ids, winner_id)
  613. }
  614. }
  615. }
  616. return
  617. }
  618. func GetClickHouseData(bid, query, value string) string {
  619. rows, err := CkhTool.Query(context.Background(), query, value)
  620. if err != nil {
  621. return ""
  622. }
  623. for rows.Next() {
  624. var id string
  625. if err := rows.Scan(&id); err == nil {
  626. return id
  627. } else {
  628. fmt.Println("查询情报信息异常:", err, bid)
  629. }
  630. }
  631. return ""
  632. }
  633. /*// SaveTransactionData 保存增量物业信息
  634. func SaveTransactionData() {
  635. fmt.Println("save projectset_wy...")
  636. savearr := make([]map[string]interface{}, 100)
  637. indexdb := 0
  638. for {
  639. select {
  640. case v := <-TransactionSaveCache:
  641. savearr[indexdb] = v
  642. indexdb++
  643. if indexdb == 100 {
  644. Transaction_Ch <- true
  645. go func(tmp []map[string]interface{}) {
  646. defer func() {
  647. <-Transaction_Ch
  648. }()
  649. MgoPro.SaveBulk("projectset_wy", tmp...)
  650. }(savearr)
  651. savearr = make([]map[string]interface{}, 100)
  652. indexdb = 0
  653. }
  654. case <-time.After(30 * time.Second):
  655. if indexdb > 0 {
  656. Transaction_Ch <- true
  657. go func(tmp []map[string]interface{}) {
  658. defer func() {
  659. <-Transaction_Ch
  660. }()
  661. MgoPro.SaveBulk("projectset_wy", tmp...)
  662. }(savearr[:indexdb])
  663. savearr = make([]map[string]interface{}, 100)
  664. indexdb = 0
  665. }
  666. }
  667. }
  668. }*/