fenwei_data.go 27 KB

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