edb_info.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package data_manage
  2. import (
  3. "fmt"
  4. "rdluck_tools/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. }
  28. type EdbInfoList struct {
  29. EdbInfoId int `orm:"column(edb_info_id);pk"`
  30. SourceName string `description:"来源名称"`
  31. Source int `description:"来源id"`
  32. EdbCode string `description:"指标编码"`
  33. EdbName string `description:"指标名称"`
  34. Frequency string `description:"频率"`
  35. Unit string `description:"单位"`
  36. StartDate time.Time `description:"起始日期"`
  37. EndDate time.Time `description:"终止日期"`
  38. ClassifyId int `description:"分类id"`
  39. UniqueCode string `description:"指标唯一编码"`
  40. CalculateFormula string `description:"计算公式"`
  41. ModifyTime string `description:"更新时间"`
  42. }
  43. type EdbInfoSearchData struct {
  44. DataTime string `description:"数据日期"`
  45. Value float64 `description:"数据"`
  46. }
  47. type EdbInfoSearchDataV1 struct {
  48. DataTime string `description:"数据日期"`
  49. Value string `description:"数据"`
  50. }
  51. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) {
  52. o := orm.NewOrm()
  53. o.Using("data")
  54. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  55. if condition != "" {
  56. sql += condition
  57. }
  58. if order == 1 {
  59. sql += ` ORDER BY end_date ASC `
  60. } else {
  61. sql += ` ORDER BY edb_info_id ASC `
  62. }
  63. _, err = o.Raw(sql, pars).QueryRows(&item)
  64. return
  65. }
  66. func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  67. o := orm.NewOrm()
  68. o.Using("data")
  69. sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  70. _, err = o.Raw(sql, maxDate, edbInfoId).Exec()
  71. return
  72. }
  73. type EdbInfoMaxAndMinInfo struct {
  74. MinDate string `description:"最小日期"`
  75. MaxDate string `description:"最大日期"`
  76. MinValue float64 `description:"最小值"`
  77. MaxValue float64 `description:"最大值"`
  78. LatestValue float64 `description:"最新值"`
  79. }
  80. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  81. o := orm.NewOrm()
  82. o.Using("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.NewOrm()
  97. o.Using("data")
  98. 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=? `
  99. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  100. return
  101. }
  102. //order:1升序,其余值为降序
  103. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  104. o := orm.NewOrm()
  105. o.Using("data")
  106. sql := ``
  107. tableName := GetEdbDataTableName(source)
  108. sql = ` SELECT * FROM %s WHERE 1=1 `
  109. sql = fmt.Sprintf(sql, tableName)
  110. if condition != "" {
  111. sql += condition
  112. }
  113. if order == 1 {
  114. sql += ` ORDER BY data_time ASC `
  115. } else {
  116. sql += ` ORDER BY data_time DESC `
  117. }
  118. _, err = o.Raw(sql, pars).QueryRows(&item)
  119. return
  120. }
  121. func GetEdbDataListAllV1(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) {
  122. o := orm.NewOrm()
  123. o.Using("data")
  124. sql := ``
  125. tableName := GetEdbDataTableName(source)
  126. sql = ` SELECT * FROM %s WHERE 1=1 `
  127. sql = fmt.Sprintf(sql, tableName)
  128. if condition != "" {
  129. sql += condition
  130. }
  131. if order == 1 {
  132. sql += ` ORDER BY data_time ASC `
  133. } else {
  134. sql += ` ORDER BY data_time DESC `
  135. }
  136. _, err = o.Raw(sql, pars).QueryRows(&item)
  137. return
  138. }
  139. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  140. o := orm.NewOrm()
  141. o.Using("data")
  142. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  143. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  144. return
  145. }
  146. func GetQuarterEdbInfo() (item []*EdbInfo, err error) {
  147. o := orm.NewOrm()
  148. o.Using("data")
  149. sql := ` SELECT c.* FROM chart_info AS a
  150. INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id
  151. INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id
  152. WHERE a.chart_type=2
  153. GROUP BY b.edb_info_id
  154. ORDER BY b.edb_info_id ASC `
  155. _, err = o.Raw(sql).QueryRows(&item)
  156. return
  157. }
  158. func ResetEdbInfoIsUpdate() (err error) {
  159. o := orm.NewOrm()
  160. o.Using("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.NewOrm()
  168. o.Using("data")
  169. //calculateTableName := GetEdbInfoCalculateTableName(source)
  170. //if calculateTableName == "" {
  171. // err = errors.New("无效的表名")
  172. // return
  173. //}
  174. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  175. //sql = fmt.Sprintf(sql, calculateTableName)
  176. if condition != "" {
  177. sql += condition
  178. }
  179. _, err = o.Raw(sql, pars).QueryRows(&items)
  180. return
  181. }