base_from_ly_index_controller.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Package data_manage
  2. // @Author gmy 2024/8/12 14:31:00
  3. package data_manage
  4. import (
  5. "encoding/json"
  6. "eta/eta_api/controllers"
  7. "eta/eta_api/models"
  8. "eta/eta_api/models/data_manage"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data"
  11. etaTrialService "eta/eta_api/services/eta_trial"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/tealeg/xlsx"
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type BaseFromLyIndexController struct {
  22. controllers.BaseAuthController
  23. }
  24. var (
  25. codeMax = 30 // 指标编码最大数量
  26. )
  27. // LyIndexList
  28. // @Title 获取指标列表
  29. // @Description 获取指标列表
  30. // @Success 200 {object} []data_manage.BaseFromLyIndexPage
  31. // @router /ly/index/list [get]
  32. func (this *BaseFromLyIndexController) LyIndexList() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. if br.ErrMsg == "" {
  36. br.IsSendEmail = false
  37. }
  38. this.Data["json"] = br
  39. this.ServeJSON()
  40. }()
  41. sysUser := this.SysUser
  42. if sysUser == nil {
  43. br.Msg = "请登录"
  44. br.ErrMsg = "请登录,SysUser Is Empty"
  45. br.Ret = 408
  46. return
  47. }
  48. pageSize, _ := this.GetInt("PageSize")
  49. currentIndex, _ := this.GetInt("CurrentIndex")
  50. searchParam := this.GetString("SearchParam")
  51. classifyId := this.GetString("ClassifyId")
  52. if pageSize <= 0 {
  53. pageSize = utils.PageSize20
  54. }
  55. if currentIndex <= 0 {
  56. currentIndex = 1
  57. }
  58. // 获取指标列表
  59. indexList, err := data.GetIndexPage(classifyId, searchParam, currentIndex, pageSize)
  60. if err != nil {
  61. br.Msg = "获取指标列表失败"
  62. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  63. return
  64. }
  65. br.Data = indexList
  66. br.Ret = 200
  67. br.Success = true
  68. br.Msg = "获取成功"
  69. }
  70. // LyIndexDataList
  71. // @Title 获取指标对应的数据列表
  72. // @Description 获取指标对应的数据列表
  73. // @Success 200 {object} data_manage.BaseFromLyDataPage
  74. // @router /ly/index/data/list [get]
  75. func (this *BaseFromLyIndexController) LyIndexDataList() {
  76. br := new(models.BaseResponse).Init()
  77. defer func() {
  78. if br.ErrMsg == "" {
  79. br.IsSendEmail = false
  80. }
  81. this.Data["json"] = br
  82. this.ServeJSON()
  83. }()
  84. sysUser := this.SysUser
  85. if sysUser == nil {
  86. br.Msg = "请登录"
  87. br.ErrMsg = "请登录,SysUser Is Empty"
  88. br.Ret = 408
  89. return
  90. }
  91. // 获取指标数据列表
  92. indexId, err := this.GetInt("IndexId")
  93. if err != nil {
  94. br.Msg = "获取指标ID失败"
  95. br.ErrMsg = "获取指标ID失败,Err:" + err.Error()
  96. return
  97. }
  98. pageSize, _ := this.GetInt("PageSize")
  99. currentIndex, _ := this.GetInt("CurrentIndex")
  100. if pageSize <= 0 {
  101. pageSize = utils.PageSize20
  102. }
  103. if currentIndex <= 0 {
  104. currentIndex = 1
  105. }
  106. indexDataList, err := data.GetIndexDataPage(indexId, currentIndex, pageSize)
  107. if err != nil {
  108. br.Msg = "获取指标数据列表失败"
  109. br.ErrMsg = "获取指标数据列表失败,Err:" + err.Error()
  110. return
  111. }
  112. br.Data = indexDataList
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "获取成功"
  116. }
  117. // LyIndexAddValidate
  118. // @Title 新增加入到指标库校验
  119. // @Description 新增加入到指标库校验
  120. // @Success 200 {object} []data_manage.IndexCheckData
  121. // @router /ly/index/add/validate [post]
  122. func (this *BaseFromLyIndexController) LyIndexAddValidate() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. if br.ErrMsg == "" {
  126. br.IsSendEmail = false
  127. }
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. sysUser := this.SysUser
  132. if sysUser == nil {
  133. br.Msg = "请登录"
  134. br.ErrMsg = "请登录,SysUser Is Empty"
  135. br.Ret = 408
  136. return
  137. }
  138. var req *data_manage.BaseFromLyIndexBatchAddCheckReq
  139. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  140. br.Msg = "参数解析异常!"
  141. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  142. return
  143. }
  144. codeLen := len(req.IndexCodes)
  145. if codeLen > codeMax {
  146. br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  147. br.ErrMsg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  148. return
  149. }
  150. // 校验指标编码是否存在
  151. addValidate, err := data.LyIndexAddValidate(req.IndexCodes)
  152. if err != nil {
  153. return
  154. }
  155. br.Data = addValidate
  156. br.Ret = 200
  157. br.Success = true
  158. br.Msg = "操作成功"
  159. }
  160. // LyIndexAdd
  161. // @Title 指标添加到指标库
  162. // @Description 指标添加到指标库
  163. // @Success 200 string "操作成功"
  164. // @router /ly/index/add [post]
  165. func (this *BaseFromLyIndexController) LyIndexAdd() {
  166. br := new(models.BaseResponse).Init()
  167. defer func() {
  168. if br.ErrMsg == "" {
  169. br.IsSendEmail = false
  170. }
  171. this.Data["json"] = br
  172. this.ServeJSON()
  173. }()
  174. sysUser := this.SysUser
  175. if sysUser == nil {
  176. br.Msg = "请登录"
  177. br.ErrMsg = "请登录,SysUser Is Empty"
  178. br.Ret = 408
  179. return
  180. }
  181. deleteCache := true
  182. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_LY_" + strconv.Itoa(sysUser.AdminId)
  183. defer func() {
  184. if deleteCache {
  185. _ = utils.Rc.Delete(cacheKey)
  186. }
  187. }()
  188. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  189. deleteCache = false
  190. br.Msg = "系统处理中,请稍后重试!"
  191. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  192. return
  193. }
  194. var req []*data_manage.AddEdbInfoReq
  195. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  196. br.Msg = "参数解析异常!"
  197. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  198. return
  199. }
  200. if len(req) == 0 {
  201. br.Msg = "请选择指标"
  202. return
  203. }
  204. if len(req) > codeMax {
  205. br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
  206. return
  207. }
  208. indexNames := make([]string, 0)
  209. resp := make([]*data_manage.NameCheckResult, 0)
  210. for _, index := range req {
  211. index.EdbCode = strings.TrimSpace(index.EdbCode)
  212. if index.EdbCode == "" {
  213. br.Msg = "指标ID不可为空"
  214. return
  215. }
  216. index.EdbName = strings.TrimSpace(index.EdbName)
  217. if index.EdbName == "" {
  218. br.Msg = "请输入指标名称"
  219. return
  220. }
  221. index.Frequency = strings.TrimSpace(index.Frequency)
  222. if index.Frequency == "" {
  223. br.Msg = "请选择频度"
  224. return
  225. }
  226. index.Unit = strings.TrimSpace(index.Unit)
  227. if index.Unit == "" {
  228. br.Msg = "请输入单位"
  229. return
  230. }
  231. if index.ClassifyId <= 0 {
  232. br.Msg = "请选择分类"
  233. return
  234. }
  235. indexNames = append(indexNames, index.EdbName)
  236. resp = append(resp, &data_manage.NameCheckResult{
  237. IndexCode: index.EdbCode,
  238. IndexName: index.EdbName,
  239. Exist: false,
  240. })
  241. }
  242. // 指标名称重复校验
  243. nameCheck, err := data.LyIndexNameCheck(indexNames, resp)
  244. if err != nil {
  245. br.Msg = err.Error()
  246. br.ErrMsg = err.Error()
  247. return
  248. }
  249. for _, v := range nameCheck {
  250. if v.Exist {
  251. br.Msg = "指标名称重复"
  252. br.Data = nameCheck
  253. br.Ret = 200
  254. br.Success = true
  255. return
  256. }
  257. }
  258. for _, v := range req {
  259. var r data.LyIndexAddReq
  260. r.EdbCode = v.EdbCode
  261. r.EdbName = v.EdbName
  262. r.Frequency = v.Frequency
  263. r.Unit = v.Unit
  264. r.ClassifyId = v.ClassifyId
  265. r.AdminId = sysUser.AdminId
  266. r.AdminRealName = sysUser.RealName
  267. // 新增指标到指标库
  268. edbInfo, e, errMsg, skip := data.LyIndexAdd(r, this.Lang)
  269. if e != nil {
  270. br.Msg = "操作失败"
  271. if errMsg != "" {
  272. br.Msg = errMsg
  273. }
  274. br.ErrMsg = e.Error()
  275. return
  276. }
  277. if skip {
  278. continue
  279. }
  280. // 试用平台更新用户累计新增指标数
  281. if utils.BusinessCode == utils.BusinessCodeSandbox {
  282. go func() {
  283. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  284. if e != nil {
  285. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  286. utils.FileLog.Info(tips)
  287. return
  288. }
  289. if adminItem.DepartmentName != "ETA试用客户" {
  290. return
  291. }
  292. var ur etaTrialService.EtaTrialUserReq
  293. ur.Mobile = adminItem.Mobile
  294. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  295. }()
  296. }
  297. // 新增操作日志
  298. {
  299. edbLog := new(data_manage.EdbInfoLog)
  300. edbLog.EdbInfoId = edbInfo.EdbInfoId
  301. edbLog.SourceName = edbInfo.SourceName
  302. edbLog.Source = edbInfo.Source
  303. edbLog.EdbCode = edbInfo.EdbCode
  304. edbLog.EdbName = edbInfo.EdbName
  305. edbLog.ClassifyId = edbInfo.ClassifyId
  306. edbLog.SysUserId = sysUser.AdminId
  307. edbLog.SysUserRealName = sysUser.RealName
  308. edbLog.CreateTime = time.Now()
  309. edbLog.Content = string(this.Ctx.Input.RequestBody)
  310. edbLog.Status = "新增指标"
  311. edbLog.Method = this.Ctx.Input.URI()
  312. go data_manage.AddEdbInfoLog(edbLog)
  313. }
  314. }
  315. br.Msg = "操作成功"
  316. br.Ret = 200
  317. br.Success = true
  318. br.IsAddLog = true
  319. }
  320. // LyIndexDataExport
  321. // @Title 导出指标数据
  322. // @Description 导出指标数据
  323. // @Success 200 string "操作成功"
  324. // @router /ly/index/data/export [get]
  325. func (this *BaseFromLyIndexController) LyIndexDataExport() {
  326. br := new(models.BaseResponse).Init()
  327. defer func() {
  328. this.Data["json"] = br
  329. this.ServeJSON()
  330. }()
  331. sysUser := this.SysUser
  332. if sysUser == nil {
  333. br.Msg = "请重新登录"
  334. return
  335. }
  336. classifyId, _ := this.GetInt("ClassifyId") //分类
  337. indexCode := this.GetString("IndexCode") //指标唯一编码
  338. //userId := sysUser.AdminId
  339. //超管账号可以查看分类下的所有频度数据
  340. /*if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  341. userId = 0
  342. }*/
  343. //获取账户所拥有权限的分类id集合
  344. /*classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  345. if err != nil {
  346. br.Msg = "获取分类数据失败"
  347. return
  348. }*/
  349. dir, _ := os.Executable()
  350. exPath := filepath.Dir(dir)
  351. downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  352. xlsxFile := xlsx.NewFile()
  353. frequencies, err := data_manage.GetLyIndexFrequency(classifyId)
  354. if err != nil {
  355. br.Msg = "查询频度失败"
  356. br.ErrMsg = "查询频度失败"
  357. return
  358. }
  359. fileName := `粮油商务网`
  360. if classifyId > 0 && indexCode == "" {
  361. lyClassify, err := data_manage.GetLyClassifyById(classifyId)
  362. if err != nil {
  363. return
  364. }
  365. fileName = lyClassify.ClassifyName
  366. }
  367. if frequencies == nil {
  368. sheet, err := xlsxFile.AddSheet("无数据")
  369. if err != nil {
  370. br.Msg = "新增Sheet失败"
  371. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  372. return
  373. }
  374. rowSecName := sheet.AddRow()
  375. celSecName := rowSecName.AddCell()
  376. celSecName.SetValue("")
  377. }
  378. for _, frequency := range frequencies {
  379. lyIndices, err := data_manage.GetLyIndexByCodeAndClassify(indexCode, classifyId, frequency)
  380. if err != nil {
  381. return
  382. }
  383. var sheet *xlsx.Sheet
  384. if len(lyIndices) > 0 {
  385. sheet, err = xlsxFile.AddSheet(*frequency)
  386. if err != nil {
  387. br.Msg = "新增Sheet失败"
  388. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  389. return
  390. }
  391. } else {
  392. continue
  393. }
  394. if indexCode != "" {
  395. fileName = lyIndices[0].IndexName
  396. }
  397. //获取指标数据
  398. rowSecName := sheet.AddRow()
  399. celSecName := rowSecName.AddCell()
  400. celSecName.SetValue("指标名称/Metric Name")
  401. rowFrequency := sheet.AddRow()
  402. celFrequency := rowFrequency.AddCell()
  403. celFrequency.SetValue("频度/Frequency")
  404. rowUnit := sheet.AddRow()
  405. celUnit := rowUnit.AddCell()
  406. celUnit.SetValue("单位/Unit")
  407. rowModifyDate := sheet.AddRow()
  408. rowModifyCell := rowModifyDate.AddCell()
  409. rowModifyCell.SetValue("更新时间/Update Time")
  410. dataMap := make(map[string]map[string]*data_manage.BaseFromLyData)
  411. var tradeCodeList []string
  412. for _, v := range lyIndices {
  413. cellSenName := rowSecName.AddCell()
  414. cellSenName.SetValue(v.IndexName)
  415. celFrequency := rowFrequency.AddCell()
  416. celFrequency.SetValue(v.Frequency)
  417. celUnit := rowUnit.AddCell()
  418. celUnit.SetValue(v.Unit)
  419. rowModifyCell := rowModifyDate.AddCell()
  420. rowModifyCell.SetValue(v.ModifyTime)
  421. tradeCodeList = append(tradeCodeList, v.IndexCode)
  422. var lyDataList []*data_manage.BaseFromLyData
  423. lyDataList, err = data_manage.GetBaseFromLyDataByIndexCode(v.IndexCode)
  424. if err != nil && err.Error() != utils.ErrNoRow() {
  425. br.ErrMsg = "GetBaseFromLyDataByIndexCode,Err:" + err.Error()
  426. br.Msg = "获取数据失败"
  427. return
  428. }
  429. for _, item := range lyDataList {
  430. if dataMap[item.IndexCode] == nil {
  431. dataMap[item.IndexCode] = make(map[string]*data_manage.BaseFromLyData)
  432. }
  433. dataMap[item.IndexCode][item.DataTime] = item
  434. }
  435. }
  436. tradeCodeStr := strings.Join(tradeCodeList, "','")
  437. tradeCodeStr = "'" + tradeCodeStr + "'"
  438. dataTimeList, err := data_manage.GetLyDataListByIndexCodes(tradeCodeStr)
  439. if err != nil {
  440. br.Msg = "获取数据失败"
  441. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  442. return
  443. }
  444. for _, dataTime := range dataTimeList {
  445. rowData := sheet.AddRow()
  446. celDate := rowData.AddCell()
  447. celDate.SetValue(dataTime)
  448. for _, m := range lyIndices {
  449. celData := rowData.AddCell()
  450. if dataMap[m.IndexCode][dataTime] != nil {
  451. celData.SetValue(dataMap[m.IndexCode][dataTime].Value)
  452. }
  453. }
  454. }
  455. }
  456. err = xlsxFile.Save(downLoadFilePath)
  457. if err != nil {
  458. //有指标无数据时先导出一遍空表
  459. sheet, err := xlsxFile.AddSheet("无数据")
  460. if err != nil {
  461. br.Msg = "新增Sheet失败"
  462. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  463. return
  464. }
  465. rowSecName := sheet.AddRow()
  466. celSecName := rowSecName.AddCell()
  467. celSecName.SetValue("")
  468. err = xlsxFile.Save(downLoadFilePath)
  469. if err != nil {
  470. br.Msg = "保存文件失败"
  471. br.ErrMsg = "保存文件失败"
  472. return
  473. }
  474. }
  475. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  476. this.Ctx.Output.Download(downLoadFilePath, fileName)
  477. defer func() {
  478. os.Remove(downLoadFilePath)
  479. }()
  480. br.Ret = 200
  481. br.Success = true
  482. br.Msg = "success"
  483. }