edb_data_base.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "time"
  9. )
  10. // 指标检索数据
  11. type EdbDataItem struct {
  12. EdbCode string `description:"指标编码"`
  13. StartDate string `description:"起始日期"`
  14. EndDate string `description:"终止日期"`
  15. EdbName string `description:"指标名称"`
  16. Unit string `description:"单位"`
  17. Frequency string `description:"频率"`
  18. DataList []*EdbData
  19. }
  20. type EdbData struct {
  21. EdbDataId int `orm:"column(edb_data_id);pk"`
  22. EdbInfoId int
  23. EdbCode string
  24. DataTime string
  25. Value string
  26. Status int
  27. CreateTime time.Time
  28. ModifyTime time.Time
  29. DataTimestamp int64
  30. }
  31. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  32. nowStr := time.Now().Format(utils.FormatDateTime)
  33. addSql += "("
  34. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  35. "," + "'" + nowStr + "'"
  36. addSql += "," + "'" + timestampStr + "'"
  37. addSql += "),"
  38. return
  39. }
  40. type AddEdbInfoReq struct {
  41. EdbCode string `description:"指标编码"`
  42. }
  43. // GetEdbInfoCountByCondition 获取指标数量
  44. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  45. o := orm.NewOrm()
  46. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  47. if condition != "" {
  48. sql += condition
  49. }
  50. err = o.Raw(sql, pars).QueryRow(&count)
  51. return
  52. }
  53. // GetEdbDataAllByEdbCode 根据指标编码获取指标数据列表
  54. func GetEdbDataAllByEdbCode(edbCode string, source, limit int) (items []*EdbInfoSearchData, err error) {
  55. var pars []interface{}
  56. pars = append(pars, edbCode)
  57. o := orm.NewOrm()
  58. tableName := GetEdbDataTableName(source)
  59. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  60. if limit > 0 {
  61. sql += ` LIMIT ? `
  62. pars = append(pars, limit)
  63. }
  64. sql = fmt.Sprintf(sql, tableName)
  65. _, err = o.Raw(sql, pars).QueryRows(&items)
  66. return
  67. }
  68. func GetEdbDataByCondition(source int, condition string, pars []interface{}) (items []*EdbInfoSearchData, err error) {
  69. o := orm.NewOrm()
  70. tableName := GetEdbDataTableName(source)
  71. sql := ` SELECT * FROM %s WHERE 1=1 `
  72. if condition != "" {
  73. sql += condition
  74. }
  75. sql += ` ORDER BY data_time DESC `
  76. sql = fmt.Sprintf(sql, tableName)
  77. _, err = o.Raw(sql, pars).QueryRows(&items)
  78. return
  79. }
  80. // GetEdbDataByDate 根据数据日期获取指标数据
  81. func GetEdbDataByDate(source int, edbCode, dataTime string) (item *EdbInfoSearchData, err error) {
  82. o := orm.NewOrm()
  83. tableName := GetEdbDataTableName(source)
  84. sql := ` SELECT * FROM %s WHERE 1=1 AND edb_code = ? AND data_time =? ORDER BY data_time DESC `
  85. sql = fmt.Sprintf(sql, tableName)
  86. err = o.Raw(sql, edbCode, dataTime).QueryRow(&item)
  87. return
  88. }
  89. func ModifyEdbDataById(source, edbDataId int, value string) (err error) {
  90. o := orm.NewOrm()
  91. tableName := GetEdbDataTableName(source)
  92. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_data_id=? `
  93. sql = fmt.Sprintf(sql, tableName)
  94. _, err = o.Raw(sql, value, edbDataId).Exec()
  95. return
  96. }
  97. func DeleteEdbDataById(source, edbDataId int) (err error) {
  98. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  99. tableName := GetEdbDataTableName(source)
  100. sql = fmt.Sprintf(sql, tableName)
  101. o := orm.NewOrm()
  102. _, err = o.Raw(sql, edbDataId).Exec()
  103. return
  104. }
  105. type RefreshEdbInfoReq struct {
  106. EdbInfoId int `description:"指标ID"`
  107. EdbCode string `description:"指标编码"`
  108. StartDate string `description:"开始日期"`
  109. }
  110. // GetAllEdbDataList 获取所有的指标数据列表
  111. func GetAllEdbDataList(edbInfoId, source int) (existDataList []*EdbData, err error) {
  112. o := orm.NewOrm()
  113. dataTableName := GetEdbDataTableName(source)
  114. fmt.Println("dataTableName:", dataTableName)
  115. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  116. sql = fmt.Sprintf(sql, dataTableName)
  117. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  118. return
  119. }
  120. // GetAllEdbDataListByTo 获取所有的指标数据列表
  121. func GetAllEdbDataListByTo(to orm.TxOrmer, edbInfoId, source int) (existDataList []*EdbData, err error) {
  122. dataTableName := GetEdbDataTableName(source)
  123. fmt.Println("dataTableName:", dataTableName)
  124. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  125. sql = fmt.Sprintf(sql, dataTableName)
  126. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  127. return
  128. }
  129. // GetFinalLastByTo 获取所有的指标数据列表
  130. func GetFinalLastByTo(to orm.TxOrmer, edbInfoId, source int, latestDate string) (finalLast EdbInfoSearchData, err error) {
  131. dataTableName := GetEdbDataTableName(source)
  132. sql := fmt.Sprintf(` SELECT data_time , value FROM %s WHERE edb_info_id=? and data_time<=? ORDER BY data_time DESC `, dataTableName)
  133. err = to.Raw(sql, edbInfoId, latestDate).QueryRow(&finalLast)
  134. if err != nil && err.Error() != utils.ErrNoRow() {
  135. return
  136. }
  137. return
  138. }
  139. // 新版本
  140. type EdbDataV1 struct {
  141. EdbDataId int `orm:"column(edb_data_id);pk"`
  142. EdbInfoId int
  143. EdbCode string
  144. DataTime string
  145. Value string
  146. Status int
  147. CreateTime time.Time
  148. ModifyTime time.Time
  149. DataTimestamp int64
  150. }
  151. type EdbDataList struct {
  152. EdbDataId int `description:" 指标数据ID"`
  153. EdbInfoId int `description:"指标ID"`
  154. DataTime string //`json:"-" description:"数据日期"`
  155. DataTimestamp int64 `description:"数据日期"`
  156. Value float64 `description:"数据值"`
  157. }
  158. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  159. tableName := GetEdbDataTableName(source)
  160. if tableName == "" {
  161. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  162. list = make([]*EdbDataList, 0)
  163. return list, err
  164. }
  165. var pars []interface{}
  166. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  167. if startDate != "" {
  168. sql += ` AND data_time>=? `
  169. pars = append(pars, startDate)
  170. }
  171. if endDate != "" {
  172. sql += ` AND data_time<=? `
  173. pars = append(pars, endDate)
  174. }
  175. sql += ` ORDER BY data_time ASC `
  176. sql = fmt.Sprintf(sql, tableName)
  177. o := orm.NewOrm()
  178. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  179. return
  180. }