base_from_predict_calculate.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // PredictCalculateController 预测计算指标
  14. type PredictCalculateController struct {
  15. BaseAuthController
  16. }
  17. // Save
  18. // @Title 新增/编辑预测指标运算接口
  19. // @Description 新增预测指标运算接口
  20. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  21. // @router /save [post]
  22. func (this *PredictCalculateController) Save() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.EdbInfoCalculateSaveReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.EdbInfoId <= 0 {
  36. addPredictCalculate(br, req, this.Lang)
  37. } else {
  38. editPredictCalculate(br, req, this.Lang)
  39. }
  40. }
  41. // AddPredictCalculate 添加预测指标运算
  42. func addPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSaveReq, lang string) {
  43. req.EdbName = strings.Trim(req.EdbName, " ")
  44. if req.EdbName == "" {
  45. br.Msg = "指标名称不能为空"
  46. return
  47. }
  48. if req.Frequency == "" {
  49. br.Msg = "频率不能为空"
  50. return
  51. }
  52. if req.Unit == "" {
  53. br.Msg = "单位不能为空"
  54. return
  55. }
  56. if req.ClassifyId <= 0 {
  57. br.Msg = "请选择分类"
  58. return
  59. }
  60. if len(req.EdbInfoIdArr) <= 0 {
  61. br.Msg = "请选择指标"
  62. return
  63. }
  64. if req.CalculateFormula == "" {
  65. br.Msg = "请填写指标"
  66. return
  67. }
  68. //加入缓存机制,避免创建同一个名称的指标 start
  69. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT_CALCULATE, ":", req.EdbName)
  70. isExist := utils.Rc.IsExist(redisKey)
  71. if isExist {
  72. br.Msg = "指标正在处理,请勿重复提交"
  73. return
  74. } else {
  75. //设置3分钟缓存
  76. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  77. defer func() {
  78. _ = utils.Rc.Delete(redisKey)
  79. }()
  80. }
  81. calculateFormula := req.CalculateFormula
  82. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  83. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  84. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  85. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  86. if strings.Contains(req.CalculateFormula, "%") {
  87. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  88. }
  89. req.CalculateFormula = calculateFormula
  90. //检验公式
  91. var formulaStr string
  92. var edbInfoIdBytes []string
  93. for _, v := range req.EdbInfoIdArr {
  94. formulaStr += v.FromTag + ","
  95. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  96. }
  97. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  98. if err != nil {
  99. br.Msg = "公式格式错误,请重新填写"
  100. return
  101. }
  102. for _, formula := range formulaSlice {
  103. formulaMap, e := utils.CheckFormula(formula)
  104. if e != nil {
  105. br.Msg = "公式错误,请重新填写"
  106. return
  107. }
  108. for _, v := range formulaMap {
  109. if !strings.Contains(formulaStr, v) {
  110. br.Msg = "公式错误,请重新填写"
  111. return
  112. }
  113. }
  114. }
  115. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  116. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(1, 0, req.EdbName, lang)
  117. if err != nil {
  118. br.Msg = "判断指标名称是否存在失败"
  119. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  120. return
  121. }
  122. if existEdbName {
  123. br.Msg = "指标名称已存在,请重新填写"
  124. br.ErrMsg = "指标名称已存在,请重新填写"
  125. return
  126. }
  127. // 关联指标信息
  128. edbInfoList := make([]*models.EdbInfo, 0)
  129. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  130. edbInfoTag := make(map[string]int)
  131. for k, v := range req.EdbInfoIdArr {
  132. edbInfoTag[v.FromTag] = v.EdbInfoId
  133. fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
  134. if err != nil {
  135. if err.Error() == utils.ErrNoRow() {
  136. br.Msg = "生成计算指标失败"
  137. br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  138. return
  139. }
  140. br.Msg = "生成计算指标失败"
  141. br.Msg = "获取指标失败:Err:" + err.Error()
  142. return
  143. }
  144. edbInfoList = append(edbInfoList, fromEdbInfo)
  145. //关联关系表
  146. {
  147. calculateMappingItem := &models.EdbInfoCalculateMapping{
  148. CreateTime: time.Now(),
  149. ModifyTime: time.Now(),
  150. Sort: k + 1,
  151. //EdbCode: edbCode,
  152. //EdbInfoId: int(edbInfoId),
  153. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  154. FromEdbCode: fromEdbInfo.EdbCode,
  155. FromEdbName: fromEdbInfo.EdbName,
  156. FromSource: fromEdbInfo.Source,
  157. FromSourceName: fromEdbInfo.SourceName,
  158. FromTag: v.FromTag,
  159. Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
  160. SourceName: "预测指标运算",
  161. }
  162. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  163. }
  164. }
  165. for _, v := range formulaSlice {
  166. formulaMap, e := utils.CheckFormula(v)
  167. if e != nil {
  168. br.Msg = "公式错误,请重新填写"
  169. return
  170. }
  171. //预先计算,判断公式是否正常
  172. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
  173. if !ok {
  174. br.Msg = "生成计算指标失败,请使用正确的计算公式"
  175. return
  176. }
  177. }
  178. // 指标入库
  179. //生成指标编码
  180. edbCode, err := utils.GenerateEdbCode(1, "2")
  181. if err != nil {
  182. br.Msg = "指标生成失败"
  183. br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
  184. return
  185. }
  186. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  187. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  188. edbInfo := &models.EdbInfo{
  189. EdbInfoType: 1,
  190. Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
  191. SourceName: "预测指标运算",
  192. EdbCode: edbCode,
  193. EdbName: req.EdbName,
  194. EdbNameSource: req.EdbName,
  195. Frequency: req.Frequency,
  196. Unit: req.Unit,
  197. ClassifyId: req.ClassifyId,
  198. SysUserId: req.AdminId,
  199. SysUserRealName: req.AdminName,
  200. CreateTime: time.Now(),
  201. ModifyTime: time.Now(),
  202. UniqueCode: uniqueCode,
  203. CalculateFormula: req.CalculateFormula,
  204. EdbType: 2,
  205. EmptyType: req.EmptyType,
  206. MaxEmptyType: req.MaxEmptyType,
  207. Extra: req.Extra,
  208. EdbNameEn: req.EdbName,
  209. UnitEn: req.Unit,
  210. 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(1, 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_CJJX {
  746. sourName = "预测超季节性"
  747. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
  748. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC {
  749. sourName = "预测拟合残差"
  750. secondEdbInfoReq := req.EdbInfoIdArr[1]
  751. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  752. if tmpErr != nil {
  753. br.Msg = "获取因变量的指标信息失败"
  754. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  755. return
  756. }
  757. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  758. br.Msg = "两个指标不允许为同一个"
  759. br.ErrMsg = "两个指标不允许为同一个"
  760. return
  761. }
  762. edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, adminId, adminName, this.Lang)
  763. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_JP {
  764. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_JP
  765. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateJp(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  766. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NH {
  767. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_NH
  768. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateNh(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
  769. } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS {
  770. sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_KSZS
  771. edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateKszs(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, req.EdbInfoIdArr, edbCode, uniqueCode, adminId, adminName, this.Lang)
  772. } else {
  773. // 获取通用的数据源处理服务
  774. baseEdbInfoModel = models.GetBasePredictEdbInfoModel(req.Source)
  775. // 没有找到的话,那么就直接返回报错吧
  776. if baseEdbInfoModel == nil {
  777. br.Msg = "无效计算方式"
  778. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  779. return
  780. }
  781. sourName = baseEdbInfoModel.GetSourceName()
  782. if req.EdbInfoId <= 0 {
  783. edbInfo, latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Add(saveParams)
  784. } else {
  785. edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
  786. if err != nil {
  787. if err.Error() == utils.ErrNoRow() {
  788. br.Msg = "指标已被删除,请刷新页面"
  789. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  790. return
  791. }
  792. br.Msg = "获取指标信息失败"
  793. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  794. return
  795. }
  796. saveParams.EdbInfo = edbInfo
  797. latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Edit(saveParams)
  798. }
  799. }
  800. if err != nil {
  801. br.Msg = "生成" + sourName + "失败"
  802. if errMsg != `` {
  803. br.Msg = errMsg
  804. }
  805. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  806. return
  807. }
  808. if edbInfo == nil {
  809. br.Msg = "生成" + sourName + "失败"
  810. br.ErrMsg = "生成" + sourName + "失败,edbInfo is nil"
  811. return
  812. }
  813. //处理同名指标
  814. {
  815. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  816. if err != nil {
  817. br.Msg = "保存失败"
  818. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  819. return
  820. }
  821. if len(edbNameList) >= 2 {
  822. for _, v := range edbNameList {
  823. edbName := v.EdbName + "(" + v.SourceName + ")"
  824. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  825. if err != nil {
  826. br.Msg = "保存失败"
  827. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  828. return
  829. }
  830. }
  831. }
  832. }
  833. // 更新指标最大最小值
  834. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  835. if err != nil {
  836. br.Msg = errMsg
  837. br.ErrMsg = err.Error()
  838. return
  839. }
  840. if req.EdbInfoId > 0 {
  841. oldEdbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  842. if err != nil {
  843. br.Msg = "获取指标信息失败"
  844. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  845. return
  846. }
  847. // 记录操作变更记录
  848. newEdbInfo := new(models.EdbInfoEditRecord)
  849. newEdbInfo.EdbName = req.EdbName
  850. newEdbInfo.Frequency = req.Frequency
  851. newEdbInfo.Unit = req.Unit
  852. newEdbInfo.OperateUserId = req.AdminId
  853. newEdbInfo.OperateUserRealName = req.AdminName
  854. err = services.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfo)
  855. if err != nil {
  856. br.Msg = "保存失败"
  857. br.ErrMsg = "保存失败,Err:" + err.Error()
  858. return
  859. }
  860. }
  861. resp := models.AddEdbInfoResp{
  862. EdbInfoId: edbInfo.EdbInfoId,
  863. UniqueCode: uniqueCode,
  864. }
  865. br.Ret = 200
  866. br.Success = true
  867. br.Msg = "保存成功"
  868. br.Data = resp
  869. br.IsAddLog = true
  870. }
  871. // Refresh
  872. // @Title 刷新计算指标接口
  873. // @Description 刷新计算指标接口
  874. // @Success 200 {object} models.RefreshEdbInfoReq
  875. // @router /refresh [post]
  876. func (this *PredictCalculateController) Refresh() {
  877. br := new(models.BaseResponse).Init()
  878. var cacheKey string
  879. defer func() {
  880. this.Data["json"] = br
  881. this.ServeJSON()
  882. }()
  883. var req models.RefreshEdbInfoReq
  884. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  885. if err != nil {
  886. br.Msg = "参数解析异常!"
  887. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  888. return
  889. }
  890. if req.EdbCode == "" {
  891. br.Msg = "请输入指标编码!"
  892. br.ErrMsg = "请输入指标编码,指标编码为空"
  893. return
  894. }
  895. if req.EdbInfoId <= 0 {
  896. br.Msg = "请输入指标ID!"
  897. br.ErrMsg = "请输入指标ID"
  898. return
  899. }
  900. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  901. if err != nil {
  902. br.Msg = "指标不存在!"
  903. br.ErrMsg = "指标不存在"
  904. return
  905. }
  906. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  907. if utils.Rc.IsExist(cacheKey) {
  908. br.Ret = 501
  909. br.Success = true
  910. br.Msg = "系统处理中,请稍后重试"
  911. br.IsSendEmail = false
  912. return
  913. }
  914. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  915. defer func() {
  916. _ = utils.Rc.Delete(cacheKey)
  917. }()
  918. startDate := req.StartDate
  919. var errMsg string
  920. endDate := time.Now().Format(utils.FormatDate)
  921. edbInfoId := edbInfo.EdbInfoId
  922. source := edbInfo.Source
  923. var latestDateStr string // 最近实际数据的日期
  924. var latestValue float64 // 最近实际数据的值
  925. var baseEdbInfoModel models.BasePredictEdbInfoInterface
  926. refreshParams := models.RefreshParams{
  927. EdbInfo: edbInfo,
  928. }
  929. switch source {
  930. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ: //刷新同比值
  931. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  932. if err != nil {
  933. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  934. break
  935. }
  936. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  937. if err != nil {
  938. errMsg = "GetEdbInfoById Err:" + err.Error()
  939. break
  940. }
  941. refreshParams.FromEdbInfo = fromEdbInfo
  942. refreshParams.StartDate = edbInfo.StartDate
  943. }
  944. switch source {
  945. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  946. //startDate = edbInfo.StartDate
  947. //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  948. //if err != nil {
  949. // return
  950. //}
  951. //startDate = sTime.Format(utils.FormatDate)
  952. startDate = ""
  953. var edbInfoIdBytes []string
  954. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  955. if err != nil {
  956. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  957. break
  958. }
  959. var formulaStr string
  960. edbInfoList := make([]*models.EdbInfo, 0)
  961. edbInfoTag := make(map[string]int)
  962. for _, v := range calculateMap {
  963. edbInfoTag[v.FromTag] = v.FromEdbInfoId
  964. formulaStr += v.FromTag + ","
  965. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  966. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  967. edbInfoList = append(edbInfoList, edbInfo)
  968. }
  969. latestDateStr, latestValue, err = models.RefreshAllPredictCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  970. if err != nil && err.Error() != utils.ErrNoRow() {
  971. errMsg = "RefreshCalculate Err:" + err.Error()
  972. break
  973. }
  974. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ: //同差值
  975. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  976. if err != nil {
  977. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  978. break
  979. }
  980. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  981. if err != nil {
  982. errMsg = "GetEdbInfoById Err:" + err.Error()
  983. break
  984. }
  985. startDate = edbInfo.StartDate
  986. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTcz.EdbCode, startDate)
  987. if err != nil && err.Error() != utils.ErrNoRow() {
  988. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  989. break
  990. }
  991. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  992. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  993. if err != nil {
  994. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  995. break
  996. }
  997. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  998. if err != nil {
  999. errMsg = "GetEdbInfoById Err:" + err.Error()
  1000. break
  1001. }
  1002. var formulaInt int
  1003. formulaInt, _ = strconv.Atoi(calculateInfo.CalculateFormula)
  1004. if formulaInt <= 0 {
  1005. errMsg = "N值输入错误,请重新输入"
  1006. break
  1007. }
  1008. startDate = edbInfo.StartDate
  1009. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNszydpjjs(edbInfoId, source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateInfo.EdbCode, startDate)
  1010. if err != nil && err.Error() != utils.ErrNoRow() {
  1011. errMsg = "RefreshAllPredictCalculateNszydpjjs Err:" + err.Error()
  1012. break
  1013. }
  1014. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP: //刷新变频
  1015. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1016. if err != nil {
  1017. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1018. break
  1019. }
  1020. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1021. if err != nil {
  1022. errMsg = "GetEdbInfoById Err:" + err.Error()
  1023. break
  1024. }
  1025. endDate = time.Now().Format(utils.FormatDate)
  1026. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateBp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, edbInfo.EmptyType)
  1027. if err != nil && err.Error() != utils.ErrNoRow() {
  1028. errMsg = "RefreshAllPredictCalculateBp Err:" + err.Error()
  1029. break
  1030. }
  1031. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ: //刷新环比值
  1032. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1033. if err != nil {
  1034. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1035. break
  1036. }
  1037. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1038. if err != nil {
  1039. errMsg = "GetEdbInfoById Err:" + err.Error()
  1040. break
  1041. }
  1042. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1043. endDate = time.Now().Format(utils.FormatDate)
  1044. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1045. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHbz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1046. if err != nil && err.Error() != utils.ErrNoRow() {
  1047. errMsg = "RefreshAllPredictCalculateHbz Err:" + err.Error()
  1048. break
  1049. }
  1050. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ: //刷新环差值
  1051. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1052. if err != nil {
  1053. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1054. break
  1055. }
  1056. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1057. if err != nil {
  1058. errMsg = "GetEdbInfoById Err:" + err.Error()
  1059. break
  1060. }
  1061. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1062. endDate = time.Now().Format(utils.FormatDate)
  1063. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1064. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1065. if err != nil && err.Error() != utils.ErrNoRow() {
  1066. errMsg = "RefreshAllPredictCalculateHcz Err:" + err.Error()
  1067. break
  1068. }
  1069. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY: //刷新累计值转月值
  1070. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1071. if err != nil {
  1072. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1073. break
  1074. }
  1075. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1076. if err != nil {
  1077. errMsg = "GetEdbInfoById Err:" + err.Error()
  1078. break
  1079. }
  1080. endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1081. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1082. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjzzy(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1083. if err != nil && err.Error() != utils.ErrNoRow() {
  1084. errMsg = "RefreshAllPredictCalculateLjzzy Err:" + err.Error()
  1085. break
  1086. }
  1087. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1088. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1089. if err != nil {
  1090. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1091. break
  1092. }
  1093. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1094. if err != nil {
  1095. errMsg = "GetEdbInfoById Err:" + err.Error()
  1096. break
  1097. }
  1098. //startDate = edbInfo.StartDate
  1099. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1100. endDate = time.Now().Format(utils.FormatDate)
  1101. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1102. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTimeShift(edbInfoId, source, edbInfo.SubSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1103. if err != nil && err.Error() != utils.ErrNoRow() {
  1104. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1105. }
  1106. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1107. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjztbpj(edbInfo)
  1108. if err != nil && err.Error() != utils.ErrNoRow() {
  1109. errMsg = "RefreshAllPredictCalculateLjztbpj Err:" + err.Error()
  1110. break
  1111. }
  1112. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: //刷新直接拼接
  1113. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateZjpj(edbInfo)
  1114. if err != nil && err.Error() != utils.ErrNoRow() {
  1115. errMsg = "RefreshAllPredictCalculateZjpj Err:" + err.Error()
  1116. break
  1117. }
  1118. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX: //超季节性
  1119. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1120. if err != nil {
  1121. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1122. break
  1123. }
  1124. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1125. if err != nil {
  1126. errMsg = "GetEdbInfoById Err:" + err.Error()
  1127. break
  1128. }
  1129. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1130. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1131. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1132. if err != nil && err.Error() != utils.ErrNoRow() {
  1133. errMsg = "RefreshAllPredictCalculateCjjx Err:" + err.Error()
  1134. break
  1135. }
  1136. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //nhcc
  1137. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNhcc(edbInfo)
  1138. if err != nil && err.Error() != utils.ErrNoRow() {
  1139. errMsg = "RefreshAllPredictCalculateNhcc Err:" + err.Error()
  1140. break
  1141. }
  1142. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP: //降频
  1143. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1144. if err != nil {
  1145. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1146. break
  1147. }
  1148. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1149. if err != nil {
  1150. errMsg = "GetEdbInfoById Err:" + err.Error()
  1151. break
  1152. }
  1153. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateJp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1154. if err != nil && err.Error() != utils.ErrNoRow() {
  1155. errMsg = "RefreshAllPredictCalculateJp Err:" + err.Error()
  1156. break
  1157. }
  1158. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH: //年化
  1159. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1160. if err != nil {
  1161. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1162. break
  1163. }
  1164. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1165. if err != nil {
  1166. errMsg = "GetEdbInfoById Err:" + err.Error()
  1167. break
  1168. }
  1169. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNh(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1170. if err != nil && err.Error() != utils.ErrNoRow() {
  1171. errMsg = "RefreshAllPredictCalculateNh Err:" + err.Error()
  1172. break
  1173. }
  1174. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: //扩散指数
  1175. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateKszs(edbInfo)
  1176. if err != nil && err.Error() != utils.ErrNoRow() {
  1177. errMsg = "RefreshAllPredictCalculateKszs Err:" + err.Error()
  1178. break
  1179. }
  1180. default:
  1181. // 获取通用的数据源处理服务
  1182. baseEdbInfoModel = models.GetBasePredictEdbInfoModel(source)
  1183. // 没有找到的话,那么就直接返回报错吧
  1184. if baseEdbInfoModel == nil {
  1185. br.Msg = "来源异常,请联系相关开发!"
  1186. br.ErrMsg = "来源异常,请联系相关开发"
  1187. return
  1188. }
  1189. latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1190. }
  1191. if errMsg != `` {
  1192. br.Msg = "刷新指标失败!"
  1193. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1194. return
  1195. }
  1196. // 更新指标最大最小值
  1197. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  1198. if err != nil {
  1199. br.Msg = errMsg
  1200. br.ErrMsg = err.Error()
  1201. return
  1202. }
  1203. // 更新ES
  1204. go logic.UpdateEs(edbInfo.EdbInfoId)
  1205. br.Ret = 200
  1206. br.Success = true
  1207. br.Msg = "获取成功"
  1208. }
  1209. // CalculateComputeCorrelation
  1210. // @Title 拟合残差计算相关性接口
  1211. // @Description 拟合残差计算相关性接口
  1212. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1213. // @Success Ret=200 返回指标id
  1214. // @router /compute_correlation [post]
  1215. func (this *PredictCalculateController) CalculateComputeCorrelation() {
  1216. br := new(models.BaseResponse).Init()
  1217. defer func() {
  1218. this.Data["json"] = br
  1219. this.ServeJSON()
  1220. }()
  1221. var req models.EdbInfoCalculateBatchSaveReq
  1222. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1223. if err != nil {
  1224. br.Msg = "参数解析异常!"
  1225. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1226. return
  1227. }
  1228. if len(req.EdbInfoIdArr) != 2 {
  1229. br.Msg = "选择的指标异常,请重新选择"
  1230. return
  1231. }
  1232. //校验时间格式
  1233. //数据格式:2022-11-01,2022-11-10
  1234. timeList := strings.Split(req.Formula, ",")
  1235. if len(timeList) != 2 {
  1236. br.Msg = "选择时间有误,请重新输入"
  1237. return
  1238. }
  1239. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1240. if err != nil {
  1241. br.Msg = "开始日期有误,请重新输入"
  1242. return
  1243. }
  1244. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1245. if err != nil {
  1246. br.Msg = "结束日期有误,请重新输入"
  1247. return
  1248. }
  1249. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1250. br.Msg = "日期间隔不得少于两天"
  1251. return
  1252. }
  1253. var nhccDate models.NhccDate // 拟合残差的日期
  1254. nhccDate.StartDate = startDate
  1255. nhccDate.EndDate = endDate
  1256. // 来源预测指标信息
  1257. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1258. if fromEdbInfoId <= 0 {
  1259. br.Msg = "请选择指标"
  1260. return
  1261. }
  1262. // 来源指标(自变量指标)
  1263. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1264. if err != nil {
  1265. br.Msg = "获取指标信息失败"
  1266. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1267. return
  1268. }
  1269. if fromEdbInfo.EdbInfoType != 1 {
  1270. br.Msg = "来源指标不是预测指标"
  1271. br.ErrMsg = "来源指标不是预测指标"
  1272. return
  1273. }
  1274. // 因变量指标
  1275. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1276. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1277. if tmpErr != nil {
  1278. br.Msg = "获取因变量的指标信息失败"
  1279. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1280. return
  1281. }
  1282. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1283. br.Msg = "两个指标不允许为同一个"
  1284. br.ErrMsg = "两个指标不允许为同一个"
  1285. return
  1286. }
  1287. rVal, err := models.CalculatePredictComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1288. if err != nil {
  1289. br.Msg = "计算失败"
  1290. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1291. return
  1292. }
  1293. br.Ret = 200
  1294. br.Success = true
  1295. br.Msg = "计算成功"
  1296. br.Data = rVal
  1297. br.IsAddLog = true
  1298. }