icpi.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package data_source
  2. import (
  3. "eta/eta_api/controllers"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_source"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "github.com/tealeg/xlsx"
  10. "os"
  11. "path/filepath"
  12. "time"
  13. )
  14. // 消费者价格指数
  15. type DataSourceIcpiController struct {
  16. controllers.BaseAuthController
  17. }
  18. // ComTradeCountryList
  19. // @Title 获取居民消费价格指数分类
  20. // @Description 获取居民消费价格指数分类
  21. // @Success 200 {object} []data_manage.ComTradeCountryItem
  22. // @router /icpi/classify/list [get]
  23. func (this *DataSourceIcpiController) IcpiClassifyList() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. sysUser := this.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. icpiObj := new(data_source.BaseFromIcpiIndex)
  37. classifyList, err := icpiObj.GetBaseFromIcpiClassifyAll()
  38. if err != nil {
  39. br.Msg = "获取分类失败"
  40. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  41. return
  42. }
  43. br.Ret = 200
  44. br.Success = true
  45. br.Msg = "获取成功"
  46. br.Data = classifyList
  47. }
  48. // @Title 获取消费者价格指数数据
  49. // @Description 获取消费者价格指数数据接口
  50. // @Param PageSize query int true "每页数据条数"
  51. // @Param CurrentIndex query int true "当前页页码,从1开始"
  52. // @Param BaseFromIcpiClassifyId query int true "分类id"
  53. // @Param KeyWord query string true "关键词"
  54. // @Success 200 {object} data_source.BaseFromIcpiIndexView
  55. // @router /icpi/index/data [get]
  56. func (this *DataSourceIcpiController) IcpiData() {
  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. if pageSize <= 0 {
  73. pageSize = utils.PageSize20
  74. }
  75. if currentIndex <= 0 {
  76. currentIndex = 1
  77. }
  78. startSize = utils.StartIndex(currentIndex, pageSize)
  79. baseFromIcpiClassifyId, _ := this.GetInt("BaseFromIcpiClassifyId")
  80. if baseFromIcpiClassifyId < 0 {
  81. br.Msg = "请选择分类"
  82. br.ErrMsg = "请选择分类"
  83. return
  84. }
  85. keyword := this.GetString("KeyWord")
  86. //获取指标
  87. var condition string
  88. var pars []interface{}
  89. if baseFromIcpiClassifyId > 0 {
  90. condition += ` AND base_from_icpi_classify_id=? `
  91. pars = append(pars, baseFromIcpiClassifyId)
  92. }
  93. if keyword != "" {
  94. condition += ` AND (index_code =? OR index_name LIKE ?) `
  95. pars = append(pars, keyword)
  96. pars = append(pars, "%"+keyword+"%")
  97. }
  98. icpiObj := new(data_source.BaseFromIcpiIndex)
  99. icpiList, err := icpiObj.GetIcpiIndex(condition, pars)
  100. if err != nil {
  101. br.Msg = "获取数据失败"
  102. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  103. return
  104. }
  105. resultList := make([]*data_source.BaseFromIcpiIndexView, 0)
  106. for _, v := range icpiList {
  107. product := new(data_source.BaseFromIcpiIndexView)
  108. product.BaseFromIcpiIndexId = v.BaseFromIcpiIndexId
  109. product.BaseFromIcpiClassifyId = v.BaseFromIcpiClassifyId
  110. product.IndexCode = v.IndexCode
  111. product.IndexName = v.IndexName
  112. product.Frequency = v.Frequency
  113. product.Unit = v.Unit
  114. product.ModifyTime = v.ModifyTime
  115. total, err := icpiObj.GetIcpiIndexDataCount(v.IndexCode)
  116. page := paging.GetPaging(currentIndex, pageSize, total)
  117. dataList, err := icpiObj.GetIcpiIndexData(v.IndexCode, startSize, pageSize)
  118. if err != nil {
  119. br.Msg = "获取数据失败"
  120. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  121. return
  122. }
  123. if dataList == nil {
  124. dataList = make([]*data_source.BaseFromIcpiDataView, 0)
  125. }
  126. product.DataList = dataList
  127. product.Paging = page
  128. resultList = append(resultList, product)
  129. }
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "获取成功"
  133. br.Data = resultList
  134. }
  135. // ExportBaiinfoList
  136. // @Title 导出ICPI数据
  137. // @Description 导出ICPI数据
  138. // @Param BaseFromIcpiClassifyId query int true "分类id"
  139. // @Param KeyWord query string true "关键词"
  140. // @Success 200 导出成功
  141. // @router /icpi/export/icpiDataList [get]
  142. func (this *DataSourceController) ExportIcpiDataList() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. this.Data["json"] = br
  146. this.ServeJSON()
  147. }()
  148. sysUser := this.SysUser
  149. if sysUser == nil {
  150. br.Msg = "请重新登录"
  151. return
  152. }
  153. baseFromIcpiClassifyId, _ := this.GetInt("BaseFromIcpiClassifyId")
  154. if baseFromIcpiClassifyId < 0 {
  155. br.Msg = "请选择分类"
  156. br.ErrMsg = "请选择分类"
  157. return
  158. }
  159. keyword := this.GetString("KeyWord")
  160. //获取指标
  161. var condition string
  162. var pars []interface{}
  163. if baseFromIcpiClassifyId >= 0 {
  164. condition += ` AND base_from_icpi_classify_id=? `
  165. pars = append(pars, baseFromIcpiClassifyId)
  166. }
  167. if keyword != "" {
  168. condition += ` AND (index_code =? OR index_name LIKE ?) `
  169. pars = append(pars, keyword)
  170. pars = append(pars, "%"+keyword+"%")
  171. }
  172. icpiObj := new(data_source.BaseFromIcpiIndex)
  173. icpiList, err := icpiObj.GetIcpiIndex(condition, pars)
  174. if err != nil {
  175. br.Msg = "获取数据失败"
  176. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  177. return
  178. }
  179. dir, _ := os.Executable()
  180. exPath := filepath.Dir(dir)
  181. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  182. xlsxFile := xlsx.NewFile()
  183. if err != nil {
  184. fmt.Println("新增Sheet失败", err.Error())
  185. return
  186. }
  187. //sheetNew.SetColWidth()
  188. //获取指标数据
  189. sheetNew, _ := xlsxFile.AddSheet("ICPI消费价格指数")
  190. windRow := sheetNew.AddRow()
  191. secNameRow := sheetNew.AddRow()
  192. indexCodeRow := sheetNew.AddRow()
  193. frequencyRow := sheetNew.AddRow()
  194. unitRow := sheetNew.AddRow()
  195. lastModifyDateRow := sheetNew.AddRow()
  196. //获取分类下指标最大数据量
  197. dataMax, err := icpiObj.GetIcpiDataMaxCount(baseFromIcpiClassifyId)
  198. if err != nil {
  199. fmt.Println("获取指标最大数据量失败", err.Error())
  200. return
  201. }
  202. fmt.Println("dataMax:", dataMax)
  203. setRowIndex := 6
  204. for k, sv := range icpiList {
  205. //获取数据
  206. dataList, err := icpiObj.GetIcpiIndexDataByCode(sv.IndexCode)
  207. if err != nil {
  208. br.Msg = "获取数据失败"
  209. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  210. return
  211. }
  212. if len(dataList) > 0 {
  213. windRow.AddCell().SetValue("ICPI消费价格指数")
  214. secNameRow.AddCell().SetValue("指标名称")
  215. indexCodeRow.AddCell().SetValue("指标ID")
  216. frequencyRow.AddCell().SetValue("频率")
  217. unitRow.AddCell().SetValue("单位")
  218. lastModifyDateRow.AddCell().SetValue("更新时间")
  219. secNameRow.AddCell().SetValue(sv.IndexName)
  220. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  221. frequencyRow.AddCell().SetValue(sv.Frequency)
  222. unitRow.AddCell().SetValue(sv.Unit)
  223. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  224. windRow.AddCell()
  225. windRow.AddCell()
  226. secNameRow.AddCell()
  227. indexCodeRow.AddCell()
  228. frequencyRow.AddCell()
  229. unitRow.AddCell()
  230. lastModifyDateRow.AddCell()
  231. min := k * 3
  232. sheetNew.SetColWidth(min, min, 15)
  233. if len(dataList) <= 0 {
  234. for n := 0; n < dataMax; n++ {
  235. rowIndex := setRowIndex + n
  236. row := sheetNew.Row(rowIndex)
  237. row.AddCell()
  238. row.AddCell()
  239. row.AddCell()
  240. row.AddCell()
  241. }
  242. } else {
  243. endRowIndex := 0
  244. for rk, dv := range dataList {
  245. rowIndex := setRowIndex + rk
  246. row := sheetNew.Row(rowIndex)
  247. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  248. displayDateCell := row.AddCell()
  249. style := new(xlsx.Style)
  250. style.ApplyAlignment = true
  251. style.Alignment.WrapText = true
  252. displayDateCell.SetStyle(style)
  253. displayDateCell.SetDate(displayDate)
  254. row.AddCell().SetValue(dv.Value)
  255. row.AddCell()
  256. endRowIndex = rowIndex
  257. }
  258. if len(dataList) < dataMax {
  259. dataLen := dataMax - len(dataList)
  260. for n := 0; n < dataLen; n++ {
  261. rowIndex := (endRowIndex + 1) + n
  262. row := sheetNew.Row(rowIndex)
  263. row.AddCell()
  264. row.AddCell()
  265. row.AddCell()
  266. row.AddCell()
  267. }
  268. }
  269. }
  270. }
  271. }
  272. err = xlsxFile.Save(downLoadnFilePath)
  273. if err != nil {
  274. //有指标无数据时先导出一遍空表
  275. sheet, err := xlsxFile.AddSheet("无数据")
  276. if err != nil {
  277. br.Msg = "新增Sheet失败"
  278. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  279. return
  280. }
  281. rowSecName := sheet.AddRow()
  282. celSecName := rowSecName.AddCell()
  283. celSecName.SetValue("")
  284. err = xlsxFile.Save(downLoadnFilePath)
  285. if err != nil {
  286. br.Msg = "保存文件失败"
  287. br.ErrMsg = "保存文件失败"
  288. return
  289. }
  290. }
  291. fileName := `ICPI消费价格指数`
  292. fileName += time.Now().Format(utils.FormatDateUnSpace) + `.xlsx` //文件名称
  293. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  294. defer func() {
  295. os.Remove(downLoadnFilePath)
  296. }()
  297. br.Ret = 200
  298. br.Success = true
  299. br.Msg = "success"
  300. }