task.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/util/gconv"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. type Transaction struct {
  13. Project_Id string `bson:"project_id"`
  14. Project_Name string `bson:"project_name"`
  15. Project_Budget float64 `bson:"project_budget"`
  16. Project_Bidamount float64 `bson:"project_bidamount"`
  17. Project_Money float64 `bson:"project_money"`
  18. Business_Type string `bson:"business_type"`
  19. Project_Bidstatus int `bson:"project_bidstatus"`
  20. Info_Id string `bson:"info_id"`
  21. Info_Ids []string `bson:"info_ids"`
  22. Information_Id string `bson:"information_id"`
  23. 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. business_type = "招标项目"
  364. }
  365. //查询情报信息
  366. //bidId = "65fbf3f566cf0db42a2a99d2"
  367. ids := gconv.Strings(data["ids"])
  368. info := FindInfomationData(ids...) //情报信息查询
  369. //TODO 查询法人库信息(待补充)
  370. winners := []string{}
  371. if winner != "" {
  372. winners = strings.Split(winner, ",")
  373. }
  374. buyer_id, agency_id := "", ""
  375. winner_ids := []string{}
  376. //buyer_id, agency_id, winner_ids = FindEntInfoData(id, buyer, agency, winners)
  377. //物业信息
  378. t := &Transaction{
  379. Project_Id: id,
  380. Project_Name: gconv.String(data["projectname"]),
  381. Project_Budget: budget,
  382. Project_Bidamount: bidamount,
  383. Project_Money: money,
  384. Business_Type: business_type,
  385. Project_Bidstatus: project_bidstatus,
  386. Info_Id: gconv.String(data["sourceinfoid"]),
  387. Info_Ids: ids,
  388. Information_Id: info.Id,
  389. Buyer: buyer,
  390. Winner: winners,
  391. Agency: agency,
  392. Buyer_Id: buyer_id,
  393. Winner_Id: winner_ids,
  394. Agency_Id: agency_id,
  395. Property_Form: property_form,
  396. SubClass: subclass,
  397. MultiPackage: gconv.Int(data["multipackage"]),
  398. Area: gconv.String(data["area"]),
  399. City: gconv.String(data["city"]),
  400. District: gconv.String(data["district"]),
  401. ZbTime: zbtime,
  402. JgTime: gconv.Int64(data["jgtime"]),
  403. StartTime: info.Starttime,
  404. EndTime: info.Endtime,
  405. Create_Time: time.Now().Unix(),
  406. Update_Time: time.Now().Unix(),
  407. //
  408. From: "project",
  409. }
  410. result := map[string]interface{}{}
  411. infomation, _ := bson.Marshal(t)
  412. bson.Unmarshal(infomation, &result)
  413. return result
  414. }
  415. // IncTransactionDataMgoToCkhAndEs 数据迁移
  416. func IncTransactionDataMgoToCkhAndEs() {
  417. /*
  418. 数据根据update_time查询
  419. 1、采购意向数据(from=bidding)只插入
  420. 2、项目信息先查,有则更新,无则插入
  421. */
  422. fmt.Println("开始执行迁移...")
  423. sess := MgoPro.GetMgoConn()
  424. defer MgoPro.DestoryMongoConn(sess)
  425. ch := make(chan bool, 2)
  426. wg := &sync.WaitGroup{}
  427. query := map[string]interface{}{
  428. "update_time": map[string]interface{}{
  429. "$gte": GetTime(0),
  430. },
  431. }
  432. it := sess.DB(MgoPro.DbName).C("projectset_wy").Find(&query).Iter()
  433. n := 0
  434. for tmp := make(map[string]interface{}); it.Next(tmp); n++ {
  435. ch <- true
  436. wg.Add(1)
  437. go func(tmp map[string]interface{}) {
  438. defer func() {
  439. <-ch
  440. wg.Done()
  441. }()
  442. from := gconv.String(tmp["from"])
  443. delete(tmp, "from") //无用字段删除
  444. delete(tmp, "_id") //无用字段删除
  445. if !SaveDataToEs(tmp) { //保存、更新es
  446. fmt.Println("数据保存es失败,项目project_id", tmp["project_id"])
  447. }
  448. if from == "bidding" { //采购意向、拟建,插入
  449. SaveDataToClickHouse(tmp)
  450. } else { //项目信息,更新,插入
  451. UpdateOrSaveDataToClickHouse(tmp)
  452. }
  453. }(tmp)
  454. if n%100 == 0 {
  455. fmt.Println("current:", n)
  456. }
  457. tmp = map[string]interface{}{}
  458. }
  459. wg.Wait()
  460. fmt.Println("迁移结束...")
  461. }
  462. type Infomation struct {
  463. Id string
  464. Starttime int64
  465. Endtime int64
  466. }
  467. // FindInfomationData 情报信息查询
  468. func FindInfomationData(ids ...string) (info Infomation) {
  469. for _, id := range ids {
  470. query := fmt.Sprintf(`SELECT id,starttime,endtime FROM %s WHERE datajson_id = ?`, Config.ClickHouse.DataBase+".information")
  471. rows, err := CkhTool.Query(context.Background(), query, id)
  472. if err != nil {
  473. continue
  474. }
  475. for rows.Next() {
  476. info = Infomation{}
  477. if err := rows.Scan(&info.Id, &info.Starttime, &info.Endtime); err != nil {
  478. fmt.Println("查询情报信息异常:", id, err)
  479. }
  480. if info.Id != "" {
  481. return
  482. }
  483. //break //目前只有一条结果
  484. }
  485. }
  486. return
  487. }
  488. // FindEntInfoData 法人信息查询
  489. func FindEntInfoData(bid, buyer, agency string, winners []string) (buyer_id, agency_id string, winner_ids []string) {
  490. winner_ids = []string{}
  491. winnerMap := map[string]bool{} //记录所有中标单位
  492. values := []interface{}{}
  493. placeholders := []string{}
  494. if buyer != "" {
  495. placeholders = append(placeholders, "?")
  496. values = append(values, buyer)
  497. }
  498. if len(winners) > 0 {
  499. for _, w := range winners {
  500. winnerMap[w] = true
  501. placeholders = append(placeholders, "?")
  502. values = append(values, w)
  503. }
  504. }
  505. if agency != "" {
  506. placeholders = append(placeholders, "?")
  507. values = append(values, agency)
  508. }
  509. if len(values) == 0 {
  510. return
  511. }
  512. query := fmt.Sprintf(`SELECT id,company_name FROM %s WHERE company_name IN (%s)`, Config.ClickHouse.DataBase+".ent_info", strings.Join(placeholders, ","))
  513. rows, err := CkhTool.Query(context.Background(), query, values...)
  514. if err != nil {
  515. return
  516. }
  517. for rows.Next() {
  518. var id, company_name string
  519. if err := rows.Scan(&id, &company_name); err == nil {
  520. if company_name == buyer {
  521. buyer_id = id
  522. } else if company_name == agency {
  523. agency_id = id
  524. } else if winnerMap[company_name] {
  525. winner_ids = append(winner_ids, id)
  526. }
  527. } else {
  528. fmt.Println("查询情报信息异常:", err, bid)
  529. }
  530. }
  531. return
  532. }
  533. // UpdateOrSaveDataToClickHouse 判断clickhouse更新or保存
  534. func UpdateOrSaveDataToClickHouse(data map[string]interface{}) (err error) {
  535. project_id := gconv.String(data["project_id"])
  536. count := FindClickHouseByProjectId(project_id) //查询
  537. if count > 0 { //更新
  538. delete(data, "create_time") //不更新创建时间
  539. delete(data, "project_id") //不更新项目id(主键)
  540. err = UpdateDataToClickHouse(data, map[string]interface{}{"project_id": project_id})
  541. if err != nil {
  542. fmt.Println("clickhouse更新失败", project_id, data)
  543. }
  544. } else { //插入
  545. err = SaveDataToClickHouse(data)
  546. if err != nil {
  547. fmt.Println("clickhouse保存失败", project_id, data)
  548. }
  549. }
  550. return
  551. }
  552. // SaveDataToClickHouse 数据保存clickhouse
  553. func SaveDataToClickHouse(data map[string]interface{}) error {
  554. fields, placeholders := []string{}, []string{}
  555. values := []interface{}{}
  556. for k, v := range data {
  557. fields = append(fields, k)
  558. values = append(values, v)
  559. placeholders = append(placeholders, "?")
  560. }
  561. query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", Config.ClickHouse.DataBase+".transaction_info", strings.Join(fields, ","), strings.Join(placeholders, ","))
  562. return CkhTool.Exec(context.Background(), query, values...)
  563. }
  564. // FindClickHouseByProjectId 根据条件count clickhouse
  565. func FindClickHouseByProjectId(project_id string) int {
  566. query := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE project_id = ?`, Config.ClickHouse.DataBase+".transaction_info")
  567. row := CkhTool.QueryRow(context.Background(), query, project_id)
  568. var count uint64
  569. row.Scan(&count)
  570. return gconv.Int(count)
  571. }
  572. // UpdateDataToClickHouse 数据更新clickhouse
  573. func UpdateDataToClickHouse(data, querys map[string]interface{}) error {
  574. sets := []string{}
  575. values := []interface{}{}
  576. for k, v := range data {
  577. sets = append(sets, fmt.Sprintf("%s=?", k))
  578. values = append(values, v)
  579. }
  580. qs := []string{}
  581. for k, v := range querys {
  582. qs = append(qs, fmt.Sprintf("%s=?", k))
  583. values = append(values, v)
  584. }
  585. query := fmt.Sprintf("ALTER TABLE %s UPDATE %s WHERE %s", Config.ClickHouse.DataBase+".transaction_info", strings.Join(sets, ","), strings.Join(qs, ","))
  586. //query := `ALTER TABLE information.transaction_info UPDATE update_time = ? WHERE project_id = '5c9ee78ca5cb26b9b7fd0b57'`
  587. return CkhTool.Exec(context.Background(), query, values...)
  588. }
  589. // SaveDataToEs es存储
  590. func SaveDataToEs(data map[string]interface{}) bool {
  591. tmp := map[string]interface{}{}
  592. for k, v := range data {
  593. if k == "project_id" {
  594. k = "_id"
  595. }
  596. tmp[k] = v
  597. }
  598. err, result := Es.GetById(Config.Es.Index, gconv.String(tmp["_id"]))
  599. if err == nil && len(result) > 0 { //存在,更新
  600. tmp["create_time"] = result["create_time"] //不更新create_time
  601. }
  602. return Es.Save(Config.Es.Index, tmp)
  603. }
  604. func FindEntInfoData2(bid, buyer, agency string, winners []string) (buyer_id, agency_id string, winner_ids []string) {
  605. query := fmt.Sprintf(`SELECT id FROM %s WHERE company_name = ?`, Config.ClickHouse.DataBase+".ent_info")
  606. if buyer != "" {
  607. buyer_id = GetClickHouseData(bid, query, buyer)
  608. }
  609. if agency != "" {
  610. agency_id = GetClickHouseData(bid, query, agency)
  611. }
  612. if len(winners) > 0 {
  613. for _, w := range winners {
  614. winner_id := GetClickHouseData(bid, query, w)
  615. if winner_id != "" {
  616. winner_ids = append(winner_ids, winner_id)
  617. }
  618. }
  619. }
  620. return
  621. }
  622. func GetClickHouseData(bid, query, value string) string {
  623. rows, err := CkhTool.Query(context.Background(), query, value)
  624. if err != nil {
  625. return ""
  626. }
  627. for rows.Next() {
  628. var id string
  629. if err := rows.Scan(&id); err == nil {
  630. return id
  631. } else {
  632. fmt.Println("查询情报信息异常:", err, bid)
  633. }
  634. }
  635. return ""
  636. }
  637. /*// SaveTransactionData 保存增量物业信息
  638. func SaveTransactionData() {
  639. fmt.Println("save projectset_wy...")
  640. savearr := make([]map[string]interface{}, 100)
  641. indexdb := 0
  642. for {
  643. select {
  644. case v := <-TransactionSaveCache:
  645. savearr[indexdb] = v
  646. indexdb++
  647. if indexdb == 100 {
  648. Transaction_Ch <- true
  649. go func(tmp []map[string]interface{}) {
  650. defer func() {
  651. <-Transaction_Ch
  652. }()
  653. MgoPro.SaveBulk("projectset_wy", tmp...)
  654. }(savearr)
  655. savearr = make([]map[string]interface{}, 100)
  656. indexdb = 0
  657. }
  658. case <-time.After(30 * time.Second):
  659. if indexdb > 0 {
  660. Transaction_Ch <- true
  661. go func(tmp []map[string]interface{}) {
  662. defer func() {
  663. <-Transaction_Ch
  664. }()
  665. MgoPro.SaveBulk("projectset_wy", tmp...)
  666. }(savearr[:indexdb])
  667. savearr = make([]map[string]interface{}, 100)
  668. indexdb = 0
  669. }
  670. }
  671. }
  672. }*/