predict_edb.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package logic
  2. import (
  3. "errors"
  4. "hongze/hongze_edb_lib/models"
  5. "hongze/hongze_edb_lib/services"
  6. "hongze/hongze_edb_lib/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // AddPredictEdbInfo 新增预测指标
  12. func AddPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName string, ruleList []models.RuleConfig, sysUserId int, sysUserName string) (edbInfo *models.EdbInfo, err error, errMsg string) {
  13. var sourceEdbInfo *models.EdbInfo
  14. // 来源指标信息校验
  15. {
  16. sourceEdbInfo, err = models.GetEdbInfoById(sourceEdbInfoId)
  17. if err != nil && err.Error() != utils.ErrNoRow() {
  18. errMsg = "新增失败"
  19. err = errors.New("获取来源指标失败,Err:" + err.Error())
  20. return
  21. }
  22. if sourceEdbInfo == nil {
  23. errMsg = "找不到该来源指标"
  24. err = nil
  25. return
  26. }
  27. //必须是普通的指标
  28. if sourceEdbInfo.EdbInfoType != 0 {
  29. errMsg = "来源指标异常,不是普通的指标"
  30. return
  31. }
  32. if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, sourceEdbInfo.Frequency) {
  33. errMsg = "预测指标只支持选择日度、周度、月度的指标"
  34. return
  35. }
  36. }
  37. var classifyInfo *models.EdbClassify
  38. // 来源分类信息校验
  39. {
  40. classifyInfo, err = models.GetEdbClassifyById(classifyId)
  41. if err != nil && err.Error() != utils.ErrNoRow() {
  42. errMsg = "新增失败"
  43. err = errors.New("获取预测指标分类失败,Err:" + err.Error())
  44. return
  45. }
  46. if classifyInfo == nil {
  47. errMsg = "找不到该预测指标分类"
  48. err = nil
  49. return
  50. }
  51. //必须是预测指标分类
  52. if classifyInfo.ClassifyType != 1 {
  53. errMsg = "预测指标分类异常,不是预测指标分类"
  54. return
  55. }
  56. }
  57. edbName = strings.Trim(edbName, " ")
  58. edbCode := sourceEdbInfo.EdbCode + "_" + time.Now().Format(utils.FormatShortDateTimeUnSpace)
  59. //判断指标名称是否存在
  60. var condition string
  61. var pars []interface{}
  62. condition += " AND edb_info_type=? "
  63. pars = append(pars, 1)
  64. condition += " AND edb_name=? "
  65. pars = append(pars, edbName)
  66. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  67. if err != nil {
  68. errMsg = "判断指标名称是否存在失败"
  69. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  70. return
  71. }
  72. if count > 0 {
  73. errMsg = "指标名称已存在,请重新填写"
  74. return
  75. }
  76. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  77. edbInfo = &models.EdbInfo{
  78. //EdbInfoId: 0,
  79. EdbInfoType: 1,
  80. SourceName: "预测指标",
  81. Source: utils.DATA_SOURCE_PREDICT,
  82. EdbCode: edbCode,
  83. EdbName: edbName,
  84. EdbNameSource: edbName,
  85. Frequency: sourceEdbInfo.Frequency,
  86. Unit: sourceEdbInfo.Unit,
  87. StartDate: sourceEdbInfo.StartDate,
  88. ClassifyId: classifyId,
  89. SysUserId: sysUserId,
  90. SysUserRealName: sysUserName,
  91. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  92. CreateTime: time.Now(),
  93. ModifyTime: time.Now(),
  94. MinValue: sourceEdbInfo.MinValue,
  95. MaxValue: sourceEdbInfo.MaxValue,
  96. CalculateFormula: sourceEdbInfo.CalculateFormula,
  97. EdbType: 1,
  98. //Sort: sourceEdbInfo.,
  99. LatestDate: sourceEdbInfo.LatestDate,
  100. LatestValue: sourceEdbInfo.LatestValue,
  101. MoveType: sourceEdbInfo.MoveType,
  102. MoveFrequency: sourceEdbInfo.MoveFrequency,
  103. NoUpdate: sourceEdbInfo.NoUpdate,
  104. ServerUrl: "",
  105. }
  106. // 关联关系表
  107. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  108. fromEdbMap := make(map[int]int)
  109. // 源指标关联关系表
  110. calculateMappingItem := &models.EdbInfoCalculateMapping{
  111. //EdbInfoCalculateMappingId: 0,
  112. //EdbInfoId: 0,
  113. Source: edbInfo.Source,
  114. SourceName: edbInfo.SourceName,
  115. EdbCode: edbInfo.EdbCode,
  116. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  117. FromEdbCode: sourceEdbInfo.EdbCode,
  118. FromEdbName: sourceEdbInfo.EdbName,
  119. FromSource: sourceEdbInfo.Source,
  120. FromSourceName: sourceEdbInfo.SourceName,
  121. //FromTag: "",
  122. Sort: 1,
  123. CreateTime: time.Now(),
  124. ModifyTime: time.Now(),
  125. }
  126. fromEdbMap[sourceEdbInfoId] = sourceEdbInfoId
  127. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  128. // 动态环差 计算列表
  129. calculateRuleList := make([]models.CalculateRule, 0)
  130. // 预测指标配置
  131. predictEdbConfList := make([]*models.PredictEdbConf, 0)
  132. for _, v := range ruleList {
  133. // 预测指标配置
  134. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  135. if tmpErr != nil {
  136. errMsg = "规则配置的截止日期异常,请重新填写"
  137. return
  138. }
  139. switch v.RuleType {
  140. case 8: //N期段线性外推值
  141. valInt, tmpErr := strconv.Atoi(v.Value)
  142. if tmpErr != nil {
  143. errMsg = "N期段线性外推值的N值异常"
  144. return
  145. }
  146. if valInt <= 1 {
  147. errMsg = "N期段线性外推值的N值必须大于1"
  148. return
  149. }
  150. case 9: //9:动态环差
  151. if v.Value == "" {
  152. errMsg = "请填写计算规则"
  153. return
  154. }
  155. formula := v.Value
  156. formula = strings.Replace(formula, "(", "(", -1)
  157. formula = strings.Replace(formula, ")", ")", -1)
  158. formula = strings.Replace(formula, ",", ",", -1)
  159. formula = strings.Replace(formula, "。", ".", -1)
  160. formula = strings.Replace(formula, "%", "*0.01", -1)
  161. v.Value = formula
  162. //检验公式
  163. var formulaStr string
  164. var edbInfoIdBytes []string
  165. for _, tmpEdbInfoId := range v.EdbInfoIdArr {
  166. formulaStr += tmpEdbInfoId.FromTag + ","
  167. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  168. }
  169. formulaMap := services.CheckFormula(formula)
  170. for _, formula := range formulaMap {
  171. if !strings.Contains(formulaStr, formula) {
  172. errMsg = "公式错误,请重新填写"
  173. return
  174. }
  175. }
  176. //关联的指标信息
  177. edbInfoList := make([]*models.EdbInfo, 0)
  178. // 动态环差规则 关系表
  179. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  180. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  181. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  182. if tmpErr != nil {
  183. err = tmpErr
  184. if err.Error() == utils.ErrNoRow() {
  185. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  186. return
  187. }
  188. errMsg = "获取指标失败:Err:" + err.Error()
  189. return
  190. }
  191. edbInfoList = append(edbInfoList, fromEdbInfo)
  192. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  193. {
  194. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  195. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  196. calculateMappingItem := &models.EdbInfoCalculateMapping{
  197. EdbInfoCalculateMappingId: 0,
  198. EdbInfoId: 0,
  199. Source: utils.DATA_SOURCE_CALCULATE,
  200. SourceName: "指标运算",
  201. EdbCode: "",
  202. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  203. FromEdbCode: fromEdbInfo.EdbCode,
  204. FromEdbName: fromEdbInfo.EdbName,
  205. FromSource: fromEdbInfo.Source,
  206. FromSourceName: fromEdbInfo.SourceName,
  207. //FromTag: tmpEdbInfoId.FromTag,
  208. Sort: k + 1,
  209. CreateTime: time.Now(),
  210. ModifyTime: time.Now(),
  211. }
  212. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  213. }
  214. }
  215. // 动态环差规则 关系表
  216. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  217. //PredictEdbConfCalculateMappingId: 0,
  218. EdbInfoId: 0,
  219. ConfigId: 0,
  220. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  221. FromEdbCode: fromEdbInfo.EdbCode,
  222. FromEdbName: fromEdbInfo.EdbName,
  223. FromSource: fromEdbInfo.Source,
  224. FromSourceName: fromEdbInfo.SourceName,
  225. FromTag: tmpEdbInfoId.FromTag,
  226. Sort: k + 1,
  227. CreateTime: time.Now(),
  228. ModifyTime: time.Now(),
  229. }
  230. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  231. }
  232. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  233. if !ok {
  234. errMsg = "生成计算指标失败,请使用正确的计算公式"
  235. return
  236. }
  237. calculateRuleList = append(calculateRuleList, models.CalculateRule{
  238. TrendsCalculateMappingList: trendsMappingList,
  239. EdbInfoList: edbInfoList,
  240. EdbInfoIdBytes: edbInfoIdBytes,
  241. Formula: formula,
  242. RuleType: v.RuleType,
  243. EndDate: v.EndDate,
  244. EdbInfoIdArr: v.EdbInfoIdArr,
  245. })
  246. }
  247. tmpPredictEdbConf := &models.PredictEdbConf{
  248. PredictEdbInfoId: 0,
  249. SourceEdbInfoId: sourceEdbInfoId,
  250. RuleType: v.RuleType,
  251. //FixedValue: v.Value,
  252. Value: v.Value,
  253. EndDate: ruleEndDate,
  254. ModifyTime: time.Now(),
  255. CreateTime: time.Now(),
  256. }
  257. edbInfo.EndDate = v.EndDate
  258. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  259. }
  260. err = models.AddPredictEdb(edbInfo, calculateMappingList, predictEdbConfList, calculateRuleList)
  261. if err != nil {
  262. errMsg = "保存失败"
  263. err = errors.New("保存失败,Err:" + err.Error())
  264. return
  265. }
  266. return
  267. }
  268. // EditPredictEdbInfo 编辑预测指标
  269. func EditPredictEdbInfo(edbInfoId, classifyId int, edbName string, ruleList []models.RuleConfig) (edbInfo *models.EdbInfo, err error, errMsg string) {
  270. // 指标信息校验
  271. {
  272. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  273. if err != nil && err.Error() != utils.ErrNoRow() {
  274. errMsg = "修改失败"
  275. err = errors.New("获取预测指标失败,Err:" + err.Error())
  276. return
  277. }
  278. if edbInfo == nil {
  279. errMsg = "找不到该预测指标"
  280. err = nil
  281. return
  282. }
  283. //必须是普通的指标
  284. if edbInfo.EdbInfoType != 1 {
  285. errMsg = "指标异常,不是预测指标"
  286. return
  287. }
  288. }
  289. var predictEdbConf *models.PredictEdbConf
  290. // 指标配置信息校验
  291. {
  292. // 查找该预测指标配置
  293. predictEdbConfList, tmpErr := models.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  294. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  295. errMsg = "修改失败"
  296. err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
  297. return
  298. }
  299. if len(predictEdbConfList) == 0 {
  300. errMsg = "找不到该预测指标配置"
  301. err = nil
  302. return
  303. }
  304. predictEdbConf = predictEdbConfList[0]
  305. }
  306. //判断指标名称是否存在
  307. var condition string
  308. var pars []interface{}
  309. condition += " AND edb_info_id<>? "
  310. pars = append(pars, edbInfoId)
  311. condition += " AND edb_info_type=? "
  312. pars = append(pars, 1)
  313. condition += " AND edb_name=? "
  314. pars = append(pars, edbName)
  315. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  316. if err != nil {
  317. errMsg = "判断指标名称是否存在失败"
  318. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  319. return
  320. }
  321. if count > 0 {
  322. errMsg = "指标名称已存在,请重新填写"
  323. return
  324. }
  325. edbInfo.EdbName = edbName
  326. edbInfo.EdbNameSource = edbName
  327. edbInfo.ClassifyId = classifyId
  328. edbInfo.ModifyTime = time.Now()
  329. updateEdbInfoCol := []string{"EdbName", "EdbNameSource", "ClassifyId", "EndDate", "ModifyTime"}
  330. var sourceEdbInfo *models.EdbInfo
  331. // 来源指标信息校验
  332. {
  333. sourceEdbInfo, err = models.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  334. if err != nil && err.Error() != utils.ErrNoRow() {
  335. errMsg = "新增失败"
  336. err = errors.New("获取来源指标失败,Err:" + err.Error())
  337. return
  338. }
  339. if sourceEdbInfo == nil {
  340. errMsg = "找不到该来源指标"
  341. err = nil
  342. return
  343. }
  344. //必须是普通的指标
  345. if sourceEdbInfo.EdbInfoType != 0 {
  346. errMsg = "来源指标异常,不是普通的指标"
  347. return
  348. }
  349. if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, sourceEdbInfo.Frequency) {
  350. errMsg = "预测指标只支持选择日度、周度、月度的指标"
  351. return
  352. }
  353. }
  354. // 预测指标配置
  355. // 关联关系表
  356. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  357. fromEdbMap := make(map[int]int)
  358. // 源指标关联关系表
  359. calculateMappingItem := &models.EdbInfoCalculateMapping{
  360. //EdbInfoCalculateMappingId: 0,
  361. EdbInfoId: edbInfoId,
  362. Source: edbInfo.Source,
  363. SourceName: edbInfo.SourceName,
  364. EdbCode: edbInfo.EdbCode,
  365. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  366. FromEdbCode: sourceEdbInfo.EdbCode,
  367. FromEdbName: sourceEdbInfo.EdbName,
  368. FromSource: sourceEdbInfo.Source,
  369. FromSourceName: sourceEdbInfo.SourceName,
  370. //FromTag: "",
  371. Sort: 1,
  372. CreateTime: time.Now(),
  373. ModifyTime: time.Now(),
  374. }
  375. fromEdbMap[sourceEdbInfo.EdbInfoId] = sourceEdbInfo.EdbInfoId
  376. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  377. // 动态环差 计算列表
  378. calculateRuleList := make([]models.CalculateRule, 0)
  379. // 预测指标配置
  380. predictEdbConfList := make([]*models.PredictEdbConf, 0)
  381. for _, v := range ruleList {
  382. // 预测指标配置
  383. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  384. if tmpErr != nil {
  385. errMsg = "规则配置的截止日期异常,请重新填写"
  386. return
  387. }
  388. switch v.RuleType {
  389. case 8: //N期段线性外推值
  390. valInt, tmpErr := strconv.Atoi(v.Value)
  391. if tmpErr != nil {
  392. errMsg = "N期段线性外推值的N值异常"
  393. return
  394. }
  395. if valInt <= 1 {
  396. errMsg = "N期段线性外推值的N值必须大于1"
  397. return
  398. }
  399. case 9: //9:动态环差
  400. if v.Value == "" {
  401. errMsg = "请填写计算规则"
  402. return
  403. }
  404. formula := v.Value
  405. formula = strings.Replace(formula, "(", "(", -1)
  406. formula = strings.Replace(formula, ")", ")", -1)
  407. formula = strings.Replace(formula, ",", ",", -1)
  408. formula = strings.Replace(formula, "。", ".", -1)
  409. formula = strings.Replace(formula, "%", "*0.01", -1)
  410. v.Value = formula
  411. //检验公式
  412. var formulaStr string
  413. var edbInfoIdBytes []string
  414. for _, tmpEdbInfoId := range v.EdbInfoIdArr {
  415. formulaStr += tmpEdbInfoId.FromTag + ","
  416. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  417. }
  418. formulaMap := services.CheckFormula(formula)
  419. for _, formula := range formulaMap {
  420. if !strings.Contains(formulaStr, formula) {
  421. errMsg = "公式错误,请重新填写"
  422. return
  423. }
  424. }
  425. //关联的指标信息
  426. edbInfoList := make([]*models.EdbInfo, 0)
  427. // 动态环差规则 关系表
  428. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  429. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  430. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  431. if tmpErr != nil {
  432. err = tmpErr
  433. if err.Error() == utils.ErrNoRow() {
  434. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  435. return
  436. }
  437. errMsg = "获取指标失败:Err:" + err.Error()
  438. return
  439. }
  440. edbInfoList = append(edbInfoList, fromEdbInfo)
  441. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  442. {
  443. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  444. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  445. calculateMappingItem := &models.EdbInfoCalculateMapping{
  446. EdbInfoCalculateMappingId: 0,
  447. EdbInfoId: edbInfoId,
  448. Source: utils.DATA_SOURCE_CALCULATE,
  449. SourceName: "指标运算",
  450. EdbCode: "",
  451. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  452. FromEdbCode: fromEdbInfo.EdbCode,
  453. FromEdbName: fromEdbInfo.EdbName,
  454. FromSource: fromEdbInfo.Source,
  455. FromSourceName: fromEdbInfo.SourceName,
  456. //FromTag: tmpEdbInfoId.FromTag,
  457. Sort: k + 1,
  458. CreateTime: time.Now(),
  459. ModifyTime: time.Now(),
  460. }
  461. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  462. }
  463. }
  464. // 动态环差规则 关系表
  465. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  466. //PredictEdbConfCalculateMappingId: 0,
  467. EdbInfoId: edbInfoId,
  468. ConfigId: 0,
  469. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  470. FromEdbCode: fromEdbInfo.EdbCode,
  471. FromEdbName: fromEdbInfo.EdbName,
  472. FromSource: fromEdbInfo.Source,
  473. FromSourceName: fromEdbInfo.SourceName,
  474. FromTag: tmpEdbInfoId.FromTag,
  475. Sort: k + 1,
  476. CreateTime: time.Now(),
  477. ModifyTime: time.Now(),
  478. }
  479. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  480. }
  481. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  482. if !ok {
  483. errMsg = "生成计算指标失败,请使用正确的计算公式"
  484. return
  485. }
  486. calculateRuleList = append(calculateRuleList, models.CalculateRule{
  487. TrendsCalculateMappingList: trendsMappingList,
  488. EdbInfoList: edbInfoList,
  489. EdbInfoIdBytes: edbInfoIdBytes,
  490. Formula: formula,
  491. RuleType: v.RuleType,
  492. EndDate: v.EndDate,
  493. EdbInfoIdArr: v.EdbInfoIdArr,
  494. })
  495. }
  496. tmpPredictEdbConf := &models.PredictEdbConf{
  497. PredictEdbInfoId: edbInfoId,
  498. SourceEdbInfoId: sourceEdbInfo.EdbInfoId,
  499. RuleType: v.RuleType,
  500. //FixedValue: v.Value,
  501. Value: v.Value,
  502. EndDate: ruleEndDate,
  503. ModifyTime: time.Now(),
  504. CreateTime: time.Now(),
  505. }
  506. edbInfo.EndDate = v.EndDate
  507. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  508. }
  509. err = models.EditPredictEdb(edbInfo, updateEdbInfoCol, calculateMappingList, predictEdbConfList, calculateRuleList)
  510. if err != nil {
  511. errMsg = "保存失败"
  512. err = errors.New("保存失败,Err:" + err.Error())
  513. return
  514. }
  515. return
  516. }
  517. // RefreshPredictEdbInfo 更新基础预测指标规则中的动态数据
  518. func RefreshPredictEdbInfo(edbInfoId int) (edbInfo *models.EdbInfo, err error, errMsg string) {
  519. // 指标信息校验
  520. {
  521. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  522. if err != nil && err.Error() != utils.ErrNoRow() {
  523. errMsg = "刷新失败"
  524. err = errors.New("获取预测指标失败,Err:" + err.Error())
  525. return
  526. }
  527. if edbInfo == nil {
  528. errMsg = "找不到该预测指标"
  529. err = nil
  530. return
  531. }
  532. //必须是普通的指标
  533. if edbInfo.EdbInfoType != 1 {
  534. errMsg = "指标异常,不是预测指标"
  535. return
  536. }
  537. }
  538. // 配置 与 指标的 关联关系表
  539. list, err := models.GetPredictEdbConfCalculateMappingListByEdbInfoId(edbInfoId)
  540. if err != nil {
  541. return
  542. }
  543. // 没有关联指标,不需要刷新
  544. if len(list) <= 0 {
  545. return
  546. }
  547. // 配置关联的指标信息
  548. predictEdbConfCalculateMappingListMap := make(map[int][]*models.PredictEdbConfCalculateMapping)
  549. configIdList := make([]int, 0) //关联配置id
  550. edbInfoIdList := make([]int, 0) //关联指标配置id
  551. edbInfoIdMap := make(map[int]int, 0) //关联指标配置map
  552. for _, v := range list {
  553. configList, ok := predictEdbConfCalculateMappingListMap[v.ConfigId]
  554. if !ok {
  555. configList = make([]*models.PredictEdbConfCalculateMapping, 0)
  556. configIdList = append(configIdList, v.ConfigId)
  557. }
  558. if _, ok := edbInfoIdMap[v.FromEdbInfoId]; !ok {
  559. edbInfoIdList = append(edbInfoIdList, v.FromEdbInfoId)
  560. }
  561. configList = append(configList, v)
  562. predictEdbConfCalculateMappingListMap[v.ConfigId] = configList
  563. }
  564. predictEdbConfList, err := models.GetPredictEdbConfListByConfigIdList(configIdList)
  565. if err != nil {
  566. errMsg = "刷新失败"
  567. err = errors.New("获取预测指标配置信息失败,Err:" + err.Error())
  568. return
  569. }
  570. if len(predictEdbConfList) == 0 {
  571. errMsg = "找不到该预测指标配置"
  572. err = nil
  573. return
  574. }
  575. // 指标信息
  576. edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIdList)
  577. if err != nil {
  578. err = errors.New("获取关联指标失败,Err:" + err.Error())
  579. return
  580. }
  581. // 指标信息map
  582. edbInfoListMap := make(map[int]*models.EdbInfo)
  583. for _, v := range edbInfoList {
  584. edbInfoListMap[v.EdbInfoId] = v
  585. }
  586. // 刷新所有的规则
  587. for _, v := range predictEdbConfList {
  588. switch v.RuleType {
  589. case 9:
  590. if v.Value == "" {
  591. errMsg = "请填写计算规则"
  592. return
  593. }
  594. formula := v.Value
  595. // 动态环差规则 关系表
  596. trendsMappingList := predictEdbConfCalculateMappingListMap[v.ConfigId]
  597. // 关联标签
  598. edbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  599. //关联的指标信息
  600. edbInfoList := make([]*models.EdbInfo, 0)
  601. for _, trendsMapping := range trendsMappingList {
  602. tmpEdbInfo, ok := edbInfoListMap[trendsMapping.FromEdbInfoId]
  603. if ok {
  604. edbInfoList = append(edbInfoList, tmpEdbInfo)
  605. }
  606. // 关联标签
  607. edbInfoIdArr = append(edbInfoIdArr, models.EdbInfoFromTag{
  608. EdbInfoId: trendsMapping.FromEdbInfoId,
  609. FromTag: trendsMapping.FromTag,
  610. })
  611. }
  612. //检验公式
  613. var formulaStr string
  614. var edbInfoIdBytes []string
  615. for _, tmpEdbInfoId := range edbInfoIdArr {
  616. formulaStr += tmpEdbInfoId.FromTag + ","
  617. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  618. }
  619. formulaMap := services.CheckFormula(formula)
  620. for _, formula := range formulaMap {
  621. if !strings.Contains(formulaStr, formula) {
  622. errMsg = "公式错误,请重新填写"
  623. return
  624. }
  625. }
  626. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  627. if !ok {
  628. errMsg = "生成计算指标失败,请使用正确的计算公式"
  629. return
  630. }
  631. rule := models.CalculateRule{
  632. EdbInfoId: v.PredictEdbInfoId,
  633. ConfigId: v.ConfigId,
  634. TrendsCalculateMappingList: trendsMappingList,
  635. EdbInfoList: edbInfoList,
  636. EdbInfoIdBytes: edbInfoIdBytes,
  637. Formula: formula,
  638. RuleType: v.RuleType,
  639. EndDate: v.EndDate.Format(utils.FormatDate),
  640. EdbInfoIdArr: edbInfoIdArr,
  641. }
  642. err = models.RefreshCalculateByRuleBy9(rule)
  643. if err != nil {
  644. return
  645. }
  646. }
  647. }
  648. return
  649. }