predict_edb_info.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "github.com/yidane/formula"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // AddPredictEdbInfo 新增预测指标
  17. func AddPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName string, ruleList []request.RuleConfig, sysUserId int, sysUserName, requestBody, requestUrl string) (edbInfo *data_manage.EdbInfo, err error, errMsg string) {
  18. var sourceEdbInfo *data_manage.EdbInfo
  19. // 来源指标信息校验
  20. {
  21. sourceEdbInfo, err = data_manage.GetEdbInfoById(sourceEdbInfoId)
  22. if err != nil && err.Error() != utils.ErrNoRow() {
  23. errMsg = "新增失败"
  24. err = errors.New("获取来源指标失败,Err:" + err.Error())
  25. return
  26. }
  27. if sourceEdbInfo == nil {
  28. errMsg = "找不到该来源指标"
  29. err = nil
  30. return
  31. }
  32. //必须是普通的指标
  33. if sourceEdbInfo.EdbInfoType != 0 {
  34. errMsg = "来源指标异常,不是普通的指标"
  35. return
  36. }
  37. if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, sourceEdbInfo.Frequency) {
  38. errMsg = "预测指标只支持选择日度、周度、月度的指标"
  39. return
  40. }
  41. }
  42. var classifyInfo *data_manage.EdbClassify
  43. // 来源分类信息校验
  44. {
  45. classifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
  46. if err != nil && err.Error() != utils.ErrNoRow() {
  47. errMsg = "新增失败"
  48. err = errors.New("获取预测指标分类失败,Err:" + err.Error())
  49. return
  50. }
  51. if classifyInfo == nil {
  52. errMsg = "找不到该预测指标分类"
  53. err = nil
  54. return
  55. }
  56. //必须是预测指标分类
  57. if classifyInfo.ClassifyType != 1 {
  58. errMsg = "预测指标分类异常,不是预测指标分类"
  59. return
  60. }
  61. }
  62. edbName = strings.Trim(edbName, " ")
  63. edbCode := sourceEdbInfo.EdbCode + "_" + time.Now().Format(utils.FormatShortDateTimeUnSpace)
  64. // 判断该来源指标是否已经被引用了
  65. {
  66. //predictEdbConf, tmpErr := data_manage.GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId)
  67. //if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  68. // errMsg = "新增失败"
  69. // err = tmpErr
  70. // return
  71. //}
  72. // 如果该来源指标已经被引用了,那么不允许再次使用
  73. //if predictEdbConf != nil {
  74. // //获取预测指标详情
  75. // predictEdbInfo, tmpErr := data_manage.GetEdbInfoById(predictEdbConf.PredictEdbInfoId)
  76. // if tmpErr != nil {
  77. // errMsg = "新增失败"
  78. // err = tmpErr
  79. // return
  80. // }
  81. // //获取预测指标的分类
  82. // edbClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(predictEdbInfo.ClassifyId)
  83. // if tmpErr != nil {
  84. // errMsg = "新增失败"
  85. // err = tmpErr
  86. // return
  87. // }
  88. // errMsg = "该指标已存在数据库,目录为:" + edbClassifyInfo.ClassifyName + ",请重新选择指标"
  89. // err = errors.New(errMsg)
  90. // return
  91. //}
  92. }
  93. //判断指标名称是否存在
  94. var condition string
  95. var pars []interface{}
  96. condition += " AND edb_info_type=? "
  97. pars = append(pars, 1)
  98. condition += " AND edb_name=? "
  99. pars = append(pars, edbName)
  100. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  101. if err != nil {
  102. errMsg = "判断指标名称是否存在失败"
  103. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  104. return
  105. }
  106. if count > 0 {
  107. errMsg = "指标名称已存在,请重新填写"
  108. return
  109. }
  110. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  111. edbInfo = &data_manage.EdbInfo{
  112. //EdbInfoId: 0,
  113. EdbInfoType: 1,
  114. SourceName: "预测指标",
  115. Source: utils.DATA_SOURCE_PREDICT,
  116. EdbCode: edbCode,
  117. EdbName: edbName,
  118. EdbNameSource: edbName,
  119. Frequency: sourceEdbInfo.Frequency,
  120. Unit: sourceEdbInfo.Unit,
  121. StartDate: sourceEdbInfo.StartDate,
  122. ClassifyId: classifyId,
  123. SysUserId: sysUserId,
  124. SysUserRealName: sysUserName,
  125. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  126. CreateTime: time.Now(),
  127. ModifyTime: time.Now(),
  128. MinValue: sourceEdbInfo.MinValue,
  129. MaxValue: sourceEdbInfo.MaxValue,
  130. CalculateFormula: sourceEdbInfo.CalculateFormula,
  131. EdbType: 1,
  132. //Sort: sourceEdbInfo.,
  133. LatestDate: sourceEdbInfo.LatestDate,
  134. LatestValue: sourceEdbInfo.LatestValue,
  135. MoveType: sourceEdbInfo.MoveType,
  136. MoveFrequency: sourceEdbInfo.MoveFrequency,
  137. NoUpdate: sourceEdbInfo.NoUpdate,
  138. ServerUrl: "",
  139. }
  140. // 关联关系表
  141. calculateMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  142. fromEdbMap := make(map[int]int)
  143. // 源指标关联关系表
  144. calculateMappingItem := &data_manage.EdbInfoCalculateMapping{
  145. //EdbInfoCalculateMappingId: 0,
  146. //EdbInfoId: 0,
  147. Source: edbInfo.Source,
  148. SourceName: edbInfo.SourceName,
  149. EdbCode: edbInfo.EdbCode,
  150. FromEdbInfoId: sourceEdbInfo.EdbInfoId,
  151. FromEdbCode: sourceEdbInfo.EdbCode,
  152. FromEdbName: sourceEdbInfo.EdbName,
  153. FromSource: sourceEdbInfo.Source,
  154. FromSourceName: sourceEdbInfo.SourceName,
  155. //FromTag: "",
  156. Sort: 1,
  157. CreateTime: time.Now(),
  158. ModifyTime: time.Now(),
  159. }
  160. fromEdbMap[sourceEdbInfoId] = sourceEdbInfoId
  161. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  162. // 预测指标配置
  163. predictEdbConfList := make([]*data_manage.PredictEdbConf, 0)
  164. for _, v := range ruleList {
  165. // 预测指标配置
  166. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  167. if tmpErr != nil {
  168. errMsg = "规则配置的截止日期异常,请重新填写"
  169. return
  170. }
  171. // 没有数据,自己瞎测试
  172. //switch v.RuleType {
  173. //case 3: //3:同比
  174. // v.Value = "0.1"
  175. //case 4: //4:同差
  176. // v.Value = "10"
  177. //case 5: //5:环比
  178. // v.Value = "0.1"
  179. //case 6: //6:环差
  180. // v.Value = "0.1"
  181. //case 7: //7:N期移动均值
  182. // v.Value = "5"
  183. //case 8: //8:N期段线性外推值
  184. // v.Value = "5"
  185. //}
  186. switch v.RuleType {
  187. case 8: //N期段线性外推值
  188. valInt, tmpErr := strconv.Atoi(v.Value)
  189. if tmpErr != nil {
  190. errMsg = "N期段线性外推值的N值异常"
  191. return
  192. }
  193. if valInt <= 1 {
  194. errMsg = "N期段线性外推值的N值必须大于1"
  195. return
  196. }
  197. case 9: //9:动态环差
  198. for _, v := range v.EdbInfoIdArr {
  199. fromEdbMap[v.EdbInfoId] = v.EdbInfoId
  200. }
  201. }
  202. tmpPredictEdbConf := &data_manage.PredictEdbConf{
  203. PredictEdbInfoId: 0,
  204. SourceEdbInfoId: sourceEdbInfoId,
  205. RuleType: v.RuleType,
  206. //FixedValue: v.Value,
  207. Value: v.Value,
  208. EndDate: ruleEndDate,
  209. ModifyTime: time.Now(),
  210. CreateTime: time.Now(),
  211. }
  212. edbInfo.EndDate = v.EndDate
  213. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  214. }
  215. err = data_manage.AddPredictEdb(edbInfo, calculateMappingItem, predictEdbConfList)
  216. if err != nil {
  217. errMsg = "保存失败"
  218. err = errors.New("保存失败,Err:" + err.Error())
  219. return
  220. }
  221. //新增操作日志
  222. {
  223. edbLog := new(data_manage.EdbInfoLog)
  224. edbLog.EdbInfoId = edbInfo.EdbInfoId
  225. edbLog.SourceName = edbInfo.SourceName
  226. edbLog.Source = edbInfo.Source
  227. edbLog.EdbCode = edbInfo.EdbCode
  228. edbLog.EdbName = edbInfo.EdbName
  229. edbLog.ClassifyId = edbInfo.ClassifyId
  230. edbLog.SysUserId = sysUserId
  231. edbLog.SysUserRealName = sysUserName
  232. edbLog.CreateTime = time.Now()
  233. edbLog.Content = requestBody
  234. edbLog.Status = "新增指标"
  235. edbLog.Method = requestUrl
  236. go data_manage.AddEdbInfoLog(edbLog)
  237. }
  238. //添加es
  239. AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  240. return
  241. }
  242. // EditPredictEdbInfo 编辑预测指标
  243. func EditPredictEdbInfo(edbInfoId, classifyId int, edbName string, ruleList []request.RuleConfig, sysUserId int, sysUserName, requestBody, requestUrl string) (edbInfo *data_manage.EdbInfo, err error, errMsg string) {
  244. // 指标信息校验
  245. {
  246. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  247. if err != nil && err.Error() != utils.ErrNoRow() {
  248. errMsg = "修改失败"
  249. err = errors.New("获取预测指标失败,Err:" + err.Error())
  250. return
  251. }
  252. if edbInfo == nil {
  253. errMsg = "找不到该预测指标"
  254. err = nil
  255. return
  256. }
  257. //必须是普通的指标
  258. if edbInfo.EdbInfoType != 1 {
  259. errMsg = "指标异常,不是预测指标"
  260. return
  261. }
  262. }
  263. var predictEdbConf *data_manage.PredictEdbConf
  264. // 指标配置信息校验
  265. {
  266. // 查找该预测指标配置
  267. predictEdbConfList, tmpErr := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  268. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  269. errMsg = "修改失败"
  270. err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
  271. return
  272. }
  273. if len(predictEdbConfList) == 0 {
  274. errMsg = "找不到该预测指标配置"
  275. err = nil
  276. return
  277. }
  278. predictEdbConf = predictEdbConfList[0]
  279. }
  280. //判断指标名称是否存在
  281. var condition string
  282. var pars []interface{}
  283. condition += " AND edb_info_id<>? "
  284. pars = append(pars, edbInfoId)
  285. condition += " AND edb_info_type=? "
  286. pars = append(pars, 1)
  287. condition += " AND edb_name=? "
  288. pars = append(pars, edbName)
  289. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  290. if err != nil {
  291. errMsg = "判断指标名称是否存在失败"
  292. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  293. return
  294. }
  295. if count > 0 {
  296. errMsg = "指标名称已存在,请重新填写"
  297. return
  298. }
  299. edbInfo.EdbName = edbName
  300. edbInfo.EdbNameSource = edbName
  301. edbInfo.ClassifyId = classifyId
  302. edbInfo.ModifyTime = time.Now()
  303. updateEdbInfoCol := []string{"EdbName", "EdbNameSource", "ClassifyId", "EndDate", "ModifyTime"}
  304. // 预测指标配置
  305. predictEdbConfList := make([]*data_manage.PredictEdbConf, 0)
  306. for _, v := range ruleList {
  307. // 预测指标配置
  308. ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  309. if tmpErr != nil {
  310. errMsg = "规则配置的截止日期异常,请重新填写"
  311. return
  312. }
  313. switch v.RuleType {
  314. case 8: //N期段线性外推值
  315. valInt, tmpErr := strconv.Atoi(v.Value)
  316. if tmpErr != nil {
  317. errMsg = "N期段线性外推值的N值异常"
  318. return
  319. }
  320. if valInt <= 1 {
  321. errMsg = "N期段线性外推值的N值必须大于1"
  322. return
  323. }
  324. case 9: //9:动态环差
  325. }
  326. tmpPredictEdbConf := &data_manage.PredictEdbConf{
  327. PredictEdbInfoId: edbInfoId,
  328. SourceEdbInfoId: predictEdbConf.SourceEdbInfoId,
  329. RuleType: v.RuleType,
  330. //FixedValue: v.Value,
  331. Value: v.Value,
  332. EndDate: ruleEndDate,
  333. ModifyTime: time.Now(),
  334. CreateTime: time.Now(),
  335. }
  336. predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
  337. edbInfo.EndDate = v.EndDate
  338. }
  339. err = data_manage.EditPredictEdb(edbInfo, predictEdbConfList, updateEdbInfoCol)
  340. if err != nil {
  341. errMsg = "保存失败"
  342. err = errors.New("保存失败,Err:" + err.Error())
  343. return
  344. }
  345. //新增操作日志
  346. {
  347. edbLog := new(data_manage.EdbInfoLog)
  348. edbLog.EdbInfoId = edbInfo.EdbInfoId
  349. edbLog.SourceName = edbInfo.SourceName
  350. edbLog.Source = edbInfo.Source
  351. edbLog.EdbCode = edbInfo.EdbCode
  352. edbLog.EdbName = edbInfo.EdbName
  353. edbLog.ClassifyId = edbInfo.ClassifyId
  354. edbLog.SysUserId = sysUserId
  355. edbLog.SysUserRealName = sysUserName
  356. edbLog.CreateTime = time.Now()
  357. edbLog.Content = requestBody
  358. edbLog.Status = "编辑指标"
  359. edbLog.Method = requestUrl
  360. go data_manage.AddEdbInfoLog(edbLog)
  361. }
  362. //添加es
  363. AddOrEditEdbInfoToEs(edbInfoId)
  364. // 刷新关联指标
  365. go EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true)
  366. return
  367. }
  368. // RefreshPredictEdbInfo 刷新预测指标
  369. func RefreshPredictEdbInfo(edbInfoId int, refreshAll bool) (edbInfo *data_manage.EdbInfo, isAsync bool, err error, errMsg string) {
  370. // 指标信息校验
  371. {
  372. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  373. if err != nil && err.Error() != utils.ErrNoRow() {
  374. errMsg = "刷新失败"
  375. err = errors.New("获取预测指标失败,Err:" + err.Error())
  376. return
  377. }
  378. if edbInfo == nil {
  379. errMsg = "找不到该预测指标"
  380. err = nil
  381. return
  382. }
  383. //必须是预测的指标
  384. if edbInfo.EdbInfoType != 1 {
  385. errMsg = "指标异常,不是预测指标"
  386. return
  387. }
  388. }
  389. err, isAsync = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, refreshAll)
  390. return
  391. }
  392. // MovePredictEdbInfo 移动预测指标
  393. func MovePredictEdbInfo(edbInfoId, classifyId, prevEdbInfoId, nextEdbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (err error, errMsg string) {
  394. //判断分类是否存在
  395. count, _ := data_manage.GetEdbClassifyCountById(classifyId)
  396. if count <= 0 {
  397. errMsg = "分类已被删除,不可移动,请刷新页面"
  398. return
  399. }
  400. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  401. if err != nil {
  402. if err != nil && err.Error() != utils.ErrNoRow() {
  403. errMsg = "移动失败"
  404. err = errors.New("获取预测指标失败,Err:" + err.Error())
  405. return
  406. }
  407. if edbInfo == nil {
  408. errMsg = "找不到该预测指标"
  409. err = nil
  410. return
  411. }
  412. return
  413. }
  414. // 移动权限校验
  415. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType)
  416. if !button.MoveButton {
  417. errMsg = "无权限操作"
  418. err = nil
  419. return
  420. return
  421. }
  422. //如果改变了分类,那么移动该指标数据
  423. if edbInfo.ClassifyId != classifyId {
  424. err = data_manage.MoveEdbInfo(edbInfoId, classifyId)
  425. if err != nil {
  426. errMsg = "移动失败"
  427. err = errors.New("移动预测指标失败,Err:" + err.Error())
  428. return
  429. }
  430. }
  431. updateCol := make([]string, 0)
  432. //如果有传入 上一个兄弟节点分类id
  433. if prevEdbInfoId > 0 {
  434. prevEdbInfo, tmpErr := data_manage.GetEdbInfoById(prevEdbInfoId)
  435. if tmpErr != nil {
  436. errMsg = "移动失败"
  437. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  438. return
  439. }
  440. //如果是移动在两个兄弟节点之间
  441. if nextEdbInfoId > 0 {
  442. //下一个兄弟节点
  443. nextEdbInfo, tmpErr := data_manage.GetEdbInfoById(nextEdbInfoId)
  444. if tmpErr != nil {
  445. errMsg = "移动失败"
  446. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  447. return
  448. }
  449. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  450. if prevEdbInfo.Sort == nextEdbInfo.Sort || prevEdbInfo.Sort == edbInfo.Sort {
  451. //变更兄弟节点的排序
  452. updateSortStr := `sort + 2`
  453. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  454. } else {
  455. //如果下一个兄弟的排序权重正好是上个兄弟节点 的下一层,那么需要再加一层了
  456. if nextEdbInfo.Sort-prevEdbInfo.Sort == 1 {
  457. //变更兄弟节点的排序
  458. updateSortStr := `sort + 1`
  459. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  460. }
  461. }
  462. }
  463. edbInfo.Sort = prevEdbInfo.Sort + 1
  464. edbInfo.ModifyTime = time.Now()
  465. updateCol = append(updateCol, "Sort", "ModifyTime")
  466. } else {
  467. firstClassify, tmpErr := data_manage.GetFirstEdbInfoByClassifyId(classifyId)
  468. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  469. errMsg = "移动失败"
  470. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
  471. return
  472. }
  473. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  474. if firstClassify != nil && firstClassify.Sort == 0 {
  475. updateSortStr := ` sort + 1 `
  476. _ = data_manage.UpdateEdbInfoSortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.EdbInfoId-1, updateSortStr)
  477. }
  478. edbInfo.Sort = 0 //那就是排在第一位
  479. edbInfo.ModifyTime = time.Now()
  480. updateCol = append(updateCol, "Sort", "ModifyTime")
  481. }
  482. //更新
  483. if len(updateCol) > 0 {
  484. err = edbInfo.Update(updateCol)
  485. }
  486. if err != nil {
  487. errMsg = "移动失败"
  488. err = errors.New("修改失败,Err:" + err.Error())
  489. return
  490. }
  491. //新增操作日志
  492. {
  493. edbLog := new(data_manage.EdbInfoLog)
  494. edbLog.EdbInfoId = edbInfo.EdbInfoId
  495. edbLog.SourceName = edbInfo.SourceName
  496. edbLog.Source = edbInfo.Source
  497. edbLog.EdbCode = edbInfo.EdbCode
  498. edbLog.EdbName = edbInfo.EdbName
  499. edbLog.ClassifyId = edbInfo.ClassifyId
  500. edbLog.SysUserId = sysUser.AdminId
  501. edbLog.SysUserRealName = sysUser.RealName
  502. edbLog.CreateTime = time.Now()
  503. edbLog.Content = requestBody
  504. edbLog.Status = "移动指标"
  505. edbLog.Method = requestUrl
  506. go data_manage.AddEdbInfoLog(edbLog)
  507. }
  508. return
  509. }
  510. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  511. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []data_manage.PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*data_manage.EdbDataList) (predictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error, errMsg string) {
  512. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  513. if err != nil {
  514. return
  515. }
  516. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  517. if err != nil {
  518. return
  519. }
  520. // 开始预测数据的时间
  521. startDate := latestDate
  522. // 如果有筛选时间的话
  523. if filtrateStartDateStr != `` {
  524. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  525. if tmpErr != nil {
  526. err = tmpErr
  527. return
  528. }
  529. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  530. if filtrateStartDate.After(latestDate) {
  531. startDate = filtrateStartDate.AddDate(0, 0, -1)
  532. }
  533. }
  534. //var dateArr []string
  535. // 对应日期的值
  536. existMap := make(map[string]float64)
  537. for _, v := range realPredictEdbInfoData {
  538. //dateArr = append(dateArr, v.DataTime)
  539. existMap[v.DataTime] = v.Value
  540. }
  541. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  542. //dataValue := lastDataValue
  543. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  544. for _, predictEdbConf := range predictEdbConfList {
  545. dataEndTime := endDate
  546. if predictEdbConf.EndDate.Before(dataEndTime) {
  547. dataEndTime = predictEdbConf.EndDate
  548. }
  549. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  550. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  551. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  552. continue
  553. }
  554. switch predictEdbConf.RuleType {
  555. case 1: //1:最新
  556. var lastDataValue float64 //最新值
  557. tmpAllData := make([]*data_manage.EdbDataList, 0)
  558. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  559. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  560. lenTmpAllData := len(tmpAllData)
  561. if lenTmpAllData > 0 {
  562. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  563. }
  564. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  565. tmpMaxValue = lastDataValue
  566. tmpMinValue = lastDataValue
  567. case 2: //2:固定值
  568. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  569. if tmpErr != nil {
  570. err = tmpErr
  571. return
  572. }
  573. dataValue, _ := tmpValDecimal.Float64()
  574. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  575. tmpMaxValue = dataValue
  576. tmpMinValue = dataValue
  577. case 3: //3:同比
  578. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  579. if tmpErr != nil {
  580. err = tmpErr
  581. return
  582. }
  583. tbValue, _ := tmpValDecimal.Float64()
  584. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  585. case 4: //4:同差
  586. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  587. if tmpErr != nil {
  588. err = tmpErr
  589. return
  590. }
  591. tcValue, _ := tmpValDecimal.Float64()
  592. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  593. case 5: //5:环比
  594. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  595. if tmpErr != nil {
  596. err = tmpErr
  597. return
  598. }
  599. hbValue, _ := tmpValDecimal.Float64()
  600. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  601. case 6: //6:环差
  602. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  603. if tmpErr != nil {
  604. err = tmpErr
  605. return
  606. }
  607. hcValue, _ := tmpValDecimal.Float64()
  608. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  609. case 7: //7:N期移动均值
  610. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  611. if tmpErr != nil {
  612. err = tmpErr
  613. return
  614. }
  615. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  616. case 8: //8:N期段线性外推值
  617. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  618. if tmpErr != nil {
  619. err = tmpErr
  620. return
  621. }
  622. if nValue <= 1 {
  623. errMsg = `N期段线性外推值的N值必须大于1`
  624. err = errors.New(errMsg)
  625. return
  626. }
  627. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  628. if err != nil {
  629. return
  630. }
  631. case 9: //9:动态环差”预测规则;
  632. //规则计算的环差值map
  633. hcDataMap := make(map[string]float64)
  634. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  635. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  636. if tmpErr != nil {
  637. err = tmpErr
  638. return
  639. }
  640. for _, v := range tmpPredictEdbRuleDataList {
  641. hcDataMap[v.DataTime] = v.Value
  642. }
  643. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  644. if len(predictEdbConf.DataList) <= 0 {
  645. return
  646. }
  647. for _, v := range predictEdbConf.DataList {
  648. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  649. if tmpErr != nil {
  650. continue
  651. }
  652. // 只处理时间段内的数据
  653. if currentDate.Before(startDate) || currentDate.After(endDate) {
  654. continue
  655. }
  656. hcDataMap[v.DataTime] = v.Value
  657. }
  658. }
  659. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  660. case 10: //10:根据 给定终值后插值 规则获取预测数据
  661. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  662. if tmpErr != nil {
  663. err = tmpErr
  664. return
  665. }
  666. finalValue, _ := tmpValDecimal.Float64()
  667. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  668. case 11: //11:根据 季节性 规则获取预测数据
  669. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  670. if err != nil {
  671. return
  672. }
  673. case 12: //12:根据 移动平均同比 规则获取预测数据
  674. var moveAverageConf MoveAverageConf
  675. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  676. if tmpErr != nil {
  677. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  678. return
  679. }
  680. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  681. if err != nil {
  682. return
  683. }
  684. case 13: //13:根据 同比增速差值 规则获取预测数据
  685. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  686. if tmpErr != nil {
  687. err = tmpErr
  688. return
  689. }
  690. tbEndValue, _ := tmpValDecimal.Float64()
  691. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  692. case 14: //14:根据 一元线性拟合 规则获取预测数据
  693. var ruleConf RuleLineNhConf
  694. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  695. if err != nil {
  696. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  697. return
  698. }
  699. // 规则计算的拟合残差值map
  700. newNhccDataMap := make(map[string]float64)
  701. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  702. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  703. if tmpErr != nil {
  704. err = tmpErr
  705. return
  706. }
  707. for _, v := range tmpPredictEdbRuleDataList {
  708. newNhccDataMap[v.DataTime] = v.Value
  709. }
  710. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  711. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  712. if err != nil {
  713. return
  714. }
  715. }
  716. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  717. if err != nil {
  718. return
  719. }
  720. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  721. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  722. if err != nil {
  723. return
  724. }
  725. case 16: //16:年度值倒推
  726. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  727. if err != nil {
  728. return
  729. }
  730. }
  731. // 下一个规则的开始日期
  732. {
  733. lenPredictEdbInfoData := len(predictEdbInfoData)
  734. if lenPredictEdbInfoData > 0 {
  735. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  736. if startDate.Before(tmpDataEndTime) {
  737. startDate = tmpDataEndTime
  738. }
  739. }
  740. }
  741. if tmpMinValue < minValue {
  742. minValue = tmpMinValue
  743. }
  744. if tmpMaxValue > maxValue {
  745. maxValue = tmpMaxValue
  746. }
  747. }
  748. return
  749. }
  750. // GetPredictEdbDayList 获取预测指标日期列表
  751. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  752. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  753. if dataDateType == `` {
  754. dataDateType = `交易日`
  755. }
  756. switch frequency {
  757. case "日度":
  758. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  759. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  760. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  761. continue
  762. }
  763. dayList = append(dayList, currDate)
  764. }
  765. case "周度":
  766. //nextDate := startDate.AddDate(0, 0, 7)
  767. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  768. dayList = append(dayList, currDate)
  769. }
  770. case "旬度":
  771. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  772. nextDate := currDate.AddDate(0, 0, 1)
  773. //每个月的10号、20号、最后一天,那么就写入
  774. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  775. dayList = append(dayList, currDate)
  776. }
  777. currDate = nextDate
  778. }
  779. case "月度":
  780. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  781. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  782. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  783. dayList = append(dayList, currDate)
  784. }
  785. currDate = currDate.AddDate(0, 0, 1)
  786. }
  787. case "季度":
  788. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  789. // 每月的最后一天
  790. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  791. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  792. // 季度日期就写入,否则不写入
  793. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  794. dayList = append(dayList, currDate)
  795. }
  796. }
  797. currDate = currDate.AddDate(0, 0, 1)
  798. }
  799. case "半年度":
  800. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  801. // 每月的最后一天
  802. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  803. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  804. // 半年度日期就写入,否则不写入
  805. if currDate.Month() == 6 || currDate.Month() == 12 {
  806. dayList = append(dayList, currDate)
  807. }
  808. }
  809. currDate = currDate.AddDate(0, 0, 1)
  810. }
  811. case "年度":
  812. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  813. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  814. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  815. dayList = append(dayList, currDate)
  816. }
  817. }
  818. }
  819. return
  820. }
  821. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
  822. func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *data_manage.EdbInfo, dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  823. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  824. if err != nil {
  825. errMsg = `获取预测指标信息失败`
  826. return
  827. }
  828. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  829. return
  830. }
  831. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  832. func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  833. // 非计算指标,直接从表里获取数据
  834. if edbInfo.EdbType != 1 {
  835. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  836. endDate = ``
  837. }
  838. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  839. }
  840. // 查找该预测指标配置
  841. predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  842. if err != nil && err.Error() != utils.ErrNoRow() {
  843. errMsg = "获取预测指标配置信息失败"
  844. return
  845. }
  846. if len(predictEdbConfList) == 0 {
  847. errMsg = "获取预测指标配置信息失败"
  848. err = errors.New(errMsg)
  849. return
  850. }
  851. predictEdbConf = predictEdbConfList[0]
  852. // 来源指标
  853. sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  854. if err != nil {
  855. if err.Error() == utils.ErrNoRow() {
  856. errMsg = "找不到来源指标信息"
  857. err = errors.New(errMsg)
  858. }
  859. return
  860. }
  861. allDataList := make([]*data_manage.EdbDataList, 0)
  862. //获取指标数据(实际已生成)
  863. dataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  864. if err != nil {
  865. return
  866. }
  867. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  868. if startDate != `` {
  869. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
  870. if err != nil {
  871. return
  872. }
  873. } else {
  874. allDataList = dataList
  875. }
  876. // 获取预测指标未来的数据
  877. predictDataList := make([]*data_manage.EdbDataList, 0)
  878. endDateStr := edbInfo.EndDate //预测指标的结束日期
  879. if isTimeBetween && endDate != `` { //如果是时间区间,同时截止日期不为空的情况,那么
  880. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  881. endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  882. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  883. if reqEndDateTime.Before(endDateTime) {
  884. endDateStr = endDate
  885. }
  886. }
  887. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  888. predictEdbConfDataList := make([]data_manage.PredictEdbConfAndData, 0)
  889. for _, v := range predictEdbConfList {
  890. predictEdbConfDataList = append(predictEdbConfDataList, data_manage.PredictEdbConfAndData{
  891. ConfigId: v.ConfigId,
  892. PredictEdbInfoId: v.PredictEdbInfoId,
  893. SourceEdbInfoId: v.SourceEdbInfoId,
  894. RuleType: v.RuleType,
  895. FixedValue: v.FixedValue,
  896. Value: v.Value,
  897. EndDate: v.EndDate,
  898. ModifyTime: v.ModifyTime,
  899. CreateTime: v.CreateTime,
  900. DataList: make([]*data_manage.EdbDataList, 0),
  901. })
  902. }
  903. //var predictMinValue, predictMaxValue float64
  904. predictDataList, _, _, err, _ = GetChartPredictEdbInfoDataListByConfList(predictEdbConfDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  905. if err != nil {
  906. return
  907. }
  908. dataList = append(dataList, predictDataList...)
  909. //if len(predictDataList) > 0 {
  910. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  911. // if edbInfo.MinValue > predictMinValue {
  912. // edbInfo.MinValue = predictMinValue
  913. // }
  914. //
  915. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  916. // if edbInfo.MaxValue < predictMaxValue {
  917. // edbInfo.MaxValue = predictMaxValue
  918. // }
  919. //}
  920. return
  921. }
  922. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  923. func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  924. startDateReal := startDate
  925. calendarPreYear := 0
  926. if calendar == "农历" {
  927. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  928. if err != nil {
  929. fmt.Println("time.Parse:" + err.Error())
  930. }
  931. calendarPreYear = newStartDateReal.Year() - 1
  932. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  933. startDateReal = newStartDateReal.Format(utils.FormatDate)
  934. }
  935. //实际数据的截止日期
  936. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  937. if tmpErr != nil {
  938. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  939. return
  940. }
  941. latestDateYear := latestDate.Year() //实际数据截止年份
  942. // 曲线图
  943. if chartType == 1 {
  944. resultDataList = dataList
  945. return
  946. }
  947. if calendar == "农历" {
  948. if len(dataList) <= 0 {
  949. resultDataList = data_manage.EdbDataResult{}
  950. } else {
  951. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  952. if tmpErr != nil {
  953. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  954. return
  955. }
  956. // 处理季节图的截止日期
  957. for k, edbDataItems := range result.List {
  958. var cuttingDataTimestamp int64
  959. // 切割的日期时间字符串
  960. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  961. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  962. if edbDataItems.Year >= latestDateYear {
  963. for _, tmpData := range edbDataItems.Items {
  964. if tmpData.DataTime == cuttingDataTimeStr {
  965. cuttingDataTimestamp = tmpData.DataTimestamp
  966. break
  967. }
  968. }
  969. }
  970. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  971. result.List[k] = edbDataItems
  972. }
  973. if result.List[0].Year != calendarPreYear {
  974. itemList := make([]*data_manage.EdbDataList, 0)
  975. items := new(data_manage.EdbDataItems)
  976. //items.Year = calendarPreYear
  977. items.Items = itemList
  978. newResult := new(data_manage.EdbDataResult)
  979. newResult.List = append(newResult.List, items)
  980. newResult.List = append(newResult.List, result.List...)
  981. resultDataList = newResult
  982. } else {
  983. resultDataList = result
  984. }
  985. }
  986. } else {
  987. currentYear := time.Now().Year()
  988. quarterDataList := make([]*data_manage.QuarterData, 0)
  989. quarterMap := make(map[int][]*data_manage.EdbDataList)
  990. var quarterArr []int
  991. for _, v := range dataList {
  992. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  993. if tmpErr != nil {
  994. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  995. return
  996. }
  997. year := itemDate.Year()
  998. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  999. timestamp := newItemDate.UnixNano() / 1e6
  1000. v.DataTimestamp = timestamp
  1001. if findVal, ok := quarterMap[year]; !ok {
  1002. quarterArr = append(quarterArr, year)
  1003. findVal = append(findVal, v)
  1004. quarterMap[year] = findVal
  1005. } else {
  1006. findVal = append(findVal, v)
  1007. quarterMap[year] = findVal
  1008. }
  1009. }
  1010. for _, v := range quarterArr {
  1011. itemList := quarterMap[v]
  1012. quarterItem := new(data_manage.QuarterData)
  1013. quarterItem.Year = v
  1014. quarterItem.DataList = itemList
  1015. //如果等于最后的实际日期,那么将切割时间戳记录
  1016. if v == latestDateYear {
  1017. var cuttingDataTimestamp int64
  1018. for _, tmpData := range itemList {
  1019. if tmpData.DataTime == latestDateStr {
  1020. cuttingDataTimestamp = tmpData.DataTimestamp
  1021. break
  1022. }
  1023. }
  1024. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  1025. } else if v > latestDateYear {
  1026. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1027. if len(itemList) > 0 {
  1028. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1029. }
  1030. }
  1031. quarterDataList = append(quarterDataList, quarterItem)
  1032. }
  1033. resultDataList = quarterDataList
  1034. }
  1035. return
  1036. }
  1037. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  1038. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  1039. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
  1040. return
  1041. }
  1042. // GetCalculateByRuleByNineParams 获取预测规则9的计算参数
  1043. func GetCalculateByRuleByNineParams(req request.RuleConfig) (formula string, edbInfoList []*data_manage.EdbInfo, edbInfoIdBytes []string, err error, errMsg string) {
  1044. formula = req.Value
  1045. formula = strings.Replace(formula, "(", "(", -1)
  1046. formula = strings.Replace(formula, ")", ")", -1)
  1047. formula = strings.Replace(formula, ",", ",", -1)
  1048. formula = strings.Replace(formula, "。", ".", -1)
  1049. formula = strings.Replace(formula, "%", "*0.01", -1)
  1050. //检验公式
  1051. var checkFormulaStr string
  1052. for _, tmpEdbInfoId := range req.EdbInfoIdArr {
  1053. checkFormulaStr += tmpEdbInfoId.FromTag + ","
  1054. edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
  1055. }
  1056. formulaMap := CheckFormula(formula)
  1057. for _, tmpFormula := range formulaMap {
  1058. if !strings.Contains(checkFormulaStr, tmpFormula) {
  1059. errMsg = "公式错误,请重新填写"
  1060. return
  1061. }
  1062. }
  1063. //关联的指标信息
  1064. edbInfoList = make([]*data_manage.EdbInfo, 0)
  1065. for _, tmpEdbInfoId := range req.EdbInfoIdArr {
  1066. fromEdbInfo, tmpErr := data_manage.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
  1067. if tmpErr != nil {
  1068. if tmpErr.Error() == utils.ErrNoRow() {
  1069. err = errors.New("指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在")
  1070. } else {
  1071. err = errors.New("获取指标失败:Err:" + tmpErr.Error())
  1072. }
  1073. errMsg = "数据计算失败"
  1074. return
  1075. }
  1076. edbInfoList = append(edbInfoList, fromEdbInfo)
  1077. }
  1078. ok, _ := CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
  1079. if !ok {
  1080. errMsg = "生成计算指标失败,请使用正确的计算公式"
  1081. err = errors.New(errMsg)
  1082. }
  1083. return
  1084. }
  1085. // CalculateByRuleByNine 动态环差规则计算入库
  1086. func CalculateByRuleByNine(formulaStr string, edbInfoList []*data_manage.EdbInfo, edbInfoIdBytes []string) (dataList []*data_manage.EdbDataList, err error) {
  1087. realSaveDataMap := make(map[string]map[int]float64)
  1088. saveDataMap := make(map[string]map[int]float64)
  1089. dateList := make([]string, 0) //日期
  1090. formulaStr = strings.ToUpper(formulaStr)
  1091. // 获取关联指标数据
  1092. for edbInfoIndex, v := range edbInfoList {
  1093. sourceDataList, _, _, tmpErr, _ := GetPredictDataListByPredictEdbInfo(v, "", "", false)
  1094. if tmpErr != nil {
  1095. err = tmpErr
  1096. return
  1097. }
  1098. dataMap := make(map[string]float64)
  1099. for _, dv := range sourceDataList {
  1100. // 实际数据
  1101. if val, ok := realSaveDataMap[dv.DataTime]; ok {
  1102. if _, ok := val[v.EdbInfoId]; !ok {
  1103. val[v.EdbInfoId] = dv.Value
  1104. }
  1105. } else {
  1106. temp := make(map[int]float64)
  1107. temp[v.EdbInfoId] = dv.Value
  1108. realSaveDataMap[dv.DataTime] = temp
  1109. }
  1110. // saveDataMap 待计算的数据
  1111. if val, ok := saveDataMap[dv.DataTime]; ok {
  1112. if _, ok := val[v.EdbInfoId]; !ok {
  1113. val[v.EdbInfoId] = dv.Value
  1114. }
  1115. } else {
  1116. temp2 := make(map[int]float64)
  1117. temp2[v.EdbInfoId] = dv.Value
  1118. saveDataMap[dv.DataTime] = temp2
  1119. }
  1120. // 以第一个指标的日期作为基准日期
  1121. if edbInfoIndex == 0 {
  1122. dateList = append(dateList, dv.DataTime)
  1123. }
  1124. }
  1125. item := new(CalculateItems)
  1126. item.EdbInfoId = v.EdbInfoId
  1127. item.DataMap = dataMap
  1128. }
  1129. //数据处理,将日期内不全的数据做补全
  1130. handleDateSaveDataMap(dateList, realSaveDataMap, saveDataMap, edbInfoList)
  1131. // 添加数据
  1132. dataList = make([]*data_manage.EdbDataList, 0)
  1133. // 计算规则
  1134. formulaMap := CheckFormula(formulaStr)
  1135. existDataMap := make(map[string]string)
  1136. for k, date := range dateList {
  1137. sv := saveDataMap[date]
  1138. //fmt.Println(date, sv)
  1139. formulaFormStr := ReplaceFormula(edbInfoList, sv, formulaMap, formulaStr, edbInfoIdBytes)
  1140. if formulaFormStr == `` {
  1141. //计算公式异常,那么就移除该指标
  1142. continue
  1143. }
  1144. //fmt.Println(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
  1145. expression := formula.NewExpression(formulaFormStr)
  1146. calResult, tmpErr := expression.Evaluate()
  1147. if tmpErr != nil {
  1148. // 分母为0的报错
  1149. if strings.Contains(tmpErr.Error(), "divide by zero") {
  1150. continue
  1151. }
  1152. err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  1153. return
  1154. }
  1155. calVal, tmpErr := calResult.Float64()
  1156. if tmpErr != nil {
  1157. err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  1158. fmt.Println(err)
  1159. return
  1160. }
  1161. saveValue, _ := decimal.NewFromFloat(calVal).RoundCeil(4).Float64() //utils.SubFloatToString(calVal, 4)
  1162. dataTime, _ := time.Parse(utils.FormatDate, date)
  1163. timestamp := dataTime.UnixNano() / 1e6
  1164. if _, existOk := existDataMap[date]; !existOk {
  1165. tmpPredictEdbRuleData := &data_manage.EdbDataList{
  1166. EdbDataId: k,
  1167. EdbInfoId: 0,
  1168. DataTime: date,
  1169. DataTimestamp: timestamp,
  1170. Value: saveValue,
  1171. }
  1172. dataList = append(dataList, tmpPredictEdbRuleData)
  1173. }
  1174. existDataMap[date] = date
  1175. }
  1176. return
  1177. }
  1178. // ModifyPredictEdbBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的基础信息
  1179. func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo) {
  1180. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1181. if err != nil {
  1182. return
  1183. }
  1184. for _, v := range list {
  1185. v.Frequency = sourceEDdbInfo.Frequency
  1186. v.Unit = sourceEDdbInfo.Unit
  1187. v.Update([]string{"Frequency", "Unit"})
  1188. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1189. }
  1190. }
  1191. // ModifyPredictEdbEnBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的英文基础信息
  1192. func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo) {
  1193. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1194. if err != nil {
  1195. return
  1196. }
  1197. for _, v := range list {
  1198. v.Frequency = sourceEDdbInfo.Frequency
  1199. v.UnitEn = sourceEDdbInfo.UnitEn
  1200. v.Update([]string{"Frequency", "UnitEn"})
  1201. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1202. }
  1203. }