fenwei_data.go 28 KB

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