predict_edb.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. package logic
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // AddPredictEdbInfo 新增预测指标
  11. func AddPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName, dataDateType string, ruleList []models.RuleConfig, minValue, maxValue float64, sysUserId int, sysUserName string) (edbInfo *models.EdbInfo, err error, errMsg string) {
  12. var sourceEdbInfo *models.EdbInfo
  13. // 来源指标信息校验
  14. {
  15. sourceEdbInfo, err = models.GetEdbInfoById(sourceEdbInfoId)
  16. if err != nil && err.Error() != utils.ErrNoRow() {
  17. errMsg = "新增失败"
  18. err = errors.New("获取来源指标失败,Err:" + err.Error())
  19. return
  20. }
  21. if sourceEdbInfo == nil {
  22. errMsg = "找不到该来源指标"
  23. err = errors.New(errMsg)
  24. return
  25. }
  26. //必须是普通的指标
  27. if sourceEdbInfo.EdbInfoType != 0 {
  28. errMsg = "来源指标异常,不是普通的指标"
  29. err = errors.New(errMsg)
  30. return
  31. }
  32. //if !utils.InArrayByStr([]string{"日度", "周度", "月度", "年度"}, sourceEdbInfo.Frequency) {
  33. // errMsg = "预测指标只支持选择日度、周度、月度、年度的指标"
  34. // err = errors.New(errMsg)
  35. // return
  36. //}
  37. }
  38. var classifyInfo *models.EdbClassify
  39. // 来源分类信息校验
  40. {
  41. classifyInfo, err = models.GetEdbClassifyById(classifyId)
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. errMsg = "新增失败"
  44. err = errors.New("获取预测指标分类失败,Err:" + err.Error())
  45. return
  46. }
  47. if classifyInfo == nil {
  48. errMsg = "找不到该预测指标分类"
  49. err = errors.New(errMsg)
  50. return
  51. }
  52. //必须是预测指标分类
  53. if classifyInfo.ClassifyType != 1 {
  54. errMsg = "预测指标分类异常,不是预测指标分类"
  55. err = errors.New(errMsg)
  56. return
  57. }
  58. }
  59. edbName = strings.Trim(edbName, " ")
  60. edbCode := sourceEdbInfo.EdbCode + "_" + time.Now().Format(utils.FormatShortDateTimeUnSpace)
  61. //判断指标名称是否存在
  62. var condition string
  63. var pars []interface{}
  64. condition += " AND edb_info_type=? "
  65. pars = append(pars, 1)
  66. condition += " AND edb_name=? "
  67. pars = append(pars, edbName)
  68. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  69. if err != nil {
  70. errMsg = "判断指标名称是否存在失败"
  71. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  72. return
  73. }
  74. if count > 0 {
  75. errMsg = "指标名称已存在,请重新填写"
  76. err = errors.New(errMsg)
  77. return
  78. }
  79. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  80. if dataDateType == `` {
  81. dataDateType = `自然日`
  82. }
  83. edbInfo = &models.EdbInfo{
  84. //EdbInfoId: 0,
  85. EdbInfoType: 1,
  86. SourceName: "预测指标",
  87. Source: utils.DATA_SOURCE_PREDICT,
  88. EdbCode: edbCode,
  89. EdbName: edbName,
  90. EdbNameSource: edbName,
  91. Frequency: sourceEdbInfo.Frequency,
  92. Unit: sourceEdbInfo.Unit,
  93. StartDate: sourceEdbInfo.StartDate,
  94. ClassifyId: classifyId,
  95. SysUserId: sysUserId,
  96. SysUserRealName: sysUserName,
  97. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  98. CreateTime: time.Now(),
  99. ModifyTime: time.Now(),
  100. MinValue: minValue,
  101. MaxValue: maxValue,
  102. CalculateFormula: sourceEdbInfo.CalculateFormula,
  103. EdbType: 1,
  104. //Sort: sourceEdbInfo.,
  105. LatestDate: sourceEdbInfo.LatestDate,
  106. LatestValue: sourceEdbInfo.LatestValue,
  107. MoveType: sourceEdbInfo.MoveType,
  108. MoveFrequency: sourceEdbInfo.MoveFrequency,
  109. NoUpdate: sourceEdbInfo.NoUpdate,
  110. ServerUrl: "",
  111. DataDateType: dataDateType,
  112. }
  113. // 关联关系表
  114. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  115. fromEdbMap := make(map[int]int)
  116. // 源指标关联关系表
  117. calculateMappingItem := &models.EdbInfoCalculateMapping{
  118. //EdbInfoCalculateMappingId: 0,
  119. //EdbInfoId: 0,
  120. Source: edbInfo.Source,
  121. SourceName: edbInfo.SourceName,
  122. EdbCode: edbInfo.EdbCode,
  123. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  124. FromEdbCode: sourceEdbInfo.EdbCode,
  125. FromEdbName: sourceEdbInfo.EdbName,
  126. FromSource: sourceEdbInfo.Source,
  127. FromSourceName: sourceEdbInfo.SourceName,
  128. //FromTag: "",
  129. Sort: 1,
  130. CreateTime: time.Now(),
  131. ModifyTime: time.Now(),
  132. }
  133. fromEdbMap[sourceEdbInfoId] = sourceEdbInfoId
  134. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  135. // 动态环差 计算列表
  136. calculateRuleMap := make(map[int]models.CalculateRule, 0)
  137. // 预测指标配置
  138. predictEdbConfList := make([]*models.PredictEdbConf, 0)
  139. for ruleIndex, v := range ruleList {
  140. // 预测指标配置
  141. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  142. if tmpErr != nil {
  143. errMsg = "规则配置的截止日期异常,请重新填写"
  144. err = errors.New(errMsg)
  145. return
  146. }
  147. //1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差,10:根据 给定终值后插值 规则获取预测数据,11:根据 季节性 规则获取预测数据,12:根据 移动平均同比 规则获取预测数据
  148. // 环比、环差、动态环差、季节性、移动平均同比不支持年度
  149. if sourceEdbInfo.Frequency == "年度" && utils.InArrayByInt([]int{5, 6, 11, 12}, v.RuleType) {
  150. errMsg = "环比、环差、动态环差、季节性、移动平均同比不支持年度指标"
  151. err = errors.New(errMsg)
  152. return
  153. }
  154. switch v.RuleType {
  155. case 8: //N期段线性外推值
  156. valInt, tmpErr := strconv.Atoi(v.Value)
  157. if tmpErr != nil {
  158. errMsg = "N期段线性外推值的N值异常"
  159. err = errors.New(errMsg)
  160. return
  161. }
  162. if valInt <= 1 {
  163. errMsg = "N期段线性外推值的N值必须大于1"
  164. err = errors.New(errMsg)
  165. return
  166. }
  167. case 9: //9:动态环差
  168. if v.Value == "" {
  169. errMsg = "请填写计算规则"
  170. err = errors.New(errMsg)
  171. return
  172. }
  173. formula := v.Value
  174. formula = strings.Replace(formula, "(", "(", -1)
  175. formula = strings.Replace(formula, ")", ")", -1)
  176. formula = strings.Replace(formula, ",", ",", -1)
  177. formula = strings.Replace(formula, "。", ".", -1)
  178. formula = strings.Replace(formula, "%", "*0.01", -1)
  179. v.Value = formula
  180. //检验公式
  181. var formulaStr string
  182. var edbInfoIdBytes []string
  183. for _, tmpEdbInfoId := range v.EdbInfoIdArr {
  184. formulaStr += tmpEdbInfoId.FromTag + ","
  185. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  186. }
  187. formulaMap := utils.CheckFormula(formula)
  188. for _, formula := range formulaMap {
  189. if !strings.Contains(formulaStr, formula) {
  190. errMsg = "公式错误,请重新填写"
  191. err = errors.New(errMsg)
  192. return
  193. }
  194. }
  195. //关联的指标信息
  196. edbInfoList := make([]*models.EdbInfo, 0)
  197. // 动态环差规则 关系表
  198. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  199. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  200. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  201. if tmpErr != nil {
  202. err = tmpErr
  203. if err.Error() == utils.ErrNoRow() {
  204. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  205. err = errors.New(errMsg)
  206. return
  207. }
  208. errMsg = "获取指标失败:Err:" + err.Error()
  209. err = errors.New(errMsg)
  210. return
  211. }
  212. edbInfoList = append(edbInfoList, fromEdbInfo)
  213. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  214. {
  215. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  216. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  217. calculateMappingItem := &models.EdbInfoCalculateMapping{
  218. EdbInfoCalculateMappingId: 0,
  219. EdbInfoId: 0,
  220. Source: utils.DATA_SOURCE_CALCULATE,
  221. SourceName: "指标运算",
  222. EdbCode: "",
  223. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  224. FromEdbCode: fromEdbInfo.EdbCode,
  225. FromEdbName: fromEdbInfo.EdbName,
  226. FromSource: fromEdbInfo.Source,
  227. FromSourceName: fromEdbInfo.SourceName,
  228. //FromTag: tmpEdbInfoId.FromTag,
  229. Sort: k + 1,
  230. CreateTime: time.Now(),
  231. ModifyTime: time.Now(),
  232. }
  233. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  234. }
  235. }
  236. // 动态环差规则 关系表
  237. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  238. //PredictEdbConfCalculateMappingId: 0,
  239. EdbInfoId: 0,
  240. ConfigId: 0,
  241. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  242. FromEdbCode: fromEdbInfo.EdbCode,
  243. FromEdbName: fromEdbInfo.EdbName,
  244. FromSource: fromEdbInfo.Source,
  245. FromSourceName: fromEdbInfo.SourceName,
  246. FromTag: tmpEdbInfoId.FromTag,
  247. Sort: k + 1,
  248. CreateTime: time.Now(),
  249. ModifyTime: time.Now(),
  250. }
  251. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  252. }
  253. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  254. if !ok {
  255. errMsg = "生成计算指标失败,请使用正确的计算公式"
  256. err = errors.New(errMsg)
  257. return
  258. }
  259. calculateRuleMap[ruleIndex] = models.CalculateRule{
  260. TrendsCalculateMappingList: trendsMappingList,
  261. EdbInfoList: edbInfoList,
  262. EdbInfoIdBytes: edbInfoIdBytes,
  263. Formula: formula,
  264. RuleType: v.RuleType,
  265. EndDate: v.EndDate,
  266. EdbInfoIdArr: v.EdbInfoIdArr,
  267. }
  268. case 14: //14:根据 一元线性拟合 规则获取预测数据
  269. if v.Value == "" {
  270. errMsg = "请填写一元线性拟合规则"
  271. err = errors.New(errMsg)
  272. return
  273. }
  274. //关联的指标信息
  275. edbInfoList := make([]*models.EdbInfo, 0)
  276. // 动态环差规则 关系表
  277. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  278. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  279. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  280. if tmpErr != nil {
  281. err = tmpErr
  282. if err.Error() == utils.ErrNoRow() {
  283. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  284. err = errors.New(errMsg)
  285. return
  286. }
  287. errMsg = "获取指标失败:Err:" + err.Error()
  288. err = errors.New(errMsg)
  289. return
  290. }
  291. edbInfoList = append(edbInfoList, fromEdbInfo)
  292. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  293. {
  294. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  295. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  296. tmpCalculateMappingItem := &models.EdbInfoCalculateMapping{
  297. EdbInfoCalculateMappingId: 0,
  298. EdbInfoId: 0,
  299. Source: utils.DATA_SOURCE_CALCULATE,
  300. SourceName: "指标运算",
  301. EdbCode: "",
  302. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  303. FromEdbCode: fromEdbInfo.EdbCode,
  304. FromEdbName: fromEdbInfo.EdbName,
  305. FromSource: fromEdbInfo.Source,
  306. FromSourceName: fromEdbInfo.SourceName,
  307. //FromTag: tmpEdbInfoId.FromTag,
  308. Sort: k + 1,
  309. CreateTime: time.Now(),
  310. ModifyTime: time.Now(),
  311. }
  312. calculateMappingList = append(calculateMappingList, tmpCalculateMappingItem)
  313. }
  314. }
  315. // 动态环差规则 关系表
  316. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  317. //PredictEdbConfCalculateMappingId: 0,
  318. EdbInfoId: 0,
  319. ConfigId: 0,
  320. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  321. FromEdbCode: fromEdbInfo.EdbCode,
  322. FromEdbName: fromEdbInfo.EdbName,
  323. FromSource: fromEdbInfo.Source,
  324. FromSourceName: fromEdbInfo.SourceName,
  325. FromTag: tmpEdbInfoId.FromTag,
  326. Sort: k + 1,
  327. CreateTime: time.Now(),
  328. ModifyTime: time.Now(),
  329. }
  330. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  331. }
  332. calculateRuleMap[ruleIndex] = models.CalculateRule{
  333. TrendsCalculateMappingList: trendsMappingList,
  334. EdbInfoList: edbInfoList,
  335. //EdbInfoIdBytes: edbInfoIdBytes,
  336. //Formula: formula,
  337. RuleType: v.RuleType,
  338. EndDate: v.EndDate,
  339. EdbInfoIdArr: v.EdbInfoIdArr,
  340. }
  341. }
  342. tmpPredictEdbConf := &models.PredictEdbConf{
  343. PredictEdbInfoId: 0,
  344. SourceEdbInfoId: sourceEdbInfoId,
  345. RuleType: v.RuleType,
  346. //FixedValue: v.Value,
  347. Value: v.Value,
  348. EndDate: ruleEndDate,
  349. ModifyTime: time.Now(),
  350. CreateTime: time.Now(),
  351. }
  352. edbInfo.EndDate = v.EndDate
  353. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  354. }
  355. err, errMsg = models.AddPredictEdb(edbInfo, calculateMappingList, predictEdbConfList, calculateRuleMap)
  356. return
  357. }
  358. // EditPredictEdbInfo 编辑预测指标
  359. func EditPredictEdbInfo(edbInfoId, classifyId int, edbName, dataDateType string, ruleList []models.RuleConfig, minValue, maxValue float64) (edbInfo *models.EdbInfo, err error, errMsg string) {
  360. // 指标信息校验
  361. {
  362. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  363. if err != nil && err.Error() != utils.ErrNoRow() {
  364. errMsg = "修改失败"
  365. err = errors.New("获取预测指标失败,Err:" + err.Error())
  366. return
  367. }
  368. if edbInfo == nil {
  369. errMsg = "找不到该预测指标"
  370. err = errors.New(errMsg)
  371. return
  372. }
  373. //必须是普通的指标
  374. if edbInfo.EdbInfoType != 1 {
  375. errMsg = "指标异常,不是预测指标"
  376. err = errors.New(errMsg)
  377. return
  378. }
  379. }
  380. var predictEdbConf *models.PredictEdbConf
  381. // 指标配置信息校验
  382. {
  383. // 查找该预测指标配置
  384. predictEdbConfList, tmpErr := models.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  385. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  386. errMsg = "修改失败"
  387. err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
  388. return
  389. }
  390. if len(predictEdbConfList) == 0 {
  391. errMsg = "找不到该预测指标配置"
  392. err = errors.New(errMsg)
  393. return
  394. }
  395. predictEdbConf = predictEdbConfList[0]
  396. }
  397. //判断指标名称是否存在
  398. var condition string
  399. var pars []interface{}
  400. condition += " AND edb_info_id<>? "
  401. pars = append(pars, edbInfoId)
  402. condition += " AND edb_info_type=? "
  403. pars = append(pars, 1)
  404. condition += " AND edb_name=? "
  405. pars = append(pars, edbName)
  406. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  407. if err != nil {
  408. errMsg = "判断指标名称是否存在失败"
  409. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  410. return
  411. }
  412. if count > 0 {
  413. errMsg = "指标名称已存在,请重新填写"
  414. err = errors.New(errMsg)
  415. return
  416. }
  417. if dataDateType == `` {
  418. dataDateType = `自然日`
  419. }
  420. edbInfo.EdbName = edbName
  421. edbInfo.EdbNameSource = edbName
  422. edbInfo.ClassifyId = classifyId
  423. edbInfo.MinValue = minValue
  424. edbInfo.MaxValue = maxValue
  425. edbInfo.DataDateType = dataDateType
  426. edbInfo.ModifyTime = time.Now()
  427. updateEdbInfoCol := []string{"EdbName", "EdbNameSource", "ClassifyId", "EndDate", "MinValue", "MaxValue", "DataDateType", "ModifyTime"}
  428. var sourceEdbInfo *models.EdbInfo
  429. // 来源指标信息校验
  430. {
  431. sourceEdbInfo, err = models.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  432. if err != nil && err.Error() != utils.ErrNoRow() {
  433. errMsg = "新增失败"
  434. err = errors.New("获取来源指标失败,Err:" + err.Error())
  435. return
  436. }
  437. if sourceEdbInfo == nil {
  438. errMsg = "找不到该来源指标"
  439. err = errors.New(errMsg)
  440. return
  441. }
  442. //必须是普通的指标
  443. if sourceEdbInfo.EdbInfoType != 0 {
  444. errMsg = "来源指标异常,不是普通的指标"
  445. err = errors.New(errMsg)
  446. return
  447. }
  448. //if !utils.InArrayByStr([]string{"日度", "周度", "月度", "年度"}, sourceEdbInfo.Frequency) {
  449. // errMsg = "预测指标只支持选择日度、周度、月度、年度的指标"
  450. // err = errors.New(errMsg)
  451. // return
  452. //}
  453. }
  454. // 预测指标配置
  455. // 关联关系表
  456. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  457. fromEdbMap := make(map[int]int)
  458. // 源指标关联关系表
  459. calculateMappingItem := &models.EdbInfoCalculateMapping{
  460. //EdbInfoCalculateMappingId: 0,
  461. EdbInfoId: edbInfoId,
  462. Source: edbInfo.Source,
  463. SourceName: edbInfo.SourceName,
  464. EdbCode: edbInfo.EdbCode,
  465. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  466. FromEdbCode: sourceEdbInfo.EdbCode,
  467. FromEdbName: sourceEdbInfo.EdbName,
  468. FromSource: sourceEdbInfo.Source,
  469. FromSourceName: sourceEdbInfo.SourceName,
  470. //FromTag: "",
  471. Sort: 1,
  472. CreateTime: time.Now(),
  473. ModifyTime: time.Now(),
  474. }
  475. fromEdbMap[sourceEdbInfo.EdbInfoId] = sourceEdbInfo.EdbInfoId
  476. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  477. // 动态环差 计算列表
  478. calculateRuleMap := make(map[int]models.CalculateRule, 0)
  479. // 预测指标配置
  480. predictEdbConfList := make([]*models.PredictEdbConf, 0)
  481. for ruleIndex, v := range ruleList {
  482. // 预测指标配置
  483. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  484. if tmpErr != nil {
  485. errMsg = "规则配置的截止日期异常,请重新填写"
  486. err = errors.New(errMsg)
  487. return
  488. }
  489. //1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差,10:根据 给定终值后插值 规则获取预测数据,11:根据 季节性 规则获取预测数据,12:根据 移动平均同比 规则获取预测数据
  490. // 环比、环差、动态环差、季节性、移动平均同比不支持年度
  491. if sourceEdbInfo.Frequency == "年度" && utils.InArrayByInt([]int{5, 6, 11, 12}, v.RuleType) {
  492. errMsg = "环比、环差、动态环差、季节性、移动平均同比不支持年度指标"
  493. err = errors.New(errMsg)
  494. return
  495. }
  496. switch v.RuleType {
  497. case 8: //N期段线性外推值
  498. valInt, tmpErr := strconv.Atoi(v.Value)
  499. if tmpErr != nil {
  500. errMsg = "N期段线性外推值的N值异常"
  501. err = errors.New(errMsg)
  502. return
  503. }
  504. if valInt <= 1 {
  505. errMsg = "N期段线性外推值的N值必须大于1"
  506. err = errors.New(errMsg)
  507. return
  508. }
  509. case 9: //9:动态环差
  510. if v.Value == "" {
  511. errMsg = "请填写计算规则"
  512. err = errors.New(errMsg)
  513. return
  514. }
  515. formula := v.Value
  516. formula = strings.Replace(formula, "(", "(", -1)
  517. formula = strings.Replace(formula, ")", ")", -1)
  518. formula = strings.Replace(formula, ",", ",", -1)
  519. formula = strings.Replace(formula, "。", ".", -1)
  520. formula = strings.Replace(formula, "%", "*0.01", -1)
  521. v.Value = formula
  522. //检验公式
  523. var formulaStr string
  524. var edbInfoIdBytes []string
  525. for _, tmpEdbInfoId := range v.EdbInfoIdArr {
  526. formulaStr += tmpEdbInfoId.FromTag + ","
  527. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  528. }
  529. formulaMap := utils.CheckFormula(formula)
  530. for _, formula := range formulaMap {
  531. if !strings.Contains(formulaStr, formula) {
  532. errMsg = "公式错误,请重新填写"
  533. err = errors.New(errMsg)
  534. return
  535. }
  536. }
  537. //关联的指标信息
  538. edbInfoList := make([]*models.EdbInfo, 0)
  539. // 动态环差规则 关系表
  540. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  541. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  542. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  543. if tmpErr != nil {
  544. err = tmpErr
  545. if err.Error() == utils.ErrNoRow() {
  546. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  547. err = errors.New(errMsg)
  548. return
  549. }
  550. errMsg = "获取指标失败:Err:" + err.Error()
  551. err = errors.New(errMsg)
  552. return
  553. }
  554. edbInfoList = append(edbInfoList, fromEdbInfo)
  555. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  556. {
  557. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  558. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  559. calculateMappingItem := &models.EdbInfoCalculateMapping{
  560. EdbInfoCalculateMappingId: 0,
  561. EdbInfoId: edbInfoId,
  562. Source: utils.DATA_SOURCE_CALCULATE,
  563. SourceName: "指标运算",
  564. EdbCode: "",
  565. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  566. FromEdbCode: fromEdbInfo.EdbCode,
  567. FromEdbName: fromEdbInfo.EdbName,
  568. FromSource: fromEdbInfo.Source,
  569. FromSourceName: fromEdbInfo.SourceName,
  570. //FromTag: tmpEdbInfoId.FromTag,
  571. Sort: k + 1,
  572. CreateTime: time.Now(),
  573. ModifyTime: time.Now(),
  574. }
  575. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  576. }
  577. }
  578. // 动态环差规则 关系表
  579. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  580. //PredictEdbConfCalculateMappingId: 0,
  581. EdbInfoId: edbInfoId,
  582. ConfigId: 0,
  583. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  584. FromEdbCode: fromEdbInfo.EdbCode,
  585. FromEdbName: fromEdbInfo.EdbName,
  586. FromSource: fromEdbInfo.Source,
  587. FromSourceName: fromEdbInfo.SourceName,
  588. FromTag: tmpEdbInfoId.FromTag,
  589. Sort: k + 1,
  590. CreateTime: time.Now(),
  591. ModifyTime: time.Now(),
  592. }
  593. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  594. }
  595. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  596. if !ok {
  597. errMsg = "生成计算指标失败,请使用正确的计算公式"
  598. err = errors.New(errMsg)
  599. return
  600. }
  601. calculateRuleMap[ruleIndex] = models.CalculateRule{
  602. TrendsCalculateMappingList: trendsMappingList,
  603. EdbInfoList: edbInfoList,
  604. EdbInfoIdBytes: edbInfoIdBytes,
  605. Formula: formula,
  606. RuleType: v.RuleType,
  607. EndDate: v.EndDate,
  608. EdbInfoIdArr: v.EdbInfoIdArr,
  609. }
  610. case 14: //14:根据 一元线性拟合 规则获取预测数据
  611. if v.Value == "" {
  612. errMsg = "请填写一元线性拟合规则"
  613. err = errors.New(errMsg)
  614. return
  615. }
  616. //关联的指标信息
  617. edbInfoList := make([]*models.EdbInfo, 0)
  618. // 动态环差规则 关系表
  619. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  620. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  621. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  622. if tmpErr != nil {
  623. err = tmpErr
  624. if err.Error() == utils.ErrNoRow() {
  625. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  626. err = errors.New(errMsg)
  627. return
  628. }
  629. errMsg = "获取指标失败:Err:" + err.Error()
  630. err = errors.New(errMsg)
  631. return
  632. }
  633. edbInfoList = append(edbInfoList, fromEdbInfo)
  634. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  635. {
  636. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  637. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  638. tmpCalculateMappingItem := &models.EdbInfoCalculateMapping{
  639. EdbInfoCalculateMappingId: 0,
  640. EdbInfoId: 0,
  641. Source: utils.DATA_SOURCE_CALCULATE,
  642. SourceName: "指标运算",
  643. EdbCode: "",
  644. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  645. FromEdbCode: fromEdbInfo.EdbCode,
  646. FromEdbName: fromEdbInfo.EdbName,
  647. FromSource: fromEdbInfo.Source,
  648. FromSourceName: fromEdbInfo.SourceName,
  649. //FromTag: tmpEdbInfoId.FromTag,
  650. Sort: k + 1,
  651. CreateTime: time.Now(),
  652. ModifyTime: time.Now(),
  653. }
  654. calculateMappingList = append(calculateMappingList, tmpCalculateMappingItem)
  655. }
  656. }
  657. // 动态环差规则 关系表
  658. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  659. //PredictEdbConfCalculateMappingId: 0,
  660. EdbInfoId: 0,
  661. ConfigId: 0,
  662. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  663. FromEdbCode: fromEdbInfo.EdbCode,
  664. FromEdbName: fromEdbInfo.EdbName,
  665. FromSource: fromEdbInfo.Source,
  666. FromSourceName: fromEdbInfo.SourceName,
  667. FromTag: tmpEdbInfoId.FromTag,
  668. Sort: k + 1,
  669. CreateTime: time.Now(),
  670. ModifyTime: time.Now(),
  671. }
  672. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  673. }
  674. calculateRuleMap[ruleIndex] = models.CalculateRule{
  675. TrendsCalculateMappingList: trendsMappingList,
  676. EdbInfoList: edbInfoList,
  677. //EdbInfoIdBytes: edbInfoIdBytes,
  678. //Formula: formula,
  679. RuleType: v.RuleType,
  680. EndDate: v.EndDate,
  681. EdbInfoIdArr: v.EdbInfoIdArr,
  682. }
  683. }
  684. tmpPredictEdbConf := &models.PredictEdbConf{
  685. PredictEdbInfoId: edbInfoId,
  686. SourceEdbInfoId: sourceEdbInfo.EdbInfoId,
  687. RuleType: v.RuleType,
  688. //FixedValue: v.Value,
  689. Value: v.Value,
  690. EndDate: ruleEndDate,
  691. ModifyTime: time.Now(),
  692. CreateTime: time.Now(),
  693. }
  694. edbInfo.EndDate = v.EndDate
  695. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  696. }
  697. err, errMsg = models.EditPredictEdb(edbInfo, updateEdbInfoCol, calculateMappingList, predictEdbConfList, calculateRuleMap)
  698. return
  699. }
  700. // RefreshPredictEdbInfo 更新基础预测指标规则中的动态数据
  701. func RefreshPredictEdbInfo(edbInfoId int) (edbInfo *models.EdbInfo, err error, errMsg string) {
  702. // 指标信息校验
  703. {
  704. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  705. if err != nil && err.Error() != utils.ErrNoRow() {
  706. errMsg = "刷新失败"
  707. err = errors.New("获取预测指标失败,Err:" + err.Error())
  708. return
  709. }
  710. if edbInfo == nil {
  711. errMsg = "找不到该预测指标"
  712. err = nil
  713. return
  714. }
  715. //必须是普通的指标
  716. if edbInfo.EdbInfoType != 1 {
  717. errMsg = "指标异常,不是预测指标"
  718. return
  719. }
  720. }
  721. // 配置 与 指标的 关联关系表
  722. list, err := models.GetPredictEdbConfCalculateMappingListByEdbInfoId(edbInfoId)
  723. if err != nil {
  724. return
  725. }
  726. // 没有关联指标,不需要刷新
  727. if len(list) <= 0 {
  728. return
  729. }
  730. // 配置关联的指标信息
  731. predictEdbConfCalculateMappingListMap := make(map[int][]*models.PredictEdbConfCalculateMapping)
  732. configIdList := make([]int, 0) //关联配置id
  733. edbInfoIdList := make([]int, 0) //关联指标配置id
  734. edbInfoIdMap := make(map[int]int, 0) //关联指标配置map
  735. for _, v := range list {
  736. configList, ok := predictEdbConfCalculateMappingListMap[v.ConfigId]
  737. if !ok {
  738. configList = make([]*models.PredictEdbConfCalculateMapping, 0)
  739. configIdList = append(configIdList, v.ConfigId)
  740. }
  741. if _, ok := edbInfoIdMap[v.FromEdbInfoId]; !ok {
  742. edbInfoIdList = append(edbInfoIdList, v.FromEdbInfoId)
  743. }
  744. configList = append(configList, v)
  745. predictEdbConfCalculateMappingListMap[v.ConfigId] = configList
  746. }
  747. predictEdbConfList, err := models.GetPredictEdbConfListByConfigIdList(configIdList)
  748. if err != nil {
  749. errMsg = "刷新失败"
  750. err = errors.New("获取预测指标配置信息失败,Err:" + err.Error())
  751. return
  752. }
  753. if len(predictEdbConfList) == 0 {
  754. errMsg = "找不到该预测指标配置"
  755. err = nil
  756. return
  757. }
  758. // 指标信息
  759. edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIdList)
  760. if err != nil {
  761. err = errors.New("获取关联指标失败,Err:" + err.Error())
  762. return
  763. }
  764. // 指标信息map
  765. edbInfoListMap := make(map[int]*models.EdbInfo)
  766. for _, v := range edbInfoList {
  767. edbInfoListMap[v.EdbInfoId] = v
  768. }
  769. predictEdbConfAndDataList := make([]*models.PredictEdbConfAndData, 0)
  770. // 刷新所有的规则
  771. for _, v := range predictEdbConfList {
  772. // 每次规则计算的时候,产生的临时数据
  773. resultDataList := make([]*models.EdbInfoSearchData, 0)
  774. switch v.RuleType {
  775. case 9: //动态环差值
  776. if v.Value == "" {
  777. errMsg = "请填写计算规则"
  778. return
  779. }
  780. formula := v.Value
  781. // 动态环差规则 关系表
  782. trendsMappingList := predictEdbConfCalculateMappingListMap[v.ConfigId]
  783. // 关联标签
  784. edbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  785. //关联的指标信息
  786. edbInfoList := make([]*models.EdbInfo, 0)
  787. for _, trendsMapping := range trendsMappingList {
  788. tmpEdbInfo, ok := edbInfoListMap[trendsMapping.FromEdbInfoId]
  789. if ok {
  790. edbInfoList = append(edbInfoList, tmpEdbInfo)
  791. }
  792. // 关联标签
  793. edbInfoIdArr = append(edbInfoIdArr, models.EdbInfoFromTag{
  794. EdbInfoId: trendsMapping.FromEdbInfoId,
  795. FromTag: trendsMapping.FromTag,
  796. })
  797. }
  798. //检验公式
  799. var formulaStr string
  800. var edbInfoIdBytes []string
  801. for _, tmpEdbInfoId := range edbInfoIdArr {
  802. formulaStr += tmpEdbInfoId.FromTag + ","
  803. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  804. }
  805. formulaMap := utils.CheckFormula(formula)
  806. for _, formula := range formulaMap {
  807. if !strings.Contains(formulaStr, formula) {
  808. errMsg = "公式错误,请重新填写"
  809. return
  810. }
  811. }
  812. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  813. if !ok {
  814. errMsg = "生成计算指标失败,请使用正确的计算公式"
  815. return
  816. }
  817. rule := models.CalculateRule{
  818. EdbInfoId: v.PredictEdbInfoId,
  819. ConfigId: v.ConfigId,
  820. TrendsCalculateMappingList: trendsMappingList,
  821. EdbInfoList: edbInfoList,
  822. EdbInfoIdBytes: edbInfoIdBytes,
  823. Formula: formula,
  824. RuleType: v.RuleType,
  825. EndDate: v.EndDate.Format(utils.FormatDate),
  826. EdbInfoIdArr: edbInfoIdArr,
  827. }
  828. resultDataList, err = models.RefreshCalculateByRuleBy9(rule)
  829. if err != nil {
  830. return
  831. }
  832. case 14: //14:根据 一元线性拟合 规则获取预测数据
  833. if v.Value == "" {
  834. errMsg = "一元线性拟合规则信息未配置"
  835. return
  836. }
  837. err, errMsg = models.RefreshCalculateByRuleByLineNh(*edbInfo, predictEdbConfAndDataList, *v)
  838. if err != nil {
  839. return
  840. }
  841. }
  842. // 规则配置(含数据)
  843. tmpPredictEdbConfAndData := &models.PredictEdbConfAndData{
  844. ConfigId: 0,
  845. PredictEdbInfoId: 0,
  846. SourceEdbInfoId: v.SourceEdbInfoId,
  847. RuleType: v.RuleType,
  848. FixedValue: v.FixedValue,
  849. Value: v.Value,
  850. EndDate: v.EndDate,
  851. ModifyTime: v.ModifyTime,
  852. CreateTime: v.CreateTime,
  853. DataList: resultDataList,
  854. }
  855. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  856. }
  857. return
  858. }