fenwei_data.go 28 KB

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