predict_edb.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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, 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, 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: edbInfo.Source,
  231. SourceName: edbInfo.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: edbInfo.Source,
  318. SourceName: edbInfo.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. {
  380. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  381. if err != nil && err.Error() != utils.ErrNoRow() {
  382. errMsg = "修改失败"
  383. err = errors.New("获取预测指标失败,Err:" + err.Error())
  384. return
  385. }
  386. if edbInfo == nil {
  387. errMsg = "找不到该预测指标"
  388. err = errors.New(errMsg)
  389. return
  390. }
  391. //必须是普通的指标
  392. if edbInfo.EdbInfoType != 1 {
  393. errMsg = "指标异常,不是预测指标"
  394. err = errors.New(errMsg)
  395. return
  396. }
  397. }
  398. var predictEdbConf *models.PredictEdbConf
  399. // 指标配置信息校验
  400. {
  401. // 查找该预测指标配置
  402. predictEdbConfList, tmpErr := models.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  403. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  404. errMsg = "修改失败"
  405. err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
  406. return
  407. }
  408. if len(predictEdbConfList) == 0 {
  409. errMsg = "找不到该预测指标配置"
  410. err = errors.New(errMsg)
  411. return
  412. }
  413. predictEdbConf = predictEdbConfList[0]
  414. }
  415. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  416. existEdbName, err := CheckExistByEdbNameAndEdbInfoId(utils.PREDICT_EDB_INFO_TYPE, edbInfoId, edbName, lang)
  417. if err != nil {
  418. errMsg = "判断指标名称是否存在失败"
  419. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  420. return
  421. }
  422. if existEdbName {
  423. errMsg = "指标名称已存在,请重新填写"
  424. err = errors.New(errMsg)
  425. return
  426. }
  427. if dataDateType == `` {
  428. dataDateType = `自然日`
  429. }
  430. switch lang {
  431. case utils.EnLangVersion:
  432. edbInfo.EdbNameEn = edbName
  433. default:
  434. edbInfo.EdbName = edbName
  435. }
  436. edbInfo.EdbNameSource = edbName
  437. edbInfo.ClassifyId = classifyId
  438. edbInfo.MinValue = minValue
  439. edbInfo.MaxValue = maxValue
  440. edbInfo.DataDateType = dataDateType
  441. edbInfo.ModifyTime = time.Now()
  442. updateEdbInfoCol := []string{"EdbName", "EdbNameEn", "EdbNameSource", "ClassifyId", "EndDate", "MinValue", "MaxValue", "DataDateType", "ModifyTime"}
  443. var sourceEdbInfo *models.EdbInfo
  444. // 来源指标信息校验
  445. {
  446. sourceEdbInfo, err = models.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  447. if err != nil && err.Error() != utils.ErrNoRow() {
  448. errMsg = "新增失败"
  449. err = errors.New("获取来源指标失败,Err:" + err.Error())
  450. return
  451. }
  452. if sourceEdbInfo == nil {
  453. errMsg = "找不到该来源指标"
  454. err = errors.New(errMsg)
  455. return
  456. }
  457. //必须是普通的指标
  458. if sourceEdbInfo.EdbInfoType != 0 {
  459. errMsg = "来源指标异常,不是普通的指标"
  460. err = errors.New(errMsg)
  461. return
  462. }
  463. //if !utils.InArrayByStr([]string{"日度", "周度", "月度", "年度"}, sourceEdbInfo.Frequency) {
  464. // errMsg = "预测指标只支持选择日度、周度、月度、年度的指标"
  465. // err = errors.New(errMsg)
  466. // return
  467. //}
  468. }
  469. // 预测指标配置
  470. // 关联关系表
  471. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  472. fromEdbMap := make(map[int]int)
  473. // 源指标关联关系表
  474. calculateMappingItem := &models.EdbInfoCalculateMapping{
  475. //EdbInfoCalculateMappingId: 0,
  476. EdbInfoId: edbInfoId,
  477. Source: edbInfo.Source,
  478. SourceName: edbInfo.SourceName,
  479. EdbCode: edbInfo.EdbCode,
  480. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  481. FromEdbCode: sourceEdbInfo.EdbCode,
  482. FromEdbName: sourceEdbInfo.EdbName,
  483. FromSource: sourceEdbInfo.Source,
  484. FromSourceName: sourceEdbInfo.SourceName,
  485. //FromTag: "",
  486. Sort: 1,
  487. CreateTime: time.Now(),
  488. ModifyTime: time.Now(),
  489. }
  490. fromEdbMap[sourceEdbInfo.EdbInfoId] = sourceEdbInfo.EdbInfoId
  491. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  492. // 动态环差 计算列表
  493. calculateRuleMap := make(map[int]models.CalculateRule, 0)
  494. // 预测指标配置
  495. predictEdbConfList := make([]*models.PredictEdbConf, 0)
  496. for ruleIndex, v := range ruleList {
  497. // 预测指标配置
  498. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  499. if tmpErr != nil {
  500. errMsg = "规则配置的截止日期异常,请重新填写"
  501. err = errors.New(errMsg)
  502. return
  503. }
  504. //1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差,10:根据 给定终值后插值 规则获取预测数据,11:根据 季节性 规则获取预测数据,12:根据 移动平均同比 规则获取预测数据
  505. // 环比、环差、动态环差、季节性、移动平均同比不支持年度
  506. if sourceEdbInfo.Frequency == "年度" && utils.InArrayByInt([]int{5, 6, 11, 12}, v.RuleType) {
  507. errMsg = "环比、环差、动态环差、季节性、移动平均同比不支持年度指标"
  508. err = errors.New(errMsg)
  509. return
  510. }
  511. switch v.RuleType {
  512. case 8: //N期段线性外推值
  513. valInt, tmpErr := strconv.Atoi(v.Value)
  514. if tmpErr != nil {
  515. errMsg = "N期段线性外推值的N值异常"
  516. err = errors.New(errMsg)
  517. return
  518. }
  519. if valInt <= 1 {
  520. errMsg = "N期段线性外推值的N值必须大于1"
  521. err = errors.New(errMsg)
  522. return
  523. }
  524. case 9: //9:动态环差
  525. if v.Value == "" {
  526. errMsg = "请填写计算规则"
  527. err = errors.New(errMsg)
  528. return
  529. }
  530. formula := v.Value
  531. formula = strings.Replace(formula, "(", "(", -1)
  532. formula = strings.Replace(formula, ")", ")", -1)
  533. formula = strings.Replace(formula, ",", ",", -1)
  534. formula = strings.Replace(formula, "。", ".", -1)
  535. formula = strings.Replace(formula, "%", "*0.01", -1)
  536. v.Value = formula
  537. //检验公式
  538. var formulaStr string
  539. var edbInfoIdBytes []string
  540. for _, tmpEdbInfoId := range v.EdbInfoIdArr {
  541. formulaStr += tmpEdbInfoId.FromTag + ","
  542. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  543. }
  544. formulaSlice, tErr := utils.CheckFormulaJson(formula)
  545. if tErr != nil {
  546. errMsg = "公式格式错误,请重新填写"
  547. err = errors.New(errMsg)
  548. return
  549. }
  550. for _, fm := range formulaSlice {
  551. formulaMap, e := utils.CheckFormula(fm)
  552. if e != nil {
  553. err = fmt.Errorf("公式错误,请重新填写")
  554. return
  555. }
  556. for _, f := range formulaMap {
  557. if !strings.Contains(formulaStr, f) {
  558. errMsg = "公式错误,请重新填写"
  559. err = errors.New(errMsg)
  560. return
  561. }
  562. }
  563. }
  564. //关联的指标信息
  565. edbInfoList := make([]*models.EdbInfo, 0)
  566. // 动态环差规则 关系表
  567. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  568. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  569. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  570. if tmpErr != nil {
  571. err = tmpErr
  572. if err.Error() == utils.ErrNoRow() {
  573. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  574. err = errors.New(errMsg)
  575. return
  576. }
  577. errMsg = "获取指标失败:Err:" + err.Error()
  578. err = errors.New(errMsg)
  579. return
  580. }
  581. edbInfoList = append(edbInfoList, fromEdbInfo)
  582. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  583. {
  584. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  585. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  586. calculateMappingItem := &models.EdbInfoCalculateMapping{
  587. EdbInfoCalculateMappingId: 0,
  588. EdbInfoId: edbInfoId,
  589. Source: utils.DATA_SOURCE_CALCULATE,
  590. SourceName: "指标运算",
  591. EdbCode: "",
  592. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  593. FromEdbCode: fromEdbInfo.EdbCode,
  594. FromEdbName: fromEdbInfo.EdbName,
  595. FromSource: fromEdbInfo.Source,
  596. FromSourceName: fromEdbInfo.SourceName,
  597. //FromTag: tmpEdbInfoId.FromTag,
  598. Sort: k + 1,
  599. CreateTime: time.Now(),
  600. ModifyTime: time.Now(),
  601. }
  602. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  603. }
  604. }
  605. // 动态环差规则 关系表
  606. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  607. //PredictEdbConfCalculateMappingId: 0,
  608. EdbInfoId: edbInfoId,
  609. ConfigId: 0,
  610. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  611. FromEdbCode: fromEdbInfo.EdbCode,
  612. FromEdbName: fromEdbInfo.EdbName,
  613. FromSource: fromEdbInfo.Source,
  614. FromSourceName: fromEdbInfo.SourceName,
  615. FromTag: tmpEdbInfoId.FromTag,
  616. Sort: k + 1,
  617. CreateTime: time.Now(),
  618. ModifyTime: time.Now(),
  619. }
  620. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  621. }
  622. for _, f := range formulaSlice {
  623. formulaMap, e := utils.CheckFormula(f)
  624. if e != nil {
  625. err = fmt.Errorf("公式错误,请重新填写")
  626. return
  627. }
  628. //预先计算,判断公式是否正常
  629. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, f, edbInfoIdBytes)
  630. if !ok {
  631. errMsg = "生成计算指标失败,请使用正确的计算公式"
  632. err = errors.New(errMsg)
  633. return
  634. }
  635. }
  636. calculateRuleMap[ruleIndex] = models.CalculateRule{
  637. TrendsCalculateMappingList: trendsMappingList,
  638. EdbInfoList: edbInfoList,
  639. EdbInfoIdBytes: edbInfoIdBytes,
  640. Formula: formula,
  641. RuleType: v.RuleType,
  642. EndDate: v.EndDate,
  643. EdbInfoIdArr: v.EdbInfoIdArr,
  644. }
  645. case 14: //14:根据 一元线性拟合 规则获取预测数据
  646. if v.Value == "" {
  647. errMsg = "请填写一元线性拟合规则"
  648. err = errors.New(errMsg)
  649. return
  650. }
  651. //关联的指标信息
  652. edbInfoList := make([]*models.EdbInfo, 0)
  653. // 动态环差规则 关系表
  654. trendsMappingList := make([]*models.PredictEdbConfCalculateMapping, 0)
  655. for k, tmpEdbInfoId := range v.EdbInfoIdArr {
  656. fromEdbInfo, tmpErr := models.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  657. if tmpErr != nil {
  658. err = tmpErr
  659. if err.Error() == utils.ErrNoRow() {
  660. errMsg = "指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在"
  661. err = errors.New(errMsg)
  662. return
  663. }
  664. errMsg = "获取指标失败:Err:" + err.Error()
  665. err = errors.New(errMsg)
  666. return
  667. }
  668. edbInfoList = append(edbInfoList, fromEdbInfo)
  669. //总的 预测指标与所有相关联指标的关系表(不仅仅该条规则)
  670. {
  671. if _, ok := fromEdbMap[tmpEdbInfoId.EdbInfoId]; !ok {
  672. fromEdbMap[tmpEdbInfoId.EdbInfoId] = tmpEdbInfoId.EdbInfoId
  673. tmpCalculateMappingItem := &models.EdbInfoCalculateMapping{
  674. EdbInfoCalculateMappingId: 0,
  675. EdbInfoId: 0,
  676. Source: utils.DATA_SOURCE_CALCULATE,
  677. SourceName: "指标运算",
  678. EdbCode: "",
  679. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  680. FromEdbCode: fromEdbInfo.EdbCode,
  681. FromEdbName: fromEdbInfo.EdbName,
  682. FromSource: fromEdbInfo.Source,
  683. FromSourceName: fromEdbInfo.SourceName,
  684. //FromTag: tmpEdbInfoId.FromTag,
  685. Sort: k + 1,
  686. CreateTime: time.Now(),
  687. ModifyTime: time.Now(),
  688. }
  689. calculateMappingList = append(calculateMappingList, tmpCalculateMappingItem)
  690. }
  691. }
  692. // 动态环差规则 关系表
  693. tmpPredictEdbConfCalculateMapping := &models.PredictEdbConfCalculateMapping{
  694. //PredictEdbConfCalculateMappingId: 0,
  695. EdbInfoId: 0,
  696. ConfigId: 0,
  697. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  698. FromEdbCode: fromEdbInfo.EdbCode,
  699. FromEdbName: fromEdbInfo.EdbName,
  700. FromSource: fromEdbInfo.Source,
  701. FromSourceName: fromEdbInfo.SourceName,
  702. FromTag: tmpEdbInfoId.FromTag,
  703. Sort: k + 1,
  704. CreateTime: time.Now(),
  705. ModifyTime: time.Now(),
  706. }
  707. trendsMappingList = append(trendsMappingList, tmpPredictEdbConfCalculateMapping)
  708. }
  709. calculateRuleMap[ruleIndex] = models.CalculateRule{
  710. TrendsCalculateMappingList: trendsMappingList,
  711. EdbInfoList: edbInfoList,
  712. //EdbInfoIdBytes: edbInfoIdBytes,
  713. //Formula: formula,
  714. RuleType: v.RuleType,
  715. EndDate: v.EndDate,
  716. EdbInfoIdArr: v.EdbInfoIdArr,
  717. }
  718. }
  719. tmpPredictEdbConf := &models.PredictEdbConf{
  720. PredictEdbInfoId: edbInfoId,
  721. SourceEdbInfoId: sourceEdbInfo.EdbInfoId,
  722. RuleType: v.RuleType,
  723. //FixedValue: v.Value,
  724. Value: v.Value,
  725. EndDate: ruleEndDate,
  726. ModifyTime: time.Now(),
  727. CreateTime: time.Now(),
  728. }
  729. edbInfo.EndDate = v.EndDate
  730. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  731. }
  732. err, errMsg = models.EditPredictEdb(edbInfo, updateEdbInfoCol, calculateMappingList, predictEdbConfList, calculateRuleMap)
  733. return
  734. }
  735. // RefreshPredictEdbInfo 更新基础预测指标规则中的动态数据
  736. func RefreshPredictEdbInfo(edbInfoId int) (edbInfo *models.EdbInfo, err error, errMsg string) {
  737. // 指标信息校验
  738. {
  739. edbInfo, err = models.GetEdbInfoById(edbInfoId)
  740. if err != nil && err.Error() != utils.ErrNoRow() {
  741. errMsg = "刷新失败"
  742. err = errors.New("获取预测指标失败,Err:" + err.Error())
  743. return
  744. }
  745. if edbInfo == nil {
  746. errMsg = "找不到该预测指标"
  747. err = nil
  748. return
  749. }
  750. //必须是普通的指标
  751. if edbInfo.EdbInfoType != 1 {
  752. errMsg = "指标异常,不是预测指标"
  753. return
  754. }
  755. }
  756. // 配置 与 指标的 关联关系表
  757. list, err := models.GetPredictEdbConfCalculateMappingListByEdbInfoId(edbInfoId)
  758. if err != nil {
  759. return
  760. }
  761. // 没有关联指标,不需要刷新
  762. if len(list) <= 0 {
  763. return
  764. }
  765. // 配置关联的指标信息
  766. predictEdbConfCalculateMappingListMap := make(map[int][]*models.PredictEdbConfCalculateMapping)
  767. configIdList := make([]int, 0) //关联配置id
  768. edbInfoIdList := make([]int, 0) //关联指标配置id
  769. edbInfoIdMap := make(map[int]int, 0) //关联指标配置map
  770. for _, v := range list {
  771. configList, ok := predictEdbConfCalculateMappingListMap[v.ConfigId]
  772. if !ok {
  773. configList = make([]*models.PredictEdbConfCalculateMapping, 0)
  774. configIdList = append(configIdList, v.ConfigId)
  775. }
  776. if _, ok := edbInfoIdMap[v.FromEdbInfoId]; !ok {
  777. edbInfoIdList = append(edbInfoIdList, v.FromEdbInfoId)
  778. }
  779. configList = append(configList, v)
  780. predictEdbConfCalculateMappingListMap[v.ConfigId] = configList
  781. }
  782. predictEdbConfList, err := models.GetPredictEdbConfListByConfigIdList(configIdList)
  783. if err != nil {
  784. errMsg = "刷新失败"
  785. err = errors.New("获取预测指标配置信息失败,Err:" + err.Error())
  786. return
  787. }
  788. if len(predictEdbConfList) == 0 {
  789. errMsg = "找不到该预测指标配置"
  790. err = nil
  791. return
  792. }
  793. // 指标信息
  794. edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIdList)
  795. if err != nil {
  796. err = errors.New("获取关联指标失败,Err:" + err.Error())
  797. return
  798. }
  799. // 指标信息map
  800. edbInfoListMap := make(map[int]*models.EdbInfo)
  801. for _, v := range edbInfoList {
  802. edbInfoListMap[v.EdbInfoId] = v
  803. }
  804. predictEdbConfAndDataList := make([]*models.PredictEdbConfAndData, 0)
  805. // 刷新所有的规则
  806. for _, v := range predictEdbConfList {
  807. // 每次规则计算的时候,产生的临时数据
  808. resultDataList := make([]*models.EdbInfoSearchData, 0)
  809. switch v.RuleType {
  810. case 9: //动态环差值
  811. if v.Value == "" {
  812. errMsg = "请填写计算规则"
  813. return
  814. }
  815. // todo 动态环差的空值类型处理
  816. formula := v.Value
  817. // 动态环差规则 关系表
  818. trendsMappingList := predictEdbConfCalculateMappingListMap[v.ConfigId]
  819. // 关联标签
  820. edbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  821. //关联的指标信息
  822. edbInfoList := make([]*models.EdbInfo, 0)
  823. for _, trendsMapping := range trendsMappingList {
  824. tmpEdbInfo, ok := edbInfoListMap[trendsMapping.FromEdbInfoId]
  825. if ok {
  826. edbInfoList = append(edbInfoList, tmpEdbInfo)
  827. }
  828. // 关联标签
  829. edbInfoIdArr = append(edbInfoIdArr, models.EdbInfoFromTag{
  830. EdbInfoId: trendsMapping.FromEdbInfoId,
  831. FromTag: trendsMapping.FromTag,
  832. })
  833. }
  834. //检验公式
  835. var formulaStr string
  836. var edbInfoIdBytes []string
  837. for _, tmpEdbInfoId := range edbInfoIdArr {
  838. formulaStr += tmpEdbInfoId.FromTag + ","
  839. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  840. }
  841. formulaSlice, tErr := utils.CheckFormulaJson(formula)
  842. if tErr != nil {
  843. errMsg = "公式格式错误,请重新填写"
  844. err = errors.New(errMsg)
  845. return
  846. }
  847. for _, fm := range formulaSlice {
  848. formulaMap, e := utils.CheckFormula(fm)
  849. if e != nil {
  850. err = fmt.Errorf("公式错误,请重新填写")
  851. return
  852. }
  853. for _, f := range formulaMap {
  854. if !strings.Contains(formulaStr, f) {
  855. errMsg = "公式错误,请重新填写"
  856. err = errors.New(errMsg)
  857. return
  858. }
  859. }
  860. //预先计算,判断公式是否正常
  861. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, fm, edbInfoIdBytes)
  862. if !ok {
  863. errMsg = "生成计算指标失败,请使用正确的计算公式"
  864. return
  865. }
  866. }
  867. rule := models.CalculateRule{
  868. EdbInfoId: v.PredictEdbInfoId,
  869. ConfigId: v.ConfigId,
  870. TrendsCalculateMappingList: trendsMappingList,
  871. EdbInfoList: edbInfoList,
  872. EdbInfoIdBytes: edbInfoIdBytes,
  873. Formula: formula,
  874. RuleType: v.RuleType,
  875. EndDate: v.EndDate.Format(utils.FormatDate),
  876. EdbInfoIdArr: edbInfoIdArr,
  877. }
  878. resultDataList, err = models.RefreshCalculateByRuleBy9(rule)
  879. if err != nil {
  880. return
  881. }
  882. case 14: //14:根据 一元线性拟合 规则获取预测数据
  883. if v.Value == "" {
  884. errMsg = "一元线性拟合规则信息未配置"
  885. return
  886. }
  887. err, errMsg = models.RefreshCalculateByRuleByLineNh(*edbInfo, predictEdbConfAndDataList, *v)
  888. if err != nil {
  889. return
  890. }
  891. }
  892. // 规则配置(含数据)
  893. tmpPredictEdbConfAndData := &models.PredictEdbConfAndData{
  894. ConfigId: 0,
  895. PredictEdbInfoId: 0,
  896. SourceEdbInfoId: v.SourceEdbInfoId,
  897. RuleType: v.RuleType,
  898. FixedValue: v.FixedValue,
  899. Value: v.Value,
  900. EndDate: v.EndDate,
  901. ModifyTime: v.ModifyTime,
  902. CreateTime: v.CreateTime,
  903. DataList: resultDataList,
  904. }
  905. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  906. }
  907. return
  908. }
  909. // checkExistByEdbName
  910. // @Description: 根据指标名称校验该指标是否存在库中
  911. // @author: Roc
  912. // @datetime 2024-04-18 14:58:52
  913. // @param edbInfoType int
  914. // @param edbName string
  915. // @param lang string
  916. // @return has bool
  917. // @return err error
  918. func checkExistByEdbName(edbInfoType int, edbName, lang string) (has bool, err error) {
  919. var condition string
  920. var pars []interface{}
  921. condition += " AND edb_info_type=? "
  922. pars = append(pars, edbInfoType)
  923. switch lang {
  924. case utils.EnLangVersion:
  925. condition += " AND edb_name_en = ? "
  926. default:
  927. condition += " AND edb_name=? "
  928. }
  929. pars = append(pars, edbName)
  930. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  931. if err != nil {
  932. return
  933. }
  934. if count > 0 {
  935. has = true
  936. return
  937. }
  938. return
  939. }
  940. // checkExistByEdbNameAndEdbInfoId
  941. // @Description: 根据指标名称和指标ID校验库中是否还存在其他同名指标
  942. // @author: Roc
  943. // @datetime 2024-04-18 15:00:19
  944. // @param edbInfoType int
  945. // @param edbInfoId int
  946. // @param edbName string
  947. // @param lang string
  948. // @return has bool
  949. // @return err error
  950. func checkExistByEdbNameAndEdbInfoId(edbInfoType, edbInfoId int, edbName, lang string) (has bool, err error) {
  951. var condition string
  952. var pars []interface{}
  953. condition += " AND edb_info_type=? "
  954. pars = append(pars, edbInfoType)
  955. condition += " AND edb_info_id<>? "
  956. pars = append(pars, edbInfoId)
  957. switch lang {
  958. case utils.EnLangVersion:
  959. condition += " AND edb_name_en = ? "
  960. default:
  961. condition += " AND edb_name=? "
  962. }
  963. pars = append(pars, edbName)
  964. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  965. if err != nil {
  966. return
  967. }
  968. if count > 0 {
  969. has = true
  970. return
  971. }
  972. return
  973. }
  974. // CheckExistByEdbNameAndEdbInfoId
  975. // @Description: 根据指标名称和指标ID校验库中是否还存在其他同名指标
  976. // @author: Roc
  977. // @datetime 2024-04-18 15:01:44
  978. // @param edbInfoType int
  979. // @param edbInfoId int
  980. // @param edbName string
  981. // @param lang string
  982. // @return has bool
  983. // @return err error
  984. func CheckExistByEdbNameAndEdbInfoId(edbInfoType, edbInfoId int, edbName, lang string) (has bool, err error) {
  985. // 指标没有入库的情况
  986. if edbInfoId == 0 {
  987. return checkExistByEdbName(edbInfoType, edbName, lang)
  988. }
  989. //指标已经入库的情况
  990. return checkExistByEdbNameAndEdbInfoId(edbInfoType, edbInfoId, edbName, lang)
  991. }
  992. // AddStaticPredictEdbInfo 新增静态指标数据
  993. func AddStaticPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName, frequency, unit string, sysUserId int, sysUserName, lang string) (edbInfo *models.EdbInfo, err error, errMsg string) {
  994. var sourceEdbInfo *models.EdbInfo
  995. // 来源指标信息校验
  996. {
  997. sourceEdbInfo, err = models.GetEdbInfoById(sourceEdbInfoId)
  998. if err != nil && err.Error() != utils.ErrNoRow() {
  999. errMsg = "新增失败"
  1000. err = errors.New("获取来源指标失败,Err:" + err.Error())
  1001. return
  1002. }
  1003. if sourceEdbInfo == nil {
  1004. errMsg = "找不到该来源指标"
  1005. err = errors.New(errMsg)
  1006. return
  1007. }
  1008. }
  1009. var classifyInfo *models.EdbClassify
  1010. // 来源分类信息校验
  1011. {
  1012. classifyInfo, err = models.GetEdbClassifyById(classifyId)
  1013. if err != nil && err.Error() != utils.ErrNoRow() {
  1014. errMsg = "新增失败"
  1015. err = errors.New("获取预测指标分类失败,Err:" + err.Error())
  1016. return
  1017. }
  1018. if classifyInfo == nil {
  1019. errMsg = "找不到该预测指标分类"
  1020. err = errors.New(errMsg)
  1021. return
  1022. }
  1023. //必须是预测指标分类
  1024. if classifyInfo.ClassifyType != 1 {
  1025. errMsg = "预测指标分类异常,不是预测指标分类"
  1026. err = errors.New(errMsg)
  1027. return
  1028. }
  1029. }
  1030. edbName = strings.Trim(edbName, " ")
  1031. edbCode := sourceEdbInfo.EdbCode + "_" + time.Now().Format(utils.FormatShortDateTimeUnSpace)
  1032. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  1033. existEdbName, err := CheckExistByEdbNameAndEdbInfoId(1, 0, edbName, lang)
  1034. if err != nil {
  1035. errMsg = "判断指标名称是否存在失败"
  1036. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1037. return
  1038. }
  1039. if existEdbName {
  1040. errMsg = "指标名称已存在,请重新填写"
  1041. err = errors.New(errMsg)
  1042. return
  1043. }
  1044. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1045. edbInfo = &models.EdbInfo{
  1046. //EdbInfoId: 0,
  1047. EdbInfoType: sourceEdbInfo.EdbInfoType,
  1048. SourceName: sourceEdbInfo.SourceName,
  1049. Source: sourceEdbInfo.Source,
  1050. EdbCode: edbCode,
  1051. EdbName: edbName,
  1052. EdbNameSource: edbName,
  1053. Frequency: frequency,
  1054. Unit: unit,
  1055. StartDate: sourceEdbInfo.StartDate,
  1056. EndDate: sourceEdbInfo.EndDate,
  1057. ClassifyId: classifyId,
  1058. SysUserId: sysUserId,
  1059. SysUserRealName: sysUserName,
  1060. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  1061. CreateTime: time.Now(),
  1062. ModifyTime: time.Now(),
  1063. MinValue: sourceEdbInfo.MinValue,
  1064. MaxValue: sourceEdbInfo.MaxValue,
  1065. EndValue: sourceEdbInfo.EndValue,
  1066. CalculateFormula: sourceEdbInfo.CalculateFormula,
  1067. EdbType: sourceEdbInfo.EdbType,
  1068. //Sort: sourceEdbInfo.,
  1069. LatestDate: sourceEdbInfo.LatestDate,
  1070. LatestValue: sourceEdbInfo.LatestValue,
  1071. MoveType: sourceEdbInfo.MoveType,
  1072. MoveFrequency: sourceEdbInfo.MoveFrequency,
  1073. NoUpdate: sourceEdbInfo.NoUpdate,
  1074. IsUpdate: sourceEdbInfo.IsUpdate,
  1075. ServerUrl: "",
  1076. EdbNameEn: edbName,
  1077. UnitEn: sourceEdbInfo.UnitEn,
  1078. DataDateType: sourceEdbInfo.DataDateType,
  1079. Sort: models.GetAddEdbMaxSortByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE),
  1080. IsStaticData: 1,
  1081. }
  1082. // 关联关系表
  1083. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  1084. fromEdbMap := make(map[int]int)
  1085. // 源指标关联关系表
  1086. calculateMappingItem := &models.EdbInfoCalculateMapping{
  1087. //EdbInfoCalculateMappingId: 0,
  1088. //EdbInfoId: 0,
  1089. Source: edbInfo.Source,
  1090. SourceName: edbInfo.SourceName,
  1091. EdbCode: edbInfo.EdbCode,
  1092. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  1093. FromEdbCode: sourceEdbInfo.EdbCode,
  1094. FromEdbName: sourceEdbInfo.EdbName,
  1095. FromSource: sourceEdbInfo.Source,
  1096. FromSourceName: sourceEdbInfo.SourceName,
  1097. //FromTag: "",
  1098. Sort: 1,
  1099. CreateTime: time.Now(),
  1100. ModifyTime: time.Now(),
  1101. }
  1102. fromEdbMap[sourceEdbInfoId] = sourceEdbInfoId
  1103. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  1104. newPredictEdbConfList := make([]*models.PredictEdbConf, 0)
  1105. //查询原先的预测指标配置项
  1106. if sourceEdbInfo.EdbType == 1 {
  1107. // 查找该预测指标配置
  1108. predictEdbConfList, tmpErr := models.GetPredictEdbConfListById(sourceEdbInfo.EdbInfoId)
  1109. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1110. errMsg = "获取预测指标配置信息失败"
  1111. err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
  1112. return
  1113. }
  1114. if len(predictEdbConfList) > 0 {
  1115. // 遍历
  1116. for _, v := range predictEdbConfList {
  1117. tmpPredictEdbConf := &models.PredictEdbConf{
  1118. PredictEdbInfoId: 0,
  1119. SourceEdbInfoId: sourceEdbInfoId,
  1120. RuleType: v.RuleType,
  1121. FixedValue: v.FixedValue,
  1122. Value: v.Value,
  1123. EmptyType: v.EmptyType,
  1124. MaxEmptyType: v.MaxEmptyType,
  1125. EndDate: v.EndDate,
  1126. ModifyTime: time.Now(),
  1127. CreateTime: time.Now(),
  1128. }
  1129. newPredictEdbConfList = append(newPredictEdbConfList, tmpPredictEdbConf)
  1130. }
  1131. }
  1132. }
  1133. err, errMsg = models.AddPredictStaticEdb(edbInfo, sourceEdbInfo, calculateMappingList, newPredictEdbConfList)
  1134. return
  1135. }