base_from_predict_calculate.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // PredictCalculateController 预测计算指标
  14. type PredictCalculateController struct {
  15. BaseAuthController
  16. }
  17. // Save
  18. // @Title 新增/编辑预测指标运算接口
  19. // @Description 新增预测指标运算接口
  20. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  21. // @router /save [post]
  22. func (this *PredictCalculateController) Save() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.EdbInfoCalculateSaveReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.EdbInfoId <= 0 {
  36. addPredictCalculate(br, req, this.Lang)
  37. } else {
  38. editPredictCalculate(br, req, this.Lang)
  39. }
  40. }
  41. // AddPredictCalculate 添加预测指标运算
  42. func addPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSaveReq, lang string) {
  43. req.EdbName = strings.Trim(req.EdbName, " ")
  44. if req.EdbName == "" {
  45. br.Msg = "指标名称不能为空"
  46. return
  47. }
  48. if req.Frequency == "" {
  49. br.Msg = "频率不能为空"
  50. return
  51. }
  52. if req.Unit == "" {
  53. br.Msg = "单位不能为空"
  54. return
  55. }
  56. if req.ClassifyId <= 0 {
  57. br.Msg = "请选择分类"
  58. return
  59. }
  60. if len(req.EdbInfoIdArr) <= 0 {
  61. br.Msg = "请选择指标"
  62. return
  63. }
  64. if req.CalculateFormula == "" {
  65. br.Msg = "请填写指标"
  66. return
  67. }
  68. //加入缓存机制,避免创建同一个名称的指标 start
  69. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT_CALCULATE, ":", req.EdbName)
  70. isExist := utils.Rc.IsExist(redisKey)
  71. if isExist {
  72. br.Msg = "指标正在处理,请勿重复提交"
  73. return
  74. } else {
  75. //设置3分钟缓存
  76. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  77. defer func() {
  78. _ = utils.Rc.Delete(redisKey)
  79. }()
  80. }
  81. calculateFormula := req.CalculateFormula
  82. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  83. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  84. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  85. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  86. if strings.Contains(req.CalculateFormula, "%") {
  87. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  88. }
  89. req.CalculateFormula = calculateFormula
  90. //检验公式
  91. var formulaStr string
  92. var edbInfoIdBytes []string
  93. for _, v := range req.EdbInfoIdArr {
  94. formulaStr += v.FromTag + ","
  95. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  96. }
  97. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  98. if err != nil {
  99. br.Msg = "公式格式错误,请重新填写"
  100. return
  101. }
  102. for _, formula := range formulaSlice {
  103. formulaMap, e := utils.CheckFormula(formula)
  104. if e != nil {
  105. br.Msg = "公式错误,请重新填写"
  106. return
  107. }
  108. for _, v := range formulaMap {
  109. if !strings.Contains(formulaStr, v) {
  110. br.Msg = "公式错误,请重新填写"
  111. return
  112. }
  113. }
  114. }
  115. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  116. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(1, 0, req.EdbName, lang)
  117. if err != nil {
  118. br.Msg = "判断指标名称是否存在失败"
  119. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  120. return
  121. }
  122. if existEdbName {
  123. br.Msg = "指标名称已存在,请重新填写"
  124. br.ErrMsg = "指标名称已存在,请重新填写"
  125. return
  126. }
  127. // 关联指标信息
  128. edbInfoList := make([]*models.EdbInfo, 0)
  129. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  130. edbInfoTag := make(map[string]int)
  131. for k, v := range req.EdbInfoIdArr {
  132. edbInfoTag[v.FromTag] = v.EdbInfoId
  133. fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
  134. if err != nil {
  135. if err.Error() == utils.ErrNoRow() {
  136. br.Msg = "生成计算指标失败"
  137. br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  138. return
  139. }
  140. br.Msg = "生成计算指标失败"
  141. br.Msg = "获取指标失败:Err:" + err.Error()
  142. return
  143. }
  144. edbInfoList = append(edbInfoList, fromEdbInfo)
  145. //关联关系表
  146. {
  147. calculateMappingItem := &models.EdbInfoCalculateMapping{
  148. CreateTime: time.Now(),
  149. ModifyTime: time.Now(),
  150. Sort: k + 1,
  151. //EdbCode: edbCode,
  152. //EdbInfoId: int(edbInfoId),
  153. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  154. FromEdbCode: fromEdbInfo.EdbCode,
  155. FromEdbName: fromEdbInfo.EdbName,
  156. FromSource: fromEdbInfo.Source,
  157. FromSourceName: fromEdbInfo.SourceName,
  158. FromTag: v.FromTag,
  159. Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
  160. SourceName: "预测指标运算",
  161. }
  162. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  163. }
  164. }
  165. for _, v := range formulaSlice {
  166. formulaMap, e := utils.CheckFormula(v)
  167. if e != nil {
  168. br.Msg = "公式错误,请重新填写"
  169. return
  170. }
  171. //预先计算,判断公式是否正常
  172. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
  173. if !ok {
  174. br.Msg = "生成计算指标失败,请使用正确的计算公式"
  175. return
  176. }
  177. }
  178. // 指标入库
  179. //生成指标编码
  180. edbCode, err := utils.GenerateEdbCode(1, "2")
  181. if err != nil {
  182. br.Msg = "指标生成失败"
  183. br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
  184. return
  185. }
  186. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  187. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  188. edbInfo := &models.EdbInfo{
  189. EdbInfoType: 1,
  190. Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
  191. SourceName: "预测指标运算",
  192. EdbCode: edbCode,
  193. EdbName: req.EdbName,
  194. EdbNameSource: req.EdbName,
  195. Frequency: req.Frequency,
  196. Unit: req.Unit,
  197. ClassifyId: req.ClassifyId,
  198. SysUserId: req.AdminId,
  199. SysUserRealName: req.AdminName,
  200. CreateTime: time.Now(),
  201. ModifyTime: time.Now(),
  202. UniqueCode: uniqueCode,
  203. CalculateFormula: req.CalculateFormula,
  204. EdbType: 2,
  205. EmptyType: req.EmptyType,
  206. MaxEmptyType: req.MaxEmptyType,
  207. Extra: req.Extra,
  208. EdbNameEn: req.EdbName,
  209. UnitEn: req.Unit,
  210. }
  211. edbInfoId, err := models.AddEdbInfo(edbInfo)
  212. if err != nil {
  213. br.Msg = "生成计算指标失败"
  214. br.Msg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  215. return
  216. }
  217. edbInfo.EdbInfoId = int(edbInfoId)
  218. //处理同名指标
  219. {
  220. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  221. if err != nil {
  222. br.Msg = "保存失败"
  223. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  224. return
  225. }
  226. if len(edbNameList) >= 2 {
  227. for _, v := range edbNameList {
  228. edbName := v.EdbName + "(" + v.SourceName + ")"
  229. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  230. if err != nil {
  231. br.Msg = "保存失败"
  232. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  233. return
  234. }
  235. }
  236. }
  237. }
  238. //关联关系表入库
  239. {
  240. if len(calculateMappingList) > 0 {
  241. for k, v := range calculateMappingList {
  242. v.EdbCode = edbInfo.EdbCode
  243. v.EdbInfoId = edbInfo.EdbInfoId
  244. calculateMappingList[k] = v
  245. }
  246. go models.AddEdbInfoCalculateMappingMulti(calculateMappingList)
  247. }
  248. }
  249. // 开始添加预测指标
  250. latestDateStr, latestValue, err := models.AddPredictCalculateData(edbInfoList, edbInfoTag, edbInfo, edbCode, req.CalculateFormula, edbInfoIdBytes)
  251. if err != nil {
  252. br.Msg = "生成计算指标失败"
  253. br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
  254. return
  255. }
  256. // 更新指标最大最小值
  257. err, errMsg := models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  258. if err != nil {
  259. br.Msg = errMsg
  260. br.ErrMsg = err.Error()
  261. return
  262. }
  263. resp := models.AddEdbInfoResp{
  264. EdbInfoId: int(edbInfoId),
  265. UniqueCode: uniqueCode,
  266. }
  267. br.Ret = 200
  268. br.Success = true
  269. br.Msg = "保存成功"
  270. br.Data = resp
  271. br.IsAddLog = true
  272. }
  273. // editPredictCalculate 编辑预测指标运算
  274. func editPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSaveReq, lang string) {
  275. req.EdbName = strings.Trim(req.EdbName, " ")
  276. if req.EdbName == "" {
  277. br.Msg = "指标名称不能为空"
  278. return
  279. }
  280. if req.Frequency == "" {
  281. br.Msg = "频率不能为空"
  282. return
  283. }
  284. if req.Unit == "" {
  285. br.Msg = "单位不能为空"
  286. return
  287. }
  288. if req.ClassifyId <= 0 {
  289. br.Msg = "请选择分类"
  290. return
  291. }
  292. if len(req.EdbInfoIdArr) <= 0 {
  293. br.Msg = "请选择指标"
  294. return
  295. }
  296. if req.CalculateFormula == "" {
  297. br.Msg = "请填写指标"
  298. return
  299. }
  300. //加入缓存机制,避免创建同一个名称的指标 start
  301. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT_CALCULATE, ":", req.EdbName)
  302. isExist := utils.Rc.IsExist(redisKey)
  303. if isExist {
  304. br.Msg = "指标正在处理,请勿重复提交"
  305. // return
  306. } else {
  307. //设置3分钟缓存
  308. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  309. defer func() {
  310. _ = utils.Rc.Delete(redisKey)
  311. }()
  312. }
  313. calculateFormula := req.CalculateFormula
  314. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  315. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  316. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  317. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  318. if strings.Contains(req.CalculateFormula, "%") {
  319. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  320. }
  321. req.CalculateFormula = calculateFormula
  322. //检验公式
  323. var formulaStr string
  324. var edbInfoIdBytes []string
  325. for _, v := range req.EdbInfoIdArr {
  326. formulaStr += v.FromTag + ","
  327. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  328. }
  329. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  330. if err != nil {
  331. br.Msg = "公式格式错误,请重新填写"
  332. return
  333. }
  334. for _, formula := range formulaSlice {
  335. formulaMap, e := utils.CheckFormula(formula)
  336. if e != nil {
  337. br.Msg = "公式错误,请重新填写"
  338. return
  339. }
  340. for _, v := range formulaMap {
  341. if !strings.Contains(formulaStr, v) {
  342. br.Msg = "公式错误,请重新填写"
  343. return
  344. }
  345. }
  346. }
  347. // 获取计算指标信息
  348. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  349. if err != nil {
  350. br.Msg = "获取指标信息失败"
  351. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  352. return
  353. }
  354. // 记录旧的指标信息
  355. oldEdbName := edbInfo.EdbName
  356. oldFrequency := edbInfo.Frequency
  357. oldUnit := edbInfo.Unit
  358. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  359. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(edbInfo.EdbInfoType, edbInfo.EdbInfoId, req.EdbName, lang)
  360. if err != nil {
  361. br.Msg = "判断指标名称是否存在失败"
  362. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  363. return
  364. }
  365. if existEdbName {
  366. br.Msg = "指标名称已存在,请重新填写"
  367. br.ErrMsg = "指标名称已存在,请重新填写"
  368. return
  369. }
  370. // 关联指标信息
  371. edbInfoList := make([]*models.EdbInfo, 0)
  372. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  373. edbInfoTag := make(map[string]int)
  374. for k, v := range req.EdbInfoIdArr {
  375. edbInfoTag[v.FromTag] = v.EdbInfoId
  376. fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
  377. if err != nil {
  378. if err.Error() == utils.ErrNoRow() {
  379. br.Msg = "生成计算指标失败"
  380. br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  381. return
  382. }
  383. br.Msg = "生成计算指标失败"
  384. br.Msg = "获取指标失败:Err:" + err.Error()
  385. return
  386. }
  387. edbInfoList = append(edbInfoList, fromEdbInfo)
  388. //关联关系表
  389. {
  390. calculateMappingItem := &models.EdbInfoCalculateMapping{
  391. CreateTime: time.Now(),
  392. ModifyTime: time.Now(),
  393. Sort: k + 1,
  394. EdbCode: edbInfo.EdbCode,
  395. EdbInfoId: edbInfo.EdbInfoId,
  396. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  397. FromEdbCode: fromEdbInfo.EdbCode,
  398. FromEdbName: fromEdbInfo.EdbName,
  399. FromSource: fromEdbInfo.Source,
  400. FromSourceName: fromEdbInfo.SourceName,
  401. FromTag: v.FromTag,
  402. Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
  403. SourceName: "预测指标运算",
  404. }
  405. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  406. }
  407. }
  408. for _, v := range formulaSlice {
  409. formulaMap, e := utils.CheckFormula(v)
  410. if e != nil {
  411. br.Msg = "公式错误,请重新填写"
  412. return
  413. }
  414. //预先计算,判断公式是否正常
  415. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
  416. if !ok {
  417. br.Msg = "生成计算指标失败,请使用正确的计算公式"
  418. return
  419. }
  420. }
  421. //处理同名指标
  422. {
  423. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  424. if err != nil {
  425. br.Msg = "保存失败"
  426. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  427. return
  428. }
  429. if len(edbNameList) >= 2 {
  430. for _, v := range edbNameList {
  431. edbName := v.EdbName + "(" + v.SourceName + ")"
  432. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  433. if err != nil {
  434. br.Msg = "保存失败"
  435. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  436. return
  437. }
  438. }
  439. }
  440. }
  441. switch lang {
  442. case utils.EnLangVersion:
  443. edbInfo.EdbNameEn = req.EdbName
  444. edbInfo.UnitEn = req.Unit
  445. default:
  446. edbInfo.EdbName = req.EdbName
  447. edbInfo.Unit = req.Unit
  448. }
  449. edbInfo.Frequency = req.Frequency
  450. edbInfo.ClassifyId = req.ClassifyId
  451. edbInfo.CalculateFormula = req.CalculateFormula
  452. edbInfo.EmptyType = req.EmptyType
  453. edbInfo.MaxEmptyType = req.MaxEmptyType
  454. edbInfo.Extra = req.Extra
  455. err = models.EditPredictCalculate(edbInfo, calculateMappingList)
  456. if err != nil {
  457. br.Msg = "修改计算指标失败"
  458. br.Msg = "修改计算指标失败,Calculate Err:" + err.Error()
  459. return
  460. }
  461. // 开始添加预测指标
  462. latestDateStr, latestValue, err := models.AddPredictCalculateData(edbInfoList, edbInfoTag, edbInfo, edbInfo.EdbCode, req.CalculateFormula, edbInfoIdBytes)
  463. if err != nil {
  464. br.Msg = "生成计算指标失败"
  465. br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
  466. return
  467. }
  468. // 更新指标最大最小值
  469. err, errMsg := models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  470. if err != nil {
  471. br.Msg = errMsg
  472. br.ErrMsg = err.Error()
  473. return
  474. }
  475. // 记录基础信息变更日志
  476. oldEdbInfo := new(models.EdbInfo)
  477. oldEdbInfo.EdbInfoId = req.EdbInfoId
  478. oldEdbInfo.EdbName = oldEdbName
  479. oldEdbInfo.Frequency = oldFrequency
  480. oldEdbInfo.Unit = oldUnit
  481. newEdbInfoEditRecord := new(models.EdbInfoEditRecord)
  482. newEdbInfoEditRecord.EdbName = req.EdbName
  483. newEdbInfoEditRecord.Frequency = req.Frequency
  484. newEdbInfoEditRecord.Unit = req.Unit
  485. newEdbInfoEditRecord.OperateUserId = req.AdminId
  486. newEdbInfoEditRecord.OperateUserRealName = req.AdminName
  487. err = services.AddEditEdbInfoRecord(oldEdbInfo, newEdbInfoEditRecord)
  488. if err != nil {
  489. br.Msg = "记录基础信息变更日志失败"
  490. br.ErrMsg = err.Error()
  491. return
  492. }
  493. resp := models.AddEdbInfoResp{
  494. EdbInfoId: edbInfo.EdbInfoId,
  495. UniqueCode: edbInfo.UniqueCode,
  496. }
  497. br.Ret = 200
  498. br.Success = true
  499. br.Msg = "保存成功"
  500. br.Data = resp
  501. br.IsAddLog = true
  502. }
  503. // CalculateBatchSave
  504. // @Title 累计值转月-同比值-同差等计算新增
  505. // @Description 累计值转月-同比值-同差等计算新增接口
  506. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  507. // @Success Ret=200 返回指标id
  508. // @router /batch/save [post]
  509. func (this *PredictCalculateController) CalculateBatchSave() {
  510. br := new(models.BaseResponse).Init()
  511. defer func() {
  512. this.Data["json"] = br
  513. this.ServeJSON()
  514. }()
  515. var req models.EdbInfoCalculateBatchSaveReq
  516. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  517. if err != nil {
  518. br.Msg = "参数解析异常!"
  519. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  520. return
  521. }
  522. req.EdbName = strings.Trim(req.EdbName, " ")
  523. if req.EdbName == "" {
  524. br.Msg = "指标名称不能为空"
  525. return
  526. }
  527. if req.Frequency == "" {
  528. br.Msg = "频率不能为空"
  529. return
  530. }
  531. if req.Unit == "" {
  532. br.Msg = "单位不能为空"
  533. return
  534. }
  535. if req.ClassifyId <= 0 {
  536. br.Msg = "请选择分类"
  537. return
  538. }
  539. // 基础指标id
  540. fromEdbInfoId := req.FromEdbInfoId
  541. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC} // 不需要传入来源指标id的 指标类型
  542. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  543. br.Msg = "请选择指标"
  544. return
  545. }
  546. //加入缓存机制,避免创建同一个名称的指标 start
  547. redisKey := fmt.Sprint("edb_lib:predict_calculate:batch:save:", req.Source, ":", req.EdbName)
  548. isExist := utils.Rc.IsExist(redisKey)
  549. if isExist {
  550. br.Msg = "指标正在处理,请勿重复提交"
  551. return
  552. } else {
  553. //设置3分钟缓存
  554. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  555. defer func() {
  556. _ = utils.Rc.Delete(redisKey)
  557. }()
  558. }
  559. //加入缓存机制,避免创建同一个名称的指标 end
  560. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  561. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(1, req.EdbInfoId, req.EdbName, this.Lang)
  562. if err != nil {
  563. br.Msg = "判断指标名称是否存在失败"
  564. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  565. return
  566. }
  567. if existEdbName {
  568. br.Msg = "指标名称已存在,请重新填写"
  569. br.ErrMsg = "指标名称已存在,请重新填写"
  570. return
  571. }
  572. //生成指标编码
  573. edbCode, err := utils.GenerateEdbCode(1, "2")
  574. if err != nil {
  575. br.Msg = "指标生成失败"
  576. br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
  577. return
  578. }
  579. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  580. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  581. adminId := req.AdminId
  582. adminName := req.AdminName
  583. var sourName string
  584. var errMsg string
  585. var edbInfo *models.EdbInfo
  586. var latestDateStr string // 最近实际数据的日期
  587. var latestValue float64 // 最近实际数据的值
  588. var formulaInt int
  589. var nhccDate models.NhccDate // 拟合残差的日期
  590. switch req.Source {
  591. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  592. if req.Formula == "" {
  593. br.Msg = "请填写N值"
  594. return
  595. }
  596. formulaInt, _ = strconv.Atoi(req.Formula)
  597. if formulaInt <= 0 {
  598. br.Msg = "N值输入错误,请重新输入"
  599. return
  600. }
  601. /*case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
  602. //直接拼接指标
  603. //校验时间格式
  604. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  605. if err != nil {
  606. br.Msg = "拼接日期有误,请重新输入"
  607. return
  608. }*/
  609. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //拟合残差指标
  610. //指标校验
  611. if len(req.EdbInfoIdArr) != 2 {
  612. br.Msg = "选择的指标异常,请重新选择"
  613. return
  614. }
  615. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  616. //校验时间格式
  617. //数据格式:2022-11-01,2022-11-10
  618. timeList := strings.Split(req.Formula, ",")
  619. if len(timeList) != 2 {
  620. br.Msg = "选择时间有误,请重新输入"
  621. return
  622. }
  623. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  624. if err != nil {
  625. br.Msg = "开始日期有误,请重新输入"
  626. return
  627. }
  628. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  629. if err != nil {
  630. br.Msg = "结束日期有误,请重新输入"
  631. return
  632. }
  633. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  634. br.Msg = "日期间隔不得少于两天"
  635. return
  636. }
  637. nhccDate.StartDate = startDate
  638. nhccDate.EndDate = endDate
  639. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  640. if req.Formula == "" {
  641. br.Msg = "请填写alpha值"
  642. return
  643. }
  644. a, e := strconv.ParseFloat(req.Formula, 64)
  645. if e != nil {
  646. br.Msg = "alpha值输入错误, 请重新输入"
  647. return
  648. }
  649. if a <= 0 || a >= 1 {
  650. br.Msg = "alpha值输入错误, 请重新输入"
  651. return
  652. }
  653. }
  654. if err != nil {
  655. br.Msg = "获取指标信息失败"
  656. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  657. return
  658. }
  659. // 记录原始指标信息
  660. var oldEdbInfo *models.EdbInfo
  661. if req.EdbInfoId > 0 {
  662. oldEdbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
  663. if err != nil {
  664. br.Msg = "获取指标信息失败"
  665. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  666. return
  667. }
  668. }
  669. // 来源预测指标信息
  670. var fromEdbInfo *models.EdbInfo
  671. if fromEdbInfoId > 0 {
  672. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  673. if err != nil {
  674. br.Msg = "获取指标信息失败"
  675. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  676. return
  677. }
  678. if fromEdbInfo.EdbInfoType != 1 {
  679. br.Msg = "来源指标不是预测指标"
  680. br.ErrMsg = "来源指标不是预测指标"
  681. return
  682. }
  683. }
  684. var baseEdbInfoModel models.BasePredictEdbInfoInterface
  685. saveParams := models.BatchSaveCalculateBatchParams{
  686. Req: req,
  687. FromEdbInfo: fromEdbInfo,
  688. EdbCode: edbCode,
  689. UniqueCode: uniqueCode,
  690. SysUserId: req.AdminId,
  691. SysUserRealName: req.AdminName,
  692. Lang: this.Lang,
  693. }
  694. if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ {
  695. if req.EdbInfoId <= 0 {
  696. if req.Frequency != fromEdbInfo.Frequency {
  697. br.Msg = "当前频度和原指标频度不一致"
  698. return
  699. }
  700. if req.Unit != "无" {
  701. br.Msg = "单位只允许为无,禁止选择其他单位"
  702. return
  703. }
  704. }
  705. }
  706. if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ {
  707. if req.EdbInfoId <= 0 {
  708. if req.Frequency != fromEdbInfo.Frequency {
  709. br.Msg = "当前频度和原指标频度不一致"
  710. return
  711. }
  712. if req.Unit != fromEdbInfo.Unit {
  713. br.Msg = "当前单位和原指标单位不一致"
  714. return
  715. }
  716. }
  717. sourName = "预测同差"
  718. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateTcz(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  719. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS {
  720. sourName = "预测N数值移动平均计算"
  721. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateNszydpjjs(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  722. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_BP {
  723. sourName = "预测变频"
  724. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  725. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ {
  726. checkDataList, tmpErr := models.GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, "")
  727. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  728. br.Msg = "判断环比值是否可计算失败"
  729. br.ErrMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  730. return
  731. }
  732. for _, v := range checkDataList {
  733. if v.Value <= 0 {
  734. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  735. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  736. return
  737. }
  738. }
  739. sourName = "预测环比值"
  740. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
  741. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ {
  742. sourName = "预测环差值"
  743. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
  744. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY {
  745. sourName = "预测累计值转月值"
  746. if fromEdbInfo.Frequency != "月度" {
  747. br.Msg = "请选择月度指标"
  748. return
  749. }
  750. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  751. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT {
  752. sourName = "预测时间移位"
  753. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  754. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX {
  755. sourName = "预测超季节性"
  756. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
  757. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC {
  758. sourName = "预测拟合残差"
  759. secondEdbInfoReq := req.EdbInfoIdArr[1]
  760. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  761. if tmpErr != nil {
  762. br.Msg = "获取因变量的指标信息失败"
  763. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  764. return
  765. }
  766. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  767. br.Msg = "两个指标不允许为同一个"
  768. br.ErrMsg = "两个指标不允许为同一个"
  769. return
  770. }
  771. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, adminId, adminName, this.Lang)
  772. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_JP {
  773. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_JP
  774. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateJp(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  775. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NH {
  776. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_NH
  777. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateNh(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  778. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS {
  779. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_KSZS
  780. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateKszs(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, req.EdbInfoIdArr, edbCode, uniqueCode, adminId, adminName, this.Lang)
  781. } else {
  782. // 获取通用的数据源处理服务
  783. baseEdbInfoModel = models.GetBasePredictEdbInfoModel(req.Source)
  784. // 没有找到的话,那么就直接返回报错吧
  785. if baseEdbInfoModel == nil {
  786. br.Msg = "无效计算方式"
  787. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  788. return
  789. }
  790. sourName = baseEdbInfoModel.GetSourceName()
  791. if req.EdbInfoId <= 0 {
  792. edbInfo, latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Add(saveParams)
  793. } else {
  794. edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
  795. if err != nil {
  796. if err.Error() == utils.ErrNoRow() {
  797. br.Msg = "指标已被删除,请刷新页面"
  798. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  799. return
  800. }
  801. br.Msg = "获取指标信息失败"
  802. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  803. return
  804. }
  805. saveParams.EdbInfo = edbInfo
  806. latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Edit(saveParams)
  807. }
  808. }
  809. if err != nil {
  810. br.Msg = "生成" + sourName + "失败"
  811. if errMsg != `` {
  812. br.Msg = errMsg
  813. }
  814. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  815. return
  816. }
  817. if edbInfo == nil {
  818. br.Msg = "生成" + sourName + "失败"
  819. br.ErrMsg = "生成" + sourName + "失败,edbInfo is nil"
  820. return
  821. }
  822. //处理同名指标
  823. {
  824. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  825. if err != nil {
  826. br.Msg = "保存失败"
  827. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  828. return
  829. }
  830. if len(edbNameList) >= 2 {
  831. for _, v := range edbNameList {
  832. edbName := v.EdbName + "(" + v.SourceName + ")"
  833. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  834. if err != nil {
  835. br.Msg = "保存失败"
  836. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  837. return
  838. }
  839. }
  840. }
  841. }
  842. // 更新指标最大最小值
  843. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  844. if err != nil {
  845. br.Msg = errMsg
  846. br.ErrMsg = err.Error()
  847. return
  848. }
  849. if req.EdbInfoId > 0 {
  850. // 记录操作变更记录
  851. newEdbInfo := new(models.EdbInfoEditRecord)
  852. newEdbInfo.EdbName = req.EdbName
  853. newEdbInfo.Frequency = req.Frequency
  854. newEdbInfo.Unit = req.Unit
  855. newEdbInfo.OperateUserId = req.AdminId
  856. newEdbInfo.OperateUserRealName = req.AdminName
  857. err = services.AddEditEdbInfoRecord(oldEdbInfo, newEdbInfo)
  858. if err != nil {
  859. br.Msg = "保存失败"
  860. br.ErrMsg = "保存失败,Err:" + err.Error()
  861. return
  862. }
  863. }
  864. resp := models.AddEdbInfoResp{
  865. EdbInfoId: edbInfo.EdbInfoId,
  866. UniqueCode: uniqueCode,
  867. }
  868. br.Ret = 200
  869. br.Success = true
  870. br.Msg = "保存成功"
  871. br.Data = resp
  872. br.IsAddLog = true
  873. }
  874. // Refresh
  875. // @Title 刷新计算指标接口
  876. // @Description 刷新计算指标接口
  877. // @Success 200 {object} models.RefreshEdbInfoReq
  878. // @router /refresh [post]
  879. func (this *PredictCalculateController) Refresh() {
  880. br := new(models.BaseResponse).Init()
  881. var cacheKey string
  882. defer func() {
  883. this.Data["json"] = br
  884. this.ServeJSON()
  885. }()
  886. var req models.RefreshEdbInfoReq
  887. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  888. if err != nil {
  889. br.Msg = "参数解析异常!"
  890. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  891. return
  892. }
  893. if req.EdbCode == "" {
  894. br.Msg = "请输入指标编码!"
  895. br.ErrMsg = "请输入指标编码,指标编码为空"
  896. return
  897. }
  898. if req.EdbInfoId <= 0 {
  899. br.Msg = "请输入指标ID!"
  900. br.ErrMsg = "请输入指标ID"
  901. return
  902. }
  903. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  904. if err != nil {
  905. br.Msg = "指标不存在!"
  906. br.ErrMsg = "指标不存在"
  907. return
  908. }
  909. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  910. if utils.Rc.IsExist(cacheKey) {
  911. br.Ret = 501
  912. br.Success = true
  913. br.Msg = "系统处理中,请稍后重试"
  914. br.IsSendEmail = false
  915. return
  916. }
  917. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  918. defer func() {
  919. _ = utils.Rc.Delete(cacheKey)
  920. }()
  921. startDate := req.StartDate
  922. var errMsg string
  923. endDate := time.Now().Format(utils.FormatDate)
  924. edbInfoId := edbInfo.EdbInfoId
  925. source := edbInfo.Source
  926. var latestDateStr string // 最近实际数据的日期
  927. var latestValue float64 // 最近实际数据的值
  928. var baseEdbInfoModel models.BasePredictEdbInfoInterface
  929. refreshParams := models.RefreshParams{
  930. EdbInfo: edbInfo,
  931. }
  932. switch source {
  933. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ: //刷新同比值
  934. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  935. if err != nil {
  936. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  937. break
  938. }
  939. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  940. if err != nil {
  941. errMsg = "GetEdbInfoById Err:" + err.Error()
  942. break
  943. }
  944. refreshParams.FromEdbInfo = fromEdbInfo
  945. refreshParams.StartDate = edbInfo.StartDate
  946. }
  947. switch source {
  948. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  949. //startDate = edbInfo.StartDate
  950. //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  951. //if err != nil {
  952. // return
  953. //}
  954. //startDate = sTime.Format(utils.FormatDate)
  955. startDate = ""
  956. var edbInfoIdBytes []string
  957. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  958. if err != nil {
  959. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  960. break
  961. }
  962. var formulaStr string
  963. edbInfoList := make([]*models.EdbInfo, 0)
  964. edbInfoTag := make(map[string]int)
  965. for _, v := range calculateMap {
  966. edbInfoTag[v.FromTag] = v.FromEdbInfoId
  967. formulaStr += v.FromTag + ","
  968. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  969. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  970. edbInfoList = append(edbInfoList, edbInfo)
  971. }
  972. latestDateStr, latestValue, err = models.RefreshAllPredictCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  973. if err != nil && err.Error() != utils.ErrNoRow() {
  974. errMsg = "RefreshCalculate Err:" + err.Error()
  975. break
  976. }
  977. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ: //同差值
  978. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  979. if err != nil {
  980. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  981. break
  982. }
  983. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  984. if err != nil {
  985. errMsg = "GetEdbInfoById Err:" + err.Error()
  986. break
  987. }
  988. startDate = edbInfo.StartDate
  989. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTcz.EdbCode, startDate)
  990. if err != nil && err.Error() != utils.ErrNoRow() {
  991. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  992. break
  993. }
  994. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  995. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  996. if err != nil {
  997. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  998. break
  999. }
  1000. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1001. if err != nil {
  1002. errMsg = "GetEdbInfoById Err:" + err.Error()
  1003. break
  1004. }
  1005. var formulaInt int
  1006. formulaInt, _ = strconv.Atoi(calculateInfo.CalculateFormula)
  1007. if formulaInt <= 0 {
  1008. errMsg = "N值输入错误,请重新输入"
  1009. break
  1010. }
  1011. startDate = edbInfo.StartDate
  1012. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNszydpjjs(edbInfoId, source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateInfo.EdbCode, startDate)
  1013. if err != nil && err.Error() != utils.ErrNoRow() {
  1014. errMsg = "RefreshAllPredictCalculateNszydpjjs Err:" + err.Error()
  1015. break
  1016. }
  1017. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP: //刷新变频
  1018. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1019. if err != nil {
  1020. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1021. break
  1022. }
  1023. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1024. if err != nil {
  1025. errMsg = "GetEdbInfoById Err:" + err.Error()
  1026. break
  1027. }
  1028. endDate = time.Now().Format(utils.FormatDate)
  1029. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateBp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1030. if err != nil && err.Error() != utils.ErrNoRow() {
  1031. errMsg = "RefreshAllPredictCalculateBp Err:" + err.Error()
  1032. break
  1033. }
  1034. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ: //刷新环比值
  1035. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1036. if err != nil {
  1037. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1038. break
  1039. }
  1040. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1041. if err != nil {
  1042. errMsg = "GetEdbInfoById Err:" + err.Error()
  1043. break
  1044. }
  1045. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1046. endDate = time.Now().Format(utils.FormatDate)
  1047. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1048. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHbz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1049. if err != nil && err.Error() != utils.ErrNoRow() {
  1050. errMsg = "RefreshAllPredictCalculateHbz Err:" + err.Error()
  1051. break
  1052. }
  1053. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ: //刷新环差值
  1054. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1055. if err != nil {
  1056. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1057. break
  1058. }
  1059. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1060. if err != nil {
  1061. errMsg = "GetEdbInfoById Err:" + err.Error()
  1062. break
  1063. }
  1064. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1065. endDate = time.Now().Format(utils.FormatDate)
  1066. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1067. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1068. if err != nil && err.Error() != utils.ErrNoRow() {
  1069. errMsg = "RefreshAllPredictCalculateHcz Err:" + err.Error()
  1070. break
  1071. }
  1072. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY: //刷新累计值转月值
  1073. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1074. if err != nil {
  1075. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1076. break
  1077. }
  1078. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1079. if err != nil {
  1080. errMsg = "GetEdbInfoById Err:" + err.Error()
  1081. break
  1082. }
  1083. endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1084. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1085. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjzzy(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1086. if err != nil && err.Error() != utils.ErrNoRow() {
  1087. errMsg = "RefreshAllPredictCalculateLjzzy Err:" + err.Error()
  1088. break
  1089. }
  1090. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1091. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1092. if err != nil {
  1093. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1094. break
  1095. }
  1096. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1097. if err != nil {
  1098. errMsg = "GetEdbInfoById Err:" + err.Error()
  1099. break
  1100. }
  1101. //startDate = edbInfo.StartDate
  1102. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1103. endDate = time.Now().Format(utils.FormatDate)
  1104. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1105. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTimeShift(edbInfoId, source, edbInfo.SubSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1106. if err != nil && err.Error() != utils.ErrNoRow() {
  1107. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1108. }
  1109. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1110. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjztbpj(edbInfo)
  1111. if err != nil && err.Error() != utils.ErrNoRow() {
  1112. errMsg = "RefreshAllPredictCalculateLjztbpj Err:" + err.Error()
  1113. break
  1114. }
  1115. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: //刷新直接拼接
  1116. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateZjpj(edbInfo)
  1117. if err != nil && err.Error() != utils.ErrNoRow() {
  1118. errMsg = "RefreshAllPredictCalculateZjpj Err:" + err.Error()
  1119. break
  1120. }
  1121. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX: //超季节性
  1122. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1123. if err != nil {
  1124. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1125. break
  1126. }
  1127. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1128. if err != nil {
  1129. errMsg = "GetEdbInfoById Err:" + err.Error()
  1130. break
  1131. }
  1132. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1133. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1134. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1135. if err != nil && err.Error() != utils.ErrNoRow() {
  1136. errMsg = "RefreshAllPredictCalculateCjjx Err:" + err.Error()
  1137. break
  1138. }
  1139. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //nhcc
  1140. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNhcc(edbInfo)
  1141. if err != nil && err.Error() != utils.ErrNoRow() {
  1142. errMsg = "RefreshAllPredictCalculateNhcc Err:" + err.Error()
  1143. break
  1144. }
  1145. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP: //降频
  1146. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1147. if err != nil {
  1148. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1149. break
  1150. }
  1151. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1152. if err != nil {
  1153. errMsg = "GetEdbInfoById Err:" + err.Error()
  1154. break
  1155. }
  1156. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateJp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1157. if err != nil && err.Error() != utils.ErrNoRow() {
  1158. errMsg = "RefreshAllPredictCalculateJp Err:" + err.Error()
  1159. break
  1160. }
  1161. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH: //年化
  1162. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1163. if err != nil {
  1164. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1165. break
  1166. }
  1167. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1168. if err != nil {
  1169. errMsg = "GetEdbInfoById Err:" + err.Error()
  1170. break
  1171. }
  1172. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNh(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1173. if err != nil && err.Error() != utils.ErrNoRow() {
  1174. errMsg = "RefreshAllPredictCalculateNh Err:" + err.Error()
  1175. break
  1176. }
  1177. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: //扩散指数
  1178. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateKszs(edbInfo)
  1179. if err != nil && err.Error() != utils.ErrNoRow() {
  1180. errMsg = "RefreshAllPredictCalculateKszs Err:" + err.Error()
  1181. break
  1182. }
  1183. default:
  1184. // 获取通用的数据源处理服务
  1185. baseEdbInfoModel = models.GetBasePredictEdbInfoModel(source)
  1186. // 没有找到的话,那么就直接返回报错吧
  1187. if baseEdbInfoModel == nil {
  1188. br.Msg = "来源异常,请联系相关开发!"
  1189. br.ErrMsg = "来源异常,请联系相关开发"
  1190. return
  1191. }
  1192. latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1193. }
  1194. if errMsg != `` {
  1195. br.Msg = "刷新指标失败!"
  1196. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1197. return
  1198. }
  1199. // 更新指标最大最小值
  1200. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  1201. if err != nil {
  1202. br.Msg = errMsg
  1203. br.ErrMsg = err.Error()
  1204. return
  1205. }
  1206. // 更新ES
  1207. go logic.UpdateEs(edbInfo.EdbInfoId)
  1208. br.Ret = 200
  1209. br.Success = true
  1210. br.Msg = "获取成功"
  1211. }
  1212. // CalculateComputeCorrelation
  1213. // @Title 拟合残差计算相关性接口
  1214. // @Description 拟合残差计算相关性接口
  1215. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1216. // @Success Ret=200 返回指标id
  1217. // @router /compute_correlation [post]
  1218. func (this *PredictCalculateController) CalculateComputeCorrelation() {
  1219. br := new(models.BaseResponse).Init()
  1220. defer func() {
  1221. this.Data["json"] = br
  1222. this.ServeJSON()
  1223. }()
  1224. var req models.EdbInfoCalculateBatchSaveReq
  1225. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1226. if err != nil {
  1227. br.Msg = "参数解析异常!"
  1228. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1229. return
  1230. }
  1231. if len(req.EdbInfoIdArr) != 2 {
  1232. br.Msg = "选择的指标异常,请重新选择"
  1233. return
  1234. }
  1235. //校验时间格式
  1236. //数据格式:2022-11-01,2022-11-10
  1237. timeList := strings.Split(req.Formula, ",")
  1238. if len(timeList) != 2 {
  1239. br.Msg = "选择时间有误,请重新输入"
  1240. return
  1241. }
  1242. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1243. if err != nil {
  1244. br.Msg = "开始日期有误,请重新输入"
  1245. return
  1246. }
  1247. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1248. if err != nil {
  1249. br.Msg = "结束日期有误,请重新输入"
  1250. return
  1251. }
  1252. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1253. br.Msg = "日期间隔不得少于两天"
  1254. return
  1255. }
  1256. var nhccDate models.NhccDate // 拟合残差的日期
  1257. nhccDate.StartDate = startDate
  1258. nhccDate.EndDate = endDate
  1259. // 来源预测指标信息
  1260. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1261. if fromEdbInfoId <= 0 {
  1262. br.Msg = "请选择指标"
  1263. return
  1264. }
  1265. // 来源指标(自变量指标)
  1266. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1267. if err != nil {
  1268. br.Msg = "获取指标信息失败"
  1269. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1270. return
  1271. }
  1272. if fromEdbInfo.EdbInfoType != 1 {
  1273. br.Msg = "来源指标不是预测指标"
  1274. br.ErrMsg = "来源指标不是预测指标"
  1275. return
  1276. }
  1277. // 因变量指标
  1278. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1279. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1280. if tmpErr != nil {
  1281. br.Msg = "获取因变量的指标信息失败"
  1282. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1283. return
  1284. }
  1285. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1286. br.Msg = "两个指标不允许为同一个"
  1287. br.ErrMsg = "两个指标不允许为同一个"
  1288. return
  1289. }
  1290. rVal, err := models.CalculatePredictComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1291. if err != nil {
  1292. br.Msg = "计算失败"
  1293. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1294. return
  1295. }
  1296. br.Ret = 200
  1297. br.Success = true
  1298. br.Msg = "计算成功"
  1299. br.Data = rVal
  1300. br.IsAddLog = true
  1301. }