predict_edb.go 34 KB

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