ccf_data.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. // CCFClassify
  15. // @Title CCF化纤分类
  16. // @Description CCF化纤分类接口
  17. // @Success 200 {object} data_manage.BaseFromCCFClassifyItem
  18. // @router /ccf/classify [get]
  19. func (this *EdbInfoController) CCFClassify() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. classifies, e := data_manage.GetBaseFromCCFClassify()
  36. if e != nil {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "获取CCF化纤分类失败, Err: " + e.Error()
  39. return
  40. }
  41. classifyOb := new(data_manage.BaseFromCCFClassify)
  42. resp := make([]*data_manage.BaseFromCCFClassifyItem, 0)
  43. parentMap := make(map[int][]*data_manage.BaseFromCCFClassifyItem)
  44. for _, v := range classifies {
  45. t := classifyOb.Format2Item(v)
  46. if v.ParentId == 0 {
  47. resp = append(resp, t)
  48. }
  49. if v.ParentId > 0 {
  50. if parentMap[v.ParentId] == nil {
  51. parentMap[v.ParentId] = make([]*data_manage.BaseFromCCFClassifyItem, 0)
  52. }
  53. parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
  54. }
  55. }
  56. for _, v := range resp {
  57. v.Child = parentMap[v.ClassifyId]
  58. }
  59. br.Data = resp
  60. br.Ret = 200
  61. br.Success = true
  62. br.Msg = "获取成功"
  63. }
  64. // CCFIndexData
  65. // @Title 获取CCF化纤数据
  66. // @Description 获取CCF化纤接口
  67. // @Param PageSize query int true "每页数据条数"
  68. // @Param CurrentIndex query int true "当前页页码,从1开始"
  69. // @Param ClassifyId query string true "分类id"
  70. // @Success 200 {object} data_manage.LzFrequency
  71. // @router /ccf/index/data [get]
  72. func (this *EdbInfoController) CCFIndexData() {
  73. br := new(models.BaseResponse).Init()
  74. defer func() {
  75. if br.ErrMsg == "" {
  76. br.IsSendEmail = false
  77. }
  78. this.Data["json"] = br
  79. this.ServeJSON()
  80. }()
  81. sysUser := this.SysUser
  82. if sysUser == nil {
  83. br.Msg = "请登录"
  84. br.ErrMsg = "请登录,SysUser Is Empty"
  85. br.Ret = 408
  86. return
  87. }
  88. pageSize, _ := this.GetInt("PageSize")
  89. currentIndex, _ := this.GetInt("CurrentIndex")
  90. var startSize int
  91. if pageSize <= 0 {
  92. pageSize = utils.PageSize20
  93. }
  94. if currentIndex <= 0 {
  95. currentIndex = 1
  96. }
  97. startSize = utils.StartIndex(currentIndex, pageSize)
  98. classifyId, _ := this.GetInt("ClassifyId")
  99. if classifyId < 0 {
  100. br.Msg = "请选择分类"
  101. br.ErrMsg = "请选择分类"
  102. return
  103. }
  104. // 获取指标
  105. var condition string
  106. var pars []interface{}
  107. if classifyId >= 0 {
  108. condition += ` AND classify_id=? `
  109. pars = append(pars, classifyId)
  110. }
  111. indexes, err := data_manage.GetCCFIndex(condition, pars)
  112. if err != nil {
  113. br.Msg = "获取数据失败"
  114. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  115. return
  116. }
  117. indexCodes := make([]string, 0)
  118. for _, v := range indexes {
  119. indexCodes = append(indexCodes, v.IndexCode)
  120. }
  121. indexCounts, e := data_manage.GetCCFIndexDataCountGroup(indexCodes)
  122. if e != nil {
  123. br.Msg = "获取失败"
  124. br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
  125. return
  126. }
  127. countMap := make(map[string]int)
  128. for _, v := range indexCounts {
  129. countMap[v.IndexCode] = v.Count
  130. }
  131. resultList := make([]*data_manage.BaseFromCCFIndexList, 0)
  132. for _, v := range indexes {
  133. product := new(data_manage.BaseFromCCFIndexList)
  134. product.BaseFromCcfIndexId = v.BaseFromCcfIndexId
  135. product.ClassifyId = v.ClassifyId
  136. product.Unit = v.Unit
  137. product.IndexCode = v.IndexCode
  138. product.IndexName = v.IndexName
  139. product.Frequency = v.Frequency
  140. product.CreateTime = v.CreateTime
  141. product.ModifyTime = v.ModifyTime
  142. total := countMap[v.IndexCode]
  143. page := paging.GetPaging(currentIndex, pageSize, total)
  144. dataList, e := data_manage.GetCCFIndexData(v.IndexCode, startSize, pageSize)
  145. if e != nil {
  146. br.Msg = "获取数据失败"
  147. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  148. return
  149. }
  150. if dataList == nil {
  151. dataList = make([]*data_manage.BaseFromCCFData, 0)
  152. }
  153. product.DataList = dataList
  154. product.Paging = page
  155. resultList = append(resultList, product)
  156. }
  157. br.Ret = 200
  158. br.Success = true
  159. br.Msg = "获取成功"
  160. br.Data = resultList
  161. }
  162. // CCFSearchList
  163. // @Title CCF模糊搜索
  164. // @Description CCF模糊搜索
  165. // @Param Keyword query string ture "关键字搜索"
  166. // @Success 200 {object} data_manage.BaseFromCCFIndexSearchItem
  167. // @router /ccf/search_list [get]
  168. func (this *EdbInfoController) CCFSearchList() {
  169. br := new(models.BaseResponse).Init()
  170. defer func() {
  171. if br.ErrMsg == "" {
  172. br.IsSendEmail = false
  173. }
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. list := make([]*data_manage.BaseFromCCFIndexSearchItem, 0)
  185. var err error
  186. // 关键字
  187. keyword := this.GetString("Keyword")
  188. if keyword != "" {
  189. keyWordArr := strings.Split(keyword, " ")
  190. if len(keyWordArr) > 0 {
  191. condition := ""
  192. for _, v := range keyWordArr {
  193. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  194. }
  195. list, err = data_manage.GetCCFItemList(condition)
  196. if err != nil {
  197. br.ErrMsg = "获取失败,Err:" + err.Error()
  198. br.Msg = "获取失败"
  199. return
  200. }
  201. }
  202. } else {
  203. list, err = data_manage.GetCCFItemList("")
  204. if err != nil {
  205. br.ErrMsg = "获取失败,Err:" + err.Error()
  206. br.Msg = "获取失败"
  207. return
  208. }
  209. }
  210. br.Ret = 200
  211. br.Success = true
  212. br.Msg = "获取成功"
  213. br.Data = list
  214. }
  215. // CCFSingleData
  216. // @Title 获取CCF数据
  217. // @Description 获取CCF单条数据接口
  218. // @Param IndexCode query string true "指标唯一编码"
  219. // @Success 200 {object} models.BaseResponse
  220. // @router /ccf/single_data [get]
  221. func (this *EdbInfoController) CCFSingleData() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. if br.ErrMsg == "" {
  225. br.IsSendEmail = false
  226. }
  227. this.Data["json"] = br
  228. this.ServeJSON()
  229. }()
  230. sysUser := this.SysUser
  231. if sysUser == nil {
  232. br.Msg = "请登录"
  233. br.ErrMsg = "请登录,SysUser Is Empty"
  234. br.Ret = 408
  235. return
  236. }
  237. indexCode := this.GetString("IndexCode")
  238. indexInfo, err := data_manage.GetBaseFromCCFIndexByIndexCode(indexCode)
  239. if err != nil {
  240. br.Msg = "获取指标信息失败"
  241. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  242. return
  243. }
  244. dataTmpList, err := data_manage.GetCCFIndexDataByCode(indexCode)
  245. if err != nil {
  246. br.Msg = "获取数据失败"
  247. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  248. return
  249. }
  250. var ret data_manage.CCFSingleDataResp
  251. var dataList []*data_manage.CCFSingleData
  252. ret.ClassifyId = indexInfo.ClassifyId
  253. ret.BaseFromCcfIndexId = indexInfo.BaseFromCcfIndexId
  254. ret.IndexCode = indexInfo.IndexCode
  255. ret.IndexName = indexInfo.IndexName
  256. ret.Frequency = indexInfo.Frequency
  257. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  258. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  259. ret.Unit = indexInfo.Unit
  260. for _, v := range dataTmpList {
  261. tmp := &data_manage.CCFSingleData{
  262. Value: v.Value,
  263. DataTime: v.DataTime,
  264. }
  265. dataList = append(dataList, tmp)
  266. }
  267. ret.Data = dataList
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "获取成功"
  271. br.Data = ret
  272. }
  273. // ExportCCFList
  274. // @Title 导出CCF数据
  275. // @Description 导出CCF数据
  276. // @Param IndexName query string false "名称关键词"
  277. // @Param IndexCode query string false "指标唯一编码"
  278. // @Param ClassifyId query string true "分类"
  279. // @Success 200 导出成功
  280. // @router /ccf/export [get]
  281. func (this *EdbInfoController) ExportCCFList() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. if br.ErrMsg == "" {
  285. br.IsSendEmail = false
  286. }
  287. this.Data["json"] = br
  288. this.ServeJSON()
  289. }()
  290. sysUser := this.SysUser
  291. if sysUser == nil {
  292. br.Msg = "请重新登录"
  293. return
  294. }
  295. indexCode := this.GetString("IndexCode") //指标唯一编码
  296. classifyId, _ := this.GetInt("ClassifyId")
  297. dir, _ := os.Executable()
  298. exPath := filepath.Dir(dir)
  299. downFile := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  300. xlsxFile := xlsx.NewFile()
  301. var pars []interface{}
  302. condition := ""
  303. if classifyId > 0 {
  304. //获取指标
  305. condition += " AND classify_id=?"
  306. pars = append(pars, classifyId)
  307. }
  308. if indexCode != "" {
  309. //获取指标
  310. condition += " AND index_code=?"
  311. pars = append(pars, indexCode)
  312. }
  313. indexList, err := data_manage.GetCCFIndex(condition, pars)
  314. if err != nil {
  315. fmt.Println("获取数据失败,Err:" + err.Error())
  316. return
  317. }
  318. if len(indexList) <= 0 {
  319. fmt.Println("indexList 为空")
  320. br.Ret = 200
  321. br.Success = true
  322. br.Msg = "success"
  323. return
  324. }
  325. sheetNew := new(xlsx.Sheet)
  326. sheetNew, err = xlsxFile.AddSheet("CCF化纤")
  327. //sheetNew.SetColWidth()
  328. //获取指标数据
  329. windRow := sheetNew.AddRow()
  330. secNameRow := sheetNew.AddRow()
  331. indexCodeRow := sheetNew.AddRow()
  332. frequencyRow := sheetNew.AddRow()
  333. unitRow := sheetNew.AddRow()
  334. lastModifyDateRow := sheetNew.AddRow()
  335. //获取分类下指标最大数据量
  336. var dataMax int
  337. setRowIndex := 6
  338. indexCodeList := make([]string, 0)
  339. for _, v := range indexList {
  340. indexCodeList = append(indexCodeList, v.IndexCode)
  341. }
  342. dataListMap := make(map[string][]*data_manage.BaseFromCCFData)
  343. if len(indexList) > 0 {
  344. allDataList, e := data_manage.GetCCFIndexDataByCodes(indexCodeList)
  345. if e != nil {
  346. br.Msg = "获取数据失败"
  347. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  348. return
  349. }
  350. for _, v := range allDataList {
  351. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  352. }
  353. for _, v := range dataListMap {
  354. if len(v) > dataMax {
  355. dataMax = len(v)
  356. }
  357. }
  358. }
  359. for k, sv := range indexList {
  360. //获取数据
  361. dataList, ok := dataListMap[sv.IndexCode]
  362. if !ok {
  363. continue
  364. }
  365. if len(dataList) > 0 {
  366. windRow.AddCell().SetValue("CCF化纤")
  367. secNameRow.AddCell().SetValue("指标名称")
  368. indexCodeRow.AddCell().SetValue("指标ID")
  369. frequencyRow.AddCell().SetValue("频率")
  370. unitRow.AddCell().SetValue("单位")
  371. lastModifyDateRow.AddCell().SetValue("更新时间")
  372. secNameRow.AddCell().SetValue(sv.IndexName)
  373. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  374. frequencyRow.AddCell().SetValue(sv.Frequency)
  375. unitRow.AddCell().SetValue(sv.Unit)
  376. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  377. windRow.AddCell()
  378. windRow.AddCell()
  379. secNameRow.AddCell()
  380. indexCodeRow.AddCell()
  381. frequencyRow.AddCell()
  382. unitRow.AddCell()
  383. lastModifyDateRow.AddCell()
  384. minCol := k * 3
  385. sheetNew.SetColWidth(minCol, minCol, 15)
  386. if len(dataList) <= 0 {
  387. for n := 0; n < dataMax; n++ {
  388. rowIndex := setRowIndex + n
  389. row := sheetNew.Row(rowIndex)
  390. row.AddCell()
  391. row.AddCell()
  392. row.AddCell()
  393. }
  394. } else {
  395. endRowIndex := 0
  396. for rk, dv := range dataList {
  397. rowIndex := setRowIndex + rk
  398. row := sheetNew.Row(rowIndex)
  399. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  400. displayDateCell := row.AddCell()
  401. style := new(xlsx.Style)
  402. style.ApplyAlignment = true
  403. style.Alignment.WrapText = true
  404. displayDateCell.SetStyle(style)
  405. displayDateCell.SetDate(displayDate)
  406. row.AddCell().SetValue(dv.Value)
  407. row.AddCell()
  408. endRowIndex = rowIndex
  409. }
  410. if len(dataList) < dataMax {
  411. dataLen := dataMax - len(dataList)
  412. for n := 0; n < dataLen; n++ {
  413. rowIndex := (endRowIndex + 1) + n
  414. row := sheetNew.Row(rowIndex)
  415. row.AddCell()
  416. row.AddCell()
  417. row.AddCell()
  418. }
  419. }
  420. }
  421. }
  422. }
  423. err = xlsxFile.Save(downFile)
  424. if err != nil {
  425. //有指标无数据时先导出一遍空表
  426. sheet, err := xlsxFile.AddSheet("无数据")
  427. if err != nil {
  428. br.Msg = "新增Sheet失败"
  429. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  430. return
  431. }
  432. rowSecName := sheet.AddRow()
  433. celSecName := rowSecName.AddCell()
  434. celSecName.SetValue("")
  435. err = xlsxFile.Save(downFile)
  436. if err != nil {
  437. br.Msg = "保存文件失败"
  438. br.ErrMsg = "保存文件失败"
  439. return
  440. }
  441. }
  442. fileName := `CCF化纤信息`
  443. //if len(indexList) > 0 {
  444. // fileName = indexList[0].IndexName
  445. //}
  446. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  447. this.Ctx.Output.Download(downFile, fileName)
  448. defer func() {
  449. os.Remove(downFile)
  450. }()
  451. br.Ret = 200
  452. br.Success = true
  453. br.Msg = "success"
  454. }
  455. // CCFStockClassify
  456. // @Title CCF化纤信息-装置分类
  457. // @Description CCF化纤信息-装置分类
  458. // @Success 200 {object} data_manage.CCFStockClassify
  459. // @router /ccf/stock/classify [get]
  460. func (this *EdbInfoController) CCFStockClassify() {
  461. br := new(models.BaseResponse).Init()
  462. defer func() {
  463. if br.ErrMsg == "" {
  464. br.IsSendEmail = false
  465. }
  466. this.Data["json"] = br
  467. this.ServeJSON()
  468. }()
  469. sysUser := this.SysUser
  470. if sysUser == nil {
  471. br.Msg = "请登录"
  472. br.ErrMsg = "请登录,SysUser Is Empty"
  473. br.Ret = 408
  474. return
  475. }
  476. classifyOb := new(data_manage.CCFStockClassify)
  477. items, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "ccf_stock_classify_id ASC")
  478. if e != nil {
  479. br.Msg = "获取失败"
  480. br.ErrMsg = "获取CCF化纤分类列表失败, Err: " + e.Error()
  481. return
  482. }
  483. resp := make([]*data_manage.CCFStockClassifyItem, 0)
  484. for _, v := range items {
  485. resp = append(resp, v.Format2Item())
  486. }
  487. br.Ret = 200
  488. br.Success = true
  489. br.Msg = "获取成功"
  490. br.Data = resp
  491. }
  492. // CCFStockTable
  493. // @Title CCF化纤信息-装置表格详情
  494. // @Description CCF化纤信息-装置表格详情
  495. // @Param ClassifyId query int true "分类ID"
  496. // @Param TableDate query string false "表格日期"
  497. // @Success 200 {object} data_manage.CCFStockExcel
  498. // @router /ccf/stock/table [get]
  499. func (this *EdbInfoController) CCFStockTable() {
  500. br := new(models.BaseResponse).Init()
  501. defer func() {
  502. if br.ErrMsg == "" {
  503. br.IsSendEmail = false
  504. }
  505. this.Data["json"] = br
  506. this.ServeJSON()
  507. }()
  508. sysUser := this.SysUser
  509. if sysUser == nil {
  510. br.Msg = "请登录"
  511. br.ErrMsg = "请登录,SysUser Is Empty"
  512. br.Ret = 408
  513. return
  514. }
  515. classifyId, _ := this.GetInt("ClassifyId", 0)
  516. tableDate := this.GetString("TableDate")
  517. if tableDate != "" {
  518. _, e := time.ParseInLocation(utils.FormatDate, tableDate, time.Local)
  519. if e != nil {
  520. br.Msg = "日期格式有误"
  521. return
  522. }
  523. }
  524. excelOb := new(data_manage.CCFStockExcel)
  525. cond := ``
  526. pars := make([]interface{}, 0)
  527. if classifyId > 0 {
  528. cond += fmt.Sprintf(` AND %s = ?`, excelOb.Cols().ClassifyId)
  529. pars = append(pars, classifyId)
  530. }
  531. if tableDate != "" {
  532. cond += fmt.Sprintf(` AND %s = ?`, excelOb.Cols().ExcelDate)
  533. pars = append(pars, tableDate)
  534. }
  535. // 若无tableDate默认取最近的有数据的表格
  536. item, e := excelOb.GetItemByCondition(cond, pars, fmt.Sprintf("%s DESC", excelOb.Cols().ExcelDate))
  537. if e != nil {
  538. if e.Error() == utils.ErrNoRow() {
  539. br.Ret = 200
  540. br.Success = true
  541. br.Msg = "该日期暂无数据"
  542. br.Data = data_manage.CCFStockExcel{}
  543. return
  544. }
  545. br.Msg = "获取失败"
  546. br.ErrMsg = "获取CCF化纤装置表格失败, Err: " + e.Error()
  547. return
  548. }
  549. resp := item.Format2Item()
  550. br.Ret = 200
  551. br.Success = true
  552. br.Msg = "获取成功"
  553. br.Data = resp
  554. }