predict_edb.go 34 KB

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