gl_data.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_eta_api/models"
  7. "hongze/hz_eta_api/models/data_manage"
  8. "hongze/hz_eta_api/utils"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. )
  13. // @Title 钢联数据分类
  14. // @Description 隆众数据分类接口
  15. // @Success 200 {object} data_manage.LzClassify
  16. // @router /gl/classify [get]
  17. func (this *EdbInfoController) GlClassify() {
  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.GetGlSurveyClassify()
  31. if err != nil {
  32. br.Msg = "获取分类失败"
  33. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  34. return
  35. }
  36. br.Ret = 200
  37. br.Success = true
  38. br.Msg = "获取成功"
  39. br.Data = classifyList
  40. }
  41. // @Title 钢联数据频度
  42. // @Description 钢联数据频度接口
  43. // @Param BreedShortName query string true "分类名称"
  44. // @Success 200 {object} data_manage.LzFrequency
  45. // @router /gl/frequency [get]
  46. func (this *EdbInfoController) GlFrequency() {
  47. br := new(models.BaseResponse).Init()
  48. defer func() {
  49. this.Data["json"] = br
  50. this.ServeJSON()
  51. }()
  52. sysUser := this.SysUser
  53. if sysUser == nil {
  54. br.Msg = "请登录"
  55. br.ErrMsg = "请登录,SysUser Is Empty"
  56. br.Ret = 408
  57. return
  58. }
  59. breedShortName := this.GetString("BreedShortName")
  60. if breedShortName == "" {
  61. br.Msg = "分类不可为空"
  62. br.ErrMsg = "分类不可为空"
  63. return
  64. }
  65. fmt.Println("breedShortName:", breedShortName)
  66. frequencyList, err := data_manage.GetGlFrequencyByClassifyId(breedShortName)
  67. if err != nil {
  68. br.Msg = "获取频度失败"
  69. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  70. return
  71. }
  72. br.Ret = 200
  73. br.Success = true
  74. br.Msg = "获取成功"
  75. br.Data = frequencyList
  76. }
  77. // @Title 获取钢联数据
  78. // @Description 获取钢联数据接口
  79. // @Param BreedShortName query string true "分类名称"
  80. // @Param Frequency query string true "频度名称"
  81. // @Param PageSize query int true "每页数据条数"
  82. // @Param CurrentIndex query int true "当前页页码,从1开始"
  83. // @Success 200 {object} data_manage.LzFrequency
  84. // @router /gl/data [get]
  85. func (this *EdbInfoController) GlData() {
  86. br := new(models.BaseResponse).Init()
  87. defer func() {
  88. this.Data["json"] = br
  89. this.ServeJSON()
  90. }()
  91. sysUser := this.SysUser
  92. if sysUser == nil {
  93. br.Msg = "请登录"
  94. br.ErrMsg = "请登录,SysUser Is Empty"
  95. br.Ret = 408
  96. return
  97. }
  98. pageSize, _ := this.GetInt("PageSize")
  99. currentIndex, _ := this.GetInt("CurrentIndex")
  100. var startSize int
  101. if pageSize <= 0 {
  102. pageSize = utils.PageSize20
  103. }
  104. if currentIndex <= 0 {
  105. currentIndex = 1
  106. }
  107. startSize = utils.StartIndex(currentIndex, pageSize)
  108. breedShortName := this.GetString("BreedShortName")
  109. if breedShortName == "" {
  110. br.Msg = "请选择分类"
  111. br.ErrMsg = "请选择分类"
  112. return
  113. }
  114. frequency := this.GetString("Frequency")
  115. if frequency == "" {
  116. br.Msg = "请选择频度"
  117. br.ErrMsg = "请选择频度"
  118. return
  119. }
  120. //获取指标
  121. glList, err := data_manage.GetGlIndex(breedShortName, frequency)
  122. if err != nil {
  123. br.Msg = "获取数据失败"
  124. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  125. return
  126. }
  127. resultList := make([]*data_manage.GlIndexList, 0)
  128. for _, v := range glList {
  129. product := new(data_manage.GlIndexList)
  130. product.UnitName = v.UnitName
  131. product.Id = v.Id
  132. product.IndexCode = v.IndexCode
  133. product.IndexName = v.IndexName
  134. product.UnitName = v.UnitName
  135. product.FrequencyName = v.FrequencyName
  136. product.UpdateTime = v.UpdateTime
  137. total, err := data_manage.GetGlIndexDataCount(v.IndexCode)
  138. page := paging.GetPaging(currentIndex, pageSize, total)
  139. dataList, err := data_manage.GetGlIndexData(v.IndexCode, startSize, pageSize)
  140. if err != nil {
  141. br.Msg = "获取数据失败"
  142. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  143. return
  144. }
  145. if dataList == nil {
  146. dataList = make([]*data_manage.GlIndexData, 0)
  147. }
  148. product.DataList = dataList
  149. product.Paging = page
  150. resultList = append(resultList, product)
  151. }
  152. br.Ret = 200
  153. br.Success = true
  154. br.Msg = "获取成功"
  155. br.Data = resultList
  156. }
  157. // GlSearchList
  158. // @Title 钢联模糊搜索
  159. // @Description 钢联模糊搜索
  160. // @Param Keyword query string ture "关键字搜索"
  161. // @Success 200 {object} models.BaseResponse
  162. // @router /gl/search_list [get]
  163. func (this *EdbInfoController) GlSearchList() {
  164. br := new(models.BaseResponse).Init()
  165. defer func() {
  166. this.Data["json"] = br
  167. this.ServeJSON()
  168. }()
  169. sysUser := this.SysUser
  170. if sysUser == nil {
  171. br.Msg = "请重新登录"
  172. return
  173. }
  174. //关键字
  175. keyword := this.GetString("Keyword")
  176. list, err := data_manage.GetGlItemList(keyword)
  177. if err != nil {
  178. br.ErrMsg = "获取失败,Err:" + err.Error()
  179. br.Msg = "获取失败"
  180. return
  181. }
  182. br.Ret = 200
  183. br.Success = true
  184. br.Msg = "获取成功"
  185. br.Data = list
  186. }
  187. // GlSingleData
  188. // @Title 获取钢联数据
  189. // @Description 获取钢联单条数据接口
  190. // @Param IndexCode query string true "指标唯一编码"
  191. // @Success 200 {object} models.BaseResponse
  192. // @router /gl/single_data [get]
  193. func (this *EdbInfoController) GlSingleData() {
  194. br := new(models.BaseResponse).Init()
  195. defer func() {
  196. this.Data["json"] = br
  197. this.ServeJSON()
  198. }()
  199. sysUser := this.SysUser
  200. if sysUser == nil {
  201. br.Msg = "请登录"
  202. br.ErrMsg = "请登录,SysUser Is Empty"
  203. br.Ret = 408
  204. return
  205. }
  206. indexCode := this.GetString("IndexCode")
  207. dataList, err := data_manage.GetGlDataByCode(indexCode)
  208. if err != nil {
  209. br.Msg = "获取数据失败"
  210. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  211. return
  212. }
  213. br.Ret = 200
  214. br.Success = true
  215. br.Msg = "获取成功"
  216. br.Data = dataList
  217. }
  218. // ExportGlList
  219. // @Title 导出钢联数据
  220. // @Description 导出钢联数据
  221. // @Param QuotaName query string false "名称关键词"
  222. // @Param IndexCode query string false "指标唯一编码"
  223. // @Param BreedName query string true "分类"
  224. // @Param Frequency query string true "频度"
  225. // @Param UnitName query string false "单位"
  226. // @Param ModifyTime query string false "更新时间"
  227. // @Success 200 导出成功
  228. // @router /export/glList [get]
  229. func (this *EdbInfoController) ExportGlList() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. sysUser := this.SysUser
  236. if sysUser == nil {
  237. br.Msg = "请重新登录"
  238. return
  239. }
  240. breedName := this.GetString("BreedName") //分类
  241. //frequency := this.GetString("Frequency")
  242. secNameList := make([]*models.EdbdataExportList, 0)
  243. dir, _ := os.Executable()
  244. exPath := filepath.Dir(dir)
  245. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  246. xlsxFile := xlsx.NewFile()
  247. frequencies, err := data_manage.GetGlFrequency(breedName)
  248. if err != nil {
  249. fmt.Println("GetGlFrequency err:", err.Error())
  250. utils.FileLog.Info("GetGlFrequency err:" + err.Error())
  251. return
  252. }
  253. if breedName != "" {
  254. for _, frequency := range frequencies {
  255. //获取指标
  256. secNameList, err := data_manage.GetGlIndex(breedName, *frequency)
  257. if err != nil {
  258. fmt.Println("获取数据失败,Err:" + err.Error())
  259. return
  260. }
  261. if len(secNameList) <= 0 {
  262. fmt.Println("secNameList长度为0")
  263. return
  264. }
  265. if breedName == "1/3焦煤" {
  266. breedName = "三分之一焦煤"
  267. }
  268. sheetNew, err := xlsxFile.AddSheet(*frequency)
  269. if err != nil {
  270. fmt.Println("新增Sheet失败", err.Error())
  271. return
  272. }
  273. //sheetNew.SetColWidth()
  274. //获取指标数据
  275. windRow := sheetNew.AddRow()
  276. secNameRow := sheetNew.AddRow()
  277. indexCodeRow := sheetNew.AddRow()
  278. frequencyRow := sheetNew.AddRow()
  279. unitRow := sheetNew.AddRow()
  280. lastModifyDateRow := sheetNew.AddRow()
  281. //获取分类下指标最大数据量
  282. dataMax, err := data_manage.GetGlDataMaxCount(breedName)
  283. if err != nil {
  284. fmt.Println("获取指标最大数据量失败", err.Error())
  285. return
  286. }
  287. fmt.Println("dataMax:", dataMax)
  288. setRowIndex := 6
  289. for k, sv := range secNameList {
  290. //获取数据
  291. dataList, err := data_manage.GetGlDataByCode(sv.IndexCode)
  292. if err != nil {
  293. br.Msg = "获取数据失败"
  294. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  295. return
  296. }
  297. if len(dataList) > 0 {
  298. windRow.AddCell().SetValue("钢联")
  299. secNameRow.AddCell().SetValue("指标名称")
  300. indexCodeRow.AddCell().SetValue("指标ID")
  301. frequencyRow.AddCell().SetValue("频率")
  302. unitRow.AddCell().SetValue("单位")
  303. lastModifyDateRow.AddCell().SetValue("更新时间")
  304. secNameRow.AddCell().SetValue(sv.IndexName)
  305. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  306. frequencyRow.AddCell().SetValue(sv.FrequencyName)
  307. unitRow.AddCell().SetValue(sv.UnitName)
  308. lastModifyDateRow.AddCell().SetValue(sv.UpdateTime)
  309. windRow.AddCell()
  310. windRow.AddCell()
  311. secNameRow.AddCell()
  312. indexCodeRow.AddCell()
  313. frequencyRow.AddCell()
  314. unitRow.AddCell()
  315. lastModifyDateRow.AddCell()
  316. min := k * 3
  317. sheetNew.SetColWidth(min, min, 15)
  318. if len(dataList) <= 0 {
  319. for n := 0; n < dataMax; n++ {
  320. rowIndex := setRowIndex + n
  321. row := sheetNew.Row(rowIndex)
  322. row.AddCell()
  323. row.AddCell()
  324. row.AddCell()
  325. }
  326. } else {
  327. endRowIndex := 0
  328. for rk, dv := range dataList {
  329. rowIndex := setRowIndex + rk
  330. row := sheetNew.Row(rowIndex)
  331. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  332. displayDateCell := row.AddCell()
  333. style := new(xlsx.Style)
  334. style.ApplyAlignment = true
  335. style.Alignment.WrapText = true
  336. displayDateCell.SetStyle(style)
  337. displayDateCell.SetDate(displayDate)
  338. row.AddCell().SetValue(dv.InputValue)
  339. row.AddCell()
  340. endRowIndex = rowIndex
  341. }
  342. if len(dataList) < dataMax {
  343. dataLen := dataMax - len(dataList)
  344. for n := 0; n < dataLen; n++ {
  345. rowIndex := (endRowIndex + 1) + n
  346. row := sheetNew.Row(rowIndex)
  347. row.AddCell()
  348. row.AddCell()
  349. row.AddCell()
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. } else {
  357. name := this.GetString("QuotaName")
  358. indexCode := this.GetString("IndexCode") //指标唯一编码
  359. unit := this.GetString("UnitName")
  360. modifyTime := this.GetString("ModifyTime")
  361. frequency := this.GetString("Frequency")
  362. sheet, err := xlsxFile.AddSheet(frequency)
  363. if err != nil {
  364. br.Msg = "新增Sheet失败"
  365. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  366. return
  367. }
  368. //获取指标数据
  369. windRow := sheet.AddRow()
  370. windRow.AddCell().SetValue("钢联")
  371. rowSecName := sheet.AddRow()
  372. celSecName := rowSecName.AddCell()
  373. celSecName.SetValue("指标名称")
  374. cellSenName := rowSecName.AddCell()
  375. cellSenName.SetValue(name)
  376. indexCodeRow := sheet.AddRow()
  377. indexCodeRow.AddCell().SetValue("指标ID")
  378. indexCodeRow.AddCell().SetValue(indexCode)
  379. rowFrequency := sheet.AddRow()
  380. celFrequency := rowFrequency.AddCell()
  381. celFrequency.SetValue("频率")
  382. cellFrequency := rowFrequency.AddCell()
  383. cellFrequency.SetValue(frequency)
  384. rowUnit := sheet.AddRow()
  385. celUnit := rowUnit.AddCell()
  386. celUnit.SetValue("单位")
  387. cellUnit := rowUnit.AddCell()
  388. cellUnit.SetValue(unit)
  389. rowModifyDate := sheet.AddRow()
  390. rowModifyCell := rowModifyDate.AddCell()
  391. rowModifyCell.SetValue("更新时间")
  392. rowModifyCell = rowModifyDate.AddCell()
  393. rowModifyCell.SetValue(modifyTime)
  394. //获取数据
  395. dataList, err := data_manage.GetGlDataByCode(indexCode)
  396. if err != nil {
  397. br.Msg = "获取数据失败"
  398. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  399. return
  400. }
  401. fmt.Println("len(dataList):", len(dataList))
  402. dataMax, err := data_manage.GetGlDataMaxCount(breedName)
  403. fmt.Println("dataMax:", dataMax)
  404. if len(dataList) <= 0 {
  405. for n := 0; n < dataMax; n++ {
  406. rowIndex := 6 + n
  407. row := sheet.Row(rowIndex)
  408. row.AddCell()
  409. row.AddCell()
  410. row.AddCell()
  411. }
  412. } else {
  413. endRowIndex := 0
  414. for rk, dv := range dataList {
  415. rowIndex := 6 + rk
  416. row := sheet.Row(rowIndex)
  417. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  418. displayDateCell := row.AddCell()
  419. style := new(xlsx.Style)
  420. style.ApplyAlignment = true
  421. style.Alignment.WrapText = true
  422. displayDateCell.SetStyle(style)
  423. displayDateCell.SetDate(displayDate)
  424. row.AddCell().SetValue(dv.InputValue)
  425. row.AddCell()
  426. endRowIndex = rowIndex
  427. }
  428. if len(dataList) < dataMax {
  429. dataLen := dataMax - len(dataList)
  430. for n := 0; n < dataLen; n++ {
  431. rowIndex := (endRowIndex + 1) + n
  432. row := sheet.Row(rowIndex)
  433. row.AddCell()
  434. row.AddCell()
  435. row.AddCell()
  436. }
  437. }
  438. }
  439. }
  440. err = xlsxFile.Save(downLoadnFilePath)
  441. if err != nil {
  442. //有指标无数据时先导出一遍空表
  443. sheet, err := xlsxFile.AddSheet("无数据")
  444. if err != nil {
  445. br.Msg = "新增Sheet失败"
  446. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  447. return
  448. }
  449. rowSecName := sheet.AddRow()
  450. celSecName := rowSecName.AddCell()
  451. celSecName.SetValue("")
  452. err = xlsxFile.Save(downLoadnFilePath)
  453. if err != nil {
  454. br.Msg = "保存文件失败"
  455. br.ErrMsg = "保存文件失败"
  456. return
  457. }
  458. }
  459. fileName := `钢联数据`
  460. if len(secNameList) > 0 {
  461. fileName = secNameList[0].ClassifyName
  462. }
  463. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  464. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  465. defer func() {
  466. os.Remove(downLoadnFilePath)
  467. }()
  468. br.Ret = 200
  469. br.Success = true
  470. br.Msg = "success"
  471. }