predict_edb.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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, 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. {
  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, 0)
  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. }