edb_info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. _, err = o.Raw(sql, pars).QueryRows(&item)
  57. return
  58. }
  59. func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  60. o := orm.NewOrm()
  61. o.Using("data")
  62. sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  63. _, err = o.Raw(sql, maxDate, edbInfoId).Exec()
  64. return
  65. }
  66. type EdbInfoMaxAndMinInfo struct {
  67. MinDate string `description:"最小日期"`
  68. MaxDate string `description:"最大日期"`
  69. MinValue float64 `description:"最小值"`
  70. MaxValue float64 `description:"最大值"`
  71. }
  72. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  73. o := orm.NewOrm()
  74. o.Using("data")
  75. sql := ``
  76. tableName := ``
  77. if source == utils.DATA_SOURCE_THS {
  78. tableName = `edb_data_ths`
  79. } else if source == utils.DATA_SOURCE_WIND {
  80. tableName = `edb_data_wind`
  81. } else if source == utils.DATA_SOURCE_PB {
  82. tableName = `edb_data_pb`
  83. } else if source == utils.DATA_SOURCE_CALCULATE {
  84. tableName = "edb_data_calculate"
  85. } else {
  86. errors.New("无效的渠道:" + strconv.Itoa(source))
  87. return
  88. }
  89. 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=? `
  90. sql = fmt.Sprintf(sql, tableName)
  91. fmt.Println("sql:", sql)
  92. err = o.Raw(sql, edbCode).QueryRow(&item)
  93. return
  94. }
  95. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  96. o := orm.NewOrm()
  97. o.Using("data")
  98. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,modify_time=NOW() WHERE edb_info_id=? `
  99. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, edbInfoId).Exec()
  100. return
  101. }
  102. func GetEdbDataListAll(condition string, pars []interface{}, source 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. sql += ` ORDER BY data_time ASC `
  113. _, err = o.Raw(sql, pars).QueryRows(&item)
  114. return
  115. }
  116. func GetEdbDataTableName(source int) (tableName string) {
  117. if source == utils.DATA_SOURCE_THS {
  118. tableName = `edb_data_ths`
  119. } else if source == utils.DATA_SOURCE_WIND {
  120. tableName = `edb_data_wind`
  121. } else if source == utils.DATA_SOURCE_PB {
  122. tableName = `edb_data_pb`
  123. } else if source == utils.DATA_SOURCE_CALCULATE {
  124. tableName = `edb_data_calculate`
  125. } else {
  126. tableName = ""
  127. errors.New("无效的渠道:" + strconv.Itoa(source))
  128. return
  129. }
  130. return
  131. }
  132. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  133. o := orm.NewOrm()
  134. o.Using("data")
  135. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  136. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  137. return
  138. }