task.go 21 KB

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