edb_info.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "hongze/hongze_edb_lib/utils"
  9. "strconv"
  10. "time"
  11. )
  12. type EdbInfo struct {
  13. EdbInfoId int `orm:"column(edb_info_id);pk"`
  14. SourceName string `description:"来源名称"`
  15. Source int `description:"来源id"`
  16. EdbCode string `description:"指标编码"`
  17. EdbName string `description:"指标名称"`
  18. EdbNameSource string `description:"指标名称来源"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. StartDate string `description:"起始日期"`
  22. EndDate string `description:"终止日期"`
  23. ClassifyId int `description:"分类id"`
  24. SysUserId int
  25. SysUserRealName string
  26. UniqueCode string `description:"指标唯一编码"`
  27. CreateTime time.Time
  28. ModifyTime time.Time
  29. MinValue float64 `description:"指标最小值"`
  30. MaxValue float64 `description:"指标最大值"`
  31. CalculateFormula string `description:"计算公式"`
  32. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  33. Sort int `description:"排序字段"`
  34. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  35. MoveFrequency string `description:"移动频度"`
  36. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  37. ServerUrl string `description:"服务器地址"`
  38. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  39. EdbNameEn string `description:"英文指标名称"`
  40. UnitEn string `description:"英文单位"`
  41. LatestDate string `description:"数据最新日期"`
  42. LatestValue float64 `description:"数据最新值"`
  43. ChartImage string `description:"图表图片"`
  44. Calendar string `description:"公历/农历"`
  45. }
  46. // AddEdbInfo 添加指标
  47. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  48. o := orm.NewOrm()
  49. lastId, err = o.Insert(item)
  50. return
  51. }
  52. // EdbInfoList 指标数据列表
  53. type EdbInfoList struct {
  54. EdbInfoId int `orm:"column(edb_info_id);pk"`
  55. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  56. SourceName string `description:"来源名称"`
  57. Source int `description:"来源id"`
  58. EdbCode string `description:"指标编码"`
  59. EdbNameEn string `description:"英文指标名称"`
  60. EdbName string `description:"指标名称"`
  61. Frequency string `description:"频率"`
  62. FrequencyEn string `description:"英文频率"`
  63. Unit string `description:"单位"`
  64. UnitEn string `description:"英文单位"`
  65. StartDate string `description:"起始日期"`
  66. EndDate string `description:"终止日期"`
  67. LatestDate string `description:"数据最新日期"`
  68. LatestValue float64 `description:"数据最新值"`
  69. ClassifyId int `description:"分类id"`
  70. UniqueCode string `description:"指标唯一编码"`
  71. SysUserId int `description:"创建人id"`
  72. SysUserRealName string `description:"创建人姓名"`
  73. ModifyTime string `description:"最新修改时间"`
  74. CreateTime string `description:"创建时间"`
  75. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  76. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  77. ChartImage string `description:"图表图片"`
  78. RuleType int `description:"预测规则,1:最新,2:固定值"`
  79. FixedValue float64 `description:"固定值"`
  80. DataList []*EdbData `description:"实际指标数据"`
  81. PredictDataList []*EdbData `description:"预测指标数据"`
  82. Button EdbClassifyItemsButton `description:"操作权限"`
  83. IsEnEdb bool `description:"是否展示英文标识"`
  84. }
  85. // EdbClassifyItemsButton 操作按钮
  86. type EdbClassifyItemsButton struct {
  87. AddButton bool `description:"是否可添加"`
  88. OpButton bool `description:"是否可编辑"`
  89. DeleteButton bool `description:"是否可删除"`
  90. MoveButton bool `description:"是否可移动"`
  91. ShowEdbRelation bool `description:"是否展示关联指标"`
  92. ShowChartRelation bool `description:"是否展示关联图表"`
  93. }
  94. // GetEdbInfoByName 根据指标名称获取所有的指标数据列表
  95. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  96. o := orm.NewOrm()
  97. sql := ` SELECT * FROM edb_info WHERE edb_name=? `
  98. _, err = o.Raw(sql, edbName).QueryRows(&items)
  99. return
  100. }
  101. // ModifyEdbInfoNameSource 根据来源修改指标名称
  102. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  103. o := orm.NewOrm()
  104. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  105. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  106. return
  107. }
  108. // GetEdbInfoById 根据指标id获取指标信息
  109. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  110. o := orm.NewOrm()
  111. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  112. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  113. return
  114. }
  115. // GetEdbInfoByIdList 根据指标id列表获取指标信息
  116. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  117. num := len(edbInfoIdList)
  118. if num <= 0 {
  119. return
  120. }
  121. o := orm.NewOrm()
  122. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  123. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  124. return
  125. }
  126. // Update 更新EdbInfo信息
  127. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  128. o := orm.NewOrm()
  129. _, err = o.Update(edbInfo, cols...)
  130. return
  131. }
  132. // EdbInfoSearchData
  133. type EdbInfoSearchData struct {
  134. EdbDataId int `description:"数据ID"`
  135. DataTime string `description:"数据日期"`
  136. Value float64 `description:"数据"`
  137. }
  138. // GetEdbDataListAll 获取指标数据列表 order:1升序,其余值为降序
  139. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  140. o := orm.NewOrm()
  141. sql := ``
  142. tableName := GetEdbDataTableName(source)
  143. sql = ` SELECT * FROM %s WHERE 1=1 `
  144. sql = fmt.Sprintf(sql, tableName)
  145. if condition != "" {
  146. sql += condition
  147. }
  148. if order == 1 {
  149. sql += ` ORDER BY data_time ASC `
  150. } else {
  151. sql += ` ORDER BY data_time DESC `
  152. }
  153. _, err = o.Raw(sql, pars).QueryRows(&item)
  154. return
  155. }
  156. // GetEdbDataListAllByTo 根据事务链接获取指标数据列表 order:1升序,其余值为降序
  157. func GetEdbDataListAllByTo(to orm.TxOrmer, condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  158. sql := ``
  159. tableName := GetEdbDataTableName(source)
  160. sql = ` SELECT * FROM %s WHERE 1=1 `
  161. sql = fmt.Sprintf(sql, tableName)
  162. if condition != "" {
  163. sql += condition
  164. }
  165. if order == 1 {
  166. sql += ` ORDER BY data_time ASC `
  167. } else {
  168. sql += ` ORDER BY data_time DESC `
  169. }
  170. _, err = to.Raw(sql, pars).QueryRows(&item)
  171. return
  172. }
  173. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  174. type EdbInfoMaxAndMinInfo struct {
  175. MinDate string `description:"最小日期"`
  176. MaxDate string `description:"最大日期"`
  177. MinValue float64 `description:"最小值"`
  178. MaxValue float64 `description:"最大值"`
  179. LatestValue float64 `description:"最新值"`
  180. LatestDate string `description:"实际数据最新日期"`
  181. }
  182. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  183. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  184. o := orm.NewOrm()
  185. sql := ``
  186. tableName := GetEdbDataTableName(source)
  187. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
  188. sql = fmt.Sprintf(sql, tableName)
  189. err = o.Raw(sql, edbCode).QueryRow(&item)
  190. var latest_value float64
  191. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  192. sql = fmt.Sprintf(sql, tableName)
  193. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  194. item.LatestValue = latest_value
  195. return
  196. }
  197. // ModifyEdbInfoMaxAndMinInfo 修改指标的最新数据信息
  198. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  199. o := orm.NewOrm()
  200. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  201. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  202. return
  203. }
  204. // GetEdbDataCount 获取edb指标数据的数量; order:1升序,其余值为降序
  205. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  206. o := orm.NewOrm()
  207. sql := ``
  208. tableName := GetEdbDataTableName(source)
  209. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  210. sql = fmt.Sprintf(sql, tableName)
  211. if condition != "" {
  212. sql += condition
  213. }
  214. err = o.Raw(sql, pars).QueryRow(&count)
  215. return
  216. }
  217. // GetLastEdbData 获取最近的一条指标数据
  218. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  219. o := orm.NewOrm()
  220. sql := ``
  221. tableName := GetEdbDataTableName(source)
  222. sql = ` SELECT * FROM %s WHERE 1=1 `
  223. sql = fmt.Sprintf(sql, tableName)
  224. if condition != "" {
  225. sql += condition
  226. }
  227. sql += ` ORDER BY data_time DESC `
  228. err = o.Raw(sql, pars).QueryRow(&item)
  229. return
  230. }
  231. // GetEdbInfoByEdbCode 根据指标code获取指标信息
  232. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  233. o := orm.NewOrm()
  234. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  235. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  236. return
  237. }
  238. // GetEdbInfoOnlyByEdbCode 仅根据指标code获取指标信息
  239. func GetEdbInfoOnlyByEdbCode(edbCode string) (item *EdbInfo, err error) {
  240. o := orm.NewOrm()
  241. sql := ` SELECT * FROM edb_info WHERE edb_code=? `
  242. err = o.Raw(sql, edbCode).QueryRow(&item)
  243. return
  244. }
  245. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  246. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  247. o := orm.NewOrm()
  248. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  249. if condition != "" {
  250. sql += condition
  251. }
  252. _, err = o.Raw(sql, pars).QueryRows(&items)
  253. return
  254. }
  255. // GetEdbInfoCalculateCountByCondition 获取关联指标数量
  256. func GetEdbInfoCalculateCountByCondition(condition string, pars []interface{}) (count int, err error) {
  257. o := orm.NewOrm()
  258. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  259. if condition != "" {
  260. sql += condition
  261. }
  262. err = o.Raw(sql, pars).QueryRow(&count)
  263. return
  264. }
  265. // 优化版本-处理数据精度问题
  266. type EdbInfoSearchDataV1 struct {
  267. EdbDataId int `description:"数据ID"`
  268. DataTime string `description:"数据日期"`
  269. Value string `description:"数据"`
  270. }
  271. // 优化版本-处理数据精度问题
  272. func GetEdbDataListAllV1(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  273. o := orm.NewOrm()
  274. sql := ``
  275. tableName := GetEdbDataTableName(source)
  276. sql = ` SELECT * FROM %s WHERE 1=1 `
  277. sql = fmt.Sprintf(sql, tableName)
  278. if condition != "" {
  279. sql += condition
  280. }
  281. if order == 1 {
  282. sql += ` ORDER BY data_time ASC `
  283. } else {
  284. sql += ` ORDER BY data_time DESC `
  285. }
  286. _, err = o.Raw(sql, pars).QueryRows(&item)
  287. return
  288. }
  289. // GetEdbDataListAllV1ByTo 通过事务链接获取数据列表
  290. func GetEdbDataListAllV1ByTo(to orm.TxOrmer, condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  291. sql := ``
  292. tableName := GetEdbDataTableName(source)
  293. sql = ` SELECT * FROM %s WHERE 1=1 `
  294. sql = fmt.Sprintf(sql, tableName)
  295. if condition != "" {
  296. sql += condition
  297. }
  298. if order == 1 {
  299. sql += ` ORDER BY data_time ASC `
  300. } else {
  301. sql += ` ORDER BY data_time DESC `
  302. }
  303. _, err = to.Raw(sql, pars).QueryRows(&item)
  304. return
  305. }
  306. // GetEdbInfoByCondition 获取指标列表
  307. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfo, err error) {
  308. o := orm.NewOrm()
  309. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  310. if condition != "" {
  311. sql += condition
  312. }
  313. if order == 1 {
  314. sql += ` ORDER BY end_date ASC `
  315. } else {
  316. sql += ` ORDER BY edb_info_id ASC `
  317. }
  318. _, err = o.Raw(sql, pars).QueryRows(&item)
  319. return
  320. }
  321. // UnifiedModifyEdbInfoMaxAndMinInfo 统一修改指标的最大最小值
  322. func UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error, errMsg string) {
  323. // 修改最大最小值
  324. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.EdbCode)
  325. if err != nil {
  326. if err.Error() == utils.ErrNoRow() {
  327. err = nil
  328. return
  329. }
  330. errMsg = "刷新指标失败!"
  331. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  332. return
  333. }
  334. if maxAndMinItem != nil {
  335. err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  336. if err != nil {
  337. errMsg = "刷新指标失败!"
  338. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  339. return
  340. }
  341. }
  342. // 修改关联的预测指标
  343. go ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(edbInfo.EdbInfoId, maxAndMinItem)
  344. return
  345. }
  346. // UnifiedModifyPredictEdbInfoMaxAndMinInfo 统一修改预测运算指标的最大最小值
  347. func UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo *EdbInfo, latestDateStr string, latestValue float64) (err error, errMsg string) {
  348. // 修改最大最小值
  349. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.EdbCode)
  350. if err != nil {
  351. if err.Error() == utils.ErrNoRow() {
  352. err = nil
  353. return
  354. }
  355. errMsg = "刷新指标失败!"
  356. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  357. return
  358. }
  359. if maxAndMinItem != nil {
  360. maxAndMinItem.LatestDate = latestDateStr
  361. maxAndMinItem.LatestValue = latestValue
  362. err = ModifyPredictEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  363. if err != nil {
  364. errMsg = "刷新指标失败!"
  365. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  366. return
  367. }
  368. }
  369. return
  370. }
  371. // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据
  372. func GetChartPredictEdbInfoDataList(predictEdbConf PredictEdbConf, latestDateStr string, lastDataValue float64, endDateStr, frequency string, order int) (predictEdbInfoData []*EdbInfoSearchData, err error) {
  373. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  374. if err != nil {
  375. return
  376. }
  377. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  378. if err != nil {
  379. return
  380. }
  381. // 开始预测数据的时间
  382. startDate := latestDate
  383. dataValue := lastDataValue
  384. if predictEdbConf.RuleType == 2 {
  385. dataValue = predictEdbConf.FixedValue
  386. }
  387. //获取后面的预测数据
  388. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  389. predictEdbInfoData = make([]*EdbInfoSearchData, 0)
  390. // order:1升序,其余值为降序
  391. if order == 1 {
  392. for k, v := range dayList {
  393. predictEdbInfoData = append(predictEdbInfoData, &EdbInfoSearchData{
  394. EdbDataId: predictEdbConf.PredictEdbInfoId + 10000000000 + k,
  395. DataTime: v.Format(utils.FormatDate),
  396. Value: dataValue,
  397. })
  398. }
  399. } else {
  400. lenDayList := len(dayList)
  401. if lenDayList > 0 {
  402. for i := lenDayList - 1; i >= 0; i-- {
  403. v := dayList[i]
  404. predictEdbInfoData = append(predictEdbInfoData, &EdbInfoSearchData{
  405. EdbDataId: predictEdbConf.PredictEdbInfoId + 10000000000 + i,
  406. DataTime: v.Format(utils.FormatDate),
  407. Value: dataValue,
  408. })
  409. }
  410. }
  411. }
  412. return
  413. }
  414. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  415. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency string, realPredictEdbInfoData []*EdbInfoSearchData) (predictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
  416. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  417. if err != nil {
  418. return
  419. }
  420. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  421. if err != nil {
  422. return
  423. }
  424. // 开始预测数据的时间
  425. startDate := latestDate
  426. // 如果有筛选时间的话
  427. if filtrateStartDateStr != `` {
  428. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  429. if tmpErr != nil {
  430. err = tmpErr
  431. return
  432. }
  433. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  434. if filtrateStartDate.After(latestDate) {
  435. startDate = filtrateStartDate.AddDate(0, 0, -1)
  436. }
  437. }
  438. //var dateArr []string
  439. // 对应日期的值
  440. existMap := make(map[string]float64)
  441. for _, v := range realPredictEdbInfoData {
  442. //dateArr = append(dateArr, v.DataTime)
  443. existMap[v.DataTime] = v.Value
  444. }
  445. predictEdbInfoData = make([]*EdbInfoSearchData, 0)
  446. //dataValue := lastDataValue
  447. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  448. for _, predictEdbConf := range predictEdbConfList {
  449. dataEndTime := endDate
  450. if predictEdbConf.EndDate.Before(dataEndTime) {
  451. dataEndTime = predictEdbConf.EndDate
  452. }
  453. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  454. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency)
  455. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  456. continue
  457. }
  458. switch predictEdbConf.RuleType {
  459. case 1: //1:最新
  460. var lastDataValue float64 //最新值
  461. tmpAllData := make([]*EdbInfoSearchData, 0)
  462. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  463. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  464. lenTmpAllData := len(tmpAllData)
  465. if lenTmpAllData > 0 {
  466. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  467. }
  468. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  469. tmpMaxValue = lastDataValue
  470. tmpMinValue = lastDataValue
  471. case 2: //2:固定值
  472. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  473. if tmpErr != nil {
  474. err = tmpErr
  475. return
  476. }
  477. dataValue, _ := tmpValDecimal.Float64()
  478. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  479. tmpMaxValue = dataValue
  480. tmpMinValue = dataValue
  481. case 3: //3:同比
  482. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  483. if tmpErr != nil {
  484. err = tmpErr
  485. return
  486. }
  487. tbValue, _ := tmpValDecimal.Float64()
  488. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  489. case 4: //4:同差
  490. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  491. if tmpErr != nil {
  492. err = tmpErr
  493. return
  494. }
  495. tcValue, _ := tmpValDecimal.Float64()
  496. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  497. case 5: //5:环比
  498. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  499. if tmpErr != nil {
  500. err = tmpErr
  501. return
  502. }
  503. hbValue, _ := tmpValDecimal.Float64()
  504. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  505. case 6: //6:环差
  506. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  507. if tmpErr != nil {
  508. err = tmpErr
  509. return
  510. }
  511. hcValue, _ := tmpValDecimal.Float64()
  512. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  513. case 7: //7:N期移动均值
  514. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  515. if tmpErr != nil {
  516. err = tmpErr
  517. return
  518. }
  519. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  520. case 8: //8:N期段线性外推值
  521. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  522. if tmpErr != nil {
  523. err = tmpErr
  524. return
  525. }
  526. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  527. if err != nil {
  528. return
  529. }
  530. case 9: //9:动态环差”预测规则;
  531. hcDataMap := make(map[string]float64) //规则计算的环差值map
  532. if predictEdbConf.PredictEdbInfoId > 0 {
  533. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  534. if tmpErr != nil {
  535. err = tmpErr
  536. return
  537. }
  538. for _, v := range tmpPredictEdbRuleDataList {
  539. hcDataMap[v.DataTime] = v.Value
  540. }
  541. } else {
  542. if len(predictEdbConf.DataList) <= 0 {
  543. return
  544. }
  545. for _, v := range predictEdbConf.DataList {
  546. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  547. if tmpErr != nil {
  548. continue
  549. }
  550. // 只处理时间段内的数据
  551. if currentDate.Before(startDate) || currentDate.After(endDate) {
  552. continue
  553. }
  554. hcDataMap[v.DataTime] = v.Value
  555. }
  556. }
  557. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  558. case 10: //10:根据 给定终值后插值 规则获取预测数据
  559. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  560. if tmpErr != nil {
  561. err = tmpErr
  562. return
  563. }
  564. finalValue, _ := tmpValDecimal.Float64()
  565. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  566. case 11: //11:根据 季节性 规则获取预测数据
  567. var seasonConf SeasonConf
  568. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  569. if tmpErr != nil {
  570. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  571. return
  572. }
  573. calendar := "公历"
  574. if seasonConf.Calendar == "农历" {
  575. calendar = "农历"
  576. }
  577. yearList := make([]int, 0)
  578. //选择方式,1:连续N年;2:指定年份
  579. if seasonConf.YearType == 1 {
  580. if seasonConf.NValue < 1 {
  581. err = errors.New("连续N年不允许小于1")
  582. return
  583. }
  584. currYear := time.Now().Year()
  585. for i := 0; i < seasonConf.NValue; i++ {
  586. yearList = append(yearList, currYear-i-1)
  587. }
  588. } else {
  589. yearList = seasonConf.YearList
  590. }
  591. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  592. if err != nil {
  593. return
  594. }
  595. case 12: //12:根据 移动平均同比 规则获取预测数据
  596. var moveAverageConf MoveAverageConf
  597. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  598. if tmpErr != nil {
  599. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  600. return
  601. }
  602. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  603. if err != nil {
  604. return
  605. }
  606. case 13: //13:根据 同比增速差值 规则获取预测数据
  607. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  608. if tmpErr != nil {
  609. err = tmpErr
  610. return
  611. }
  612. tbEndValue, _ := tmpValDecimal.Float64()
  613. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  614. case 14: //14:根据 一元线性拟合 规则获取预测数据
  615. var ruleConf RuleLineNhConf
  616. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  617. if err != nil {
  618. err = errors.New("一元线性拟合规则配置信息异常:" + err.Error())
  619. return
  620. }
  621. // 规则计算的拟合残差值map
  622. newNhccDataMap := make(map[string]float64)
  623. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  624. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  625. if tmpErr != nil {
  626. err = tmpErr
  627. return
  628. }
  629. for _, v := range tmpPredictEdbRuleDataList {
  630. newNhccDataMap[v.DataTime] = v.Value
  631. }
  632. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  633. newNhccDataMap, err, _ = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  634. }
  635. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  636. if err != nil {
  637. return
  638. }
  639. }
  640. //startDate = dataEndTime.AddDate(0, 0, 1)
  641. if startDate.Before(dataEndTime) {
  642. startDate = dataEndTime
  643. }
  644. if tmpMinValue < minValue {
  645. minValue = tmpMinValue
  646. }
  647. if tmpMaxValue < maxValue {
  648. maxValue = tmpMaxValue
  649. }
  650. }
  651. return
  652. }
  653. // GetPredictEdbDayList 获取预测指标日期列表
  654. // GetPredictEdbDayList 获取预测指标日期列表
  655. func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
  656. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  657. switch frequency {
  658. case "日度":
  659. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  660. //周六、日排除
  661. if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
  662. continue
  663. }
  664. dayList = append(dayList, currDate)
  665. }
  666. case "周度":
  667. //nextDate := startDate.AddDate(0, 0, 7)
  668. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  669. dayList = append(dayList, currDate)
  670. }
  671. case "旬度":
  672. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  673. nextDate := currDate.AddDate(0, 0, 1)
  674. //每个月的10号、20号、最后一天,那么就写入
  675. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  676. dayList = append(dayList, currDate)
  677. }
  678. currDate = nextDate
  679. }
  680. case "月度":
  681. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  682. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  683. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  684. dayList = append(dayList, currDate)
  685. }
  686. currDate = currDate.AddDate(0, 0, 1)
  687. }
  688. case "季度":
  689. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  690. // 每月的最后一天
  691. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  692. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  693. // 季度日期就写入,否则不写入
  694. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  695. dayList = append(dayList, currDate)
  696. }
  697. }
  698. currDate = currDate.AddDate(0, 0, 1)
  699. }
  700. case "半年度":
  701. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  702. // 每月的最后一天
  703. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  704. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  705. // 半年度日期就写入,否则不写入
  706. if currDate.Month() == 6 || currDate.Month() == 12 {
  707. dayList = append(dayList, currDate)
  708. }
  709. }
  710. currDate = currDate.AddDate(0, 0, 1)
  711. }
  712. case "年度":
  713. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  714. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  715. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  716. dayList = append(dayList, currDate)
  717. }
  718. }
  719. }
  720. return
  721. }
  722. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  723. func GetPredictDataListByPredictEdbInfo(edbInfo *EdbInfo, order int, startDate string) (dataList []*EdbInfoSearchData, sourceEdbInfoItem *EdbInfo, err error, errMsg string) {
  724. // 查找该预测指标配置
  725. predictEdbConfList, err := GetPredictEdbConfAndDataListById(edbInfo.EdbInfoId)
  726. if err != nil && err.Error() != utils.ErrNoRow() {
  727. errMsg = "获取预测指标配置信息失败"
  728. return
  729. }
  730. if len(predictEdbConfList) == 0 {
  731. errMsg = "获取预测指标配置信息失败"
  732. err = errors.New(errMsg)
  733. return
  734. }
  735. predictEdbConf := predictEdbConfList[0]
  736. // 来源指标
  737. sourceEdbInfoItem, err = GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  738. if err != nil {
  739. if err.Error() == utils.ErrNoRow() {
  740. errMsg = "找不到来源指标信息"
  741. err = errors.New(errMsg)
  742. }
  743. return
  744. }
  745. dataList, err, errMsg = GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem, predictEdbConfList, order, startDate)
  746. return
  747. }
  748. // GetPredictDataListByPredictEdbConfList 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  749. func GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem *EdbInfo, predictEdbConfList []*PredictEdbConfAndData, order int, startDate string) (dataList []*EdbInfoSearchData, err error, errMsg string) {
  750. allDataList := make([]*EdbInfoSearchData, 0)
  751. //获取指标数据(实际已生成)
  752. var condition string
  753. var pars []interface{}
  754. condition += " AND edb_info_id=? "
  755. pars = append(pars, sourceEdbInfoItem.EdbInfoId)
  756. if startDate != "" {
  757. condition += " AND data_time>=? "
  758. pars = append(pars, startDate)
  759. }
  760. tmpDataList, err := GetEdbDataListAll(condition, pars, sourceEdbInfoItem.Source, 1)
  761. if err != nil {
  762. return
  763. }
  764. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  765. if startDate != `` {
  766. allDataList, err = GetEdbDataListAll(" AND edb_info_id=? ", []interface{}{sourceEdbInfoItem.EdbInfoId}, sourceEdbInfoItem.Source, 1)
  767. if err != nil {
  768. return
  769. }
  770. } else {
  771. allDataList = tmpDataList
  772. }
  773. // 获取预测指标未来的数据
  774. predictDataList := make([]*EdbInfoSearchData, 0)
  775. endDateStr := edbInfo.EndDate //预测指标的结束日期
  776. var predictMinValue, predictMaxValue float64
  777. // 如果有配置的预测规则,那么就进行预测
  778. if len(predictEdbConfList) > 0 {
  779. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, allDataList)
  780. if err != nil {
  781. return
  782. }
  783. }
  784. //order:1升序,其余值为降序
  785. if order == 1 {
  786. dataList = append(tmpDataList, predictDataList...)
  787. } else {
  788. // 先倒序预测数据
  789. lenPredictDataList := len(predictDataList)
  790. if lenPredictDataList > 0 {
  791. for k := range predictDataList {
  792. dataList = append(dataList, predictDataList[lenPredictDataList-k-1])
  793. }
  794. }
  795. // 接着倒序实际指标
  796. lenDataList := len(tmpDataList)
  797. for k := range tmpDataList {
  798. dataList = append(dataList, tmpDataList[lenDataList-k-1])
  799. }
  800. }
  801. if len(predictDataList) > 0 {
  802. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  803. if edbInfo.MinValue > predictMinValue {
  804. edbInfo.MinValue = predictMinValue
  805. }
  806. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  807. if edbInfo.MaxValue < predictMaxValue {
  808. edbInfo.MaxValue = predictMaxValue
  809. }
  810. }
  811. return
  812. }
  813. // GetPredictEdbDataListAll 获取该预测指标所有的数据 ,order:1升序,其余值为降序
  814. func GetPredictEdbDataListAll(edbInfo *EdbInfo, order int) (items []*EdbInfoSearchData, err error) {
  815. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  816. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, 1, "")
  817. } else {
  818. var condition string
  819. var pars []interface{}
  820. condition += " AND edb_info_id=? "
  821. pars = append(pars, edbInfo.EdbInfoId)
  822. items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, order)
  823. }
  824. return
  825. }
  826. // GetPredictEdbDataListAllByStartDate 根据开始日期获取该预测指标所有的数据 ,order:1升序,其余值为降序
  827. func GetPredictEdbDataListAllByStartDate(edbInfo *EdbInfo, order int, startDate string) (items []*EdbInfoSearchData, err error) {
  828. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  829. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, order, startDate)
  830. } else {
  831. var condition string
  832. var pars []interface{}
  833. condition += " AND edb_info_id=? "
  834. pars = append(pars, edbInfo.EdbInfoId)
  835. if startDate != "" {
  836. condition += " AND data_time>=? "
  837. pars = append(pars, startDate)
  838. }
  839. items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, order)
  840. }
  841. return
  842. }
  843. // ModifyPredictEdbInfoMaxAndMinInfo 修改预测指标的最新数据信息
  844. func ModifyPredictEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  845. o := orm.NewOrm()
  846. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  847. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.LatestDate, item.LatestValue, edbInfoId).Exec()
  848. return
  849. }
  850. // ModifyCalculateEdbInfo 修改计算指标信息
  851. func ModifyCalculateEdbInfo(edbName, frequency, unit, calculateFormula string, classifyId, edbInfoId int) (err error) {
  852. o := orm.NewOrm()
  853. sql := ` UPDATE edb_info
  854. SET
  855. edb_name =?,
  856. edb_name_source =?,
  857. frequency = ?,
  858. unit = ?,
  859. classify_id = ?,
  860. calculate_formula=?,
  861. modify_time = NOW()
  862. WHERE edb_info_id = ?`
  863. _, err = o.Raw(sql, edbName, edbName, frequency, unit, classifyId, calculateFormula, edbInfoId).Exec()
  864. return
  865. }
  866. func GetEdbInfoItemByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  867. o := orm.NewOrm()
  868. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  869. if condition != "" {
  870. sql += condition
  871. }
  872. err = o.Raw(sql, pars).QueryRow(&item)
  873. return
  874. }