yongyi_data.go 12 KB

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