icpi.go 8.7 KB

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