base_from_predict_calculate.go 45 KB

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