edb_info.go 40 KB

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