main.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "go.mongodb.org/mongo-driver/bson"
  6. utils "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "log"
  9. "strings"
  10. "time"
  11. )
  12. func main() {
  13. //getWinnerArea()
  14. //winnerCity()
  15. //test1()
  16. statisticWinner()
  17. }
  18. // getWinnerArea 获取中标单位所在区域
  19. func getWinnerArea() {
  20. /**
  21. 本次只获取 北京、天津、河北三个地区
  22. */
  23. MgoP := &mongodb.MongodbSim{
  24. //MongodbAddr: "127.0.0.1:27080",
  25. MongodbAddr: "172.17.4.85:27080",
  26. DbName: "qfw",
  27. Size: 10,
  28. //Direct: true,
  29. }
  30. MgoP.InitPool()
  31. // 定义年份的时间范围
  32. year := 2023
  33. now := time.Now()
  34. startTime := time.Date(year, 1, 1, 0, 0, 0, 0, now.Location())
  35. endTime := time.Date(year+1, 1, 1, 0, 0, 0, 0, now.Location())
  36. where := map[string]interface{}{
  37. "firsttime": bson.M{
  38. "$gte": startTime.Unix(),
  39. "$lt": endTime.Unix(),
  40. },
  41. }
  42. type WinnerArea struct {
  43. Winner string `json:"winner"`
  44. Area string `json:"area"`
  45. City string `json:"city"`
  46. District string `json:"district"`
  47. UniqueKey string `json:"unique_key"`
  48. WinnerProvince string `json:"winner_province"`
  49. WinnerCity string `json:"winner_city"`
  50. WinnerDistrict string `json:"winner_district"`
  51. Count int `json:"count"`
  52. }
  53. sess := MgoP.GetMgoConn()
  54. defer MgoP.DestoryMongoConn(sess)
  55. query := sess.DB("qfw").C("projectset_20230904").Find(where).Select(map[string]interface{}{"list": 0}).Iter()
  56. count := 0
  57. areas := []string{"北京", "北京市", "天津", "天津市", "河北", "河北省"}
  58. listMap := make(map[string]WinnerArea)
  59. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  60. if count%10000 == 0 {
  61. log.Println("current ---", count, tmp["projectname"])
  62. }
  63. // 有中标单位
  64. if _, ok := tmp["s_winner"]; ok {
  65. if IsInStringArray(utils.ObjToString(tmp["area"]), areas) {
  66. key := utils.ObjToString(tmp["city"]) + "-" + utils.ObjToString(tmp["district"]) + "_" + utils.ObjToString(tmp["s_winner"])
  67. // 获取当前值
  68. windata, found := listMap[key]
  69. if !found {
  70. windata = WinnerArea{
  71. Winner: utils.ObjToString(tmp["s_winner"]),
  72. Area: utils.ObjToString(tmp["area"]),
  73. City: utils.ObjToString(tmp["city"]),
  74. District: utils.ObjToString(tmp["district"]),
  75. UniqueKey: key,
  76. Count: 1, // 初始化计数
  77. }
  78. listMap[key] = windata
  79. } else {
  80. windata.Count++
  81. listMap[key] = windata
  82. }
  83. }
  84. }
  85. }
  86. log.Println("2023年 总数是:", len(listMap))
  87. for k, _ := range listMap {
  88. data := structToMap(listMap[k])
  89. //log.Println(data)
  90. MgoP.Save("wcc_2023_winner_area_0608", data)
  91. }
  92. }
  93. // winnerCity 更新中标单位省市区
  94. func winnerCity() {
  95. // 本地
  96. //Mgo := &mongodb.MongodbSim{
  97. // MongodbAddr: "127.0.0.1:27017",
  98. // Size: 10,
  99. // DbName: "wcc",
  100. //}
  101. //Mgo.InitPool()
  102. // 线上163
  103. Mgo := &mongodb.MongodbSim{
  104. MongodbAddr: "172.17.189.140:27080",
  105. //MongodbAddr: "127.0.0.1:27083",
  106. Size: 10,
  107. DbName: "qfw",
  108. UserName: "SJZY_RWbid_ES",
  109. Password: "SJZY@B4i4D5e6S",
  110. //Direct: true,
  111. }
  112. Mgo.InitPool()
  113. sess := Mgo.GetMgoConn()
  114. defer Mgo.DestoryMongoConn(sess)
  115. coll := "wcc_2023_winner_area_0608"
  116. query := sess.DB("qfw").C(coll).Find(nil).Select(nil).Iter()
  117. count := 0
  118. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  119. if count%10000 == 0 {
  120. log.Println("current ---", count, tmp["winner"])
  121. }
  122. data := map[string]interface{}{
  123. "buyer": tmp["winner"],
  124. }
  125. res := GetAreaInfo(data)
  126. if res != nil {
  127. update := map[string]interface{}{
  128. "winner_province": res["area"],
  129. "winner_city": res["city"],
  130. "winner_district": res["district"],
  131. }
  132. Mgo.UpdateById(coll, tmp["_id"], map[string]interface{}{"$set": update})
  133. }
  134. }
  135. }
  136. // statisticWinner 统计中标单位区域分布
  137. func statisticWinner() {
  138. //cellsLine := []string{"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "P", "Q", "R", "S", "T"}
  139. f, err := excelize.OpenFile("./中标项目区域分布.xlsx")
  140. if err != nil {
  141. fmt.Println(err)
  142. return
  143. }
  144. defer func() {
  145. if err := f.Close(); err != nil {
  146. fmt.Println(err)
  147. }
  148. }()
  149. rows, err := f.GetRows("2019")
  150. if err != nil {
  151. fmt.Println(err)
  152. return
  153. }
  154. cells, _ := f.GetCols("2019")
  155. //log.Println(rows, cells)
  156. // 线上163
  157. Mgo := &mongodb.MongodbSim{
  158. //MongodbAddr: "172.17.189.140:27080",
  159. MongodbAddr: "127.0.0.1:27083",
  160. Size: 10,
  161. DbName: "qfw",
  162. UserName: "SJZY_RWbid_ES",
  163. Password: "SJZY@B4i4D5e6S",
  164. Direct: true,
  165. }
  166. Mgo.InitPool()
  167. coll := "wcc_2019_winner_area_0608"
  168. for i := 1; i < len(rows); i++ {
  169. rowData := make([]interface{}, 0)
  170. area1 := strings.Replace(rows[i][0], " ", "", -1)
  171. for j := 1; j < len(cells); j++ {
  172. area2 := strings.Replace(cells[j][0], " ", "", -1)
  173. where := make(map[string]interface{})
  174. if i < 18 {
  175. if area1 == "北京市" {
  176. where["city"] = "北京市"
  177. } else {
  178. where["city"] = "北京市"
  179. where["district"] = area1
  180. }
  181. } else if i < 35 {
  182. if area1 == "天津市" {
  183. where["city"] = "天津市"
  184. } else {
  185. where["city"] = "天津市"
  186. where["district"] = area1
  187. }
  188. } else {
  189. if area1 == "河北省" {
  190. where["area"] = "河北"
  191. } else {
  192. where["area"] = "河北"
  193. where["city"] = area1
  194. }
  195. }
  196. if j < 18 {
  197. if area2 == "北京市" {
  198. where["winner_city"] = "北京市"
  199. } else {
  200. where["winner_city"] = "北京市"
  201. where["winner_district"] = area2
  202. }
  203. } else if j < 35 {
  204. if area2 == "天津市" {
  205. where["winner_city"] = "天津市"
  206. } else {
  207. where["winner_city"] = "天津市"
  208. where["winner_district"] = area2
  209. }
  210. } else {
  211. if area2 == "河北省" {
  212. where["winner_province"] = "河北"
  213. } else {
  214. where["winner_province"] = "河北"
  215. where["winner_city"] = area2
  216. }
  217. }
  218. num := Mgo.Count(coll, where)
  219. log.Println("iiiiiii", i, area1, ",jjjjjjjj", j, area2, num)
  220. rowData = append(rowData, num)
  221. }
  222. //_ = xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "C", line), &subtitles)
  223. f.SetSheetRow("2019", fmt.Sprintf("%s%d", "B", i+1), &rowData)
  224. }
  225. f.Save()
  226. }