fenwei_data.go 28 KB

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