edb_info.go 43 KB

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