predict_edb.go 32 KB

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