edb_info.go 48 KB

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