national_statistics.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "eta/eta_api/controllers"
  7. "eta/eta_api/models"
  8. "eta/eta_api/models/data_manage"
  9. "eta/eta_api/utils"
  10. "os"
  11. "path/filepath"
  12. "time"
  13. )
  14. // BaseFromNationalStatisticsController 数据源-国家统计局
  15. type BaseFromNationalStatisticsController struct {
  16. controllers.BaseAuthController
  17. }
  18. // DbList
  19. // @Title 数据库列表
  20. // @Description 数据库列表
  21. // @Success 200 Ret=200 获取成功
  22. // @router /base_from_national_statistics/db_list [get]
  23. func (this *BaseFromNationalStatisticsController) DbList() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. if br.ErrMsg == "" {
  27. br.IsSendEmail = false
  28. }
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. sysUser := this.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录, SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. type DbInfo struct {
  40. Dbcode string `description:"数据库编码"`
  41. Dbname string `description:"数据库名称"`
  42. Sort int `description:"排序"`
  43. Children []DbInfo `description:"数据库列表"`
  44. }
  45. // 基本不会变动
  46. data := `[{"dbcode":"hgyd","dbname":"月度数据","sort":1,"children":null},{"dbcode":"hgjd","dbname":"季度数据","sort":2,"children":null},{"dbcode":"hgnd","dbname":"年度数据","sort":3,"children":null},{"dbcode":"","dbname":"地区数据","sort":4,"children":[{"dbcode":"fsyd","dbname":"分省月度数据","sort":1,"children":null},{"dbcode":"fsjd","dbname":"分省季度数据","sort":2,"children":null},{"dbcode":"fsnd","dbname":"分省年度数据","sort":3,"children":null},{"dbcode":"csyd","dbname":"主要城市月度价格","sort":4,"children":null},{"dbcode":"csnd","dbname":"主要城市年度数据","sort":5,"children":null},{"dbcode":"gatyd","dbname":"港澳台月度数据","sort":6,"children":null},{"dbcode":"gatnd","dbname":"港澳台年度数据","sort":7,"children":null}]},{"dbcode":"","dbname":"国际数据","sort":5,"children":[{"dbcode":"gjyd","dbname":"主要国家(地区)月度数据","sort":1,"children":null},{"dbcode":"gjydsdj","dbname":"三大经济体月度数据","sort":2,"children":null},{"dbcode":"gjydsc","dbname":"国际市场月度商品价格","sort":3,"children":null},{"dbcode":"gjnd","dbname":"主要国家(地区)年度数据","sort":4,"children":null}]}]`
  47. var list []DbInfo
  48. if e := json.Unmarshal([]byte(data), &list); e != nil {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = "解析数据库列表信息失败, Err: " + e.Error()
  51. return
  52. }
  53. br.Data = list
  54. br.Ret = 200
  55. br.Success = true
  56. br.Msg = "获取成功"
  57. }
  58. // ClassifyList
  59. // @Title 指标分类列表
  60. // @Description 指标分类列表
  61. // @Param Dbcode query string true "数据库编码"
  62. // @Success 200 Ret=200 获取成功
  63. // @router /base_from_national_statistics/classify_list [get]
  64. func (this *BaseFromNationalStatisticsController) ClassifyList() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. if br.ErrMsg == "" {
  68. br.IsSendEmail = false
  69. }
  70. this.Data["json"] = br
  71. this.ServeJSON()
  72. }()
  73. sysUser := this.SysUser
  74. if sysUser == nil {
  75. br.Msg = "请登录"
  76. br.ErrMsg = "请登录, SysUser Is Empty"
  77. br.Ret = 408
  78. return
  79. }
  80. dbcode := this.GetString("Dbcode", "")
  81. if dbcode == "" {
  82. br.Msg = "参数有误"
  83. return
  84. }
  85. classifyOB := new(data_manage.BaseFromNationalStatisticsClassify)
  86. classifyCond := ` AND dbcode = ? AND wdcode = ? `
  87. classifyPars := make([]interface{}, 0)
  88. classifyPars = append(classifyPars, dbcode, "zb")
  89. classifies, e := classifyOB.GetItemsByCondition(classifyCond, classifyPars, []string{}, "base_from_national_statistics_classify_id ASC")
  90. if e != nil {
  91. br.Msg = "获取失败"
  92. br.ErrMsg = "获取统计局指标分类列表失败, Err: " + e.Error()
  93. return
  94. }
  95. classifyMap := make(map[string]*data_manage.BaseFromNationalStatisticsClassify)
  96. for _, c := range classifies {
  97. classifyMap[c.Id] = c
  98. }
  99. topClassify := make([]*data_manage.BaseFromNationalStatisticsClassify, 0)
  100. childMap := make(map[string][]*data_manage.BaseFromNationalStatisticsClassify)
  101. //parentMap := make(map[string]*data_manage.BaseFromNationalStatisticsClassify)
  102. for _, c := range classifies {
  103. if c.Pid != "" {
  104. if childMap[c.Pid] == nil {
  105. childMap[c.Pid] = make([]*data_manage.BaseFromNationalStatisticsClassify, 0)
  106. }
  107. childMap[c.Pid] = append(childMap[c.Pid], c)
  108. //parentMap[c.Id] = classifyMap[c.Pid]
  109. continue
  110. }
  111. topClassify = append(topClassify, c)
  112. }
  113. list := make([]*data_manage.BaseFromNationalStatisticsClassifyItem, 0)
  114. for _, t := range topClassify {
  115. v := new(data_manage.BaseFromNationalStatisticsClassifyItem)
  116. v.ClassifyId = t.BaseFromNationalStatisticsClassifyId
  117. v.ClassifyName = t.ClassifyName
  118. v.Dbcode = dbcode
  119. v.IsParent = t.IsParent
  120. // 二级分类
  121. v.Children = make([]*data_manage.BaseFromNationalStatisticsClassifyItem, 0)
  122. children := childMap[t.Id]
  123. if children != nil && len(children) > 0 {
  124. for _, l2 := range children {
  125. v2 := new(data_manage.BaseFromNationalStatisticsClassifyItem)
  126. v2.ClassifyId = l2.BaseFromNationalStatisticsClassifyId
  127. v2.ClassifyName = l2.ClassifyName
  128. v2.Dbcode = dbcode
  129. v2.IsParent = l2.IsParent
  130. v2.ParentId = t.BaseFromNationalStatisticsClassifyId
  131. v2.ParentIds = append(v2.ParentIds, t.BaseFromNationalStatisticsClassifyId)
  132. v2.Children = make([]*data_manage.BaseFromNationalStatisticsClassifyItem, 0)
  133. // 三级分类
  134. childLevel3 := childMap[l2.Id]
  135. if childLevel3 != nil && len(childLevel3) > 0 {
  136. for _, l3 := range childLevel3 {
  137. v3 := new(data_manage.BaseFromNationalStatisticsClassifyItem)
  138. v3.ClassifyId = l3.BaseFromNationalStatisticsClassifyId
  139. v3.ClassifyName = l3.ClassifyName
  140. v3.Dbcode = dbcode
  141. v3.IsParent = l3.IsParent
  142. v3.ParentId = l2.BaseFromNationalStatisticsClassifyId
  143. v3.ParentIds = append(v3.ParentIds, t.BaseFromNationalStatisticsClassifyId, l2.BaseFromNationalStatisticsClassifyId)
  144. v3.Children = make([]*data_manage.BaseFromNationalStatisticsClassifyItem, 0)
  145. // 目前应该最多就三级了, 以防万一多写一层
  146. childLevel4 := childMap[l3.Id]
  147. if childLevel4 != nil && len(childLevel4) > 0 {
  148. for _, l4 := range childLevel4 {
  149. v4 := new(data_manage.BaseFromNationalStatisticsClassifyItem)
  150. v4.ClassifyId = l4.BaseFromNationalStatisticsClassifyId
  151. v4.ClassifyName = l4.ClassifyName
  152. v4.Dbcode = dbcode
  153. v4.IsParent = l4.IsParent
  154. v4.ParentId = l3.BaseFromNationalStatisticsClassifyId
  155. v4.ParentIds = append(v4.ParentIds, t.BaseFromNationalStatisticsClassifyId, l2.BaseFromNationalStatisticsClassifyId, l3.BaseFromNationalStatisticsClassifyId)
  156. v4.Children = make([]*data_manage.BaseFromNationalStatisticsClassifyItem, 0)
  157. v3.Children = append(v3.Children, v4)
  158. }
  159. }
  160. v2.Children = append(v2.Children, v3)
  161. }
  162. }
  163. v.Children = append(v.Children, v2)
  164. }
  165. }
  166. list = append(list, v)
  167. }
  168. br.Data = list
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. }
  173. // IndexList
  174. // @Title 指标列表
  175. // @Description 指标列表
  176. // @Param Dbcode query string true "数据库编码"
  177. // @Param ClassifyId query int false "指标分类ID"
  178. // @Param Keywords query string false "关键词-指标名称/指标ID"
  179. // @Success 200 Ret=200 获取成功
  180. // @router /base_from_national_statistics/index_list [get]
  181. func (this *BaseFromNationalStatisticsController) IndexList() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. if br.ErrMsg == "" {
  185. br.IsSendEmail = false
  186. }
  187. this.Data["json"] = br
  188. this.ServeJSON()
  189. }()
  190. sysUser := this.SysUser
  191. if sysUser == nil {
  192. br.Msg = "请登录"
  193. br.ErrMsg = "请登录, SysUser Is Empty"
  194. br.Ret = 408
  195. return
  196. }
  197. dbcode := this.GetString("Dbcode", "")
  198. if dbcode == "" {
  199. br.Msg = "请选择指标库"
  200. return
  201. }
  202. classifyId, _ := this.GetInt("ClassifyId", 0)
  203. keywords := this.GetString("Keywords", "")
  204. indexOB := new(data_manage.BaseFromNationalStatisticsIndex)
  205. indexCond := ` AND dbcode = ?`
  206. indexPars := make([]interface{}, 0)
  207. indexPars = append(indexPars, dbcode)
  208. limitNum := 0
  209. if keywords != "" {
  210. k := fmt.Sprint("%", keywords, "%")
  211. indexCond += ` AND (index_code LIKE ? OR CONCAT(reg, index_name) LIKE ?)`
  212. indexPars = append(indexPars, k, k)
  213. limitNum = 100 // 有关键词时限制100条不然前端数据量可能会很大
  214. }
  215. if classifyId > 0 {
  216. indexCond += ` AND base_from_national_statistics_classify_id = ?`
  217. indexPars = append(indexPars, classifyId)
  218. }
  219. indexes, e := indexOB.GetItemsByCondition(indexCond, indexPars, []string{}, "base_from_national_statistics_index_id ASC", limitNum)
  220. if e != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "获取统计局指标列表失败, Err: " + e.Error()
  223. return
  224. }
  225. resp := new(data_manage.BaseFromNationalStatisticsIndexListResp)
  226. resp.List = make([]*data_manage.BaseFromNationalStatisticsIndexItem, 0)
  227. repeatMap := make(map[string]int)
  228. indexMap := make(map[int]*data_manage.BaseFromNationalStatisticsIndexItem)
  229. for _, v := range indexes {
  230. item := data_manage.FormatBaseFromNationalStatisticsIndex2Item(v)
  231. // 因为存在第三维度-地区, 在分类大于0时, 根据indexName分组
  232. if classifyId > 0 && v.Reg != "" {
  233. // 取出地区信息作为详情接口的参数
  234. if !utils.InArrayByStr(resp.RegList, v.Reg) {
  235. resp.RegList = append(resp.RegList, v.Reg)
  236. }
  237. if indexMap[repeatMap[v.IndexName]] != nil {
  238. indexMap[repeatMap[v.IndexName]].ChildrenIndexIds = append(indexMap[repeatMap[v.IndexName]].ChildrenIndexIds, v.BaseFromNationalStatisticsIndexId)
  239. }
  240. if repeatMap[v.IndexName] > 0 {
  241. continue
  242. }
  243. repeatMap[v.IndexName] = v.BaseFromNationalStatisticsIndexId
  244. indexMap[v.BaseFromNationalStatisticsIndexId] = item
  245. }
  246. // 非分类下的查询时, 指标全名为地区+指标名
  247. if classifyId == 0 && v.Reg != "" {
  248. item.IndexName = fmt.Sprintf("%s%s", item.Reg, item.IndexName)
  249. }
  250. resp.List = append(resp.List, item)
  251. }
  252. br.Data = resp
  253. br.Ret = 200
  254. br.Success = true
  255. br.Msg = "获取成功"
  256. }
  257. // IndexDetail
  258. // @Title 指标详情
  259. // @Description 指标详情
  260. // @Param ClassifyId query int false "指标分类ID"
  261. // @Param IndexId query int false "指标ID"
  262. // @Param Reg query string false "地区"
  263. // @Param IsExport query bool false "是否导出"
  264. // @Success 200 Ret=200 获取成功
  265. // @router /base_from_national_statistics/index_detail [get]
  266. func (this *BaseFromNationalStatisticsController) IndexDetail() {
  267. br := new(models.BaseResponse).Init()
  268. defer func() {
  269. if br.ErrMsg == "" {
  270. br.IsSendEmail = false
  271. }
  272. this.Data["json"] = br
  273. this.ServeJSON()
  274. }()
  275. sysUser := this.SysUser
  276. if sysUser == nil {
  277. br.Msg = "请登录"
  278. br.ErrMsg = "请登录, SysUser Is Empty"
  279. br.Ret = 408
  280. return
  281. }
  282. classifyId, _ := this.GetInt("ClassifyId", 0)
  283. indexId, _ := this.GetInt("IndexId", 0)
  284. reg := this.GetString("Reg", "")
  285. isExport, _ := this.GetBool("IsExport", false)
  286. indexOB := new(data_manage.BaseFromNationalStatisticsIndex)
  287. indexCond := ``
  288. indexPars := make([]interface{}, 0)
  289. if reg != "" {
  290. indexCond += ` AND reg = ?`
  291. indexPars = append(indexPars, reg)
  292. }
  293. // 针对此种情况-数据库含地区维度, 选择指标时
  294. if indexId > 0 && reg != "" {
  295. index := new(data_manage.BaseFromNationalStatisticsIndex)
  296. if e := index.GetItemById(indexId); e != nil {
  297. br.Msg = "获取失败"
  298. br.ErrMsg = "获取指标失败, Err: " + e.Error()
  299. return
  300. }
  301. classifyId = index.BaseFromNationalStatisticsClassifyId
  302. indexCond += ` AND index_name = ?`
  303. indexPars = append(indexPars, index.IndexName)
  304. }
  305. if classifyId > 0 {
  306. indexCond += ` AND base_from_national_statistics_classify_id = ?`
  307. indexPars = append(indexPars, classifyId)
  308. }
  309. if indexId > 0 && reg == "" {
  310. indexCond += ` AND base_from_national_statistics_index_id = ?`
  311. indexPars = append(indexPars, indexId)
  312. }
  313. indexes, e := indexOB.GetItemsByCondition(indexCond, indexPars, []string{}, "base_from_national_statistics_index_id ASC", 0)
  314. if e != nil {
  315. br.Msg = "获取失败"
  316. br.ErrMsg = "获取指标列表失败, Err: " + e.Error()
  317. return
  318. }
  319. if len(indexes) == 0 {
  320. br.Ret = 200
  321. br.Success = true
  322. br.Msg = "获取成功"
  323. return
  324. }
  325. indexIds := make([]int, 0)
  326. dbCode := ``
  327. for _, i := range indexes {
  328. if dbCode == `` {
  329. dbCode = i.Dbcode
  330. }
  331. indexIds = append(indexIds, i.BaseFromNationalStatisticsIndexId)
  332. }
  333. dbNameMap := map[string]string{
  334. "hgyd": "月度数据",
  335. "hgjd": "季度数据",
  336. "hgnd": "年度数据",
  337. "fsyd": "分省月度数据",
  338. "fsjd": "分省季度数据",
  339. "fsnd": "分省年度数据",
  340. "csyd": "主要城市月度价格",
  341. "csnd": "主要城市年度数据",
  342. "gatyd": "港澳台月度数据",
  343. "gatnd": "港澳台年度数据",
  344. "gjyd": "主要国家(地区)月度数据",
  345. "gjydsdj": "三大经济体月度数据",
  346. "gjydsc": "国际市场月度商品价格",
  347. "gjnd": "主要国家(地区)年度数据",
  348. }
  349. // 获取指标数据
  350. dataOB := new(data_manage.BaseFromNationalStatisticsData)
  351. dataCond := fmt.Sprintf(` AND base_from_national_statistics_index_id IN (%s)`, utils.GetOrmInReplace(len(indexIds)))
  352. dataPars := make([]interface{}, 0)
  353. dataPars = append(dataPars, indexIds)
  354. dataItems, e := dataOB.GetItemsByCondition(dataCond, dataPars, []string{"base_from_national_statistics_data_id", "base_from_national_statistics_index_id", "data_time", "value"}, "base_from_national_statistics_index_id ASC, data_time DESC")
  355. if e != nil {
  356. br.Msg = "获取失败"
  357. br.ErrMsg = "获取统计局指标数据失败, Err: " + e.Error()
  358. return
  359. }
  360. indexDataMap := make(map[int][]*data_manage.BaseFromNationalStatisticsIndexDetailData)
  361. for _, d := range dataItems {
  362. if indexDataMap[d.BaseFromNationalStatisticsIndexId] == nil {
  363. indexDataMap[d.BaseFromNationalStatisticsIndexId] = make([]*data_manage.BaseFromNationalStatisticsIndexDetailData, 0)
  364. }
  365. indexDataMap[d.BaseFromNationalStatisticsIndexId] = append(indexDataMap[d.BaseFromNationalStatisticsIndexId], &data_manage.BaseFromNationalStatisticsIndexDetailData{
  366. DataTime: d.DataTime.Format(utils.FormatDate),
  367. Value: d.Value,
  368. })
  369. }
  370. resp := make([]*data_manage.BaseFromNationalStatisticsIndexDetailItem, 0)
  371. for _, i := range indexes {
  372. v := new(data_manage.BaseFromNationalStatisticsIndexDetailItem)
  373. item := data_manage.FormatBaseFromNationalStatisticsIndex2Item(i)
  374. item.IndexName = fmt.Sprintf("%s%s", item.Reg, item.IndexName)
  375. v.BaseFromNationalStatisticsIndexItem = *item
  376. v.DataList = indexDataMap[i.BaseFromNationalStatisticsIndexId]
  377. resp = append(resp, v)
  378. }
  379. // 导出
  380. if isExport {
  381. NationalStatisticsExport(this, br, resp, dbNameMap[dbCode])
  382. return
  383. }
  384. br.Data = resp
  385. br.Ret = 200
  386. br.Success = true
  387. br.Msg = "获取成功"
  388. }
  389. // NationalStatisticsExport 导出Excel
  390. func NationalStatisticsExport(this *BaseFromNationalStatisticsController, br *models.BaseResponse, list []*data_manage.BaseFromNationalStatisticsIndexDetailItem, dbName string) {
  391. sysUser := this.SysUser
  392. if sysUser == nil {
  393. br.Msg = "请重新登录"
  394. return
  395. }
  396. dir, _ := os.Executable()
  397. exPath := filepath.Dir(dir)
  398. downloadPath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  399. xlsxFile := xlsx.NewFile()
  400. sheet, e := xlsxFile.AddSheet("数据报表")
  401. if e != nil {
  402. br.Msg = "新增Sheet失败"
  403. br.ErrMsg = "新增Sheet失败, Err: " + e.Error()
  404. return
  405. }
  406. nameRow := sheet.AddRow()
  407. codeRow := sheet.AddRow()
  408. frequencyRow := sheet.AddRow()
  409. unitRow := sheet.AddRow()
  410. updateTimeRow := sheet.AddRow()
  411. setRowIndex := 6 // 数据行开始
  412. for _, index := range list {
  413. nameRow.AddCell().SetString("指标名称")
  414. nameRow.AddCell().SetString(index.IndexName)
  415. nameRow.AddCell().SetString("")
  416. codeRow.AddCell().SetString("指标ID")
  417. codeRow.AddCell().SetString(index.IndexCode)
  418. codeRow.AddCell().SetString("")
  419. frequencyRow.AddCell().SetString("频率")
  420. frequencyRow.AddCell().SetString(index.Frequency)
  421. frequencyRow.AddCell().SetString("")
  422. unitRow.AddCell().SetString("单位")
  423. unitRow.AddCell().SetString(index.Unit)
  424. unitRow.AddCell().SetString("")
  425. updateTimeRow.AddCell().SetString("更新时间")
  426. updateTimeRow.AddCell().SetString(index.ModifyTime)
  427. updateTimeRow.AddCell().SetString("")
  428. for k, v := range index.DataList {
  429. row := sheet.Row(setRowIndex + k)
  430. row.AddCell().SetString(v.DataTime)
  431. row.AddCell().SetString(fmt.Sprint(v.Value))
  432. row.AddCell().SetString("")
  433. }
  434. }
  435. if e = xlsxFile.Save(downloadPath); e != nil {
  436. br.Msg = "导出失败"
  437. br.ErrMsg = "保存文件失败"
  438. return
  439. }
  440. fileName := fmt.Sprint("国家统计局", dbName, time.Now().Format("2006.01.02"), ".xlsx")
  441. this.Ctx.Output.Download(downloadPath, fileName)
  442. defer func() {
  443. _ = os.Remove(downloadPath)
  444. }()
  445. br.Ret = 200
  446. br.Success = true
  447. br.Msg = "success"
  448. }