base_from_predict_calculate.go 45 KB

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