base_from_predict_calculate.go 45 KB

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