fenwei_data.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services/data"
  8. etaTrialService "eta/eta_api/services/eta_trial"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "github.com/tealeg/xlsx"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // FenweiClassify
  20. // @Title 汾渭数据分类
  21. // @Description 汾渭数据分类接口
  22. // @Success 200 {object} data_manage.BaseFromFenweiClassifyItem
  23. // @router /fenwei/classify [get]
  24. func (this *EdbInfoController) FenweiClassify() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. if br.ErrMsg == "" {
  28. br.IsSendEmail = false
  29. }
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. classifies, e := data_manage.GetBaseFromFenweiClassify()
  41. if e != nil {
  42. br.Msg = "获取失败"
  43. br.ErrMsg = "获取汾渭数据分类失败, Err: " + e.Error()
  44. return
  45. }
  46. classifyOb := new(data_manage.BaseFromFenweiClassify)
  47. resp := make([]*data_manage.BaseFromFenweiClassifyItem, 0)
  48. parentMap := make(map[int][]*data_manage.BaseFromFenweiClassifyItem)
  49. for _, v := range classifies {
  50. t := classifyOb.Format2Item(v)
  51. if v.ParentId == 0 {
  52. resp = append(resp, t)
  53. }
  54. if v.ParentId > 0 {
  55. if parentMap[v.ParentId] == nil {
  56. parentMap[v.ParentId] = make([]*data_manage.BaseFromFenweiClassifyItem, 0)
  57. }
  58. parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
  59. }
  60. }
  61. for _, v := range resp {
  62. v.Child = parentMap[v.ClassifyId]
  63. }
  64. br.Data = resp
  65. br.Ret = 200
  66. br.Success = true
  67. br.Msg = "获取成功"
  68. }
  69. // FenweiIndexData
  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. // @Param Frequency query string true "频率"
  76. // @Success 200 {object} data_manage.LzFrequency
  77. // @router /fenwei/index/data [get]
  78. func (this *EdbInfoController) FenweiIndexData() {
  79. br := new(models.BaseResponse).Init()
  80. defer func() {
  81. if br.ErrMsg == "" {
  82. br.IsSendEmail = false
  83. }
  84. this.Data["json"] = br
  85. this.ServeJSON()
  86. }()
  87. sysUser := this.SysUser
  88. if sysUser == nil {
  89. br.Msg = "请登录"
  90. br.ErrMsg = "请登录,SysUser Is Empty"
  91. br.Ret = 408
  92. return
  93. }
  94. pageSize, _ := this.GetInt("PageSize")
  95. currentIndex, _ := this.GetInt("CurrentIndex")
  96. var startSize int
  97. if pageSize <= 0 {
  98. pageSize = utils.PageSize20
  99. }
  100. if currentIndex <= 0 {
  101. currentIndex = 1
  102. }
  103. startSize = utils.StartIndex(currentIndex, pageSize)
  104. classifyId, _ := this.GetInt("ClassifyId")
  105. if classifyId < 0 {
  106. br.Msg = "请选择分类"
  107. br.ErrMsg = "请选择分类"
  108. return
  109. }
  110. frequency := this.GetString("Frequency")
  111. // 获取指标
  112. var condition string
  113. var pars []interface{}
  114. if classifyId >= 0 {
  115. condition += ` AND classify_id=? `
  116. pars = append(pars, classifyId)
  117. }
  118. if frequency != "" {
  119. condition += ` AND frequency=? `
  120. pars = append(pars, frequency)
  121. }
  122. indexes, err := data_manage.GetFenweiIndex(condition, pars)
  123. if err != nil {
  124. br.Msg = "获取数据失败"
  125. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  126. return
  127. }
  128. indexCodes := make([]string, 0)
  129. for _, v := range indexes {
  130. indexCodes = append(indexCodes, v.IndexCode)
  131. }
  132. indexCounts, e := data_manage.GetFenweiIndexDataCountGroup(indexCodes)
  133. if e != nil {
  134. br.Msg = "获取失败"
  135. br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
  136. return
  137. }
  138. countMap := make(map[string]int)
  139. for _, v := range indexCounts {
  140. countMap[v.IndexCode] = v.Count
  141. }
  142. // 判断是否存在于指标库
  143. edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_FENWEI, indexCodes)
  144. if err != nil {
  145. return
  146. }
  147. edbCodeMap := make(map[string]*data_manage.EdbInfo)
  148. for _, v := range edbCodeList {
  149. edbCodeMap[v.EdbCode] = v
  150. }
  151. resultList := make([]*data_manage.BaseFromFenweiIndexList, 0)
  152. for _, v := range indexes {
  153. product := new(data_manage.BaseFromFenweiIndexList)
  154. product.FenweiIndexId = v.FenweiIndexId
  155. product.ClassifyId = v.ClassifyId
  156. product.Unit = v.Unit
  157. product.IndexCode = v.IndexCode
  158. product.IndexName = v.IndexName
  159. product.Frequency = v.Frequency
  160. product.CreateTime = v.CreateTime
  161. product.ModifyTime = v.ModifyTime
  162. _, ok := edbCodeMap[v.IndexCode]
  163. if ok {
  164. product.EdbExist = true
  165. } else {
  166. product.EdbExist = false
  167. }
  168. total := countMap[v.IndexCode]
  169. page := paging.GetPaging(currentIndex, pageSize, total)
  170. dataList, e := data_manage.GetFenweiIndexData(v.IndexCode, startSize, pageSize)
  171. if e != nil {
  172. br.Msg = "获取数据失败"
  173. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  174. return
  175. }
  176. if dataList == nil {
  177. dataList = make([]*data_manage.BaseFromFenweiData, 0)
  178. }
  179. product.DataList = dataList
  180. product.Paging = page
  181. resultList = append(resultList, product)
  182. }
  183. br.Ret = 200
  184. br.Success = true
  185. br.Msg = "获取成功"
  186. br.Data = resultList
  187. }
  188. // FenweiSearchList
  189. // @Title Fenwei模糊搜索
  190. // @Description Fenwei模糊搜索
  191. // @Param Keyword query string ture "关键字搜索"
  192. // @Success 200 {object} data_manage.BaseFromFenweiIndexSearchItem
  193. // @router /fenwei/search_list [get]
  194. func (this *EdbInfoController) FenweiSearchList() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. if br.ErrMsg == "" {
  198. br.IsSendEmail = false
  199. }
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. sysUser := this.SysUser
  204. if sysUser == nil {
  205. br.Msg = "请登录"
  206. br.ErrMsg = "请登录,SysUser Is Empty"
  207. br.Ret = 408
  208. return
  209. }
  210. list := make([]*data_manage.BaseFromFenweiIndexSearchItem, 0)
  211. var err error
  212. // 关键字
  213. keyword := this.GetString("Keyword")
  214. if keyword != "" {
  215. keyWordArr := strings.Split(keyword, " ")
  216. if len(keyWordArr) > 0 {
  217. condition := ""
  218. for _, v := range keyWordArr {
  219. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  220. }
  221. list, err = data_manage.GetFenweiItemList(condition)
  222. if err != nil {
  223. br.ErrMsg = "获取失败,Err:" + err.Error()
  224. br.Msg = "获取失败"
  225. return
  226. }
  227. }
  228. } else {
  229. list, err = data_manage.GetFenweiItemList("")
  230. if err != nil {
  231. br.ErrMsg = "获取失败,Err:" + err.Error()
  232. br.Msg = "获取失败"
  233. return
  234. }
  235. }
  236. br.Ret = 200
  237. br.Success = true
  238. br.Msg = "获取成功"
  239. br.Data = list
  240. }
  241. // FenweiSingleData
  242. // @Title 获取Fenwei数据
  243. // @Description 获取Fenwei单条数据接口
  244. // @Param IndexCode query string true "指标唯一编码"
  245. // @Success 200 {object} models.BaseResponse
  246. // @router /fenwei/single_data [get]
  247. func (this *EdbInfoController) FenweiSingleData() {
  248. br := new(models.BaseResponse).Init()
  249. defer func() {
  250. if br.ErrMsg == "" {
  251. br.IsSendEmail = false
  252. }
  253. this.Data["json"] = br
  254. this.ServeJSON()
  255. }()
  256. sysUser := this.SysUser
  257. if sysUser == nil {
  258. br.Msg = "请登录"
  259. br.ErrMsg = "请登录,SysUser Is Empty"
  260. br.Ret = 408
  261. return
  262. }
  263. indexCode := this.GetString("IndexCode")
  264. indexInfo, err := data_manage.GetBaseFromFenweiIndexByIndexCode(indexCode)
  265. if err != nil {
  266. br.Msg = "获取指标信息失败"
  267. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  268. return
  269. }
  270. dataTmpList, err := data_manage.GetFenweiIndexDataByCode(indexCode)
  271. if err != nil {
  272. br.Msg = "获取数据失败"
  273. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  274. return
  275. }
  276. indexCodeList := []string{indexCode}
  277. edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_FENWEI, indexCodeList)
  278. if err != nil {
  279. return
  280. }
  281. var ret data_manage.FenweiSingleDataResp
  282. var dataList []*data_manage.FenweiSingleData
  283. ret.ClassifyId = indexInfo.ClassifyId
  284. ret.FenweiIndexId = indexInfo.FenweiIndexId
  285. ret.IndexCode = indexInfo.IndexCode
  286. ret.IndexName = indexInfo.IndexName
  287. ret.Frequency = indexInfo.Frequency
  288. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  289. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  290. ret.Unit = indexInfo.Unit
  291. for _, v := range dataTmpList {
  292. tmp := &data_manage.FenweiSingleData{
  293. Value: v.Value,
  294. DataTime: v.DataTime,
  295. }
  296. dataList = append(dataList, tmp)
  297. }
  298. ret.Data = dataList
  299. if len(edbCodeList) > 0 {
  300. ret.EdbExist = true
  301. } else {
  302. ret.EdbExist = false
  303. }
  304. br.Ret = 200
  305. br.Success = true
  306. br.Msg = "获取成功"
  307. br.Data = ret
  308. }
  309. // ExportFenweiList
  310. // @Title 导出Fenwei数据
  311. // @Description 导出Fenwei数据
  312. // @Param IndexName query string false "名称关键词"
  313. // @Param IndexCode query string false "指标唯一编码"
  314. // @Param ClassifyId query string true "分类"
  315. // @Success 200 导出成功
  316. // @router /fenwei/export [get]
  317. func (this *EdbInfoController) ExportFenweiList() {
  318. br := new(models.BaseResponse).Init()
  319. defer func() {
  320. if br.ErrMsg == "" {
  321. br.IsSendEmail = false
  322. }
  323. this.Data["json"] = br
  324. this.ServeJSON()
  325. }()
  326. sysUser := this.SysUser
  327. if sysUser == nil {
  328. br.Msg = "请重新登录"
  329. return
  330. }
  331. indexCode := this.GetString("IndexCode") //指标唯一编码
  332. classifyId, _ := this.GetInt("ClassifyId")
  333. //secNameList := make([]*models.EdbdataExportList, 0)
  334. dir, _ := os.Executable()
  335. exPath := filepath.Dir(dir)
  336. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  337. xlsxFile := xlsx.NewFile()
  338. var pars []interface{}
  339. condition := ""
  340. if classifyId > 0 {
  341. //获取指标
  342. condition += " AND classify_id=?"
  343. pars = append(pars, classifyId)
  344. }
  345. if indexCode != "" {
  346. //获取指标
  347. condition += " AND index_code=?"
  348. pars = append(pars, indexCode)
  349. }
  350. indexList, err := data_manage.GetFenweiIndex(condition, pars)
  351. if err != nil {
  352. fmt.Println("获取数据失败,Err:" + err.Error())
  353. return
  354. }
  355. if len(indexList) <= 0 {
  356. fmt.Println("indexList 为空")
  357. br.Ret = 200
  358. br.Success = true
  359. br.Msg = "success"
  360. return
  361. }
  362. sheetNew := new(xlsx.Sheet)
  363. // todo 分类名称
  364. sheetNew, err = xlsxFile.AddSheet("汾渭数据")
  365. //sheetNew.SetColWidth()
  366. //获取指标数据
  367. windRow := sheetNew.AddRow()
  368. secNameRow := sheetNew.AddRow()
  369. indexCodeRow := sheetNew.AddRow()
  370. frequencyRow := sheetNew.AddRow()
  371. unitRow := sheetNew.AddRow()
  372. lastModifyDateRow := sheetNew.AddRow()
  373. //获取分类下指标最大数据量
  374. var dataMax int
  375. setRowIndex := 6
  376. indexCodeList := make([]string, 0)
  377. for _, v := range indexList {
  378. indexCodeList = append(indexCodeList, v.IndexCode)
  379. }
  380. dataListMap := make(map[string][]*data_manage.BaseFromFenweiData)
  381. if len(indexList) > 0 {
  382. allDataList, e := data_manage.GetFenweiIndexDataByCodes(indexCodeList)
  383. if e != nil {
  384. br.Msg = "获取数据失败"
  385. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  386. return
  387. }
  388. for _, v := range allDataList {
  389. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  390. }
  391. for _, v := range dataListMap {
  392. if len(v) > dataMax {
  393. dataMax = len(v)
  394. }
  395. }
  396. }
  397. for k, sv := range indexList {
  398. //获取数据
  399. dataList, ok := dataListMap[sv.IndexCode]
  400. if !ok {
  401. continue
  402. }
  403. if len(dataList) > 0 {
  404. windRow.AddCell().SetValue("汾渭数据")
  405. secNameRow.AddCell().SetValue("指标名称")
  406. indexCodeRow.AddCell().SetValue("指标ID")
  407. frequencyRow.AddCell().SetValue("频率")
  408. unitRow.AddCell().SetValue("单位")
  409. lastModifyDateRow.AddCell().SetValue("更新时间")
  410. secNameRow.AddCell().SetValue(sv.IndexName)
  411. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  412. frequencyRow.AddCell().SetValue(sv.Frequency)
  413. unitRow.AddCell().SetValue(sv.Unit)
  414. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  415. windRow.AddCell()
  416. windRow.AddCell()
  417. secNameRow.AddCell()
  418. indexCodeRow.AddCell()
  419. frequencyRow.AddCell()
  420. unitRow.AddCell()
  421. lastModifyDateRow.AddCell()
  422. min := k * 3
  423. sheetNew.SetColWidth(min, min, 15)
  424. if len(dataList) <= 0 {
  425. for n := 0; n < dataMax; n++ {
  426. rowIndex := setRowIndex + n
  427. row := sheetNew.Row(rowIndex)
  428. row.AddCell()
  429. row.AddCell()
  430. row.AddCell()
  431. }
  432. } else {
  433. endRowIndex := 0
  434. for rk, dv := range dataList {
  435. rowIndex := setRowIndex + rk
  436. row := sheetNew.Row(rowIndex)
  437. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  438. displayDateCell := row.AddCell()
  439. style := new(xlsx.Style)
  440. style.ApplyAlignment = true
  441. style.Alignment.WrapText = true
  442. displayDateCell.SetStyle(style)
  443. displayDateCell.SetDate(displayDate)
  444. row.AddCell().SetValue(dv.Value)
  445. row.AddCell()
  446. endRowIndex = rowIndex
  447. }
  448. if len(dataList) < dataMax {
  449. dataLen := dataMax - len(dataList)
  450. for n := 0; n < dataLen; n++ {
  451. rowIndex := (endRowIndex + 1) + n
  452. row := sheetNew.Row(rowIndex)
  453. row.AddCell()
  454. row.AddCell()
  455. row.AddCell()
  456. }
  457. }
  458. }
  459. }
  460. }
  461. err = xlsxFile.Save(downLoadnFilePath)
  462. if err != nil {
  463. //有指标无数据时先导出一遍空表
  464. sheet, err := xlsxFile.AddSheet("无数据")
  465. if err != nil {
  466. br.Msg = "新增Sheet失败"
  467. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  468. return
  469. }
  470. rowSecName := sheet.AddRow()
  471. celSecName := rowSecName.AddCell()
  472. celSecName.SetValue("")
  473. err = xlsxFile.Save(downLoadnFilePath)
  474. if err != nil {
  475. br.Msg = "保存文件失败"
  476. br.ErrMsg = "保存文件失败"
  477. return
  478. }
  479. }
  480. fileName := `汾渭数据`
  481. if len(indexList) > 0 {
  482. fileName = indexList[0].IndexName
  483. }
  484. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  485. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  486. defer func() {
  487. os.Remove(downLoadnFilePath)
  488. }()
  489. br.Ret = 200
  490. br.Success = true
  491. br.Msg = "success"
  492. }
  493. // GetFenWeiFrequencyList
  494. // @Title 查询频率列表
  495. // @Description 查询频率列表
  496. // @Param classifyId query int false "指标唯一编码"
  497. // @Success 200 {object} []string
  498. // @router /fenwei/frequency/list [get]
  499. func (this *EdbInfoController) GetFenWeiFrequencyList() {
  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")
  516. frequencyList, err := data_manage.GetFenWeiIndexFrequency(classifyId)
  517. if err != nil {
  518. return
  519. }
  520. br.Ret = 200
  521. br.Success = true
  522. br.Msg = "获取成功"
  523. br.Data = frequencyList
  524. }
  525. // FenWeiIndexAddValidate
  526. // @Title 新增加入到指标库校验
  527. // @Description 新增加入到指标库校验
  528. // @Param req body data_manage.BaseFromFenWeiIndexBatchAddCheckReq true "请求参数"
  529. // @Success 200 {object} []data_manage.IndexCheckData
  530. // @router /fenwei/index/add/validate [post]
  531. func (this *EdbInfoController) FenWeiIndexAddValidate() {
  532. br := new(models.BaseResponse).Init()
  533. defer func() {
  534. if br.ErrMsg == "" {
  535. br.IsSendEmail = false
  536. }
  537. this.Data["json"] = br
  538. this.ServeJSON()
  539. }()
  540. sysUser := this.SysUser
  541. if sysUser == nil {
  542. br.Msg = "请登录"
  543. br.ErrMsg = "请登录,SysUser Is Empty"
  544. br.Ret = 408
  545. return
  546. }
  547. var req *data_manage.BaseFromFenWeiIndexBatchAddCheckReq
  548. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  549. br.Msg = "参数解析异常!"
  550. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  551. return
  552. }
  553. codeLen := len(req.IndexCodes)
  554. var codeMax = 30
  555. if codeLen > codeMax {
  556. br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  557. br.ErrMsg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  558. return
  559. }
  560. // 校验指标编码是否存在
  561. addValidate, err := data.FenWeiIndexAddValidate(req.IndexCodes)
  562. if err != nil {
  563. return
  564. }
  565. br.Data = addValidate
  566. br.Ret = 200
  567. br.Success = true
  568. br.Msg = "操作成功"
  569. }
  570. // FenWeiIndexAdd
  571. // @Title 指标添加到指标库
  572. // @Description 指标添加到指标库
  573. // @Param req body []data_manage.AddEdbInfoReq true "请求参数"
  574. // @Success 200 string "操作成功"
  575. // @router /fenwei/index/add [post]
  576. func (this *EdbInfoController) FenWeiIndexAdd() {
  577. br := new(models.BaseResponse).Init()
  578. defer func() {
  579. if br.ErrMsg == "" {
  580. br.IsSendEmail = false
  581. }
  582. this.Data["json"] = br
  583. this.ServeJSON()
  584. }()
  585. sysUser := this.SysUser
  586. if sysUser == nil {
  587. br.Msg = "请登录"
  588. br.ErrMsg = "请登录,SysUser Is Empty"
  589. br.Ret = 408
  590. return
  591. }
  592. deleteCache := true
  593. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_FENWEI_" + strconv.Itoa(sysUser.AdminId)
  594. defer func() {
  595. if deleteCache {
  596. _ = utils.Rc.Delete(cacheKey)
  597. }
  598. }()
  599. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  600. deleteCache = false
  601. br.Msg = "系统处理中,请稍后重试!"
  602. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  603. return
  604. }
  605. var req []*data_manage.AddEdbInfoReq
  606. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  607. br.Msg = "参数解析异常!"
  608. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  609. return
  610. }
  611. if len(req) == 0 {
  612. br.Msg = "请选择指标"
  613. return
  614. }
  615. codeMax := 30
  616. if len(req) > codeMax {
  617. br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  618. return
  619. }
  620. indexNames := make([]string, 0)
  621. resp := make([]*data_manage.FenWeiNameCheckResult, 0)
  622. for _, index := range req {
  623. index.EdbCode = strings.TrimSpace(index.EdbCode)
  624. if index.EdbCode == "" {
  625. br.Msg = "指标ID不可为空"
  626. return
  627. }
  628. index.EdbName = strings.TrimSpace(index.EdbName)
  629. if index.EdbName == "" {
  630. br.Msg = "请输入指标名称"
  631. return
  632. }
  633. index.Frequency = strings.TrimSpace(index.Frequency)
  634. if index.Frequency == "" {
  635. br.Msg = "请选择频度"
  636. return
  637. }
  638. index.Unit = strings.TrimSpace(index.Unit)
  639. if index.Unit == "" {
  640. br.Msg = "请输入单位"
  641. return
  642. }
  643. if index.ClassifyId <= 0 {
  644. br.Msg = "请选择分类"
  645. return
  646. }
  647. indexNames = append(indexNames, index.EdbName)
  648. resp = append(resp, &data_manage.FenWeiNameCheckResult{
  649. IndexCode: index.EdbCode,
  650. IndexName: index.EdbName,
  651. Exist: false,
  652. })
  653. }
  654. // 指标名称重复校验
  655. nameCheck, err := data.FenWeiIndexNameCheck(indexNames, resp)
  656. if err != nil {
  657. br.Msg = err.Error()
  658. br.ErrMsg = err.Error()
  659. return
  660. }
  661. for _, v := range nameCheck {
  662. if v.Exist {
  663. br.Msg = "指标名称重复"
  664. br.Data = nameCheck
  665. br.Ret = 200
  666. br.Success = true
  667. return
  668. }
  669. }
  670. for _, v := range req {
  671. var fenWeiIndexAddReq data_manage.FenWeiIndexAddReq
  672. fenWeiIndexAddReq.EdbCode = v.EdbCode
  673. fenWeiIndexAddReq.EdbName = v.EdbName
  674. fenWeiIndexAddReq.Frequency = v.Frequency
  675. fenWeiIndexAddReq.Unit = v.Unit
  676. fenWeiIndexAddReq.ClassifyId = v.ClassifyId
  677. fenWeiIndexAddReq.AdminId = sysUser.AdminId
  678. fenWeiIndexAddReq.AdminRealName = sysUser.RealName
  679. // 新增指标到指标库
  680. edbInfo, e, errMsg, skip := data.FenWeiIndexAdd(fenWeiIndexAddReq, this.Lang)
  681. if e != nil {
  682. br.Msg = "操作失败"
  683. if errMsg != "" {
  684. br.Msg = errMsg
  685. }
  686. br.ErrMsg = e.Error()
  687. return
  688. }
  689. if skip {
  690. continue
  691. }
  692. // todo 下面两段代码能否抽离出来???
  693. // 试用平台更新用户累计新增指标数
  694. if utils.BusinessCode == utils.BusinessCodeSandbox {
  695. go func() {
  696. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  697. if e != nil {
  698. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  699. utils.FileLog.Info(tips)
  700. return
  701. }
  702. if adminItem.DepartmentName != "ETA试用客户" {
  703. return
  704. }
  705. var ur etaTrialService.EtaTrialUserReq
  706. ur.Mobile = adminItem.Mobile
  707. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  708. }()
  709. }
  710. // 新增操作日志
  711. {
  712. edbLog := new(data_manage.EdbInfoLog)
  713. edbLog.EdbInfoId = edbInfo.EdbInfoId
  714. edbLog.SourceName = edbInfo.SourceName
  715. edbLog.Source = edbInfo.Source
  716. edbLog.EdbCode = edbInfo.EdbCode
  717. edbLog.EdbName = edbInfo.EdbName
  718. edbLog.ClassifyId = edbInfo.ClassifyId
  719. edbLog.SysUserId = sysUser.AdminId
  720. edbLog.SysUserRealName = sysUser.RealName
  721. edbLog.CreateTime = time.Now()
  722. edbLog.Content = string(this.Ctx.Input.RequestBody)
  723. edbLog.Status = "新增指标"
  724. edbLog.Method = this.Ctx.Input.URI()
  725. go data_manage.AddEdbInfoLog(edbLog)
  726. }
  727. }
  728. br.Msg = "操作成功"
  729. br.Ret = 200
  730. br.Success = true
  731. br.IsAddLog = true
  732. }
  733. // FenWeiIndexDataExport
  734. // @Title 导出指标数据
  735. // @Description 导出指标数据
  736. // @Param IndexCode query string false "指标编码"
  737. // @Param ClassifyId query int false "分类ID"
  738. // @Success 200 string "操作成功"
  739. // @router /fenwei/index/data/export [get]
  740. func (this *EdbInfoController) FenWeiIndexDataExport() {
  741. br := new(models.BaseResponse).Init()
  742. defer func() {
  743. this.Data["json"] = br
  744. this.ServeJSON()
  745. }()
  746. sysUser := this.SysUser
  747. if sysUser == nil {
  748. br.Msg = "请重新登录"
  749. return
  750. }
  751. classifyId, _ := this.GetInt("ClassifyId") //分类
  752. indexCode := this.GetString("IndexCode") //指标唯一编码
  753. if classifyId < 0 {
  754. br.Msg = "请选择分类"
  755. return
  756. }
  757. //userId := sysUser.AdminId
  758. //超管账号可以查看分类下的所有频度数据
  759. /*if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  760. userId = 0
  761. }*/
  762. //获取账户所拥有权限的分类id集合
  763. /*classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  764. if err != nil {
  765. br.Msg = "获取分类数据失败"
  766. return
  767. }*/
  768. dir, _ := os.Executable()
  769. exPath := filepath.Dir(dir)
  770. downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  771. xlsxFile := xlsx.NewFile()
  772. frequencies, err := data_manage.GetFenWeiIndexFrequency(classifyId)
  773. if err != nil {
  774. br.Msg = "查询频度失败"
  775. br.ErrMsg = "查询频度失败"
  776. return
  777. }
  778. fileName := `汾渭网页数据`
  779. if classifyId > 0 && indexCode == "" {
  780. fenWeiClassify, err := data_manage.GetFenweiClassifyItemByClassifyId(classifyId)
  781. if err != nil {
  782. return
  783. }
  784. fileName = fenWeiClassify.ClassifyName
  785. }
  786. if frequencies == nil {
  787. sheet, err := xlsxFile.AddSheet("无数据")
  788. if err != nil {
  789. br.Msg = "新增Sheet失败"
  790. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  791. return
  792. }
  793. rowSecName := sheet.AddRow()
  794. celSecName := rowSecName.AddCell()
  795. celSecName.SetValue("")
  796. }
  797. for _, frequency := range frequencies {
  798. fenWeiIndices, err := data_manage.GetFenWeiIndexByCodeAndClassify(indexCode, classifyId, frequency)
  799. if err != nil {
  800. return
  801. }
  802. var sheet *xlsx.Sheet
  803. if len(fenWeiIndices) > 0 {
  804. sheet, err = xlsxFile.AddSheet(*frequency)
  805. if err != nil {
  806. br.Msg = "新增Sheet失败"
  807. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  808. return
  809. }
  810. } else {
  811. continue
  812. }
  813. if indexCode != "" {
  814. fileName = fenWeiIndices[0].IndexName
  815. }
  816. //获取指标数据
  817. rowSecName := sheet.AddRow()
  818. celSecName := rowSecName.AddCell()
  819. celSecName.SetValue("指标名称")
  820. rowFrequency := sheet.AddRow()
  821. celFrequency := rowFrequency.AddCell()
  822. celFrequency.SetValue("频率")
  823. rowUnit := sheet.AddRow()
  824. celUnit := rowUnit.AddCell()
  825. celUnit.SetValue("单位")
  826. rowModifyDate := sheet.AddRow()
  827. rowModifyCell := rowModifyDate.AddCell()
  828. rowModifyCell.SetValue("更新时间")
  829. dataMap := make(map[string]map[string]*data_manage.BaseFromFenweiData)
  830. var tradeCodeList []string
  831. for _, v := range fenWeiIndices {
  832. cellSenName := rowSecName.AddCell()
  833. cellSenName.SetValue(v.IndexName)
  834. celFrequency := rowFrequency.AddCell()
  835. celFrequency.SetValue(v.Frequency)
  836. celUnit := rowUnit.AddCell()
  837. celUnit.SetValue(v.Unit)
  838. rowModifyCell := rowModifyDate.AddCell()
  839. rowModifyCell.SetValue(v.ModifyTime)
  840. tradeCodeList = append(tradeCodeList, v.IndexCode)
  841. var dataList []*data_manage.BaseFromFenweiData
  842. dataList, err = data_manage.GetBaseFromFenWeiDataByIndexCode(v.IndexCode)
  843. if err != nil && err.Error() != utils.ErrNoRow() {
  844. br.ErrMsg = "GetBaseFromFenWeiDataByIndexCode,Err:" + err.Error()
  845. br.Msg = "获取数据失败"
  846. return
  847. }
  848. for _, item := range dataList {
  849. if dataMap[item.IndexCode] == nil {
  850. dataMap[item.IndexCode] = make(map[string]*data_manage.BaseFromFenweiData)
  851. }
  852. dataMap[item.IndexCode][item.DataTime] = item
  853. }
  854. }
  855. tradeCodeStr := strings.Join(tradeCodeList, "','")
  856. tradeCodeStr = "'" + tradeCodeStr + "'"
  857. dataTimeList, err := data_manage.GetFenWeiDataListByIndexCodes(tradeCodeStr)
  858. if err != nil {
  859. br.Msg = "获取数据失败"
  860. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  861. return
  862. }
  863. for _, dataTime := range dataTimeList {
  864. rowData := sheet.AddRow()
  865. celDate := rowData.AddCell()
  866. celDate.SetValue(dataTime)
  867. for _, m := range fenWeiIndices {
  868. celData := rowData.AddCell()
  869. if dataMap[m.IndexCode][dataTime] != nil {
  870. celData.SetValue(dataMap[m.IndexCode][dataTime].Value)
  871. }
  872. }
  873. }
  874. }
  875. err = xlsxFile.Save(downLoadFilePath)
  876. if err != nil {
  877. //有指标无数据时先导出一遍空表
  878. sheet, err := xlsxFile.AddSheet("无数据")
  879. if err != nil {
  880. br.Msg = "新增Sheet失败"
  881. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  882. return
  883. }
  884. rowSecName := sheet.AddRow()
  885. celSecName := rowSecName.AddCell()
  886. celSecName.SetValue("")
  887. err = xlsxFile.Save(downLoadFilePath)
  888. if err != nil {
  889. br.Msg = "保存文件失败"
  890. br.ErrMsg = "保存文件失败"
  891. return
  892. }
  893. }
  894. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  895. this.Ctx.Output.Download(downLoadFilePath, fileName)
  896. defer func() {
  897. os.Remove(downLoadFilePath)
  898. }()
  899. br.Ret = 200
  900. br.Success = true
  901. br.Msg = "success"
  902. }
  903. // GetFenWeiIndexInfo
  904. // @Title 添加指标-根据条件获取指标信息
  905. // @Description 添加指标-根据条件获取指标信息
  906. // @Param KeyWord query string false "关键字"
  907. // @Param ClassifyIds query string false "分类id"
  908. // @Param Frequencies query string false "频率"
  909. // @Param PageSize query int false "每页数据条数"
  910. // @Param CurrentIndex query int false "当前页页码,从1开始"
  911. // @Success 200 {object} data_manage.BaseFromFenWeiIndexPage
  912. // @router /fenwei/get/index/info [get]
  913. func (this *EdbInfoController) GetFenWeiIndexInfo() {
  914. br := new(models.BaseResponse).Init()
  915. defer func() {
  916. if br.ErrMsg == "" {
  917. br.IsSendEmail = false
  918. }
  919. this.Data["json"] = br
  920. this.ServeJSON()
  921. }()
  922. sysUser := this.SysUser
  923. if sysUser == nil {
  924. br.Msg = "请登录"
  925. br.ErrMsg = "请登录,SysUser Is Empty"
  926. br.Ret = 408
  927. return
  928. }
  929. pageSize, _ := this.GetInt("PageSize")
  930. currentIndex, _ := this.GetInt("CurrentIndex")
  931. if pageSize <= 0 {
  932. pageSize = utils.PageSize20
  933. }
  934. if currentIndex <= 0 {
  935. currentIndex = 1
  936. }
  937. keyWord := this.GetString("KeyWord")
  938. classifyIds := this.GetString("ClassifyIds")
  939. frequencies := this.GetString("Frequencies")
  940. classifyIdList := strings.Split(classifyIds, ",")
  941. frequencyList := strings.Split(frequencies, ",")
  942. indexInfoPage, err := data.GetFenWeiIndexInfo(keyWord, classifyIdList, frequencyList, currentIndex, pageSize)
  943. if err != nil {
  944. return
  945. }
  946. br.Ret = 200
  947. br.Success = true
  948. br.Msg = "获取成功"
  949. br.Data = indexInfoPage
  950. }