base_from_predict_calculate.go 45 KB

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