predict_edb_info.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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/services/data/data_manage_permission"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  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, false)
  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, false)
  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. var haveOperaAuth bool
  415. // 权限校验
  416. {
  417. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  418. if err != nil {
  419. errMsg = "移动失败"
  420. err = errors.New("校验指标权限失败,Err:" + err.Error())
  421. return
  422. }
  423. }
  424. // 移动权限校验
  425. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  426. if !button.MoveButton {
  427. errMsg = "无权限操作"
  428. err = nil
  429. return
  430. }
  431. //如果改变了分类,那么移动该指标数据
  432. if edbInfo.ClassifyId != classifyId {
  433. err = data_manage.MoveEdbInfo(edbInfoId, classifyId)
  434. if err != nil {
  435. errMsg = "移动失败"
  436. err = errors.New("移动预测指标失败,Err:" + err.Error())
  437. return
  438. }
  439. }
  440. updateCol := make([]string, 0)
  441. //如果有传入 上一个兄弟节点分类id
  442. if prevEdbInfoId > 0 {
  443. prevEdbInfo, tmpErr := data_manage.GetEdbInfoById(prevEdbInfoId)
  444. if tmpErr != nil {
  445. errMsg = "移动失败"
  446. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  447. return
  448. }
  449. //如果是移动在两个兄弟节点之间
  450. if nextEdbInfoId > 0 {
  451. //下一个兄弟节点
  452. nextEdbInfo, tmpErr := data_manage.GetEdbInfoById(nextEdbInfoId)
  453. if tmpErr != nil {
  454. errMsg = "移动失败"
  455. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  456. return
  457. }
  458. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  459. if prevEdbInfo.Sort == nextEdbInfo.Sort || prevEdbInfo.Sort == edbInfo.Sort {
  460. //变更兄弟节点的排序
  461. updateSortStr := `sort + 2`
  462. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  463. } else {
  464. //如果下一个兄弟的排序权重正好是上个兄弟节点 的下一层,那么需要再加一层了
  465. if nextEdbInfo.Sort-prevEdbInfo.Sort == 1 {
  466. //变更兄弟节点的排序
  467. updateSortStr := `sort + 1`
  468. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  469. }
  470. }
  471. }
  472. edbInfo.Sort = prevEdbInfo.Sort + 1
  473. edbInfo.ModifyTime = time.Now()
  474. updateCol = append(updateCol, "Sort", "ModifyTime")
  475. } else {
  476. firstClassify, tmpErr := data_manage.GetFirstEdbInfoByClassifyId(classifyId)
  477. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  478. errMsg = "移动失败"
  479. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
  480. return
  481. }
  482. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  483. if firstClassify != nil && firstClassify.Sort == 0 {
  484. updateSortStr := ` sort + 1 `
  485. _ = data_manage.UpdateEdbInfoSortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.EdbInfoId-1, updateSortStr)
  486. }
  487. edbInfo.Sort = 0 //那就是排在第一位
  488. edbInfo.ModifyTime = time.Now()
  489. updateCol = append(updateCol, "Sort", "ModifyTime")
  490. }
  491. //更新
  492. if len(updateCol) > 0 {
  493. err = edbInfo.Update(updateCol)
  494. if err != nil {
  495. errMsg = "移动失败"
  496. err = errors.New("修改失败,Err:" + err.Error())
  497. return
  498. }
  499. //添加es
  500. AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  501. }
  502. //新增操作日志
  503. {
  504. edbLog := new(data_manage.EdbInfoLog)
  505. edbLog.EdbInfoId = edbInfo.EdbInfoId
  506. edbLog.SourceName = edbInfo.SourceName
  507. edbLog.Source = edbInfo.Source
  508. edbLog.EdbCode = edbInfo.EdbCode
  509. edbLog.EdbName = edbInfo.EdbName
  510. edbLog.ClassifyId = edbInfo.ClassifyId
  511. edbLog.SysUserId = sysUser.AdminId
  512. edbLog.SysUserRealName = sysUser.RealName
  513. edbLog.CreateTime = time.Now()
  514. edbLog.Content = requestBody
  515. edbLog.Status = "移动指标"
  516. edbLog.Method = requestUrl
  517. go data_manage.AddEdbInfoLog(edbLog)
  518. }
  519. return
  520. }
  521. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  522. 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) {
  523. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  524. if err != nil {
  525. return
  526. }
  527. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  528. if err != nil {
  529. return
  530. }
  531. // 开始预测数据的时间
  532. startDate := latestDate
  533. // 如果有筛选时间的话
  534. if filtrateStartDateStr != `` {
  535. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  536. if tmpErr != nil {
  537. err = tmpErr
  538. return
  539. }
  540. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  541. if filtrateStartDate.After(latestDate) {
  542. startDate = filtrateStartDate.AddDate(0, 0, -1)
  543. }
  544. }
  545. //var dateArr []string
  546. // 对应日期的值
  547. existMap := make(map[string]float64)
  548. for _, v := range realPredictEdbInfoData {
  549. //dateArr = append(dateArr, v.DataTime)
  550. existMap[v.DataTime] = v.Value
  551. }
  552. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  553. //dataValue := lastDataValue
  554. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  555. for _, predictEdbConf := range predictEdbConfList {
  556. dataEndTime := endDate
  557. if predictEdbConf.EndDate.Before(dataEndTime) {
  558. dataEndTime = predictEdbConf.EndDate
  559. }
  560. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  561. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  562. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  563. continue
  564. }
  565. switch predictEdbConf.RuleType {
  566. case 1: //1:最新
  567. var lastDataValue float64 //最新值
  568. tmpAllData := make([]*data_manage.EdbDataList, 0)
  569. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  570. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  571. lenTmpAllData := len(tmpAllData)
  572. if lenTmpAllData > 0 {
  573. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  574. }
  575. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  576. tmpMaxValue = lastDataValue
  577. tmpMinValue = lastDataValue
  578. case 2: //2:固定值
  579. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  580. if tmpErr != nil {
  581. err = tmpErr
  582. return
  583. }
  584. dataValue, _ := tmpValDecimal.Float64()
  585. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  586. tmpMaxValue = dataValue
  587. tmpMinValue = dataValue
  588. case 3: //3:同比
  589. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  590. if tmpErr != nil {
  591. err = tmpErr
  592. return
  593. }
  594. tbValue, _ := tmpValDecimal.Float64()
  595. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  596. case 4: //4:同差
  597. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  598. if tmpErr != nil {
  599. err = tmpErr
  600. return
  601. }
  602. tcValue, _ := tmpValDecimal.Float64()
  603. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  604. case 5: //5:环比
  605. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  606. if tmpErr != nil {
  607. err = tmpErr
  608. return
  609. }
  610. hbValue, _ := tmpValDecimal.Float64()
  611. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  612. case 6: //6:环差
  613. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  614. if tmpErr != nil {
  615. err = tmpErr
  616. return
  617. }
  618. hcValue, _ := tmpValDecimal.Float64()
  619. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  620. case 7: //7:N期移动均值
  621. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  622. if tmpErr != nil {
  623. err = tmpErr
  624. return
  625. }
  626. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  627. case 8: //8:N期段线性外推值
  628. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  629. if tmpErr != nil {
  630. err = tmpErr
  631. return
  632. }
  633. if nValue <= 1 {
  634. errMsg = `N期段线性外推值的N值必须大于1`
  635. err = errors.New(errMsg)
  636. return
  637. }
  638. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  639. if err != nil {
  640. return
  641. }
  642. case 9: //9:动态环差”预测规则;
  643. //规则计算的环差值map
  644. hcDataMap := make(map[string]float64)
  645. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  646. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  647. if tmpErr != nil {
  648. err = tmpErr
  649. return
  650. }
  651. for _, v := range tmpPredictEdbRuleDataList {
  652. hcDataMap[v.DataTime] = v.Value
  653. }
  654. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  655. if len(predictEdbConf.DataList) <= 0 {
  656. return
  657. }
  658. for _, v := range predictEdbConf.DataList {
  659. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  660. if tmpErr != nil {
  661. continue
  662. }
  663. // 只处理时间段内的数据
  664. if currentDate.Before(startDate) || currentDate.After(endDate) {
  665. continue
  666. }
  667. hcDataMap[v.DataTime] = v.Value
  668. }
  669. }
  670. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  671. case 10: //10:根据 给定终值后插值 规则获取预测数据
  672. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  673. if tmpErr != nil {
  674. err = tmpErr
  675. return
  676. }
  677. finalValue, _ := tmpValDecimal.Float64()
  678. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  679. case 11: //11:根据 季节性 规则获取预测数据
  680. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  681. if err != nil {
  682. return
  683. }
  684. case 12: //12:根据 移动平均同比 规则获取预测数据
  685. var moveAverageConf MoveAverageConf
  686. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  687. if tmpErr != nil {
  688. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  689. return
  690. }
  691. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  692. if err != nil {
  693. return
  694. }
  695. case 13: //13:根据 同比增速差值 规则获取预测数据
  696. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  697. if tmpErr != nil {
  698. err = tmpErr
  699. return
  700. }
  701. tbEndValue, _ := tmpValDecimal.Float64()
  702. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  703. case 14: //14:根据 一元线性拟合 规则获取预测数据
  704. var ruleConf RuleLineNhConf
  705. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  706. if err != nil {
  707. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  708. return
  709. }
  710. // 规则计算的拟合残差值map
  711. newNhccDataMap := make(map[string]float64)
  712. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  713. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  714. if tmpErr != nil {
  715. err = tmpErr
  716. return
  717. }
  718. for _, v := range tmpPredictEdbRuleDataList {
  719. newNhccDataMap[v.DataTime] = v.Value
  720. }
  721. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  722. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  723. if err != nil {
  724. return
  725. }
  726. }
  727. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  728. if err != nil {
  729. return
  730. }
  731. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  732. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  733. if err != nil {
  734. return
  735. }
  736. case 16: //16:年度值倒推
  737. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  738. if err != nil {
  739. return
  740. }
  741. }
  742. // 下一个规则的开始日期
  743. {
  744. lenPredictEdbInfoData := len(predictEdbInfoData)
  745. if lenPredictEdbInfoData > 0 {
  746. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  747. if startDate.Before(tmpDataEndTime) {
  748. startDate = tmpDataEndTime
  749. }
  750. }
  751. }
  752. if tmpMinValue < minValue {
  753. minValue = tmpMinValue
  754. }
  755. if tmpMaxValue > maxValue {
  756. maxValue = tmpMaxValue
  757. }
  758. }
  759. return
  760. }
  761. // GetPredictEdbDayList 获取预测指标日期列表
  762. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  763. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  764. if dataDateType == `` {
  765. dataDateType = `交易日`
  766. }
  767. switch frequency {
  768. case "日度":
  769. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  770. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  771. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  772. continue
  773. }
  774. dayList = append(dayList, currDate)
  775. }
  776. case "周度":
  777. //nextDate := startDate.AddDate(0, 0, 7)
  778. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  779. dayList = append(dayList, currDate)
  780. }
  781. case "旬度":
  782. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  783. nextDate := currDate.AddDate(0, 0, 1)
  784. //每个月的10号、20号、最后一天,那么就写入
  785. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  786. dayList = append(dayList, currDate)
  787. }
  788. currDate = nextDate
  789. }
  790. case "月度":
  791. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  792. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  793. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  794. dayList = append(dayList, currDate)
  795. }
  796. currDate = currDate.AddDate(0, 0, 1)
  797. }
  798. case "季度":
  799. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  800. // 每月的最后一天
  801. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  802. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  803. // 季度日期就写入,否则不写入
  804. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  805. dayList = append(dayList, currDate)
  806. }
  807. }
  808. currDate = currDate.AddDate(0, 0, 1)
  809. }
  810. case "半年度":
  811. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  812. // 每月的最后一天
  813. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  814. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  815. // 半年度日期就写入,否则不写入
  816. if currDate.Month() == 6 || currDate.Month() == 12 {
  817. dayList = append(dayList, currDate)
  818. }
  819. }
  820. currDate = currDate.AddDate(0, 0, 1)
  821. }
  822. case "年度":
  823. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  824. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  825. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  826. dayList = append(dayList, currDate)
  827. }
  828. }
  829. }
  830. return
  831. }
  832. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
  833. 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) {
  834. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  835. if err != nil {
  836. errMsg = `获取预测指标信息失败`
  837. return
  838. }
  839. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  840. return
  841. }
  842. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  843. 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) {
  844. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  845. endDate = ``
  846. }
  847. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  848. }
  849. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  850. func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  851. startDateReal := startDate
  852. calendarPreYear := 0
  853. if calendar == "农历" {
  854. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  855. if err != nil {
  856. fmt.Println("time.Parse:" + err.Error())
  857. }
  858. calendarPreYear = newStartDateReal.Year() - 1
  859. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  860. startDateReal = newStartDateReal.Format(utils.FormatDate)
  861. }
  862. //实际数据的截止日期
  863. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  864. if tmpErr != nil {
  865. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  866. return
  867. }
  868. latestDateYear := latestDate.Year() //实际数据截止年份
  869. // 曲线图
  870. if chartType == 1 {
  871. resultDataList = dataList
  872. return
  873. }
  874. if calendar == "农历" {
  875. if len(dataList) <= 0 {
  876. resultDataList = data_manage.EdbDataResult{}
  877. } else {
  878. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  879. if tmpErr != nil {
  880. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  881. return
  882. }
  883. // 处理季节图的截止日期
  884. for k, edbDataItems := range result.List {
  885. var cuttingDataTimestamp int64
  886. // 切割的日期时间字符串
  887. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  888. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  889. if edbDataItems.Year >= latestDateYear {
  890. for _, tmpData := range edbDataItems.Items {
  891. if tmpData.DataTime == cuttingDataTimeStr {
  892. cuttingDataTimestamp = tmpData.DataTimestamp
  893. break
  894. }
  895. }
  896. }
  897. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  898. result.List[k] = edbDataItems
  899. }
  900. if result.List[0].Year != calendarPreYear {
  901. itemList := make([]*data_manage.EdbDataList, 0)
  902. items := new(data_manage.EdbDataItems)
  903. //items.Year = calendarPreYear
  904. items.Items = itemList
  905. newResult := new(data_manage.EdbDataResult)
  906. newResult.List = append(newResult.List, items)
  907. newResult.List = append(newResult.List, result.List...)
  908. resultDataList = newResult
  909. } else {
  910. resultDataList = result
  911. }
  912. }
  913. } else {
  914. currentYear := time.Now().Year()
  915. quarterDataList := make([]*data_manage.QuarterData, 0)
  916. quarterMap := make(map[int][]*data_manage.EdbDataList)
  917. var quarterArr []int
  918. for _, v := range dataList {
  919. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  920. if tmpErr != nil {
  921. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  922. return
  923. }
  924. year := itemDate.Year()
  925. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  926. timestamp := newItemDate.UnixNano() / 1e6
  927. v.DataTimestamp = timestamp
  928. if findVal, ok := quarterMap[year]; !ok {
  929. quarterArr = append(quarterArr, year)
  930. findVal = append(findVal, v)
  931. quarterMap[year] = findVal
  932. } else {
  933. findVal = append(findVal, v)
  934. quarterMap[year] = findVal
  935. }
  936. }
  937. for _, v := range quarterArr {
  938. itemList := quarterMap[v]
  939. quarterItem := new(data_manage.QuarterData)
  940. quarterItem.Year = v
  941. quarterItem.DataList = itemList
  942. //如果等于最后的实际日期,那么将切割时间戳记录
  943. if v == latestDateYear {
  944. var cuttingDataTimestamp int64
  945. for _, tmpData := range itemList {
  946. if tmpData.DataTime == latestDateStr {
  947. cuttingDataTimestamp = tmpData.DataTimestamp
  948. break
  949. }
  950. }
  951. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  952. } else if v > latestDateYear {
  953. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  954. if len(itemList) > 0 {
  955. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  956. }
  957. }
  958. quarterDataList = append(quarterDataList, quarterItem)
  959. }
  960. resultDataList = quarterDataList
  961. }
  962. return
  963. }
  964. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  965. 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) {
  966. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  967. return
  968. }
  969. // ModifyPredictEdbBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的基础信息
  970. func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, frequency, unit string) {
  971. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  972. if err != nil {
  973. return
  974. }
  975. for _, v := range list {
  976. v.Frequency = frequency
  977. v.Unit = unit
  978. v.Update([]string{"Frequency", "Unit"})
  979. AddOrEditEdbInfoToEs(v.EdbInfoId)
  980. }
  981. }
  982. // ModifyPredictEdbEnBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的英文基础信息
  983. func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, unitEn string) {
  984. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  985. if err != nil {
  986. return
  987. }
  988. for _, v := range list {
  989. v.UnitEn = unitEn
  990. v.Update([]string{"UnitEn"})
  991. AddOrEditEdbInfoToEs(v.EdbInfoId)
  992. }
  993. }
  994. // ModifyPredictEdbUnitBySourceEdbInfoId
  995. // @Description: 根据来源ETA指标修改预测指标的频度和单位基础信息
  996. // @author: Roc
  997. // @datetime 2024-01-05 11:07:39
  998. // @param sourceEdbInfoId int
  999. // @param frequency string
  1000. // @param unit string
  1001. // @return err error
  1002. func ModifyPredictEdbUnitBySourceEdbInfoId(sourceEdbInfoId int, frequency, unit string) (err error) {
  1003. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId)
  1004. if err != nil {
  1005. return
  1006. }
  1007. for _, v := range list {
  1008. v.Frequency = frequency
  1009. v.Unit = unit
  1010. v.Update([]string{"Frequency", "Unit"})
  1011. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1012. }
  1013. return
  1014. }