eia_steo.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package data_manage
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "github.com/tealeg/xlsx"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/data_manage"
  7. "hongze/hz_crm_api/utils"
  8. "os"
  9. "path/filepath"
  10. "time"
  11. )
  12. // EiaSteoClassify
  13. // @Title EiaSteo数据分类
  14. // @Description EiaSteo数据分类接口
  15. // @Success 200 {object} data_manage.LzClassify
  16. // @router /eia_steo/classify [get]
  17. func (this *EdbInfoController) EiaSteoClassify() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. sysUser := this.SysUser
  24. if sysUser == nil {
  25. br.Msg = "请登录"
  26. br.ErrMsg = "请登录,SysUser Is Empty"
  27. br.Ret = 408
  28. return
  29. }
  30. classifyList, err := data_manage.GetEiaSteoClassifyList()
  31. if err != nil {
  32. br.Msg = "获取分类失败"
  33. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  34. return
  35. }
  36. for _, v := range classifyList {
  37. if v.ClassifyName == `` {
  38. v.ClassifyName = v.ClassifyNameOriginal
  39. }
  40. }
  41. br.Ret = 200
  42. br.Success = true
  43. br.Msg = "获取成功"
  44. br.Data = classifyList
  45. }
  46. // EiaSteoData
  47. // @Title 获取EiaSteo数据
  48. // @Description 获取EiaSteo数据接口
  49. // @Param BaseFromEiaSteoClassifyId query int true "指标分类id"
  50. // @Param IndexCode query string true "指标code"
  51. // @Param IsExport query bool false "是否导出excel,默认是false"
  52. // @Param PageSize query int true "每页数据条数"
  53. // @Param CurrentIndex query int true "当前页页码,从1开始"
  54. // @Success 200 {object} data_manage.LzFrequency
  55. // @router /eia_steo/data [get]
  56. func (this *EdbInfoController) EiaSteoData() {
  57. br := new(models.BaseResponse).Init()
  58. defer func() {
  59. this.Data["json"] = br
  60. this.ServeJSON()
  61. }()
  62. sysUser := this.SysUser
  63. if sysUser == nil {
  64. br.Msg = "请登录"
  65. br.ErrMsg = "请登录,SysUser Is Empty"
  66. br.Ret = 408
  67. return
  68. }
  69. pageSize, _ := this.GetInt("PageSize")
  70. currentIndex, _ := this.GetInt("CurrentIndex")
  71. var startSize int
  72. //是否导出报表
  73. isExport, _ := this.GetBool("IsExport")
  74. if isExport {
  75. pageSize = 10000
  76. currentIndex = 1
  77. }
  78. if pageSize <= 0 {
  79. pageSize = utils.PageSize20
  80. }
  81. if currentIndex <= 0 {
  82. currentIndex = 1
  83. }
  84. startSize = utils.StartIndex(currentIndex, pageSize)
  85. var condition string
  86. var pars []interface{}
  87. indexCode := this.GetString("IndexCode")
  88. if indexCode != "" {
  89. condition += ` AND index_code = ? `
  90. pars = append(pars, utils.TrimStr(indexCode))
  91. }
  92. classifyId, _ := this.GetInt("BaseFromEiaSteoClassifyId")
  93. if classifyId > 0 {
  94. condition += ` AND base_from_eia_steo_classify_id = ? `
  95. pars = append(pars, classifyId)
  96. }
  97. //获取指标
  98. indexList, err := data_manage.GetEiaSteoIndexList(condition, pars)
  99. if err != nil {
  100. br.Msg = "获取数据失败"
  101. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  102. return
  103. }
  104. resultList := make([]data_manage.EiaSteoIndexListResp, 0)
  105. for _, v := range indexList {
  106. indexName := v.IndexName
  107. if indexName == `` {
  108. indexName = v.IndexNameOriginal
  109. }
  110. product := data_manage.EiaSteoIndexListResp{
  111. BaseFromEiaSteoIndexId: v.BaseFromEiaSteoIndexId,
  112. BaseFromEiaSteoClassifyId: v.BaseFromEiaSteoClassifyId,
  113. IndexCode: v.IndexCode,
  114. IndexName: indexName,
  115. //IndexNameOriginal: v.IndexNameOriginal,
  116. Unit: v.Unit,
  117. Frequency: v.Frequency,
  118. StartDate: v.StartDate.Format(utils.FormatDate),
  119. EndDate: v.EndDate.Format(utils.FormatDate),
  120. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  121. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  122. //Paging: tmpPaging,
  123. //DataList: nil,
  124. }
  125. total, err := data_manage.GetEiaSteoIndexDataCount(v.IndexCode)
  126. page := paging.GetPaging(currentIndex, pageSize, total)
  127. dataList, err := data_manage.GetEiaSteoIndexDataList(v.IndexCode, startSize, pageSize)
  128. if err != nil {
  129. br.Msg = "获取数据失败"
  130. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  131. return
  132. }
  133. if dataList == nil {
  134. dataList = make([]*data_manage.BaseFromEiaSteoDataItem, 0)
  135. }
  136. product.DataList = dataList
  137. product.Paging = page
  138. resultList = append(resultList, product)
  139. }
  140. //导出excel
  141. if isExport {
  142. EiaSteoDataExport(this, resultList, br)
  143. return
  144. }
  145. br.Ret = 200
  146. br.Success = true
  147. br.Msg = "获取成功"
  148. br.Data = resultList
  149. }
  150. // EiaSteoSearchList
  151. // @Title EiaSteo模糊搜索
  152. // @Description EiaSteo模糊搜索
  153. // @Param Keyword query string ture "关键字搜索"
  154. // @Success 200 {object} models.BaseResponse
  155. // @router /eia_steo/search_list [get]
  156. func (this *EdbInfoController) EiaSteoSearchList() {
  157. br := new(models.BaseResponse).Init()
  158. defer func() {
  159. this.Data["json"] = br
  160. this.ServeJSON()
  161. }()
  162. sysUser := this.SysUser
  163. if sysUser == nil {
  164. br.Msg = "请重新登录"
  165. return
  166. }
  167. //关键字
  168. keyword := this.GetString("Keyword")
  169. list, err := data_manage.GetEiaSteoItemList(utils.TrimStr(keyword))
  170. if err != nil {
  171. br.ErrMsg = "获取失败,Err:" + err.Error()
  172. br.Msg = "获取失败"
  173. return
  174. }
  175. br.Ret = 200
  176. br.Success = true
  177. br.Msg = "获取成功"
  178. br.Data = list
  179. }
  180. // EiaSteoDataExport 表格导出
  181. func EiaSteoDataExport(this *EdbInfoController, list []data_manage.EiaSteoIndexListResp, br *models.BaseResponse) {
  182. sysUser := this.SysUser
  183. if sysUser == nil {
  184. br.Msg = "请重新登录"
  185. return
  186. }
  187. dir, _ := os.Executable()
  188. exPath := filepath.Dir(dir)
  189. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  190. xlsxFile := xlsx.NewFile()
  191. sheet, err := xlsxFile.AddSheet("数据报表")
  192. //获取指标数据
  193. rowSecName := sheet.AddRow()
  194. celSecName := rowSecName.AddCell()
  195. celSecName.SetValue("指标名称")
  196. rowUnit := sheet.AddRow()
  197. celUnit := rowUnit.AddCell()
  198. celUnit.SetValue("单位")
  199. rowModifyDate := sheet.AddRow()
  200. rowModifyCell := rowModifyDate.AddCell()
  201. rowModifyCell.SetValue("更新时间")
  202. dataMap := make(map[string]map[string]*data_manage.BaseFromEiaSteoDataItem)
  203. var indexCodeList []string
  204. for _, v := range list {
  205. cellSenName := rowSecName.AddCell()
  206. cellSenName.SetValue(v.IndexName)
  207. celUnit := rowUnit.AddCell()
  208. celUnit.SetValue(v.Unit)
  209. rowModifyCell := rowModifyDate.AddCell()
  210. rowModifyCell.SetValue(v.ModifyTime)
  211. indexCodeList = append(indexCodeList, v.IndexCode)
  212. dataMap[v.IndexCode] = make(map[string]*data_manage.BaseFromEiaSteoDataItem)
  213. for _, item := range v.DataList {
  214. dataMap[item.IndexCode][item.DataTime] = item
  215. }
  216. }
  217. dateList, err := data_manage.GetEiaSteoDateList(indexCodeList)
  218. if err != nil {
  219. br.Msg = "获取数据失败"
  220. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  221. return
  222. }
  223. for _, n := range dateList {
  224. dateStr := n.DataTime //当前日期
  225. rowData := sheet.AddRow()
  226. celDate := rowData.AddCell()
  227. celDate.SetValue(dateStr)
  228. for _, m := range list {
  229. celData := rowData.AddCell()
  230. if dataMap[m.IndexCode][dateStr] != nil {
  231. celData.SetValue(dataMap[m.IndexCode][dateStr].Value)
  232. }
  233. }
  234. }
  235. err = xlsxFile.Save(downLoadnFilePath)
  236. if err != nil {
  237. //有指标无数据时先导出一遍空表
  238. sheet, err := xlsxFile.AddSheet("无数据")
  239. if err != nil {
  240. br.Msg = "新增Sheet失败"
  241. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  242. return
  243. }
  244. rowSecName := sheet.AddRow()
  245. celSecName := rowSecName.AddCell()
  246. celSecName.SetValue("")
  247. err = xlsxFile.Save(downLoadnFilePath)
  248. if err != nil {
  249. br.Msg = "保存文件失败"
  250. br.ErrMsg = "保存文件失败"
  251. return
  252. }
  253. }
  254. fileName := `EiaSteo数据`
  255. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  256. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  257. defer func() {
  258. os.Remove(downLoadnFilePath)
  259. }()
  260. br.Ret = 200
  261. br.Success = true
  262. br.Msg = "success"
  263. }