edb_info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/utils"
  6. "rdluck_tools/orm"
  7. "strconv"
  8. "time"
  9. )
  10. type EdbInfo struct {
  11. EdbInfoId int `orm:"column(edb_info_id);pk"`
  12. SourceName string `description:"来源名称"`
  13. Source int `description:"来源id"`
  14. EdbCode string `description:"指标编码"`
  15. EdbName string `description:"指标名称"`
  16. EdbNameSource string `description:"指标名称来源"`
  17. Frequency string `description:"频率"`
  18. Unit string `description:"单位"`
  19. StartDate string `description:"起始日期"`
  20. EndDate string `description:"终止日期"`
  21. ClassifyId int `description:"分类id"`
  22. SysUserId int
  23. SysUserRealName string
  24. UniqueCode string `description:"指标唯一编码"`
  25. CreateTime time.Time
  26. ModifyTime time.Time
  27. MinValue float64 `description:"指标最小值"`
  28. MaxValue float64 `description:"指标最大值"`
  29. CalculateFormula string `description:"计算公式"`
  30. }
  31. type EdbInfoList struct {
  32. EdbInfoId int `orm:"column(edb_info_id);pk"`
  33. SourceName string `description:"来源名称"`
  34. Source int `description:"来源id"`
  35. EdbCode string `description:"指标编码"`
  36. EdbName string `description:"指标名称"`
  37. Frequency string `description:"频率"`
  38. Unit string `description:"单位"`
  39. StartDate time.Time `description:"起始日期"`
  40. EndDate time.Time `description:"终止日期"`
  41. ClassifyId int `description:"分类id"`
  42. UniqueCode string `description:"指标唯一编码"`
  43. CalculateFormula string `description:"计算公式"`
  44. }
  45. type EdbInfoSearchData struct {
  46. DataTime string `description:"数据日期"`
  47. Value float64 `description:"数据"`
  48. }
  49. func GetEdbInfoByCondition(condition string, pars []interface{}) (item []*EdbInfoList, err error) {
  50. o := orm.NewOrm()
  51. o.Using("data")
  52. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  53. if condition != "" {
  54. sql += condition
  55. }
  56. sql += ` ORDER BY edb_info_id ASC `
  57. _, err = o.Raw(sql, pars).QueryRows(&item)
  58. return
  59. }
  60. func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  61. o := orm.NewOrm()
  62. o.Using("data")
  63. sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  64. _, err = o.Raw(sql, maxDate, edbInfoId).Exec()
  65. return
  66. }
  67. type EdbInfoMaxAndMinInfo struct {
  68. MinDate string `description:"最小日期"`
  69. MaxDate string `description:"最大日期"`
  70. MinValue float64 `description:"最小值"`
  71. MaxValue float64 `description:"最大值"`
  72. }
  73. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  74. o := orm.NewOrm()
  75. o.Using("data")
  76. sql := ``
  77. tableName := ``
  78. if source == utils.DATA_SOURCE_THS {
  79. tableName = `edb_data_ths`
  80. } else if source == utils.DATA_SOURCE_WIND {
  81. tableName = `edb_data_wind`
  82. } else if source == utils.DATA_SOURCE_PB {
  83. tableName = `edb_data_pb`
  84. } else {
  85. errors.New("无效的渠道:" + strconv.Itoa(source))
  86. return
  87. }
  88. 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=? `
  89. sql = fmt.Sprintf(sql, tableName)
  90. fmt.Println("sql:", sql)
  91. err = o.Raw(sql, edbCode).QueryRow(&item)
  92. return
  93. }
  94. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  95. o := orm.NewOrm()
  96. o.Using("data")
  97. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,modify_time=NOW() WHERE edb_info_id=? `
  98. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, edbInfoId).Exec()
  99. return
  100. }
  101. //order:1升序,其余值为降序
  102. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  103. o := orm.NewOrm()
  104. o.Using("data")
  105. sql := ``
  106. tableName := GetEdbDataTableName(source)
  107. sql = ` SELECT * FROM %s WHERE 1=1 `
  108. sql = fmt.Sprintf(sql, tableName)
  109. if condition != "" {
  110. sql += condition
  111. }
  112. if order == 1 {
  113. sql += ` ORDER BY data_time ASC `
  114. } else {
  115. sql += ` ORDER BY data_time DESC `
  116. }
  117. _, err = o.Raw(sql, pars).QueryRows(&item)
  118. return
  119. }
  120. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  121. o := orm.NewOrm()
  122. o.Using("data")
  123. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  124. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  125. return
  126. }
  127. func GetQuarterEdbInfo() (item []*EdbInfo, err error) {
  128. o := orm.NewOrm()
  129. o.Using("data")
  130. sql := ` SELECT c.* FROM chart_info AS a
  131. INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id
  132. INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id
  133. WHERE a.chart_type=2
  134. GROUP BY b.edb_info_id
  135. ORDER BY b.edb_info_id ASC `
  136. _, err = o.Raw(sql).QueryRows(&item)
  137. return
  138. }