base_from_predict_calculate.go 44 KB

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