task.go 21 KB

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