base_from_predict_calculate.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. if edbInfo.IsStaticData == 1 {
  907. //静态数据直接返回
  908. br.Ret = 200
  909. br.Success = true
  910. br.Msg = "获取成功"
  911. return
  912. }
  913. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  914. if utils.Rc.IsExist(cacheKey) {
  915. br.Ret = 501
  916. br.Success = true
  917. br.Msg = "系统处理中,请稍后重试"
  918. br.IsSendEmail = false
  919. return
  920. }
  921. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  922. defer func() {
  923. _ = utils.Rc.Delete(cacheKey)
  924. }()
  925. startDate := req.StartDate
  926. var errMsg string
  927. endDate := time.Now().Format(utils.FormatDate)
  928. edbInfoId := edbInfo.EdbInfoId
  929. source := edbInfo.Source
  930. var latestDateStr string // 最近实际数据的日期
  931. var latestValue float64 // 最近实际数据的值
  932. var baseEdbInfoModel models.BasePredictEdbInfoInterface
  933. refreshParams := models.RefreshParams{
  934. EdbInfo: edbInfo,
  935. }
  936. switch source {
  937. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ: //刷新同比值
  938. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  939. if err != nil {
  940. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  941. break
  942. }
  943. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  944. if err != nil {
  945. errMsg = "GetEdbInfoById Err:" + err.Error()
  946. break
  947. }
  948. refreshParams.FromEdbInfo = fromEdbInfo
  949. refreshParams.StartDate = edbInfo.StartDate
  950. }
  951. switch source {
  952. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  953. //startDate = edbInfo.StartDate
  954. //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  955. //if err != nil {
  956. // return
  957. //}
  958. //startDate = sTime.Format(utils.FormatDate)
  959. startDate = ""
  960. var edbInfoIdBytes []string
  961. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  962. if err != nil {
  963. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  964. break
  965. }
  966. var formulaStr string
  967. edbInfoList := make([]*models.EdbInfo, 0)
  968. edbInfoTag := make(map[string]int)
  969. for _, v := range calculateMap {
  970. edbInfoTag[v.FromTag] = v.FromEdbInfoId
  971. formulaStr += v.FromTag + ","
  972. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  973. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  974. edbInfoList = append(edbInfoList, edbInfo)
  975. }
  976. latestDateStr, latestValue, err = models.RefreshAllPredictCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  977. if err != nil && err.Error() != utils.ErrNoRow() {
  978. errMsg = "RefreshCalculate Err:" + err.Error()
  979. break
  980. }
  981. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ: //同差值
  982. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  983. if err != nil {
  984. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  985. break
  986. }
  987. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  988. if err != nil {
  989. errMsg = "GetEdbInfoById Err:" + err.Error()
  990. break
  991. }
  992. startDate = edbInfo.StartDate
  993. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTcz.EdbCode, startDate)
  994. if err != nil && err.Error() != utils.ErrNoRow() {
  995. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  996. break
  997. }
  998. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  999. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1000. if err != nil {
  1001. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1002. break
  1003. }
  1004. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1005. if err != nil {
  1006. errMsg = "GetEdbInfoById Err:" + err.Error()
  1007. break
  1008. }
  1009. var formulaInt int
  1010. formulaInt, _ = strconv.Atoi(calculateInfo.CalculateFormula)
  1011. if formulaInt <= 0 {
  1012. errMsg = "N值输入错误,请重新输入"
  1013. break
  1014. }
  1015. startDate = edbInfo.StartDate
  1016. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNszydpjjs(edbInfoId, source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateInfo.EdbCode, startDate)
  1017. if err != nil && err.Error() != utils.ErrNoRow() {
  1018. errMsg = "RefreshAllPredictCalculateNszydpjjs Err:" + err.Error()
  1019. break
  1020. }
  1021. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP: //刷新变频
  1022. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1023. if err != nil {
  1024. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1025. break
  1026. }
  1027. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1028. if err != nil {
  1029. errMsg = "GetEdbInfoById Err:" + err.Error()
  1030. break
  1031. }
  1032. endDate = time.Now().Format(utils.FormatDate)
  1033. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateBp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, edbInfo.EmptyType)
  1034. if err != nil && err.Error() != utils.ErrNoRow() {
  1035. errMsg = "RefreshAllPredictCalculateBp Err:" + err.Error()
  1036. break
  1037. }
  1038. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ: //刷新环比值
  1039. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1040. if err != nil {
  1041. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1042. break
  1043. }
  1044. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1045. if err != nil {
  1046. errMsg = "GetEdbInfoById Err:" + err.Error()
  1047. break
  1048. }
  1049. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1050. endDate = time.Now().Format(utils.FormatDate)
  1051. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1052. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHbz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1053. if err != nil && err.Error() != utils.ErrNoRow() {
  1054. errMsg = "RefreshAllPredictCalculateHbz Err:" + err.Error()
  1055. break
  1056. }
  1057. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ: //刷新环差值
  1058. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1059. if err != nil {
  1060. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1061. break
  1062. }
  1063. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1064. if err != nil {
  1065. errMsg = "GetEdbInfoById Err:" + err.Error()
  1066. break
  1067. }
  1068. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1069. endDate = time.Now().Format(utils.FormatDate)
  1070. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1071. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1072. if err != nil && err.Error() != utils.ErrNoRow() {
  1073. errMsg = "RefreshAllPredictCalculateHcz Err:" + err.Error()
  1074. break
  1075. }
  1076. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY: //刷新累计值转月值
  1077. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1078. if err != nil {
  1079. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1080. break
  1081. }
  1082. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1083. if err != nil {
  1084. errMsg = "GetEdbInfoById Err:" + err.Error()
  1085. break
  1086. }
  1087. endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1088. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1089. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjzzy(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1090. if err != nil && err.Error() != utils.ErrNoRow() {
  1091. errMsg = "RefreshAllPredictCalculateLjzzy Err:" + err.Error()
  1092. break
  1093. }
  1094. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1095. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1096. if err != nil {
  1097. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1098. break
  1099. }
  1100. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1101. if err != nil {
  1102. errMsg = "GetEdbInfoById Err:" + err.Error()
  1103. break
  1104. }
  1105. //startDate = edbInfo.StartDate
  1106. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1107. endDate = time.Now().Format(utils.FormatDate)
  1108. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1109. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTimeShift(edbInfoId, source, edbInfo.SubSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1110. if err != nil && err.Error() != utils.ErrNoRow() {
  1111. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1112. }
  1113. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1114. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjztbpj(edbInfo)
  1115. if err != nil && err.Error() != utils.ErrNoRow() {
  1116. errMsg = "RefreshAllPredictCalculateLjztbpj Err:" + err.Error()
  1117. break
  1118. }
  1119. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: //刷新直接拼接
  1120. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateZjpj(edbInfo)
  1121. if err != nil && err.Error() != utils.ErrNoRow() {
  1122. errMsg = "RefreshAllPredictCalculateZjpj Err:" + err.Error()
  1123. break
  1124. }
  1125. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX: //超季节性
  1126. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1127. if err != nil {
  1128. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1129. break
  1130. }
  1131. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1132. if err != nil {
  1133. errMsg = "GetEdbInfoById Err:" + err.Error()
  1134. break
  1135. }
  1136. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1137. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1138. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1139. if err != nil && err.Error() != utils.ErrNoRow() {
  1140. errMsg = "RefreshAllPredictCalculateCjjx Err:" + err.Error()
  1141. break
  1142. }
  1143. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //nhcc
  1144. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNhcc(edbInfo)
  1145. if err != nil && err.Error() != utils.ErrNoRow() {
  1146. errMsg = "RefreshAllPredictCalculateNhcc Err:" + err.Error()
  1147. break
  1148. }
  1149. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP: //降频
  1150. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1151. if err != nil {
  1152. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1153. break
  1154. }
  1155. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1156. if err != nil {
  1157. errMsg = "GetEdbInfoById Err:" + err.Error()
  1158. break
  1159. }
  1160. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateJp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1161. if err != nil && err.Error() != utils.ErrNoRow() {
  1162. errMsg = "RefreshAllPredictCalculateJp Err:" + err.Error()
  1163. break
  1164. }
  1165. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH: //年化
  1166. calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1167. if err != nil {
  1168. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1169. break
  1170. }
  1171. fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
  1172. if err != nil {
  1173. errMsg = "GetEdbInfoById Err:" + err.Error()
  1174. break
  1175. }
  1176. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNh(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1177. if err != nil && err.Error() != utils.ErrNoRow() {
  1178. errMsg = "RefreshAllPredictCalculateNh Err:" + err.Error()
  1179. break
  1180. }
  1181. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: //扩散指数
  1182. latestDateStr, latestValue, err = models.RefreshAllPredictCalculateKszs(edbInfo)
  1183. if err != nil && err.Error() != utils.ErrNoRow() {
  1184. errMsg = "RefreshAllPredictCalculateKszs Err:" + err.Error()
  1185. break
  1186. }
  1187. default:
  1188. // 获取通用的数据源处理服务
  1189. baseEdbInfoModel = models.GetBasePredictEdbInfoModel(source)
  1190. // 没有找到的话,那么就直接返回报错吧
  1191. if baseEdbInfoModel == nil {
  1192. br.Msg = "来源异常,请联系相关开发!"
  1193. br.ErrMsg = "来源异常,请联系相关开发"
  1194. return
  1195. }
  1196. latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1197. }
  1198. if errMsg != `` {
  1199. br.Msg = "刷新指标失败!"
  1200. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1201. return
  1202. }
  1203. // 更新指标最大最小值
  1204. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  1205. if err != nil {
  1206. br.Msg = errMsg
  1207. br.ErrMsg = err.Error()
  1208. return
  1209. }
  1210. // 更新ES
  1211. go logic.UpdateEs(edbInfo.EdbInfoId)
  1212. br.Ret = 200
  1213. br.Success = true
  1214. br.Msg = "获取成功"
  1215. }
  1216. // CalculateComputeCorrelation
  1217. // @Title 拟合残差计算相关性接口
  1218. // @Description 拟合残差计算相关性接口
  1219. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1220. // @Success Ret=200 返回指标id
  1221. // @router /compute_correlation [post]
  1222. func (this *PredictCalculateController) CalculateComputeCorrelation() {
  1223. br := new(models.BaseResponse).Init()
  1224. defer func() {
  1225. this.Data["json"] = br
  1226. this.ServeJSON()
  1227. }()
  1228. var req models.EdbInfoCalculateBatchSaveReq
  1229. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1230. if err != nil {
  1231. br.Msg = "参数解析异常!"
  1232. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1233. return
  1234. }
  1235. if len(req.EdbInfoIdArr) != 2 {
  1236. br.Msg = "选择的指标异常,请重新选择"
  1237. return
  1238. }
  1239. //校验时间格式
  1240. //数据格式:2022-11-01,2022-11-10
  1241. timeList := strings.Split(req.Formula, ",")
  1242. if len(timeList) != 2 {
  1243. br.Msg = "选择时间有误,请重新输入"
  1244. return
  1245. }
  1246. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1247. if err != nil {
  1248. br.Msg = "开始日期有误,请重新输入"
  1249. return
  1250. }
  1251. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1252. if err != nil {
  1253. br.Msg = "结束日期有误,请重新输入"
  1254. return
  1255. }
  1256. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1257. br.Msg = "日期间隔不得少于两天"
  1258. return
  1259. }
  1260. var nhccDate models.NhccDate // 拟合残差的日期
  1261. nhccDate.StartDate = startDate
  1262. nhccDate.EndDate = endDate
  1263. // 来源预测指标信息
  1264. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1265. if fromEdbInfoId <= 0 {
  1266. br.Msg = "请选择指标"
  1267. return
  1268. }
  1269. // 来源指标(自变量指标)
  1270. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1271. if err != nil {
  1272. br.Msg = "获取指标信息失败"
  1273. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1274. return
  1275. }
  1276. if fromEdbInfo.EdbInfoType != 1 {
  1277. br.Msg = "来源指标不是预测指标"
  1278. br.ErrMsg = "来源指标不是预测指标"
  1279. return
  1280. }
  1281. // 因变量指标
  1282. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1283. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1284. if tmpErr != nil {
  1285. br.Msg = "获取因变量的指标信息失败"
  1286. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1287. return
  1288. }
  1289. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1290. br.Msg = "两个指标不允许为同一个"
  1291. br.ErrMsg = "两个指标不允许为同一个"
  1292. return
  1293. }
  1294. rVal, err := models.CalculatePredictComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1295. if err != nil {
  1296. br.Msg = "计算失败"
  1297. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1298. return
  1299. }
  1300. br.Ret = 200
  1301. br.Success = true
  1302. br.Msg = "计算成功"
  1303. br.Data = rVal
  1304. br.IsAddLog = true
  1305. }