edb_data_base.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hongze_edb_lib/utils"
  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. // 新版本
  130. type EdbDataV1 struct {
  131. EdbDataId int `orm:"column(edb_data_id);pk"`
  132. EdbInfoId int
  133. EdbCode string
  134. DataTime string
  135. Value string
  136. Status int
  137. CreateTime time.Time
  138. ModifyTime time.Time
  139. DataTimestamp int64
  140. }
  141. type EdbDataList struct {
  142. EdbDataId int `description:" 指标数据ID"`
  143. EdbInfoId int `description:"指标ID"`
  144. DataTime string //`json:"-" description:"数据日期"`
  145. DataTimestamp int64 `description:"数据日期"`
  146. Value float64 `description:"数据值"`
  147. }
  148. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  149. tableName := GetEdbDataTableName(source)
  150. if tableName == "" {
  151. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  152. list = make([]*EdbDataList, 0)
  153. return list, err
  154. }
  155. var pars []interface{}
  156. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  157. if startDate != "" {
  158. sql += ` AND data_time>=? `
  159. pars = append(pars, startDate)
  160. }
  161. if endDate != "" {
  162. sql += ` AND data_time<=? `
  163. pars = append(pars, endDate)
  164. }
  165. sql += ` ORDER BY data_time ASC `
  166. sql = fmt.Sprintf(sql, tableName)
  167. o := orm.NewOrm()
  168. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  169. return
  170. }