task.go 20 KB

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