edb_info.go 6.9 KB

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