predict_edb_info.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 string, endDateType int, frequency, dataDateType string, realPredictEdbInfoData []*data_manage.EdbDataList) (predictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error, errMsg string) {
  523. hasEndNum := 0 //如果选择了未来期数,用来保存已经预测过的期数
  524. var endDate time.Time
  525. if endDateStr != `` {
  526. endDate, err = time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  527. if err != nil {
  528. return
  529. }
  530. }
  531. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  532. if err != nil {
  533. return
  534. }
  535. // 开始预测数据的时间
  536. startDate := latestDate
  537. // 如果有筛选时间的话
  538. if filtrateStartDateStr != `` {
  539. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  540. if tmpErr != nil {
  541. err = tmpErr
  542. return
  543. }
  544. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  545. if filtrateStartDate.After(latestDate) {
  546. startDate = filtrateStartDate.AddDate(0, 0, -1)
  547. }
  548. }
  549. //var dateArr []string
  550. // 对应日期的值
  551. existMap := make(map[string]float64)
  552. for _, v := range realPredictEdbInfoData {
  553. //dateArr = append(dateArr, v.DataTime)
  554. existMap[v.DataTime] = v.Value
  555. }
  556. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  557. //dataValue := lastDataValue
  558. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  559. for _, predictEdbConf := range predictEdbConfList {
  560. dataEndTime := endDate
  561. if !predictEdbConf.EndDate.IsZero() && predictEdbConf.EndDate.Before(dataEndTime) {
  562. dataEndTime = predictEdbConf.EndDate
  563. }
  564. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  565. endNum := predictEdbConf.EndNum - hasEndNum
  566. if endNum <= 0 && endDateType == 1 {
  567. err = fmt.Errorf("预测期数不能小于等于0")
  568. return
  569. }
  570. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType, endDateType, endNum)
  571. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  572. continue
  573. }
  574. switch predictEdbConf.RuleType {
  575. case 1: //1:最新
  576. var lastDataValue float64 //最新值
  577. tmpAllData := make([]*data_manage.EdbDataList, 0)
  578. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  579. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  580. lenTmpAllData := len(tmpAllData)
  581. if lenTmpAllData > 0 {
  582. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  583. }
  584. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  585. tmpMaxValue = lastDataValue
  586. tmpMinValue = lastDataValue
  587. case 2: //2:固定值
  588. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  589. if tmpErr != nil {
  590. err = tmpErr
  591. return
  592. }
  593. dataValue, _ := tmpValDecimal.Float64()
  594. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  595. tmpMaxValue = dataValue
  596. tmpMinValue = dataValue
  597. case 3: //3:同比
  598. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  599. if tmpErr != nil {
  600. err = tmpErr
  601. return
  602. }
  603. tbValue, _ := tmpValDecimal.Float64()
  604. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  605. case 4: //4:同差
  606. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  607. if tmpErr != nil {
  608. err = tmpErr
  609. return
  610. }
  611. tcValue, _ := tmpValDecimal.Float64()
  612. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  613. case 5: //5:环比
  614. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  615. if tmpErr != nil {
  616. err = tmpErr
  617. return
  618. }
  619. hbValue, _ := tmpValDecimal.Float64()
  620. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  621. case 6: //6:环差
  622. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  623. if tmpErr != nil {
  624. err = tmpErr
  625. return
  626. }
  627. hcValue, _ := tmpValDecimal.Float64()
  628. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  629. case 7: //7:N期移动均值
  630. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  631. if tmpErr != nil {
  632. err = tmpErr
  633. return
  634. }
  635. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  636. case 8: //8:N期段线性外推值
  637. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  638. if tmpErr != nil {
  639. err = tmpErr
  640. return
  641. }
  642. if nValue <= 1 {
  643. errMsg = `N期段线性外推值的N值必须大于1`
  644. err = errors.New(errMsg)
  645. return
  646. }
  647. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  648. if err != nil {
  649. return
  650. }
  651. case 9: //9:动态环差”预测规则;
  652. //规则计算的环差值map
  653. hcDataMap := make(map[string]float64)
  654. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  655. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  656. if tmpErr != nil {
  657. err = tmpErr
  658. return
  659. }
  660. for _, v := range tmpPredictEdbRuleDataList {
  661. hcDataMap[v.DataTime] = v.Value
  662. }
  663. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  664. if len(predictEdbConf.DataList) <= 0 {
  665. return
  666. }
  667. for _, v := range predictEdbConf.DataList {
  668. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  669. if tmpErr != nil {
  670. continue
  671. }
  672. // 只处理时间段内的数据
  673. if currentDate.Before(startDate) || currentDate.After(endDate) {
  674. continue
  675. }
  676. hcDataMap[v.DataTime] = v.Value
  677. }
  678. }
  679. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  680. case 10: //10:根据 给定终值后插值 规则获取预测数据
  681. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  682. if tmpErr != nil {
  683. err = tmpErr
  684. return
  685. }
  686. finalValue, _ := tmpValDecimal.Float64()
  687. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  688. case 11: //11:根据 季节性 规则获取预测数据
  689. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  690. if err != nil {
  691. return
  692. }
  693. case 12: //12:根据 移动平均同比 规则获取预测数据
  694. var moveAverageConf MoveAverageConf
  695. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  696. if tmpErr != nil {
  697. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  698. return
  699. }
  700. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  701. if err != nil {
  702. return
  703. }
  704. case 13: //13:根据 同比增速差值 规则获取预测数据
  705. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  706. if tmpErr != nil {
  707. err = tmpErr
  708. return
  709. }
  710. tbEndValue, _ := tmpValDecimal.Float64()
  711. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  712. case 14: //14:根据 一元线性拟合 规则获取预测数据
  713. var ruleConf RuleLineNhConf
  714. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  715. if err != nil {
  716. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  717. return
  718. }
  719. // 规则计算的拟合残差值map
  720. newNhccDataMap := make(map[string]float64)
  721. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  722. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  723. if tmpErr != nil {
  724. err = tmpErr
  725. return
  726. }
  727. for _, v := range tmpPredictEdbRuleDataList {
  728. newNhccDataMap[v.DataTime] = v.Value
  729. }
  730. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  731. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  732. if err != nil {
  733. return
  734. }
  735. }
  736. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  737. if err != nil {
  738. return
  739. }
  740. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  741. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  742. if err != nil {
  743. return
  744. }
  745. case 16: //16:年度值倒推
  746. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  747. if err != nil {
  748. return
  749. }
  750. }
  751. // 下一个规则的开始日期
  752. {
  753. lenPredictEdbInfoData := len(predictEdbInfoData)
  754. if lenPredictEdbInfoData > 0 {
  755. if endDateType == 1 {
  756. hasEndNum = predictEdbConf.EndNum
  757. }
  758. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  759. if startDate.Before(tmpDataEndTime) {
  760. startDate = tmpDataEndTime
  761. }
  762. }
  763. }
  764. if tmpMinValue < minValue {
  765. minValue = tmpMinValue
  766. }
  767. if tmpMaxValue > maxValue {
  768. maxValue = tmpMaxValue
  769. }
  770. }
  771. return
  772. }
  773. // GetPredictEdbDayList 获取预测指标日期列表
  774. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string, endDateType, endNum int) (dayList []time.Time) {
  775. if dataDateType == `` {
  776. dataDateType = `交易日`
  777. }
  778. if endDateType == 0 { // 截止日期
  779. switch frequency {
  780. case "日度":
  781. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  782. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  783. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  784. continue
  785. }
  786. dayList = append(dayList, currDate)
  787. }
  788. case "周度":
  789. //nextDate := startDate.AddDate(0, 0, 7)
  790. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  791. dayList = append(dayList, currDate)
  792. }
  793. case "旬度":
  794. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  795. nextDate := currDate.AddDate(0, 0, 1)
  796. //每个月的10号、20号、最后一天,那么就写入
  797. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  798. dayList = append(dayList, currDate)
  799. }
  800. currDate = nextDate
  801. }
  802. case "月度":
  803. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  804. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  805. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  806. dayList = append(dayList, currDate)
  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() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || 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. // 每月的最后一天
  825. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  826. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  827. // 半年度日期就写入,否则不写入
  828. if currDate.Month() == 6 || currDate.Month() == 12 {
  829. dayList = append(dayList, currDate)
  830. }
  831. }
  832. currDate = currDate.AddDate(0, 0, 1)
  833. }
  834. case "年度":
  835. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  836. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  837. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  838. dayList = append(dayList, currDate)
  839. }
  840. }
  841. }
  842. } else { // 截止期数
  843. switch frequency {
  844. case "日度":
  845. for i := 1; i <= endNum; i++ {
  846. currDate := startDate.AddDate(0, 0, i)
  847. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  848. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  849. continue
  850. }
  851. dayList = append(dayList, currDate)
  852. }
  853. case "周度":
  854. for i := 1; i <= endNum; i++ {
  855. currDate := startDate.AddDate(0, 0, i*7)
  856. dayList = append(dayList, currDate)
  857. }
  858. case "旬度":
  859. currDate := startDate
  860. for i := 1; len(dayList) < endNum; i++ {
  861. currDate = currDate.AddDate(0, 0, 1)
  862. nextDate := currDate.AddDate(0, 0, 1)
  863. //每个月的10号、20号、最后一天,那么就写入
  864. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  865. dayList = append(dayList, currDate)
  866. }
  867. }
  868. case "月度":
  869. currDate := startDate
  870. for i := 0; i <= endNum; i++ {
  871. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  872. if !currDate.Equal(startDate) {
  873. dayList = append(dayList, currDate)
  874. }
  875. currDate = currDate.AddDate(0, 0, 1)
  876. }
  877. case "季度":
  878. currDate := startDate
  879. endNum = endNum * 3
  880. for i := 0; i <= endNum; i++ {
  881. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  882. if currDate.After(startDate) {
  883. // 季度日期就写入,否则不写入
  884. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  885. dayList = append(dayList, currDate)
  886. }
  887. }
  888. currDate = currDate.AddDate(0, 0, 1)
  889. }
  890. case "半年度":
  891. currDate := startDate
  892. endNum = endNum * 6
  893. for i := 0; i <= endNum; i++ {
  894. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  895. if currDate.After(startDate) {
  896. // 季度日期就写入,否则不写入
  897. if currDate.Month() == 6 || currDate.Month() == 12 {
  898. dayList = append(dayList, currDate)
  899. }
  900. }
  901. currDate = currDate.AddDate(0, 0, 1)
  902. }
  903. case "年度":
  904. for i := 1; i <= endNum; i++ {
  905. currDate := startDate.AddDate(i, 0, 0)
  906. currDate, _ = time.ParseInLocation(utils.FormatDate, fmt.Sprintf(`%d-12-31`, currDate.Year()), time.Local)
  907. dayList = append(dayList, currDate)
  908. }
  909. }
  910. }
  911. return
  912. }
  913. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
  914. 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) {
  915. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  916. if err != nil {
  917. errMsg = `获取预测指标信息失败`
  918. return
  919. }
  920. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  921. return
  922. }
  923. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  924. 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) {
  925. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  926. endDate = ``
  927. }
  928. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  929. }
  930. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  931. func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  932. startDateReal := startDate
  933. calendarPreYear := 0
  934. if calendar == "农历" {
  935. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  936. if err != nil {
  937. fmt.Println("time.Parse:" + err.Error())
  938. }
  939. calendarPreYear = newStartDateReal.Year() - 1
  940. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  941. startDateReal = newStartDateReal.Format(utils.FormatDate)
  942. }
  943. //实际数据的截止日期
  944. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  945. if tmpErr != nil {
  946. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  947. return
  948. }
  949. latestDateYear := latestDate.Year() //实际数据截止年份
  950. // 曲线图
  951. if chartType == 1 {
  952. resultDataList = dataList
  953. return
  954. }
  955. if calendar == "农历" {
  956. if len(dataList) <= 0 {
  957. resultDataList = data_manage.EdbDataResult{}
  958. } else {
  959. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  960. if tmpErr != nil {
  961. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  962. return
  963. }
  964. // 处理季节图的截止日期
  965. for k, edbDataItems := range result.List {
  966. var cuttingDataTimestamp int64
  967. // 切割的日期时间字符串
  968. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  969. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  970. if edbDataItems.Year >= latestDateYear {
  971. for _, tmpData := range edbDataItems.Items {
  972. if tmpData.DataTime == cuttingDataTimeStr {
  973. cuttingDataTimestamp = tmpData.DataTimestamp
  974. break
  975. }
  976. }
  977. }
  978. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  979. result.List[k] = edbDataItems
  980. }
  981. if result.List[0].Year != calendarPreYear {
  982. itemList := make([]*data_manage.EdbDataList, 0)
  983. items := new(data_manage.EdbDataItems)
  984. //items.Year = calendarPreYear
  985. items.Items = itemList
  986. newResult := new(data_manage.EdbDataResult)
  987. newResult.List = append(newResult.List, items)
  988. newResult.List = append(newResult.List, result.List...)
  989. resultDataList = newResult
  990. } else {
  991. resultDataList = result
  992. }
  993. }
  994. } else {
  995. currentYear := time.Now().Year()
  996. quarterDataList := make([]*data_manage.QuarterData, 0)
  997. quarterMap := make(map[int][]*data_manage.EdbDataList)
  998. var quarterArr []int
  999. for _, v := range dataList {
  1000. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  1001. if tmpErr != nil {
  1002. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  1003. return
  1004. }
  1005. year := itemDate.Year()
  1006. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  1007. timestamp := newItemDate.UnixNano() / 1e6
  1008. v.DataTimestamp = timestamp
  1009. if findVal, ok := quarterMap[year]; !ok {
  1010. quarterArr = append(quarterArr, year)
  1011. findVal = append(findVal, v)
  1012. quarterMap[year] = findVal
  1013. } else {
  1014. findVal = append(findVal, v)
  1015. quarterMap[year] = findVal
  1016. }
  1017. }
  1018. for _, v := range quarterArr {
  1019. itemList := quarterMap[v]
  1020. quarterItem := new(data_manage.QuarterData)
  1021. quarterItem.Year = v
  1022. quarterItem.DataList = itemList
  1023. //如果等于最后的实际日期,那么将切割时间戳记录
  1024. if v == latestDateYear {
  1025. var cuttingDataTimestamp int64
  1026. for _, tmpData := range itemList {
  1027. if tmpData.DataTime == latestDateStr {
  1028. cuttingDataTimestamp = tmpData.DataTimestamp
  1029. break
  1030. }
  1031. }
  1032. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  1033. } else if v > latestDateYear {
  1034. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1035. if len(itemList) > 0 {
  1036. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1037. }
  1038. }
  1039. quarterDataList = append(quarterDataList, quarterItem)
  1040. }
  1041. resultDataList = quarterDataList
  1042. }
  1043. return
  1044. }
  1045. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  1046. 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) {
  1047. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  1048. return
  1049. }
  1050. // ModifyPredictEdbBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的基础信息
  1051. func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, frequency, unit string) {
  1052. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1053. if err != nil {
  1054. return
  1055. }
  1056. for _, v := range list {
  1057. v.Frequency = frequency
  1058. v.Unit = unit
  1059. v.Update([]string{"Frequency", "Unit"})
  1060. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1061. }
  1062. }
  1063. // ModifyPredictEdbEnBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的英文基础信息
  1064. func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, unitEn string) {
  1065. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1066. if err != nil {
  1067. return
  1068. }
  1069. for _, v := range list {
  1070. v.UnitEn = unitEn
  1071. v.Update([]string{"UnitEn"})
  1072. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1073. }
  1074. }
  1075. // ModifyPredictEdbUnitBySourceEdbInfoId
  1076. // @Description: 根据来源ETA指标修改预测指标的频度和单位基础信息
  1077. // @author: Roc
  1078. // @datetime 2024-01-05 11:07:39
  1079. // @param sourceEdbInfoId int
  1080. // @param frequency string
  1081. // @param unit string
  1082. // @return err error
  1083. func ModifyPredictEdbUnitBySourceEdbInfoId(sourceEdbInfoId int, frequency, unit string) (err error) {
  1084. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId)
  1085. if err != nil {
  1086. return
  1087. }
  1088. for _, v := range list {
  1089. v.Frequency = frequency
  1090. v.Unit = unit
  1091. v.Update([]string{"Frequency", "Unit"})
  1092. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1093. }
  1094. return
  1095. }