edb_info.go 46 KB

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