edb_info.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/shopspring/decimal"
  9. "strconv"
  10. "time"
  11. )
  12. type EdbInfo struct {
  13. EdbInfoId int `orm:"column(edb_info_id);pk"`
  14. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  15. SourceName string `description:"来源名称"`
  16. Source int `description:"来源id"`
  17. EdbCode string `description:"指标编码"`
  18. EdbName string `description:"指标名称"`
  19. EdbNameEn string `description:"英文指标名称"`
  20. EdbNameSource string `description:"指标名称来源"`
  21. Frequency string `description:"频率"`
  22. Unit string `description:"单位"`
  23. UnitEn string `description:"英文单位"`
  24. StartDate string `description:"起始日期"`
  25. EndDate string `description:"终止日期"`
  26. ClassifyId int `description:"分类id"`
  27. SysUserId int
  28. SysUserRealName string
  29. UniqueCode string `description:"指标唯一编码"`
  30. CreateTime time.Time
  31. ModifyTime time.Time
  32. MinValue float64 `description:"指标最小值"`
  33. MaxValue float64 `description:"指标最大值"`
  34. CalculateFormula string `description:"计算公式"`
  35. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  36. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  37. Sort int `description:"排序字段"`
  38. LatestDate string `description:"数据最新日期"`
  39. LatestValue float64 `description:"数据最新值"`
  40. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  41. MoveFrequency string `description:"移动频度"`
  42. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  43. ServerUrl string `description:"服务器地址"`
  44. ChartImage string `description:"图表图片"`
  45. Calendar string `description:"公历/农历" orm:"default(公历);"`
  46. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  47. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  48. TerminalCode string `description:"终端编码,用于配置在机器上"`
  49. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  50. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  51. }
  52. func (e *EdbInfo) Add() (err error) {
  53. o := orm.NewOrm()
  54. _, err = o.Insert(e)
  55. return
  56. }
  57. // AddEdbInfo 添加指标
  58. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  59. o := orm.NewOrm()
  60. lastId, err = o.Insert(item)
  61. return
  62. }
  63. // EdbInfoList 指标数据列表
  64. type EdbInfoList struct {
  65. EdbInfoId int `orm:"column(edb_info_id);pk"`
  66. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  67. SourceName string `description:"来源名称"`
  68. Source int `description:"来源id"`
  69. EdbCode string `description:"指标编码"`
  70. EdbNameEn string `description:"英文指标名称"`
  71. EdbName string `description:"指标名称"`
  72. Frequency string `description:"频率"`
  73. FrequencyEn string `description:"英文频率"`
  74. Unit string `description:"单位"`
  75. UnitEn string `description:"英文单位"`
  76. StartDate string `description:"起始日期"`
  77. EndDate string `description:"终止日期"`
  78. LatestDate string `description:"数据最新日期"`
  79. LatestValue float64 `description:"数据最新值"`
  80. ClassifyId int `description:"分类id"`
  81. UniqueCode string `description:"指标唯一编码"`
  82. SysUserId int `description:"创建人id"`
  83. SysUserRealName string `description:"创建人姓名"`
  84. ModifyTime string `description:"最新修改时间"`
  85. CreateTime string `description:"创建时间"`
  86. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  87. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  88. ChartImage string `description:"图表图片"`
  89. RuleType int `description:"预测规则,1:最新,2:固定值"`
  90. FixedValue float64 `description:"固定值"`
  91. DataList []*EdbData `description:"实际指标数据"`
  92. PredictDataList []*EdbData `description:"预测指标数据"`
  93. Button EdbClassifyItemsButton `description:"操作权限"`
  94. IsEnEdb bool `description:"是否展示英文标识"`
  95. }
  96. // EdbClassifyItemsButton 操作按钮
  97. type EdbClassifyItemsButton struct {
  98. AddButton bool `description:"是否可添加"`
  99. OpButton bool `description:"是否可编辑"`
  100. DeleteButton bool `description:"是否可删除"`
  101. MoveButton bool `description:"是否可移动"`
  102. ShowEdbRelation bool `description:"是否展示关联指标"`
  103. ShowChartRelation bool `description:"是否展示关联图表"`
  104. }
  105. // GetEdbInfoByName 根据指标名称获取所有的指标数据列表
  106. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  107. o := orm.NewOrm()
  108. sql := ` SELECT * FROM edb_info WHERE edb_name=? `
  109. _, err = o.Raw(sql, edbName).QueryRows(&items)
  110. return
  111. }
  112. // ModifyEdbInfoNameSource 根据来源修改指标名称
  113. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  114. o := orm.NewOrm()
  115. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  116. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  117. return
  118. }
  119. // GetEdbInfoById 根据指标id获取指标信息
  120. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  121. o := orm.NewOrm()
  122. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  123. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  124. return
  125. }
  126. // GetEdbInfoByUniqueCode 根据指标唯一编码获取指标信息
  127. func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
  128. o := orm.NewOrm()
  129. sql := ` SELECT * FROM edb_info WHERE unique_code=? `
  130. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  131. return
  132. }
  133. // GetEdbInfoByIdList 根据指标id列表获取指标信息
  134. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  135. num := len(edbInfoIdList)
  136. if num <= 0 {
  137. return
  138. }
  139. o := orm.NewOrm()
  140. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  141. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  142. return
  143. }
  144. // Update 更新EdbInfo信息
  145. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  146. o := orm.NewOrm()
  147. _, err = o.Update(edbInfo, cols...)
  148. return
  149. }
  150. // EdbInfoSearchData
  151. type EdbInfoSearchData struct {
  152. EdbDataId int `description:"数据ID"`
  153. DataTime string `description:"数据日期"`
  154. Value float64 `description:"数据"`
  155. }
  156. // GetEdbDataListAll 获取指标数据列表 order:1升序,其余值为降序
  157. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  158. o := orm.NewOrm()
  159. sql := ``
  160. tableName := GetEdbDataTableName(source)
  161. sql = ` SELECT * FROM %s WHERE 1=1 `
  162. sql = fmt.Sprintf(sql, tableName)
  163. if condition != "" {
  164. sql += condition
  165. }
  166. if order == 1 {
  167. sql += ` ORDER BY data_time ASC `
  168. } else {
  169. sql += ` ORDER BY data_time DESC `
  170. }
  171. _, err = o.Raw(sql, pars).QueryRows(&item)
  172. return
  173. }
  174. // GetEdbDataListAllByTo 根据事务链接获取指标数据列表 order:1升序,其余值为降序
  175. func GetEdbDataListAllByTo(to orm.TxOrmer, condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  176. sql := ``
  177. tableName := GetEdbDataTableName(source)
  178. sql = ` SELECT * FROM %s WHERE 1=1 `
  179. sql = fmt.Sprintf(sql, tableName)
  180. if condition != "" {
  181. sql += condition
  182. }
  183. if order == 1 {
  184. sql += ` ORDER BY data_time ASC `
  185. } else {
  186. sql += ` ORDER BY data_time DESC `
  187. }
  188. _, err = to.Raw(sql, pars).QueryRows(&item)
  189. return
  190. }
  191. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  192. type EdbInfoMaxAndMinInfo struct {
  193. MinDate string `description:"最小日期"`
  194. MaxDate string `description:"最大日期"`
  195. MinValue float64 `description:"最小值"`
  196. MaxValue float64 `description:"最大值"`
  197. LatestValue float64 `description:"最新值"`
  198. LatestDate string `description:"实际数据最新日期"`
  199. }
  200. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  201. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  202. o := orm.NewOrm()
  203. sql := ``
  204. tableName := GetEdbDataTableName(source)
  205. 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=? `
  206. sql = fmt.Sprintf(sql, tableName)
  207. err = o.Raw(sql, edbCode).QueryRow(&item)
  208. var latest_value float64
  209. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  210. sql = fmt.Sprintf(sql, tableName)
  211. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  212. item.LatestValue = latest_value
  213. return
  214. }
  215. type EdbIndoDataUpdateTime struct {
  216. MinDate string `description:"本次更新,数据发生变化的最早日期"`
  217. }
  218. // GetEdbDataUpdateTimeByModify 获取指标的刷新时间获取最早的一条记录
  219. func GetEdbDataUpdateTimeByModify(source int, edbCode string, dataUpdateTime string) (item *EdbIndoDataUpdateTime, err error) {
  220. o := orm.NewOrm()
  221. sql := ``
  222. tableName := GetEdbDataTableName(source)
  223. sql = ` SELECT MIN(data_time) AS min_date FROM %s WHERE edb_code=? and modify_time>=?`
  224. sql = fmt.Sprintf(sql, tableName)
  225. err = o.Raw(sql, edbCode, dataUpdateTime).QueryRow(&item)
  226. return
  227. }
  228. // ModifyEdbInfoMaxAndMinInfo 修改指标的最新数据信息
  229. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  230. o := orm.NewOrm()
  231. 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=? `
  232. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  233. return
  234. }
  235. // ModifyEdbTimeAndLastInfo 修改指标的最新数据信息(除上下限)
  236. func ModifyEdbTimeAndLastInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  237. o := orm.NewOrm()
  238. sql := `UPDATE edb_info SET start_date = ?, end_date = ?, is_update = 2,latest_date = ?, latest_value = ?, modify_time = NOW() WHERE edb_info_id = ?`
  239. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  240. return
  241. }
  242. // ModifyEdbDataUpdateTime 修改指标刷新,本次数据刷新的最早日期
  243. func ModifyEdbDataUpdateTime(edbInfoId int, dataUpdateTime, erDataUpdateDate string) (err error) {
  244. o := orm.NewOrm()
  245. sql := `UPDATE edb_info SET data_update_time = ?, er_data_update_date = ?, modify_time = NOW() WHERE edb_info_id = ?`
  246. _, err = o.Raw(sql, dataUpdateTime, erDataUpdateDate, edbInfoId).Exec()
  247. return
  248. }
  249. // GetEdbDataCount 获取edb指标数据的数量; order:1升序,其余值为降序
  250. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  251. o := orm.NewOrm()
  252. sql := ``
  253. tableName := GetEdbDataTableName(source)
  254. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  255. sql = fmt.Sprintf(sql, tableName)
  256. if condition != "" {
  257. sql += condition
  258. }
  259. err = o.Raw(sql, pars).QueryRow(&count)
  260. return
  261. }
  262. // GetLastEdbData 获取最近的一条指标数据
  263. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  264. o := orm.NewOrm()
  265. sql := ``
  266. tableName := GetEdbDataTableName(source)
  267. sql = ` SELECT * FROM %s WHERE 1=1 `
  268. sql = fmt.Sprintf(sql, tableName)
  269. if condition != "" {
  270. sql += condition
  271. }
  272. sql += ` ORDER BY data_time DESC `
  273. err = o.Raw(sql, pars).QueryRow(&item)
  274. return
  275. }
  276. // GetEdbInfoByEdbCode 根据指标code获取指标信息
  277. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  278. o := orm.NewOrm()
  279. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  280. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  281. return
  282. }
  283. // GetEdbInfoOnlyByEdbCode 仅根据指标code获取指标信息
  284. func GetEdbInfoOnlyByEdbCode(edbCode string) (item *EdbInfo, err error) {
  285. o := orm.NewOrm()
  286. sql := ` SELECT * FROM edb_info WHERE edb_code=? `
  287. err = o.Raw(sql, edbCode).QueryRow(&item)
  288. return
  289. }
  290. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  291. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  292. o := orm.NewOrm()
  293. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  294. if condition != "" {
  295. sql += condition
  296. }
  297. _, err = o.Raw(sql, pars).QueryRows(&items)
  298. return
  299. }
  300. // GetEdbInfoCalculateCountByCondition 获取关联指标数量
  301. func GetEdbInfoCalculateCountByCondition(condition string, pars []interface{}) (count int, err error) {
  302. o := orm.NewOrm()
  303. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  304. if condition != "" {
  305. sql += condition
  306. }
  307. err = o.Raw(sql, pars).QueryRow(&count)
  308. return
  309. }
  310. // 优化版本-处理数据精度问题
  311. type EdbInfoSearchDataV1 struct {
  312. EdbDataId int `description:"数据ID"`
  313. DataTime string `description:"数据日期"`
  314. Value string `description:"数据"`
  315. }
  316. // 优化版本-处理数据精度问题
  317. func GetEdbDataListAllV1(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  318. o := orm.NewOrm()
  319. sql := ``
  320. tableName := GetEdbDataTableName(source)
  321. sql = ` SELECT * FROM %s WHERE 1=1 `
  322. sql = fmt.Sprintf(sql, tableName)
  323. if condition != "" {
  324. sql += condition
  325. }
  326. if order == 1 {
  327. sql += ` ORDER BY data_time ASC `
  328. } else {
  329. sql += ` ORDER BY data_time DESC `
  330. }
  331. _, err = o.Raw(sql, pars).QueryRows(&item)
  332. return
  333. }
  334. // GetEdbDataListAllV1ByTo 通过事务链接获取数据列表
  335. func GetEdbDataListAllV1ByTo(to orm.TxOrmer, condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  336. sql := ``
  337. tableName := GetEdbDataTableName(source)
  338. sql = ` SELECT * FROM %s WHERE 1=1 `
  339. sql = fmt.Sprintf(sql, tableName)
  340. if condition != "" {
  341. sql += condition
  342. }
  343. if order == 1 {
  344. sql += ` ORDER BY data_time ASC `
  345. } else {
  346. sql += ` ORDER BY data_time DESC `
  347. }
  348. _, err = to.Raw(sql, pars).QueryRows(&item)
  349. return
  350. }
  351. // GetEdbInfoByCondition 获取指标列表
  352. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfo, err error) {
  353. o := orm.NewOrm()
  354. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  355. if condition != "" {
  356. sql += condition
  357. }
  358. if order == 1 {
  359. sql += ` ORDER BY end_date ASC `
  360. } else {
  361. sql += ` ORDER BY edb_info_id ASC `
  362. }
  363. _, err = o.Raw(sql, pars).QueryRows(&item)
  364. return
  365. }
  366. // UnifiedModifyEdbInfoMaxAndMinInfo 统一修改指标的最大最小值
  367. func UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error, errMsg string) {
  368. // 修改最大最小值
  369. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.EdbCode)
  370. if err != nil {
  371. if err.Error() == utils.ErrNoRow() {
  372. err = nil
  373. return
  374. }
  375. errMsg = "刷新指标失败!"
  376. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  377. return
  378. }
  379. if maxAndMinItem != nil {
  380. // ETA1.0.3改-如果指标有手动保存过, 那么就不更新指标的最大最小值
  381. if edbInfo.ManualSave == 1 {
  382. err = ModifyEdbTimeAndLastInfo(edbInfo.EdbInfoId, maxAndMinItem)
  383. if err != nil {
  384. errMsg = "刷新指标失败!"
  385. err = errors.New("修改指标最新日期和数据失败,err:" + err.Error())
  386. return
  387. }
  388. } else {
  389. err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  390. if err != nil {
  391. errMsg = "刷新指标失败!"
  392. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  393. return
  394. }
  395. }
  396. }
  397. // 修改关联的预测指标
  398. go ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(edbInfo.EdbInfoId, maxAndMinItem)
  399. return
  400. }
  401. // UnifiedModifyEdbInfoMaxAndMinInfo 统一修改指标的最大最小值
  402. func UnifiedModifyEdbInfoMaxAndMinInfoTmp(edbInfo *EdbInfo, dataUpdateTime string) (err error, errMsg string) {
  403. // 修改最大最小值
  404. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.EdbCode)
  405. if err != nil {
  406. if err.Error() == utils.ErrNoRow() {
  407. err = nil
  408. return
  409. }
  410. errMsg = "刷新指标失败!"
  411. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  412. return
  413. }
  414. if maxAndMinItem != nil {
  415. // ETA1.0.3改-如果指标有手动保存过, 那么就不更新指标的最大最小值
  416. if edbInfo.ManualSave == 1 {
  417. err = ModifyEdbTimeAndLastInfo(edbInfo.EdbInfoId, maxAndMinItem)
  418. if err != nil {
  419. errMsg = "刷新指标失败!"
  420. err = errors.New("修改指标最新日期和数据失败,err:" + err.Error())
  421. return
  422. }
  423. } else {
  424. err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  425. if err != nil {
  426. errMsg = "刷新指标失败!"
  427. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  428. return
  429. }
  430. }
  431. }
  432. // 获取本次刷新,指标数据更新的最早日期
  433. erDataUpdateTime, tErr := GetEdbDataUpdateTimeByModify(edbInfo.Source, edbInfo.EdbCode, dataUpdateTime)
  434. if tErr != nil {
  435. if tErr.Error() != utils.ErrNoRow() { //本次刷新指标数据无变化
  436. errMsg = "刷新指标失败!"
  437. err = errors.New("查询指标刷新,数据更新的最早日期失败,err:" + tErr.Error())
  438. return
  439. }
  440. } else if erDataUpdateTime.MinDate != "" {
  441. err = ModifyEdbDataUpdateTime(edbInfo.EdbInfoId, dataUpdateTime, erDataUpdateTime.MinDate)
  442. if err != nil {
  443. errMsg = "刷新指标失败!"
  444. err = errors.New("修改指标数据更新的最早日期失败,err:" + tErr.Error())
  445. return
  446. }
  447. }
  448. // 修改关联的预测指标
  449. go ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(edbInfo.EdbInfoId, maxAndMinItem)
  450. return
  451. }
  452. // UnifiedModifyPredictEdbInfoMaxAndMinInfo 统一修改预测运算指标的最大最小值
  453. func UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo *EdbInfo, latestDateStr string, latestValue float64) (err error, errMsg string) {
  454. // 修改最大最小值
  455. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.EdbCode)
  456. if err != nil {
  457. if err.Error() == utils.ErrNoRow() {
  458. err = nil
  459. return
  460. }
  461. errMsg = "刷新指标失败!"
  462. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  463. return
  464. }
  465. if maxAndMinItem != nil {
  466. maxAndMinItem.LatestDate = latestDateStr
  467. maxAndMinItem.LatestValue = latestValue
  468. err = ModifyPredictEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  469. if err != nil {
  470. errMsg = "刷新指标失败!"
  471. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  472. return
  473. }
  474. }
  475. return
  476. }
  477. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  478. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*EdbInfoSearchData) (predictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
  479. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  480. if err != nil {
  481. return
  482. }
  483. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  484. if err != nil {
  485. return
  486. }
  487. // 开始预测数据的时间
  488. startDate := latestDate
  489. // 如果有筛选时间的话
  490. if filtrateStartDateStr != `` {
  491. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  492. if tmpErr != nil {
  493. err = tmpErr
  494. return
  495. }
  496. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  497. if filtrateStartDate.After(latestDate) {
  498. startDate = filtrateStartDate.AddDate(0, 0, -1)
  499. }
  500. }
  501. //var dateArr []string
  502. // 对应日期的值
  503. existMap := make(map[string]float64)
  504. for _, v := range realPredictEdbInfoData {
  505. //dateArr = append(dateArr, v.DataTime)
  506. existMap[v.DataTime] = v.Value
  507. }
  508. predictEdbInfoData = make([]*EdbInfoSearchData, 0)
  509. //dataValue := lastDataValue
  510. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  511. for _, predictEdbConf := range predictEdbConfList {
  512. dataEndTime := endDate
  513. if predictEdbConf.EndDate.Before(dataEndTime) {
  514. dataEndTime = predictEdbConf.EndDate
  515. }
  516. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  517. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  518. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  519. continue
  520. }
  521. switch predictEdbConf.RuleType {
  522. case 1: //1:最新
  523. var lastDataValue float64 //最新值
  524. tmpAllData := make([]*EdbInfoSearchData, 0)
  525. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  526. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  527. lenTmpAllData := len(tmpAllData)
  528. if lenTmpAllData > 0 {
  529. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  530. }
  531. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  532. tmpMaxValue = lastDataValue
  533. tmpMinValue = lastDataValue
  534. case 2: //2:固定值
  535. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  536. if tmpErr != nil {
  537. err = tmpErr
  538. return
  539. }
  540. dataValue, _ := tmpValDecimal.Float64()
  541. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  542. tmpMaxValue = dataValue
  543. tmpMinValue = dataValue
  544. case 3: //3:同比
  545. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  546. if tmpErr != nil {
  547. err = tmpErr
  548. return
  549. }
  550. tbValue, _ := tmpValDecimal.Float64()
  551. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  552. case 4: //4:同差
  553. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  554. if tmpErr != nil {
  555. err = tmpErr
  556. return
  557. }
  558. tcValue, _ := tmpValDecimal.Float64()
  559. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  560. case 5: //5:环比
  561. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  562. if tmpErr != nil {
  563. err = tmpErr
  564. return
  565. }
  566. hbValue, _ := tmpValDecimal.Float64()
  567. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  568. case 6: //6:环差
  569. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  570. if tmpErr != nil {
  571. err = tmpErr
  572. return
  573. }
  574. hcValue, _ := tmpValDecimal.Float64()
  575. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  576. case 7: //7:N期移动均值
  577. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  578. if tmpErr != nil {
  579. err = tmpErr
  580. return
  581. }
  582. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  583. case 8: //8:N期段线性外推值
  584. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  585. if tmpErr != nil {
  586. err = tmpErr
  587. return
  588. }
  589. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  590. if err != nil {
  591. return
  592. }
  593. case 9: //9:动态环差”预测规则;
  594. hcDataMap := make(map[string]float64) //规则计算的环差值map
  595. if predictEdbConf.PredictEdbInfoId > 0 {
  596. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  597. if tmpErr != nil {
  598. err = tmpErr
  599. return
  600. }
  601. for _, v := range tmpPredictEdbRuleDataList {
  602. hcDataMap[v.DataTime] = v.Value
  603. }
  604. } else {
  605. if len(predictEdbConf.DataList) <= 0 {
  606. return
  607. }
  608. for _, v := range predictEdbConf.DataList {
  609. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  610. if tmpErr != nil {
  611. continue
  612. }
  613. // 只处理时间段内的数据
  614. if currentDate.Before(startDate) || currentDate.After(endDate) {
  615. continue
  616. }
  617. hcDataMap[v.DataTime] = v.Value
  618. }
  619. }
  620. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  621. case 10: //10:根据 给定终值后插值 规则获取预测数据
  622. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  623. if tmpErr != nil {
  624. err = tmpErr
  625. return
  626. }
  627. finalValue, _ := tmpValDecimal.Float64()
  628. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  629. case 11: //11:根据 季节性 规则获取预测数据
  630. var seasonConf SeasonConf
  631. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  632. if tmpErr != nil {
  633. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  634. return
  635. }
  636. calendar := "公历"
  637. if seasonConf.Calendar == "农历" {
  638. calendar = "农历"
  639. }
  640. yearList := make([]int, 0)
  641. //选择方式,1:连续N年;2:指定年份
  642. if seasonConf.YearType == 1 {
  643. if seasonConf.NValue < 1 {
  644. err = errors.New("连续N年不允许小于1")
  645. return
  646. }
  647. currYear := time.Now().Year()
  648. for i := 0; i < seasonConf.NValue; i++ {
  649. yearList = append(yearList, currYear-i-1)
  650. }
  651. } else {
  652. yearList = seasonConf.YearList
  653. }
  654. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  655. if err != nil {
  656. return
  657. }
  658. case 12: //12:根据 移动平均同比 规则获取预测数据
  659. var moveAverageConf MoveAverageConf
  660. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  661. if tmpErr != nil {
  662. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  663. return
  664. }
  665. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  666. if err != nil {
  667. return
  668. }
  669. case 13: //13:根据 同比增速差值 规则获取预测数据
  670. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  671. if tmpErr != nil {
  672. err = tmpErr
  673. return
  674. }
  675. tbEndValue, _ := tmpValDecimal.Float64()
  676. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  677. case 14: //14:根据 一元线性拟合 规则获取预测数据
  678. var ruleConf RuleLineNhConf
  679. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  680. if err != nil {
  681. err = errors.New("一元线性拟合规则配置信息异常:" + err.Error())
  682. return
  683. }
  684. // 规则计算的拟合残差值map
  685. newNhccDataMap := make(map[string]float64)
  686. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  687. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  688. if tmpErr != nil {
  689. err = tmpErr
  690. return
  691. }
  692. for _, v := range tmpPredictEdbRuleDataList {
  693. newNhccDataMap[v.DataTime] = v.Value
  694. }
  695. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  696. newNhccDataMap, err, _ = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  697. }
  698. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  699. if err != nil {
  700. return
  701. }
  702. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  703. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  704. if err != nil {
  705. return
  706. }
  707. case 16: //16:年度值倒推
  708. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  709. if err != nil {
  710. return
  711. }
  712. }
  713. // 下一个规则的开始日期
  714. {
  715. lenPredictEdbInfoData := len(predictEdbInfoData)
  716. if lenPredictEdbInfoData > 0 {
  717. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  718. if startDate.Before(tmpDataEndTime) {
  719. startDate = tmpDataEndTime
  720. }
  721. }
  722. }
  723. if tmpMinValue < minValue {
  724. minValue = tmpMinValue
  725. }
  726. if tmpMaxValue < maxValue {
  727. maxValue = tmpMaxValue
  728. }
  729. }
  730. return
  731. }
  732. // GetPredictEdbDayList 获取预测指标日期列表
  733. // GetPredictEdbDayList 获取预测指标日期列表
  734. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  735. if dataDateType == `` {
  736. dataDateType = `交易日`
  737. }
  738. switch frequency {
  739. case "日度":
  740. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  741. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  742. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  743. continue
  744. }
  745. dayList = append(dayList, currDate)
  746. }
  747. case "周度":
  748. //nextDate := startDate.AddDate(0, 0, 7)
  749. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  750. dayList = append(dayList, currDate)
  751. }
  752. case "旬度":
  753. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  754. nextDate := currDate.AddDate(0, 0, 1)
  755. //每个月的10号、20号、最后一天,那么就写入
  756. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  757. dayList = append(dayList, currDate)
  758. }
  759. currDate = nextDate
  760. }
  761. case "月度":
  762. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  763. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  764. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  765. dayList = append(dayList, currDate)
  766. }
  767. currDate = currDate.AddDate(0, 0, 1)
  768. }
  769. case "季度":
  770. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  771. // 每月的最后一天
  772. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  773. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  774. // 季度日期就写入,否则不写入
  775. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  776. dayList = append(dayList, currDate)
  777. }
  778. }
  779. currDate = currDate.AddDate(0, 0, 1)
  780. }
  781. case "半年度":
  782. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  783. // 每月的最后一天
  784. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  785. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  786. // 半年度日期就写入,否则不写入
  787. if currDate.Month() == 6 || currDate.Month() == 12 {
  788. dayList = append(dayList, currDate)
  789. }
  790. }
  791. currDate = currDate.AddDate(0, 0, 1)
  792. }
  793. case "年度":
  794. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  795. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  796. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  797. dayList = append(dayList, currDate)
  798. }
  799. }
  800. }
  801. return
  802. }
  803. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  804. func GetPredictDataListByPredictEdbInfo(edbInfo *EdbInfo, order int, startDate string) (dataList []*EdbInfoSearchData, sourceEdbInfoItem *EdbInfo, err error, errMsg string) {
  805. // 查找该预测指标配置
  806. predictEdbConfList, err := GetPredictEdbConfAndDataListById(edbInfo.EdbInfoId)
  807. if err != nil && err.Error() != utils.ErrNoRow() {
  808. errMsg = "获取预测指标配置信息失败"
  809. return
  810. }
  811. if len(predictEdbConfList) == 0 {
  812. errMsg = "获取预测指标配置信息失败"
  813. err = errors.New(errMsg)
  814. return
  815. }
  816. predictEdbConf := predictEdbConfList[0]
  817. // 来源指标
  818. sourceEdbInfoItem, err = GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  819. if err != nil {
  820. if err.Error() == utils.ErrNoRow() {
  821. errMsg = "找不到来源指标信息"
  822. err = errors.New(errMsg)
  823. }
  824. return
  825. }
  826. dataList, err, errMsg = GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem, predictEdbConfList, order, startDate)
  827. return
  828. }
  829. // GetPredictDataListByPredictEdbConfList 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  830. func GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem *EdbInfo, predictEdbConfList []*PredictEdbConfAndData, order int, startDate string) (dataList []*EdbInfoSearchData, err error, errMsg string) {
  831. allDataList := make([]*EdbInfoSearchData, 0)
  832. //获取指标数据(实际已生成)
  833. var condition string
  834. var pars []interface{}
  835. condition += " AND edb_info_id=? "
  836. pars = append(pars, sourceEdbInfoItem.EdbInfoId)
  837. if startDate != "" {
  838. condition += " AND data_time>=? "
  839. pars = append(pars, startDate)
  840. }
  841. tmpDataList, err := GetEdbDataListAll(condition, pars, sourceEdbInfoItem.Source, 1)
  842. if err != nil {
  843. return
  844. }
  845. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  846. if startDate != `` {
  847. allDataList, err = GetEdbDataListAll(" AND edb_info_id=? ", []interface{}{sourceEdbInfoItem.EdbInfoId}, sourceEdbInfoItem.Source, 1)
  848. if err != nil {
  849. return
  850. }
  851. } else {
  852. allDataList = tmpDataList
  853. }
  854. // 获取预测指标未来的数据
  855. predictDataList := make([]*EdbInfoSearchData, 0)
  856. endDateStr := edbInfo.EndDate //预测指标的结束日期
  857. var predictMinValue, predictMaxValue float64
  858. // 如果有配置的预测规则,那么就进行预测
  859. if len(predictEdbConfList) > 0 {
  860. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  861. if err != nil {
  862. return
  863. }
  864. }
  865. //order:1升序,其余值为降序
  866. if order == 1 {
  867. dataList = append(tmpDataList, predictDataList...)
  868. } else {
  869. // 先倒序预测数据
  870. lenPredictDataList := len(predictDataList)
  871. if lenPredictDataList > 0 {
  872. for k := range predictDataList {
  873. dataList = append(dataList, predictDataList[lenPredictDataList-k-1])
  874. }
  875. }
  876. // 接着倒序实际指标
  877. lenDataList := len(tmpDataList)
  878. for k := range tmpDataList {
  879. dataList = append(dataList, tmpDataList[lenDataList-k-1])
  880. }
  881. }
  882. if len(predictDataList) > 0 {
  883. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  884. if edbInfo.MinValue > predictMinValue {
  885. edbInfo.MinValue = predictMinValue
  886. }
  887. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  888. if edbInfo.MaxValue < predictMaxValue {
  889. edbInfo.MaxValue = predictMaxValue
  890. }
  891. }
  892. return
  893. }
  894. // GetPredictEdbDataListAll 获取该预测指标所有的数据 ,order:1升序,其余值为降序
  895. func GetPredictEdbDataListAll(edbInfo *EdbInfo, order int) (items []*EdbInfoSearchData, err error) {
  896. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  897. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, 1, "")
  898. } else {
  899. var condition string
  900. var pars []interface{}
  901. condition += " AND edb_info_id=? "
  902. pars = append(pars, edbInfo.EdbInfoId)
  903. items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, order)
  904. }
  905. return
  906. }
  907. // GetPredictEdbDataListAllByStartDate 根据开始日期获取该预测指标所有的数据 ,order:1升序,其余值为降序
  908. func GetPredictEdbDataListAllByStartDate(edbInfo *EdbInfo, order int, startDate string) (items []*EdbInfoSearchData, err error) {
  909. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  910. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, order, startDate)
  911. } else {
  912. var condition string
  913. var pars []interface{}
  914. condition += " AND edb_info_id=? "
  915. pars = append(pars, edbInfo.EdbInfoId)
  916. if startDate != "" {
  917. condition += " AND data_time>=? "
  918. pars = append(pars, startDate)
  919. }
  920. items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, order)
  921. }
  922. return
  923. }
  924. // ModifyPredictEdbInfoMaxAndMinInfo 修改预测指标的最新数据信息
  925. func ModifyPredictEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  926. o := orm.NewOrm()
  927. 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=? `
  928. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.LatestDate, item.LatestValue, edbInfoId).Exec()
  929. return
  930. }
  931. // ModifyCalculateEdbInfo 修改计算指标信息
  932. func ModifyCalculateEdbInfo(edbName, frequency, unit, calculateFormula string, classifyId, edbInfoId int) (err error) {
  933. o := orm.NewOrm()
  934. sql := ` UPDATE edb_info
  935. SET
  936. edb_name =?,
  937. edb_name_source =?,
  938. frequency = ?,
  939. unit = ?,
  940. classify_id = ?,
  941. calculate_formula=?,
  942. modify_time = NOW()
  943. WHERE edb_info_id = ?`
  944. _, err = o.Raw(sql, edbName, edbName, frequency, unit, classifyId, calculateFormula, edbInfoId).Exec()
  945. return
  946. }
  947. func GetEdbInfoItemByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  948. o := orm.NewOrm()
  949. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  950. if condition != "" {
  951. sql += condition
  952. }
  953. err = o.Raw(sql, pars).QueryRow(&item)
  954. return
  955. }
  956. func GetEdbInfoItemListByCondition() (items []*EdbInfoList, err error) {
  957. o := orm.NewOrm()
  958. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  959. _, err = o.Raw(sql).QueryRows(&items)
  960. return
  961. }
  962. // EdbInfoDetailReq 指标详情请求
  963. type EdbInfoDetailReq struct {
  964. UniqueCode string `description:"指标唯一编码"`
  965. EdbCode string `description:"指标编码"`
  966. }
  967. // EdbInfoDetailResp 指标详情返回
  968. type EdbInfoDetailResp struct {
  969. SourceName string `description:"来源名称"`
  970. EdbCode string `description:"指标编码"`
  971. EdbName string `description:"指标名称"`
  972. EdbNameSource string `description:"指标名称来源"`
  973. Frequency string `description:"频率"`
  974. Unit string `description:"单位"`
  975. StartDate string `description:"起始日期"`
  976. EndDate string `description:"终止日期"`
  977. UniqueCode string `description:"指标唯一编码"`
  978. CreateTime time.Time
  979. ModifyTime time.Time
  980. MinValue float64 `description:"指标最小值"`
  981. MaxValue float64 `description:"指标最大值"`
  982. EdbNameEn string `description:"英文指标名称"`
  983. UnitEn string `description:"英文单位"`
  984. LatestDate string `description:"数据最新日期"`
  985. LatestValue float64 `description:"数据最新值"`
  986. ChartImage string `description:"图表图片"`
  987. DataList []*EdbInfoSearchData
  988. }
  989. type AddEdbInfoParams struct {
  990. Source int `description:"来源id"`
  991. EdbCode string `description:"指标编码"`
  992. EdbName string `description:"指标名称"`
  993. Frequency string `description:"频率"`
  994. Unit string `description:"单位"`
  995. ClassifyId int `description:"分类id"`
  996. }
  997. // EdbInfoAdd 添加指标到指标库
  998. func EdbInfoAdd(req *AddEdbInfoParams, serverUrl string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
  999. source := req.Source
  1000. //判断指标名称是否存在
  1001. var condition string
  1002. var pars []interface{}
  1003. //condition += " AND source=? "
  1004. //pars = append(pars, source)
  1005. condition += " AND edb_code=? "
  1006. pars = append(pars, req.EdbCode)
  1007. items, err := GetEdbInfoByCondition(condition, pars, 0)
  1008. if err != nil {
  1009. err = errors.New("判断指标是否存在失败,Err:" + err.Error())
  1010. return
  1011. }
  1012. if len(items) > 0 {
  1013. sql := ` UPDATE edb_info SET classify_id=?,modify_time=NOW() WHERE edb_code=? `
  1014. o := orm.NewOrm()
  1015. _, err = o.Raw(sql, req.ClassifyId, req.EdbCode).Exec()
  1016. if err != nil {
  1017. err = errors.New("更新指标分类信息失败,Err:" + err.Error())
  1018. return
  1019. }
  1020. edbInfo = items[0]
  1021. return
  1022. }
  1023. edbInfo = new(EdbInfo)
  1024. edbInfo.Source = source
  1025. sourceNameMap := map[int]string{
  1026. utils.DATA_SOURCE_THS: "同花顺",
  1027. utils.DATA_SOURCE_WIND: "wind",
  1028. utils.DATA_SOURCE_PB: "彭博",
  1029. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  1030. utils.DATA_SOURCE_MANUAL: "手工数据",
  1031. utils.DATA_SOURCE_LZ: "隆众",
  1032. utils.DATA_SOURCE_YS: "SMM",
  1033. utils.DATA_SOURCE_GL: "钢联",
  1034. utils.DATA_SOURCE_ZZ: "郑商所",
  1035. utils.DATA_SOURCE_DL: "大商所",
  1036. utils.DATA_SOURCE_SH: "上期所",
  1037. utils.DATA_SOURCE_CFFEX: "中金所",
  1038. utils.DATA_SOURCE_SHFE: "上期能源",
  1039. utils.DATA_SOURCE_GIE: "欧洲天然气",
  1040. utils.DATA_SOURCE_LT: "路透",
  1041. utils.DATA_SOURCE_COAL: "中国煤炭网",
  1042. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  1043. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联化工",
  1044. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  1045. utils.DATA_SOURCE_COM_TRADE: "UN",
  1046. utils.DATA_SOURCE_SCI: "SCI",
  1047. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  1048. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  1049. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  1050. utils.DATA_SOURCE_FUBAO: "富宝数据",
  1051. }
  1052. sourceName, ok := sourceNameMap[source]
  1053. if !ok {
  1054. err = errors.New("指标来源异常")
  1055. return
  1056. }
  1057. edbInfo.SourceName = sourceName
  1058. edbType := 1 //基础指标
  1059. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1060. edbType = 2 //计算指标
  1061. }
  1062. // 钢联化工需要校验下信息是否完整
  1063. if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  1064. indexObj := new(BaseFromMysteelChemicalIndex)
  1065. tmpItem, tmpErr := indexObj.GetIndexItem(req.EdbCode)
  1066. if tmpErr != nil {
  1067. err = tmpErr
  1068. return
  1069. }
  1070. if tmpItem == nil || tmpItem.IndexName == `` {
  1071. err = errors.New("指标信息不全")
  1072. return
  1073. }
  1074. }
  1075. edbInfo.EdbCode = req.EdbCode
  1076. edbInfo.EdbName = req.EdbName
  1077. edbInfo.EdbNameSource = req.EdbName
  1078. edbInfo.Frequency = req.Frequency
  1079. edbInfo.Unit = req.Unit
  1080. edbInfo.ClassifyId = req.ClassifyId
  1081. edbInfo.SysUserId = sysUserId
  1082. edbInfo.SysUserRealName = sysUserRealName
  1083. edbInfo.StartDate = "1990-01-01"
  1084. edbInfo.CreateTime = time.Now()
  1085. edbInfo.ModifyTime = time.Now()
  1086. edbInfo.ServerUrl = serverUrl
  1087. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1088. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + req.EdbCode + timestamp)
  1089. itemVal, err := GetEdbInfoMaxAndMinInfo(source, req.EdbCode)
  1090. if itemVal != nil && err == nil {
  1091. edbInfo.MaxValue = itemVal.MaxValue
  1092. edbInfo.MinValue = itemVal.MinValue
  1093. }
  1094. edbInfo.EdbType = edbType
  1095. edbInfoId, err := AddEdbInfo(edbInfo)
  1096. if err != nil {
  1097. err = errors.New("保存失败,Err:" + err.Error())
  1098. return
  1099. }
  1100. edbInfo.EdbInfoId = int(edbInfoId)
  1101. return
  1102. }
  1103. // EdbInfoRefreshCheckReq 指标数据更新情况查询
  1104. type EdbInfoRefreshCheckReq struct {
  1105. Source int `description:"来源id"`
  1106. LatestDate string `description:"数据最新日期"`
  1107. FrequencyBatch string `description:"更新频度"`
  1108. }
  1109. // EdbInfoRefreshCheckResp 指标数据更新情况查询
  1110. type EdbInfoRefreshCheckResp struct {
  1111. UpdateNum int `description:"已更新指标数"`
  1112. UnUpdateNum int `description:"未更新指标数"`
  1113. }
  1114. // ResetEdbClassifyReq 指标重新分类请求体
  1115. type ResetEdbClassifyReq struct {
  1116. IndexCode string `description:"指标编码"`
  1117. ClassifyFirst string `description:"一级分类名称"`
  1118. ClassifySecond string `description:"二级分类名称"`
  1119. ClassifyThird string `description:"三级分类名称"`
  1120. }