task.go 21 KB

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