fenwei_data.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package data_manage
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "github.com/tealeg/xlsx"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. // FenweiClassify
  15. // @Title 汾渭数据分类
  16. // @Description 汾渭数据分类接口
  17. // @Success 200 {object} data_manage.BaseFromFenweiClassifyItem
  18. // @router /fenwei/classify [get]
  19. func (this *EdbInfoController) FenweiClassify() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. classifies, e := data_manage.GetBaseFromFenweiClassify()
  36. if e != nil {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "获取汾渭数据分类失败, Err: " + e.Error()
  39. return
  40. }
  41. classifyOb := new(data_manage.BaseFromFenweiClassify)
  42. resp := make([]*data_manage.BaseFromFenweiClassifyItem, 0)
  43. parentMap := make(map[int][]*data_manage.BaseFromFenweiClassifyItem)
  44. for _, v := range classifies {
  45. t := classifyOb.Format2Item(v)
  46. if v.ParentId == 0 {
  47. resp = append(resp, t)
  48. }
  49. if v.ParentId > 0 {
  50. if parentMap[v.ParentId] == nil {
  51. parentMap[v.ParentId] = make([]*data_manage.BaseFromFenweiClassifyItem, 0)
  52. }
  53. parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
  54. }
  55. }
  56. for _, v := range resp {
  57. v.Child = parentMap[v.ClassifyId]
  58. }
  59. br.Data = resp
  60. br.Ret = 200
  61. br.Success = true
  62. br.Msg = "获取成功"
  63. }
  64. // FenweiIndexData
  65. // @Title 获取汾渭数据
  66. // @Description 获取汾渭数据接口
  67. // @Param PageSize query int true "每页数据条数"
  68. // @Param CurrentIndex query int true "当前页页码,从1开始"
  69. // @Param ClassifyId query string true "分类id"
  70. // @Success 200 {object} data_manage.LzFrequency
  71. // @router /fenwei/index/data [get]
  72. func (this *EdbInfoController) FenweiIndexData() {
  73. br := new(models.BaseResponse).Init()
  74. defer func() {
  75. if br.ErrMsg == "" {
  76. br.IsSendEmail = false
  77. }
  78. this.Data["json"] = br
  79. this.ServeJSON()
  80. }()
  81. sysUser := this.SysUser
  82. if sysUser == nil {
  83. br.Msg = "请登录"
  84. br.ErrMsg = "请登录,SysUser Is Empty"
  85. br.Ret = 408
  86. return
  87. }
  88. pageSize, _ := this.GetInt("PageSize")
  89. currentIndex, _ := this.GetInt("CurrentIndex")
  90. var startSize int
  91. if pageSize <= 0 {
  92. pageSize = utils.PageSize20
  93. }
  94. if currentIndex <= 0 {
  95. currentIndex = 1
  96. }
  97. startSize = utils.StartIndex(currentIndex, pageSize)
  98. classifyId, _ := this.GetInt("ClassifyId")
  99. if classifyId < 0 {
  100. br.Msg = "请选择分类"
  101. br.ErrMsg = "请选择分类"
  102. return
  103. }
  104. // 获取指标
  105. var condition string
  106. var pars []interface{}
  107. if classifyId >= 0 {
  108. condition += ` AND classify_id=? `
  109. pars = append(pars, classifyId)
  110. }
  111. indexes, err := data_manage.GetFenweiIndex(condition, pars)
  112. if err != nil {
  113. br.Msg = "获取数据失败"
  114. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  115. return
  116. }
  117. indexCodes := make([]string, 0)
  118. for _, v := range indexes {
  119. indexCodes = append(indexCodes, v.IndexCode)
  120. }
  121. indexCounts, e := data_manage.GetFenweiIndexDataCountGroup(indexCodes)
  122. if e != nil {
  123. br.Msg = "获取失败"
  124. br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
  125. return
  126. }
  127. countMap := make(map[string]int)
  128. for _, v := range indexCounts {
  129. countMap[v.IndexCode] = v.Count
  130. }
  131. resultList := make([]*data_manage.BaseFromFenweiIndexList, 0)
  132. for _, v := range indexes {
  133. product := new(data_manage.BaseFromFenweiIndexList)
  134. product.FenweiIndexId = v.FenweiIndexId
  135. product.ClassifyId = v.ClassifyId
  136. product.Unit = v.Unit
  137. product.IndexCode = v.IndexCode
  138. product.IndexName = v.IndexName
  139. product.Frequency = v.Frequency
  140. product.CreateTime = v.CreateTime
  141. product.ModifyTime = v.ModifyTime
  142. total := countMap[v.IndexCode]
  143. page := paging.GetPaging(currentIndex, pageSize, total)
  144. dataList, e := data_manage.GetFenweiIndexData(v.IndexCode, startSize, pageSize)
  145. if e != nil {
  146. br.Msg = "获取数据失败"
  147. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  148. return
  149. }
  150. if dataList == nil {
  151. dataList = make([]*data_manage.BaseFromFenweiData, 0)
  152. }
  153. product.DataList = dataList
  154. product.Paging = page
  155. resultList = append(resultList, product)
  156. }
  157. br.Ret = 200
  158. br.Success = true
  159. br.Msg = "获取成功"
  160. br.Data = resultList
  161. }
  162. // FenweiSearchList
  163. // @Title Fenwei模糊搜索
  164. // @Description Fenwei模糊搜索
  165. // @Param Keyword query string ture "关键字搜索"
  166. // @Success 200 {object} data_manage.BaseFromFenweiIndexSearchItem
  167. // @router /fenwei/search_list [get]
  168. func (this *EdbInfoController) FenweiSearchList() {
  169. br := new(models.BaseResponse).Init()
  170. defer func() {
  171. if br.ErrMsg == "" {
  172. br.IsSendEmail = false
  173. }
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. list := make([]*data_manage.BaseFromFenweiIndexSearchItem, 0)
  185. var err error
  186. // 关键字
  187. keyword := this.GetString("Keyword")
  188. if keyword != "" {
  189. keyWordArr := strings.Split(keyword, " ")
  190. if len(keyWordArr) > 0 {
  191. condition := ""
  192. for _, v := range keyWordArr {
  193. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  194. }
  195. list, err = data_manage.GetFenweiItemList(condition)
  196. if err != nil {
  197. br.ErrMsg = "获取失败,Err:" + err.Error()
  198. br.Msg = "获取失败"
  199. return
  200. }
  201. }
  202. } else {
  203. list, err = data_manage.GetFenweiItemList("")
  204. if err != nil {
  205. br.ErrMsg = "获取失败,Err:" + err.Error()
  206. br.Msg = "获取失败"
  207. return
  208. }
  209. }
  210. br.Ret = 200
  211. br.Success = true
  212. br.Msg = "获取成功"
  213. br.Data = list
  214. }
  215. // FenweiSingleData
  216. // @Title 获取Fenwei数据
  217. // @Description 获取Fenwei单条数据接口
  218. // @Param IndexCode query string true "指标唯一编码"
  219. // @Success 200 {object} models.BaseResponse
  220. // @router /fenwei/single_data [get]
  221. func (this *EdbInfoController) FenweiSingleData() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. if br.ErrMsg == "" {
  225. br.IsSendEmail = false
  226. }
  227. this.Data["json"] = br
  228. this.ServeJSON()
  229. }()
  230. sysUser := this.SysUser
  231. if sysUser == nil {
  232. br.Msg = "请登录"
  233. br.ErrMsg = "请登录,SysUser Is Empty"
  234. br.Ret = 408
  235. return
  236. }
  237. indexCode := this.GetString("IndexCode")
  238. indexInfo, err := data_manage.GetBaseFromFenweiIndexByIndexCode(indexCode)
  239. if err != nil {
  240. br.Msg = "获取指标信息失败"
  241. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  242. return
  243. }
  244. dataTmpList, err := data_manage.GetFenweiIndexDataByCode(indexCode)
  245. if err != nil {
  246. br.Msg = "获取数据失败"
  247. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  248. return
  249. }
  250. var ret data_manage.FenweiSingleDataResp
  251. var dataList []*data_manage.FenweiSingleData
  252. ret.ClassifyId = indexInfo.ClassifyId
  253. ret.FenweiIndexId = indexInfo.FenweiIndexId
  254. ret.IndexCode = indexInfo.IndexCode
  255. ret.IndexName = indexInfo.IndexName
  256. ret.Frequency = indexInfo.Frequency
  257. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  258. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  259. ret.Unit = indexInfo.Unit
  260. for _, v := range dataTmpList {
  261. tmp := &data_manage.FenweiSingleData{
  262. Value: v.Value,
  263. DataTime: v.DataTime,
  264. }
  265. dataList = append(dataList, tmp)
  266. }
  267. ret.Data = dataList
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "获取成功"
  271. br.Data = ret
  272. }
  273. // ExportFenweiList
  274. // @Title 导出Fenwei数据
  275. // @Description 导出Fenwei数据
  276. // @Param IndexName query string false "名称关键词"
  277. // @Param IndexCode query string false "指标唯一编码"
  278. // @Param ClassifyId query string true "分类"
  279. // @Success 200 导出成功
  280. // @router /fenwei/export [get]
  281. func (this *EdbInfoController) ExportFenweiList() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. if br.ErrMsg == "" {
  285. br.IsSendEmail = false
  286. }
  287. this.Data["json"] = br
  288. this.ServeJSON()
  289. }()
  290. sysUser := this.SysUser
  291. if sysUser == nil {
  292. br.Msg = "请重新登录"
  293. return
  294. }
  295. indexCode := this.GetString("IndexCode") //指标唯一编码
  296. classifyId, _ := this.GetInt("ClassifyId")
  297. //secNameList := make([]*models.EdbdataExportList, 0)
  298. dir, _ := os.Executable()
  299. exPath := filepath.Dir(dir)
  300. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  301. xlsxFile := xlsx.NewFile()
  302. var pars []interface{}
  303. condition := ""
  304. if classifyId > 0 {
  305. //获取指标
  306. condition += " AND classify_id=?"
  307. pars = append(pars, classifyId)
  308. }
  309. if indexCode != "" {
  310. //获取指标
  311. condition += " AND index_code=?"
  312. pars = append(pars, indexCode)
  313. }
  314. indexList, err := data_manage.GetFenweiIndex(condition, pars)
  315. if err != nil {
  316. fmt.Println("获取数据失败,Err:" + err.Error())
  317. return
  318. }
  319. if len(indexList) <= 0 {
  320. fmt.Println("indexList 为空")
  321. br.Ret = 200
  322. br.Success = true
  323. br.Msg = "success"
  324. return
  325. }
  326. sheetNew := new(xlsx.Sheet)
  327. // todo 分类名称
  328. sheetNew, err = xlsxFile.AddSheet("汾渭数据")
  329. //sheetNew.SetColWidth()
  330. //获取指标数据
  331. windRow := sheetNew.AddRow()
  332. secNameRow := sheetNew.AddRow()
  333. indexCodeRow := sheetNew.AddRow()
  334. frequencyRow := sheetNew.AddRow()
  335. unitRow := sheetNew.AddRow()
  336. lastModifyDateRow := sheetNew.AddRow()
  337. //获取分类下指标最大数据量
  338. var dataMax int
  339. setRowIndex := 6
  340. indexCodeList := make([]string, 0)
  341. for _, v := range indexList {
  342. indexCodeList = append(indexCodeList, v.IndexCode)
  343. }
  344. dataListMap := make(map[string][]*data_manage.BaseFromFenweiData)
  345. if len(indexList) > 0 {
  346. allDataList, e := data_manage.GetFenweiIndexDataByCodes(indexCodeList)
  347. if e != nil {
  348. br.Msg = "获取数据失败"
  349. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  350. return
  351. }
  352. for _, v := range allDataList {
  353. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  354. }
  355. for _, v := range dataListMap {
  356. if len(v) > dataMax {
  357. dataMax = len(v)
  358. }
  359. }
  360. }
  361. for k, sv := range indexList {
  362. //获取数据
  363. dataList, ok := dataListMap[sv.IndexCode]
  364. if !ok {
  365. continue
  366. }
  367. if len(dataList) > 0 {
  368. windRow.AddCell().SetValue("汾渭数据")
  369. secNameRow.AddCell().SetValue("指标名称")
  370. indexCodeRow.AddCell().SetValue("指标ID")
  371. frequencyRow.AddCell().SetValue("频率")
  372. unitRow.AddCell().SetValue("单位")
  373. lastModifyDateRow.AddCell().SetValue("更新时间")
  374. secNameRow.AddCell().SetValue(sv.IndexName)
  375. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  376. frequencyRow.AddCell().SetValue(sv.Frequency)
  377. unitRow.AddCell().SetValue(sv.Unit)
  378. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  379. windRow.AddCell()
  380. windRow.AddCell()
  381. secNameRow.AddCell()
  382. indexCodeRow.AddCell()
  383. frequencyRow.AddCell()
  384. unitRow.AddCell()
  385. lastModifyDateRow.AddCell()
  386. min := k * 3
  387. sheetNew.SetColWidth(min, min, 15)
  388. if len(dataList) <= 0 {
  389. for n := 0; n < dataMax; n++ {
  390. rowIndex := setRowIndex + n
  391. row := sheetNew.Row(rowIndex)
  392. row.AddCell()
  393. row.AddCell()
  394. row.AddCell()
  395. }
  396. } else {
  397. endRowIndex := 0
  398. for rk, dv := range dataList {
  399. rowIndex := setRowIndex + rk
  400. row := sheetNew.Row(rowIndex)
  401. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  402. displayDateCell := row.AddCell()
  403. style := new(xlsx.Style)
  404. style.ApplyAlignment = true
  405. style.Alignment.WrapText = true
  406. displayDateCell.SetStyle(style)
  407. displayDateCell.SetDate(displayDate)
  408. row.AddCell().SetValue(dv.Value)
  409. row.AddCell()
  410. endRowIndex = rowIndex
  411. }
  412. if len(dataList) < dataMax {
  413. dataLen := dataMax - len(dataList)
  414. for n := 0; n < dataLen; n++ {
  415. rowIndex := (endRowIndex + 1) + n
  416. row := sheetNew.Row(rowIndex)
  417. row.AddCell()
  418. row.AddCell()
  419. row.AddCell()
  420. }
  421. }
  422. }
  423. }
  424. }
  425. err = xlsxFile.Save(downLoadnFilePath)
  426. if err != nil {
  427. //有指标无数据时先导出一遍空表
  428. sheet, err := xlsxFile.AddSheet("无数据")
  429. if err != nil {
  430. br.Msg = "新增Sheet失败"
  431. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  432. return
  433. }
  434. rowSecName := sheet.AddRow()
  435. celSecName := rowSecName.AddCell()
  436. celSecName.SetValue("")
  437. err = xlsxFile.Save(downLoadnFilePath)
  438. if err != nil {
  439. br.Msg = "保存文件失败"
  440. br.ErrMsg = "保存文件失败"
  441. return
  442. }
  443. }
  444. fileName := `汾渭数据`
  445. if len(indexList) > 0 {
  446. fileName = indexList[0].IndexName
  447. }
  448. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  449. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  450. defer func() {
  451. os.Remove(downLoadnFilePath)
  452. }()
  453. br.Ret = 200
  454. br.Success = true
  455. br.Msg = "success"
  456. }