sci99.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 /sci99/classify/list [get]
  19. func (this *DataSourceController) Sci99ClassifyList() {
  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. classifyList, err := data_source.GetBaseFromSci99Classify()
  33. if err != nil {
  34. br.Msg = "获取分类失败"
  35. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  36. return
  37. }
  38. br.Ret = 200
  39. br.Success = true
  40. br.Msg = "获取成功"
  41. br.Data = classifyList
  42. }
  43. // @Title 获取消费者价格指数数据
  44. // @Description 获取消费者价格指数数据接口
  45. // @Param PageSize query int true "每页数据条数"
  46. // @Param CurrentIndex query int true "当前页页码,从1开始"
  47. // @Param BaseFromSci99ClassifyId query int true "分类id"
  48. // @Param KeyWord query string true "关键词"
  49. // @Success 200 {object} data_source.BaseFromSci99IndexView
  50. // @router /sci99/index/data [get]
  51. func (this *DataSourceController) Sci99Data() {
  52. br := new(models.BaseResponse).Init()
  53. defer func() {
  54. this.Data["json"] = br
  55. this.ServeJSON()
  56. }()
  57. sysUser := this.SysUser
  58. if sysUser == nil {
  59. br.Msg = "请登录"
  60. br.ErrMsg = "请登录,SysUser Is Empty"
  61. br.Ret = 408
  62. return
  63. }
  64. pageSize, _ := this.GetInt("PageSize")
  65. currentIndex, _ := this.GetInt("CurrentIndex")
  66. var startSize int
  67. if pageSize <= 0 {
  68. pageSize = utils.PageSize20
  69. }
  70. if currentIndex <= 0 {
  71. currentIndex = 1
  72. }
  73. startSize = utils.StartIndex(currentIndex, pageSize)
  74. baseFromSci99ClassifyId, _ := this.GetInt("ClassifyId")
  75. if baseFromSci99ClassifyId < 0 {
  76. br.Msg = "请选择分类"
  77. br.ErrMsg = "请选择分类"
  78. return
  79. }
  80. keyword := this.GetString("KeyWord")
  81. //获取指标
  82. var condition string
  83. var pars []interface{}
  84. if baseFromSci99ClassifyId > 0 {
  85. condition += ` AND classify_id=? `
  86. pars = append(pars, baseFromSci99ClassifyId)
  87. }
  88. if keyword != "" {
  89. condition += ` AND (index_code =? OR index_name LIKE ?) `
  90. pars = append(pars, keyword)
  91. pars = append(pars, "%"+keyword+"%")
  92. }
  93. sci99List, err := data_source.GetSci99Index(condition, pars)
  94. if err != nil {
  95. br.Msg = "获取数据失败"
  96. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  97. return
  98. }
  99. resultList := make([]*data_source.BaseFromSci99IndexList, 0)
  100. for _, v := range sci99List {
  101. product := new(data_source.BaseFromSci99IndexList)
  102. product.BaseFromSciIndexId = v.BaseFromSciIndexId
  103. product.ClassifyId = v.ClassifyId
  104. product.IndexCode = v.IndexCode
  105. product.IndexName = v.IndexName
  106. product.Frequency = v.Frequency
  107. product.Unit = v.Unit
  108. product.ModifyTime = v.ModifyTime
  109. modifyTime, err := data_source.GetSci99IndexLatestDate(v.IndexCode)
  110. if err != nil && err.Error() != utils.ErrNoRow() {
  111. br.Msg = "获取更新时间失败"
  112. br.ErrMsg = "获取更新时间失败,Err:" + err.Error()
  113. return
  114. }
  115. product.ModifyTime = modifyTime
  116. total, err := data_source.GetSci99IndexDataCount(v.IndexCode)
  117. page := paging.GetPaging(currentIndex, pageSize, total)
  118. dataList, err := data_source.GetSci99IndexData(v.IndexCode, startSize, pageSize)
  119. if err != nil {
  120. br.Msg = "获取数据失败"
  121. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  122. return
  123. }
  124. if dataList == nil {
  125. dataList = make([]*data_source.BaseFromSci99DataItem, 0)
  126. }
  127. product.DataList = dataList
  128. product.Paging = page
  129. resultList = append(resultList, product)
  130. }
  131. br.Ret = 200
  132. br.Success = true
  133. br.Msg = "获取成功"
  134. br.Data = resultList
  135. }
  136. // ExportBaiinfoList
  137. // @Title 导出ICPI数据
  138. // @Description 导出ICPI数据
  139. // @Param BaseFromSci99ClassifyId query int true "分类id"
  140. // @Param KeyWord query string true "关键词"
  141. // @Success 200 导出成功
  142. // @router /sci99/export/sci99DataList [get]
  143. func (this *DataSourceController) ExportSci99DataList() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. sysUser := this.SysUser
  150. if sysUser == nil {
  151. br.Msg = "请重新登录"
  152. return
  153. }
  154. baseFromSci99ClassifyId, _ := this.GetInt("ClassifyId")
  155. if baseFromSci99ClassifyId < 0 {
  156. br.Msg = "请选择分类"
  157. br.ErrMsg = "请选择分类"
  158. return
  159. }
  160. keyword := this.GetString("KeyWord")
  161. //获取指标
  162. var condition string
  163. var pars []interface{}
  164. if baseFromSci99ClassifyId >= 0 {
  165. condition += ` AND classify_id=? `
  166. pars = append(pars, baseFromSci99ClassifyId)
  167. }
  168. if keyword != "" {
  169. condition += ` AND (index_code =? OR index_name LIKE ?) `
  170. pars = append(pars, keyword)
  171. pars = append(pars, "%"+keyword+"%")
  172. }
  173. sci99List, err := data_source.GetSci99Index(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("卓创资讯")
  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 := data_source.GetSci99DataMaxCount(baseFromSci99ClassifyId)
  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 sci99List {
  205. //获取数据
  206. dataList, err := data_source.GetSci99IndexDataByCode(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("卓创资讯")
  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 := `卓创资讯`
  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. }