edb_info.go 40 KB

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