base_from_predict_calculate.go 42 KB

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