predict_edb_info.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. // 非计算指标,直接从表里获取数据
  845. if edbInfo.EdbType != 1 {
  846. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  847. endDate = ``
  848. }
  849. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  850. }
  851. // 查找该预测指标配置
  852. predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  853. if err != nil && err.Error() != utils.ErrNoRow() {
  854. errMsg = "获取预测指标配置信息失败"
  855. return
  856. }
  857. if len(predictEdbConfList) == 0 {
  858. errMsg = "获取预测指标配置信息失败"
  859. err = errors.New(errMsg)
  860. return
  861. }
  862. predictEdbConf = predictEdbConfList[0]
  863. // 来源指标
  864. sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  865. if err != nil {
  866. if err.Error() == utils.ErrNoRow() {
  867. errMsg = "找不到来源指标信息"
  868. err = errors.New(errMsg)
  869. }
  870. return
  871. }
  872. allDataList := make([]*data_manage.EdbDataList, 0)
  873. //获取指标数据(实际已生成)
  874. dataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  875. if err != nil {
  876. return
  877. }
  878. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  879. if startDate != `` {
  880. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  881. if err != nil {
  882. return
  883. }
  884. } else {
  885. allDataList = dataList
  886. }
  887. // 获取预测指标未来的数据
  888. predictDataList := make([]*data_manage.EdbDataList, 0)
  889. endDateStr := edbInfo.EndDate //预测指标的结束日期
  890. if isTimeBetween && endDate != `` { //如果是时间区间,同时截止日期不为空的情况,那么
  891. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  892. endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  893. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  894. if reqEndDateTime.Before(endDateTime) {
  895. endDateStr = endDate
  896. }
  897. }
  898. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  899. predictEdbConfDataList := make([]data_manage.PredictEdbConfAndData, 0)
  900. for _, v := range predictEdbConfList {
  901. predictEdbConfDataList = append(predictEdbConfDataList, data_manage.PredictEdbConfAndData{
  902. ConfigId: v.ConfigId,
  903. PredictEdbInfoId: v.PredictEdbInfoId,
  904. SourceEdbInfoId: v.SourceEdbInfoId,
  905. RuleType: v.RuleType,
  906. FixedValue: v.FixedValue,
  907. Value: v.Value,
  908. EndDate: v.EndDate,
  909. ModifyTime: v.ModifyTime,
  910. CreateTime: v.CreateTime,
  911. DataList: make([]*data_manage.EdbDataList, 0),
  912. })
  913. }
  914. //var predictMinValue, predictMaxValue float64
  915. predictDataList, _, _, err, _ = GetChartPredictEdbInfoDataListByConfList(predictEdbConfDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  916. if err != nil {
  917. return
  918. }
  919. dataList = append(dataList, predictDataList...)
  920. //if len(predictDataList) > 0 {
  921. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  922. // if edbInfo.MinValue > predictMinValue {
  923. // edbInfo.MinValue = predictMinValue
  924. // }
  925. //
  926. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  927. // if edbInfo.MaxValue < predictMaxValue {
  928. // edbInfo.MaxValue = predictMaxValue
  929. // }
  930. //}
  931. return
  932. }
  933. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  934. func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  935. startDateReal := startDate
  936. calendarPreYear := 0
  937. if calendar == "农历" {
  938. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  939. if err != nil {
  940. fmt.Println("time.Parse:" + err.Error())
  941. }
  942. calendarPreYear = newStartDateReal.Year() - 1
  943. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  944. startDateReal = newStartDateReal.Format(utils.FormatDate)
  945. }
  946. //实际数据的截止日期
  947. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  948. if tmpErr != nil {
  949. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  950. return
  951. }
  952. latestDateYear := latestDate.Year() //实际数据截止年份
  953. // 曲线图
  954. if chartType == 1 {
  955. resultDataList = dataList
  956. return
  957. }
  958. if calendar == "农历" {
  959. if len(dataList) <= 0 {
  960. resultDataList = data_manage.EdbDataResult{}
  961. } else {
  962. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  963. if tmpErr != nil {
  964. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  965. return
  966. }
  967. // 处理季节图的截止日期
  968. for k, edbDataItems := range result.List {
  969. var cuttingDataTimestamp int64
  970. // 切割的日期时间字符串
  971. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  972. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  973. if edbDataItems.Year >= latestDateYear {
  974. for _, tmpData := range edbDataItems.Items {
  975. if tmpData.DataTime == cuttingDataTimeStr {
  976. cuttingDataTimestamp = tmpData.DataTimestamp
  977. break
  978. }
  979. }
  980. }
  981. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  982. result.List[k] = edbDataItems
  983. }
  984. if result.List[0].Year != calendarPreYear {
  985. itemList := make([]*data_manage.EdbDataList, 0)
  986. items := new(data_manage.EdbDataItems)
  987. //items.Year = calendarPreYear
  988. items.Items = itemList
  989. newResult := new(data_manage.EdbDataResult)
  990. newResult.List = append(newResult.List, items)
  991. newResult.List = append(newResult.List, result.List...)
  992. resultDataList = newResult
  993. } else {
  994. resultDataList = result
  995. }
  996. }
  997. } else {
  998. currentYear := time.Now().Year()
  999. quarterDataList := make([]*data_manage.QuarterData, 0)
  1000. quarterMap := make(map[int][]*data_manage.EdbDataList)
  1001. var quarterArr []int
  1002. for _, v := range dataList {
  1003. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  1004. if tmpErr != nil {
  1005. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  1006. return
  1007. }
  1008. year := itemDate.Year()
  1009. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  1010. timestamp := newItemDate.UnixNano() / 1e6
  1011. v.DataTimestamp = timestamp
  1012. if findVal, ok := quarterMap[year]; !ok {
  1013. quarterArr = append(quarterArr, year)
  1014. findVal = append(findVal, v)
  1015. quarterMap[year] = findVal
  1016. } else {
  1017. findVal = append(findVal, v)
  1018. quarterMap[year] = findVal
  1019. }
  1020. }
  1021. for _, v := range quarterArr {
  1022. itemList := quarterMap[v]
  1023. quarterItem := new(data_manage.QuarterData)
  1024. quarterItem.Year = v
  1025. quarterItem.DataList = itemList
  1026. //如果等于最后的实际日期,那么将切割时间戳记录
  1027. if v == latestDateYear {
  1028. var cuttingDataTimestamp int64
  1029. for _, tmpData := range itemList {
  1030. if tmpData.DataTime == latestDateStr {
  1031. cuttingDataTimestamp = tmpData.DataTimestamp
  1032. break
  1033. }
  1034. }
  1035. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  1036. } else if v > latestDateYear {
  1037. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1038. if len(itemList) > 0 {
  1039. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1040. }
  1041. }
  1042. quarterDataList = append(quarterDataList, quarterItem)
  1043. }
  1044. resultDataList = quarterDataList
  1045. }
  1046. return
  1047. }
  1048. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  1049. 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) {
  1050. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  1051. return
  1052. }
  1053. // ModifyPredictEdbBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的基础信息
  1054. func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, frequency, unit string) {
  1055. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1056. if err != nil {
  1057. return
  1058. }
  1059. for _, v := range list {
  1060. v.Frequency = frequency
  1061. v.Unit = unit
  1062. v.Update([]string{"Frequency", "Unit"})
  1063. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1064. }
  1065. }
  1066. // ModifyPredictEdbEnBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的英文基础信息
  1067. func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, unitEn string) {
  1068. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  1069. if err != nil {
  1070. return
  1071. }
  1072. for _, v := range list {
  1073. v.UnitEn = unitEn
  1074. v.Update([]string{"UnitEn"})
  1075. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1076. }
  1077. }
  1078. // ModifyPredictEdbUnitBySourceEdbInfoId
  1079. // @Description: 根据来源ETA指标修改预测指标的频度和单位基础信息
  1080. // @author: Roc
  1081. // @datetime 2024-01-05 11:07:39
  1082. // @param sourceEdbInfoId int
  1083. // @param frequency string
  1084. // @param unit string
  1085. // @return err error
  1086. func ModifyPredictEdbUnitBySourceEdbInfoId(sourceEdbInfoId int, frequency, unit string) (err error) {
  1087. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId)
  1088. if err != nil {
  1089. return
  1090. }
  1091. for _, v := range list {
  1092. v.Frequency = frequency
  1093. v.Unit = unit
  1094. v.Update([]string{"Frequency", "Unit"})
  1095. AddOrEditEdbInfoToEs(v.EdbInfoId)
  1096. }
  1097. return
  1098. }