predict_edb_info.go 41 KB

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