edb_info.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type EdbInfo struct {
  8. EdbInfoId int `orm:"column(edb_info_id);pk"`
  9. SourceName string `description:"来源名称"`
  10. Source int `description:"来源id"`
  11. EdbCode string `description:"指标编码"`
  12. EdbName string `description:"指标名称"`
  13. EdbNameSource string `description:"指标名称来源"`
  14. Frequency string `description:"频率"`
  15. Unit string `description:"单位"`
  16. StartDate string `description:"起始日期"`
  17. EndDate string `description:"终止日期"`
  18. ClassifyId int `description:"分类id"`
  19. SysUserId int
  20. SysUserRealName string
  21. UniqueCode string `description:"指标唯一编码"`
  22. CreateTime time.Time
  23. ModifyTime time.Time
  24. MinValue float64 `description:"指标最小值"`
  25. MaxValue float64 `description:"指标最大值"`
  26. CalculateFormula string `description:"计算公式"`
  27. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  28. }
  29. type EdbInfoList struct {
  30. EdbInfoId int `orm:"column(edb_info_id);pk"`
  31. SourceName string `description:"来源名称"`
  32. Source int `description:"来源id"`
  33. EdbCode string `description:"指标编码"`
  34. EdbName string `description:"指标名称"`
  35. Frequency string `description:"频率"`
  36. Unit string `description:"单位"`
  37. StartDate time.Time `description:"起始日期"`
  38. EndDate time.Time `description:"终止日期"`
  39. ClassifyId int `description:"分类id"`
  40. UniqueCode string `description:"指标唯一编码"`
  41. CalculateFormula string `description:"计算公式"`
  42. ModifyTime string `description:"更新时间"`
  43. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  44. }
  45. type EdbInfoSearchData struct {
  46. EdbDataId int `description:"指标数据Id"`
  47. DataTime string `description:"数据日期"`
  48. Value float64 `description:"数据"`
  49. }
  50. type EdbInfoSearchDataV1 struct {
  51. DataTime string `description:"数据日期"`
  52. Value string `description:"数据"`
  53. }
  54. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  57. if condition != "" {
  58. sql += condition
  59. }
  60. if order == 1 {
  61. sql += ` ORDER BY end_date ASC `
  62. } else {
  63. sql += ` ORDER BY edb_info_id ASC `
  64. }
  65. _, err = o.Raw(sql, pars).QueryRows(&item)
  66. return
  67. }
  68. func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  69. o := orm.NewOrmUsingDB("data")
  70. sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  71. _, err = o.Raw(sql, maxDate, edbInfoId).Exec()
  72. return
  73. }
  74. type EdbInfoMaxAndMinInfo struct {
  75. MinDate string `description:"最小日期"`
  76. MaxDate string `description:"最大日期"`
  77. MinValue float64 `description:"最小值"`
  78. MaxValue float64 `description:"最大值"`
  79. LatestValue float64 `description:"最新值"`
  80. }
  81. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := ``
  84. tableName := GetEdbDataTableName(source)
  85. 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=? `
  86. sql = fmt.Sprintf(sql, tableName)
  87. err = o.Raw(sql, edbCode).QueryRow(&item)
  88. var latest_value float64
  89. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  90. sql = fmt.Sprintf(sql, tableName)
  91. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  92. item.LatestValue = latest_value
  93. return
  94. }
  95. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  96. o := orm.NewOrmUsingDB("data")
  97. 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=? `
  98. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, 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.NewOrmUsingDB("data")
  104. sql := ``
  105. tableName := GetEdbDataTableName(source)
  106. sql = ` SELECT * FROM %s WHERE 1=1 `
  107. sql = fmt.Sprintf(sql, tableName)
  108. if condition != "" {
  109. sql += condition
  110. }
  111. if order == 1 {
  112. sql += ` ORDER BY data_time ASC `
  113. } else {
  114. sql += ` ORDER BY data_time DESC `
  115. }
  116. _, err = o.Raw(sql, pars).QueryRows(&item)
  117. return
  118. }
  119. func GetEdbDataListAllV1(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. sql := ``
  122. tableName := GetEdbDataTableName(source)
  123. sql = ` SELECT * FROM %s WHERE 1=1 `
  124. sql = fmt.Sprintf(sql, tableName)
  125. if condition != "" {
  126. sql += condition
  127. }
  128. if order == 1 {
  129. sql += ` ORDER BY data_time ASC `
  130. } else {
  131. sql += ` ORDER BY data_time DESC `
  132. }
  133. _, err = o.Raw(sql, pars).QueryRows(&item)
  134. return
  135. }
  136. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  137. o := orm.NewOrmUsingDB("data")
  138. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  139. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  140. return
  141. }
  142. func GetQuarterEdbInfo() (item []*EdbInfo, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := ` SELECT c.* FROM chart_info AS a
  145. INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id
  146. INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id
  147. WHERE a.chart_type=2
  148. GROUP BY b.edb_info_id
  149. ORDER BY b.edb_info_id ASC `
  150. _, err = o.Raw(sql).QueryRows(&item)
  151. return
  152. }
  153. func ResetEdbInfoIsUpdate() (err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := ` UPDATE edb_info SET is_update=1 `
  156. _, err = o.Raw(sql).Exec()
  157. return
  158. }
  159. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  160. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  161. o := orm.NewOrmUsingDB("data")
  162. //calculateTableName := GetEdbInfoCalculateTableName(source)
  163. //if calculateTableName == "" {
  164. // err = errors.New("无效的表名")
  165. // return
  166. //}
  167. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  168. //sql = fmt.Sprintf(sql, calculateTableName)
  169. if condition != "" {
  170. sql += condition
  171. }
  172. _, err = o.Raw(sql, pars).QueryRows(&items)
  173. return
  174. }
  175. func DeleteEdbDataByIdAndSource(edbDataId, source int) (err error) {
  176. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  177. tableName := GetEdbDataTableName(source)
  178. sql = fmt.Sprintf(sql, tableName)
  179. o := orm.NewOrmUsingDB("data")
  180. _, err = o.Raw(sql, edbDataId).Exec()
  181. return
  182. }