xlsx_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  6. "log"
  7. "strings"
  8. "testing"
  9. )
  10. func TestA(T *testing.T) {
  11. Mgo := &mongodb.MongodbSim{
  12. //MongodbAddr: "172.17.189.140:27080",
  13. MongodbAddr: "127.0.0.1:27017",
  14. Size: 10,
  15. DbName: "wcc",
  16. //UserName: "SJZY_RWbid_ES",
  17. //Password: "SJZY@B4i4D5e6S",
  18. //Direct: true,
  19. }
  20. Mgo.InitPool()
  21. f, err := excelize.OpenFile("./拼数据.xlsx")
  22. if err != nil {
  23. fmt.Println(err)
  24. return
  25. }
  26. defer func() {
  27. if err := f.Close(); err != nil {
  28. fmt.Println(err)
  29. }
  30. }()
  31. rows, err := f.GetRows("Sheet1")
  32. if err != nil {
  33. fmt.Println(err)
  34. return
  35. }
  36. cells, _ := f.GetCols("Sheet2")
  37. for i := 1; i < len(rows); i++ {
  38. log.Print(rows[i][3])
  39. if rows[i][2] == "省级" {
  40. for k, v := range cells[0] {
  41. if k == 0 || v == "" {
  42. continue
  43. }
  44. insert := make(map[string]interface{})
  45. insert["top"] = rows[i][0]
  46. insert["area"] = rows[i][3]
  47. insert["level"] = rows[i][2]
  48. insert["sub"] = v
  49. insert["name"] = rows[i][3] + v
  50. Mgo.InsertOrUpdate("wcc", "wcc_0313", insert)
  51. }
  52. }
  53. if rows[i][2] == "市级" {
  54. for k, v := range cells[1] {
  55. if k == 0 || v == "" {
  56. continue
  57. }
  58. insert := make(map[string]interface{})
  59. insert["top"] = rows[i][0]
  60. insert["area"] = rows[i][3]
  61. insert["city"] = rows[i][4]
  62. insert["level"] = rows[i][2]
  63. insert["sub"] = v
  64. insert["name"] = rows[i][4] + v
  65. Mgo.InsertOrUpdate("wcc", "wcc_0313", insert)
  66. }
  67. }
  68. if rows[i][2] == "县级" {
  69. for k, v := range cells[2] {
  70. if k == 0 || v == "" {
  71. continue
  72. }
  73. insert := make(map[string]interface{})
  74. insert["top"] = rows[i][0]
  75. insert["area"] = rows[i][3]
  76. insert["city"] = rows[i][4]
  77. insert["district"] = rows[i][5]
  78. insert["level"] = rows[i][2]
  79. insert["sub"] = v
  80. insert["name"] = rows[i][4] + rows[i][5] + v
  81. Mgo.InsertOrUpdate("wcc", "wcc_0313", insert)
  82. }
  83. }
  84. }
  85. fmt.Println("over")
  86. }
  87. func TestB(T *testing.T) {
  88. Mgo := &mongodb.MongodbSim{
  89. //MongodbAddr: "172.17.189.140:27080",
  90. MongodbAddr: "127.0.0.1:27083",
  91. Size: 10,
  92. DbName: "qfw",
  93. UserName: "SJZY_RWbid_ES",
  94. Password: "SJZY@B4i4D5e6S",
  95. Direct: true,
  96. }
  97. Mgo.InitPool()
  98. f, err := excelize.OpenFile("./剑鱼标讯_数据导出.xlsx")
  99. if err != nil {
  100. fmt.Println(err)
  101. return
  102. }
  103. defer func() {
  104. if err := f.Close(); err != nil {
  105. fmt.Println(err)
  106. }
  107. }()
  108. rows, err := f.GetRows("Sheet1")
  109. if err != nil {
  110. fmt.Println(err)
  111. return
  112. }
  113. for i := 1; i < len(rows); i++ {
  114. url := rows[i][2]
  115. log.Println(rows[i][1])
  116. id := GetIdByURL(url)
  117. re, _ := Mgo.FindById("bidding", id, nil)
  118. (*re)["wcc_id"] = i
  119. (*re)["toptype_old"] = (*re)["toptype"]
  120. (*re)["subtype_old"] = (*re)["subtype"]
  121. delete(*re, "toptype")
  122. delete(*re, "subtype")
  123. Mgo.SaveByOriID("wcc_bidding_0518", re)
  124. }
  125. log.Println("over")
  126. }
  127. // TestAAA 处理专项债券数据
  128. func TestAAA(t *testing.T) {
  129. //87 竞品
  130. Mgo := &mongodb.MongodbSim{
  131. //MongodbAddr: "172.17.189.140:27080",
  132. MongodbAddr: "127.0.0.1:27081",
  133. Size: 10,
  134. DbName: "py_theme",
  135. UserName: "",
  136. Password: "",
  137. Direct: true,
  138. }
  139. Mgo.InitPool()
  140. sess := Mgo.GetMgoConn()
  141. defer Mgo.DestoryMongoConn(sess)
  142. f, err := excelize.OpenFile("./河南新增专项债-06.xlsx")
  143. if err != nil {
  144. fmt.Println(err)
  145. return
  146. }
  147. defer func() {
  148. f.Save()
  149. if err := f.Close(); err != nil {
  150. fmt.Println(err)
  151. }
  152. }()
  153. rows, err := f.GetRows("项目列表")
  154. if err != nil {
  155. fmt.Println(err)
  156. return
  157. }
  158. keys := []string{"projectName", "totalInvestment", "regionName", "cityName", "countyName", "capital", "applyDebt", "portfolioFinancing", "specialDebtAsCapital",
  159. "expectedReturn", "projectCost", "projectTypeName3", "projectSubject", "startDate", "endDate", "operationStartDate",
  160. "operationEndDate", "sourceIncome", "constructionContent", "remarks", "portfolioFinancingSource", "costIncomePercent", "coverageMultiple", "implementingAgency",
  161. "accountingFirm", "lawFirm", "createTime", "updateTime"}
  162. titles := []string{"项目名称", "总投资(万元)", "省份", "地市", "区县", "不含专项债资本金(万元)", "申请专项债总额(万元)", "其他债务融资(万元)", "专项债作资本金(万元)", "预期收入(万元)", "成本",
  163. "项目领域", "项目业主", "建设期开始", "建设期结束", "运营期开始", "运营期结束", "收入来源", "建设内容", "特殊情况备注", "其他债务融资来源", "成本/收入(%)", "覆盖倍数", "主管部门", "会计所", "律所", "入库时间", "更新时间"}
  164. keys2 := []string{"bondName", "bondShortName", "bondNo", "regionName", "bondType1Name", "bondType2Name", "projectType1Name", "totalAmount", "issueDate", "issuePlaceName", "issueTerm", "issueInterestRate", "issuePhase", "payInterestMethodName",
  165. "newBondAmount", "counterBondAmount", "refinancingBondAmount", "redemptionMethod", "valueDate", "expiryDate", "payInterestDate", "latelyPayInterestDate", "lastPayInterest", "cumulativePayInterest"}
  166. titles2 := []string{"项目名称", "债券名称", "债券简称", "债券编码", "省份", "债券性质", "债券类型", "官方项目类型", "发行金额(万元)", "发行日期", "发行场所", "发行期限(年)", "发行利率(%)", "发行期数", "付息方式", "新增债券(亿元)", "置换债券(亿元)", "再融资债券(亿元)", "赎回方式",
  167. "起息日", "到息日", "付息日", "最近付息日", "上期已付息(亿元)", "累计付息(亿元)"}
  168. line := 0
  169. line++
  170. f.NewSheet("项目详情")
  171. f.NewSheet("债券详情")
  172. bondLine := 0
  173. //设置第一行title
  174. _ = f.SetSheetRow("项目详情", fmt.Sprintf("%s%d", "A", line), &titles)
  175. _ = f.SetSheetRow("债券详情", fmt.Sprintf("%s%d", "A", line), &titles2)
  176. //
  177. for i := 1; i < len(rows); i++ {
  178. projectName := rows[i][0]
  179. //projectName := "新建南通至宁波高速铁路(海盐段)"
  180. log.Println(projectName)
  181. line++
  182. where := map[string]interface{}{
  183. "project.jcxx.projectName": projectName,
  184. }
  185. detail, _ := Mgo.FindOne("specialbond_detail_202406", where)
  186. if len(*detail) == 0 {
  187. continue
  188. }
  189. insert := make([]interface{}, 0)
  190. if project, ok := (*detail)["project"].(map[string]interface{}); ok {
  191. if jcxx, ok := project["jcxx"].(map[string]interface{}); ok {
  192. for _, v := range keys {
  193. insert = append(insert, jcxx[v])
  194. }
  195. }
  196. }
  197. err := f.SetSheetRow("项目详情", fmt.Sprintf("%s%d", "A", line), &insert)
  198. if err != nil {
  199. log.Println(err)
  200. return
  201. }
  202. // 债券基本信息
  203. if bonds, ok := (*detail)["bond"].([]interface{}); ok {
  204. for _, bond := range bonds {
  205. if bondData, ok := bond.(map[string]interface{}); ok {
  206. if jcxx, ok := bondData["jbxx"].(map[string]interface{}); ok {
  207. //if strings.Contains(util.ObjToString(jcxx["bondName"]), "2024") {
  208. bondInsert := make([]interface{}, 0)
  209. bondInsert = append(bondInsert, projectName)
  210. for _, v := range keys2 {
  211. bondInsert = append(bondInsert, jcxx[v])
  212. }
  213. bondLine++
  214. f.SetSheetRow("债券详情", fmt.Sprintf("%s%d", "A", line), &bondInsert)
  215. //}
  216. }
  217. }
  218. }
  219. }
  220. }
  221. //query := sess.DB("py_theme").C("specialbond_detail").Find(nil).Select(nil).Iter()
  222. //count := 0
  223. //for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  224. // if count%10000 == 0 {
  225. // log.Println("current:", count)
  226. // }
  227. //
  228. // id := mongodb.BsonIdToSId(tmp["_id"])
  229. // log.Println(id)
  230. //}
  231. }
  232. func TestGetIdByURL(t *testing.T) {
  233. url := "https://www.jianyu360.cn/article/content/AHnY1xBfikoAjYsNGd4cE8JIzAvFj1jcXNlKDhROC4eWlFwA15UCZQ%3D.html"
  234. fmt.Println(GetIdByURL(url))
  235. }
  236. // TestArea 中国行政区划数据-民政部
  237. func TestArea(t *testing.T) {
  238. Mgo := &mongodb.MongodbSim{
  239. //MongodbAddr: "172.17.189.140:27080",
  240. MongodbAddr: "127.0.0.1:27083",
  241. Size: 10,
  242. DbName: "qfw",
  243. UserName: "SJZY_RWbid_ES",
  244. Password: "SJZY@B4i4D5e6S",
  245. Direct: true,
  246. }
  247. Mgo.InitPool()
  248. f, err := excelize.OpenFile("./20240530_民政部统计数据.xlsx")
  249. if err != nil {
  250. fmt.Println(err)
  251. return
  252. }
  253. defer func() {
  254. if err := f.Close(); err != nil {
  255. fmt.Println(err)
  256. }
  257. }()
  258. rows, err := f.GetRows("2023")
  259. if err != nil {
  260. fmt.Println(err)
  261. return
  262. }
  263. for i := 1; i < len(rows); i++ {
  264. data := map[string]interface{}{}
  265. if rows[i][0] == "省份" {
  266. data["level"] = 1
  267. } else if rows[i][0] == "城市" {
  268. data["level"] = 2
  269. } else {
  270. data["level"] = 3
  271. }
  272. log.Println(rows[i][1])
  273. parts := strings.Split(rows[i][1], "(")
  274. data["province"] = parts[0]
  275. data["province_code"] = rows[i][2]
  276. if len(rows[i]) > 3 {
  277. data["city"] = rows[i][3]
  278. }
  279. if len(rows[i]) > 4 {
  280. data["city_code"] = rows[i][4]
  281. }
  282. if len(rows[i]) > 5 {
  283. data["area"] = rows[i][5]
  284. }
  285. if len(rows[i]) > 6 {
  286. data["area_code"] = rows[i][6]
  287. }
  288. Mgo.Save("wcc_area_minzhengbu", data)
  289. }
  290. }
  291. // TestArea2 中国行政区划数据-统计局
  292. func TestArea2(t *testing.T) {
  293. Mgo := &mongodb.MongodbSim{
  294. //MongodbAddr: "172.17.189.140:27080",
  295. MongodbAddr: "127.0.0.1:27083",
  296. Size: 10,
  297. DbName: "qfw",
  298. UserName: "SJZY_RWbid_ES",
  299. Password: "SJZY@B4i4D5e6S",
  300. Direct: true,
  301. }
  302. Mgo.InitPool()
  303. f, err := excelize.OpenFile("./20240530_统计局统计数据.xlsx")
  304. if err != nil {
  305. fmt.Println(err)
  306. return
  307. }
  308. defer func() {
  309. if err := f.Close(); err != nil {
  310. fmt.Println(err)
  311. }
  312. }()
  313. rows, err := f.GetRows("2023")
  314. if err != nil {
  315. fmt.Println(err)
  316. return
  317. }
  318. for i := 1; i < len(rows); i++ {
  319. data := map[string]interface{}{}
  320. data["level"] = rows[i][0]
  321. log.Println(rows[i][1], rows[3])
  322. data["province"] = rows[i][1]
  323. data["province_code"] = rows[i][2]
  324. if len(rows[i]) > 3 {
  325. data["city"] = rows[i][3]
  326. }
  327. if len(rows[i]) > 4 {
  328. data["city_code"] = rows[i][4]
  329. }
  330. if len(rows[i]) > 5 {
  331. data["area"] = rows[i][5]
  332. }
  333. if len(rows[i]) > 6 {
  334. data["area_code"] = rows[i][6]
  335. }
  336. Mgo.Save("wcc_area_tongjiju", data)
  337. }
  338. }
  339. func TestHN(t *testing.T) {
  340. Mgo := &mongodb.MongodbSim{
  341. //MongodbAddr: "172.17.189.140:27080",
  342. MongodbAddr: "127.0.0.1:27083",
  343. Size: 10,
  344. DbName: "qfw",
  345. UserName: "SJZY_RWbid_ES",
  346. Password: "SJZY@B4i4D5e6S",
  347. Direct: true,
  348. }
  349. Mgo.InitPool()
  350. //181
  351. Mgo2 := &mongodb.MongodbSim{
  352. //MongodbAddr: "172.17.4.181:27001",
  353. MongodbAddr: "127.0.0.1:27001",
  354. DbName: "mixdata",
  355. Size: 10,
  356. UserName: "",
  357. Password: "",
  358. Direct: true,
  359. }
  360. Mgo2.InitPool()
  361. f, err := excelize.OpenFile("./河南单位.xlsx")
  362. if err != nil {
  363. fmt.Println(err)
  364. return
  365. }
  366. defer func() {
  367. if err := f.Close(); err != nil {
  368. fmt.Println(err)
  369. }
  370. }()
  371. sheet := "单位"
  372. rows, err := f.GetRows(sheet)
  373. if err != nil {
  374. fmt.Println(err)
  375. return
  376. }
  377. for i := 1; i < len(rows); i++ {
  378. name := rows[i][0]
  379. log.Println(i, "----", name)
  380. //res, _ := Mgo2.FindOne("special_enterprise", map[string]interface{}{"company_name": name})
  381. //if res != nil && len(*res) > 0 {
  382. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "E", i+1), "special_enterprise")
  383. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "F", i+1), (*res)["company_status"])
  384. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "G", i+1), (*res)["company_type"])
  385. //} else {
  386. // ra, _ := Mgo2.FindOne("special_gov_unit", map[string]interface{}{"company_name": name})
  387. // if ra != nil && len(*ra) > 0 {
  388. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "E", i+1), "special_gov_unit")
  389. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "G", i+1), (*ra)["company_type"])
  390. // if util.ObjToString((*ra)["company_status"]) != "" {
  391. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "F", i+1), (*ra)["company_status"])
  392. // } else {
  393. // if util.IntAll((*ra)["use_flag"]) < 5 {
  394. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "F", i+1), "正常")
  395. // } else {
  396. // f.SetCellValue(sheet, fmt.Sprintf("%s%d", "F", i+1), "废弃")
  397. // }
  398. // }
  399. // }
  400. //}
  401. //
  402. data := map[string]interface{}{
  403. "detail": name,
  404. }
  405. res := getAreaInfo(data)
  406. if res != nil {
  407. city := res["city"]
  408. district := res["district"]
  409. f.SetCellValue("单位", fmt.Sprintf("%s%d", "B", i+1), city)
  410. f.SetCellValue("单位", fmt.Sprintf("%s%d", "C", i+1), district)
  411. }
  412. }
  413. f.Save()
  414. log.Println("结束")
  415. }