custom_analysis_edb.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. excelModel "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/data_manage/excel/request"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/services/data/excel"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // EdbList
  17. // @Title 指标列表
  18. // @Description 指标列表
  19. // @Param ExcelInfoId query int true "excel的id"
  20. // @Param Keyword query string false "指标关键词"
  21. // @Success 200 {object} []excel.ExcelEdbMappingItem
  22. // @router /edb/list [get]
  23. func (c *CustomAnalysisController) EdbList() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. c.Data["json"] = br
  27. c.ServeJSON()
  28. }()
  29. excelInfoId, _ := c.GetInt("ExcelInfoId")
  30. if excelInfoId <= 0 {
  31. br.Msg = "请选择excel"
  32. br.IsSendEmail = false
  33. return
  34. }
  35. keyword := c.GetString("Keyword")
  36. keyword = strings.TrimSpace(keyword)
  37. if keyword != "" {
  38. keyword = fmt.Sprint("%", keyword, "%")
  39. }
  40. // 获取excel表详情
  41. excelInfo, err := excelModel.GetExcelInfoById(excelInfoId)
  42. if err != nil {
  43. br.Msg = "找不到该EXCEL!"
  44. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  45. return
  46. }
  47. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  48. br.Msg = "EXCEL异常!"
  49. br.IsSendEmail = false
  50. return
  51. }
  52. list, err := excelModel.GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfo.ExcelInfoId, keyword)
  53. if err != nil {
  54. br.Msg = "获取失败"
  55. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  56. return
  57. }
  58. for k, v := range list {
  59. var tmpCalculateFormula excelModel.CalculateFormula
  60. err = json.Unmarshal([]byte(v.CalculateFormula), &tmpCalculateFormula)
  61. if err != nil {
  62. br.Msg = "获取失败"
  63. br.ErrMsg = "公式转换失败,Err:" + err.Error()
  64. return
  65. }
  66. v.DateSequenceStr = tmpCalculateFormula.DateSequenceStr
  67. v.DataSequenceStr = tmpCalculateFormula.DataSequenceStr
  68. list[k] = v
  69. }
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "获取成功"
  73. br.Data = list
  74. }
  75. // AddEdb
  76. // @Title 新增指标接口
  77. // @Description 新增指标接口
  78. // @Param request body request.AddEdb true "type json string"
  79. // @Success 200 {object} data_manage.AddEdbInfoResp
  80. // @router /edb/add [post]
  81. func (c *CustomAnalysisController) AddEdb() {
  82. br := new(models.BaseResponse).Init()
  83. defer func() {
  84. c.Data["json"] = br
  85. c.ServeJSON()
  86. }()
  87. sysUser := c.SysUser
  88. if sysUser == nil {
  89. br.Msg = "请登录"
  90. br.ErrMsg = "请登录,SysUser Is Empty"
  91. br.Ret = 408
  92. return
  93. }
  94. deleteCache := true
  95. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_EDB_" + strconv.Itoa(sysUser.AdminId)
  96. defer func() {
  97. if deleteCache {
  98. _ = utils.Rc.Delete(cacheKey)
  99. }
  100. }()
  101. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  102. deleteCache = false
  103. br.Msg = "系统处理中,请稍后重试!"
  104. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  105. return
  106. }
  107. var req request.AddEdb
  108. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  109. if err != nil {
  110. br.Msg = "参数解析异常!"
  111. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  112. return
  113. }
  114. req.EdbName = strings.Trim(req.EdbName, " ")
  115. if req.EdbName == "" {
  116. br.Msg = "请填写指标名称!"
  117. br.IsSendEmail = false
  118. return
  119. }
  120. if req.ExcelInfoId <= 0 {
  121. br.Msg = "请选择excel!"
  122. br.IsSendEmail = false
  123. return
  124. }
  125. if req.ClassifyId <= 0 {
  126. br.Msg = "请选择指标分类!"
  127. br.IsSendEmail = false
  128. return
  129. }
  130. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  131. if err != nil {
  132. br.Msg = "找不到该EXCEL!"
  133. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  134. return
  135. }
  136. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  137. br.Msg = "EXCEL异常!"
  138. br.IsSendEmail = false
  139. return
  140. }
  141. //excel.GetCustomAnalysisExcelData(excelInfo)
  142. dateList, dataList, err, errMsg := excel.HandleEdbSequenceVal(req.DateSequenceVal, req.DataSequenceVal)
  143. if err != nil {
  144. br.Msg = "时间序列或数据序列异常!"
  145. if errMsg != `` {
  146. br.Msg = errMsg
  147. }
  148. br.ErrMsg = "时间序列或数据序列处理异常!err:" + err.Error()
  149. return
  150. }
  151. type CustomAnalysisData struct {
  152. ExcelInfoId int `description:"excel的id"`
  153. DateList []string
  154. DataList []float64
  155. }
  156. type Formula struct {
  157. DateSequenceStr string `description:"日期序列"`
  158. DataSequenceStr string `description:"数据序列"`
  159. }
  160. formulaByte, err := json.Marshal(Formula{
  161. DateSequenceStr: req.DateSequenceStr,
  162. DataSequenceStr: req.DataSequenceStr,
  163. })
  164. if err != nil {
  165. br.Msg = "时间序列或数据序列配置异常!"
  166. br.ErrMsg = "json序列化时间序列或数据序列异常!err:" + err.Error()
  167. return
  168. }
  169. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  170. AdminId: sysUser.AdminId,
  171. AdminName: sysUser.RealName,
  172. EdbName: req.EdbName,
  173. Frequency: req.Frequency,
  174. Unit: req.Unit,
  175. ClassifyId: req.ClassifyId,
  176. Formula: string(formulaByte), //公式
  177. Source: utils.DATA_SOURCE_CALCULATE_ZDYFX,
  178. Data: CustomAnalysisData{
  179. ExcelInfoId: req.ExcelInfoId,
  180. DateList: dateList,
  181. DataList: dataList,
  182. },
  183. }
  184. // 调用指标库去更新
  185. reqJson, err := json.Marshal(req2)
  186. if err != nil {
  187. br.Msg = "参数解析异常!"
  188. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  189. return
  190. }
  191. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson), c.Lang)
  192. if err != nil {
  193. br.Msg = "新增失败"
  194. br.ErrMsg = "新增失败,Err:" + err.Error()
  195. return
  196. }
  197. if respItem.Ret != 200 {
  198. br.Msg = respItem.Msg
  199. br.ErrMsg = respItem.ErrMsg
  200. return
  201. }
  202. resp := respItem.Data
  203. //添加es
  204. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  205. br.Ret = 200
  206. br.Success = true
  207. br.Msg = "保存成功"
  208. br.Data = resp
  209. }
  210. // EditEdb
  211. // @Title 编辑指标接口
  212. // @Description 编辑指标接口
  213. // @Param request body request.EditEdb true "type json string"
  214. // @Success 200 {object} data_manage.AddEdbInfoResp
  215. // @router /edb/edit [post]
  216. func (c *CustomAnalysisController) EditEdb() {
  217. br := new(models.BaseResponse).Init()
  218. defer func() {
  219. c.Data["json"] = br
  220. c.ServeJSON()
  221. }()
  222. sysUser := c.SysUser
  223. if sysUser == nil {
  224. br.Msg = "请登录"
  225. br.ErrMsg = "请登录,SysUser Is Empty"
  226. br.Ret = 408
  227. return
  228. }
  229. deleteCache := true
  230. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_EDB_" + strconv.Itoa(sysUser.AdminId)
  231. defer func() {
  232. if deleteCache {
  233. _ = utils.Rc.Delete(cacheKey)
  234. }
  235. }()
  236. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  237. deleteCache = false
  238. br.Msg = "系统处理中,请稍后重试!"
  239. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  240. return
  241. }
  242. var req request.EditEdb
  243. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  244. if err != nil {
  245. br.Msg = "参数解析异常!"
  246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  247. return
  248. }
  249. req.EdbName = strings.Trim(req.EdbName, " ")
  250. if req.EdbName == "" {
  251. br.Msg = "请填写指标名称!"
  252. br.IsSendEmail = false
  253. return
  254. }
  255. if req.ExcelInfoId <= 0 {
  256. br.Msg = "请选择excel!"
  257. br.IsSendEmail = false
  258. return
  259. }
  260. if req.EdbInfoId <= 0 {
  261. br.Msg = "请选择指标!"
  262. br.IsSendEmail = false
  263. return
  264. }
  265. if req.ClassifyId <= 0 {
  266. br.Msg = "请选择指标分类!"
  267. br.IsSendEmail = false
  268. return
  269. }
  270. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  271. if err != nil {
  272. br.Msg = "找不到该EXCEL!"
  273. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  274. return
  275. }
  276. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  277. br.Msg = "EXCEL异常!"
  278. br.IsSendEmail = false
  279. return
  280. }
  281. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  282. if err != nil {
  283. if err.Error() == utils.ErrNoRow() {
  284. br.Msg = "指标已被删除,请刷新页面"
  285. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  286. return
  287. }
  288. br.Msg = "获取指标信息失败"
  289. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  290. return
  291. }
  292. //excel.GetCustomAnalysisExcelData(excelInfo)
  293. dateList, dataList, err, errMsg := excel.HandleEdbSequenceVal(req.DateSequenceVal, req.DataSequenceVal)
  294. if err != nil {
  295. br.Msg = "时间序列或数据序列异常!"
  296. if errMsg != `` {
  297. br.Msg = errMsg
  298. }
  299. br.ErrMsg = "时间序列或数据序列处理异常!err:" + err.Error()
  300. return
  301. }
  302. type CustomAnalysisData struct {
  303. ExcelInfoId int `description:"excel的id"`
  304. DateList []string
  305. DataList []float64
  306. }
  307. type Formula struct {
  308. DateSequenceStr string `description:"日期序列"`
  309. DataSequenceStr string `description:"数据序列"`
  310. }
  311. formulaByte, err := json.Marshal(Formula{
  312. DateSequenceStr: req.DateSequenceStr,
  313. DataSequenceStr: req.DataSequenceStr,
  314. })
  315. if err != nil {
  316. br.Msg = "时间序列或数据序列配置异常!"
  317. br.ErrMsg = "json序列化时间序列或数据序列异常!err:" + err.Error()
  318. return
  319. }
  320. // 构造请求
  321. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  322. EdbInfoId: req.EdbInfoId,
  323. EdbName: req.EdbName,
  324. Frequency: req.Frequency,
  325. Unit: req.Unit,
  326. ClassifyId: req.ClassifyId,
  327. Formula: string(formulaByte), //公式
  328. Source: utils.DATA_SOURCE_CALCULATE_ZDYFX,
  329. Data: CustomAnalysisData{
  330. ExcelInfoId: req.ExcelInfoId,
  331. DateList: dateList,
  332. DataList: dataList,
  333. },
  334. }
  335. // 调用指标库去更新
  336. reqJson, err := json.Marshal(req2)
  337. if err != nil {
  338. br.Msg = "参数解析异常!"
  339. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  340. return
  341. }
  342. respItem, err := data.BatchEditEdbCalculateData(string(reqJson), c.Lang)
  343. if err != nil {
  344. br.Msg = "编辑失败"
  345. br.ErrMsg = "编辑失败,Err:" + err.Error()
  346. return
  347. }
  348. if respItem.Ret != 200 {
  349. br.Msg = respItem.Msg
  350. br.ErrMsg = respItem.ErrMsg
  351. return
  352. }
  353. resp := respItem.Data
  354. //添加es
  355. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  356. // 修改关联的预测指标基础信息
  357. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "保存成功"
  361. br.Data = resp
  362. }
  363. // EdbRefresh
  364. // @Title 指标列表
  365. // @Description 指标列表
  366. // @Param ExcelInfoId query int true "excel的id"
  367. // @Success 200 {object} []excel.ExcelEdbMappingItem
  368. // @router /edb/refresh [get]
  369. func (c *CustomAnalysisController) EdbRefresh() {
  370. br := new(models.BaseResponse).Init()
  371. defer func() {
  372. c.Data["json"] = br
  373. c.ServeJSON()
  374. }()
  375. excelInfoId, _ := c.GetInt("ExcelInfoId")
  376. if excelInfoId <= 0 {
  377. br.Msg = "请选择excel"
  378. br.IsSendEmail = false
  379. return
  380. }
  381. cacheKey := "CACHE_EXCEL_EDB_REFRESH_" + strconv.Itoa(c.SysUser.AdminId)
  382. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  383. br.Msg = "系统处理中,请稍后重试!"
  384. br.ErrMsg = "系统处理中,请稍后重试!" + c.SysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  385. return
  386. }
  387. defer func() {
  388. _ = utils.Rc.Delete(cacheKey)
  389. }()
  390. // 获取excel表详情
  391. excelInfo, err := excelModel.GetExcelInfoById(excelInfoId)
  392. if err != nil {
  393. br.Msg = "找不到该EXCEL!"
  394. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  395. return
  396. }
  397. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  398. br.Msg = "EXCEL异常!"
  399. br.IsSendEmail = false
  400. return
  401. }
  402. err, errMsg, isSendEmail := excel.Refresh(excelInfo, c.Lang)
  403. if err != nil {
  404. br.Msg = "刷新失败"
  405. if errMsg != `` {
  406. br.Msg = errMsg
  407. }
  408. br.ErrMsg = "刷新失败,Err:" + err.Error()
  409. br.IsSendEmail = isSendEmail
  410. return
  411. }
  412. br.Ret = 200
  413. br.Success = true
  414. br.Msg = "刷新成功"
  415. }
  416. //func init() {
  417. // excelInfo, err := excelModel.GetExcelInfoById(160)
  418. // if err != nil {
  419. // fmt.Println("查找excel失败:", err)
  420. // return
  421. // }
  422. // _, err, _ = excel.GenerateExcelCustomAnalysisExcel(excelInfo)
  423. // if err != nil {
  424. // fmt.Println("生成excel失败:", err)
  425. // return
  426. // }
  427. //}