base_from_predict_calculate.go 43 KB

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