edb_collect.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. // EdbCollect 指标收藏
  11. type EdbCollect struct {
  12. EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
  13. EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
  14. EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
  15. EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
  16. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
  17. SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
  18. Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
  19. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  20. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  21. }
  22. func (m *EdbCollect) TableName() string {
  23. return "edb_collect"
  24. }
  25. type EdbCollectCols struct {
  26. PrimaryId string
  27. EdbCollectClassifyId string
  28. EdbInfoId string
  29. EdbCode string
  30. SysUserId string
  31. SysRealName string
  32. Sort string
  33. CreateTime string
  34. ModifyTime string
  35. }
  36. func (m *EdbCollect) Cols() EdbCollectCols {
  37. return EdbCollectCols{
  38. PrimaryId: "edb_collect_id",
  39. EdbCollectClassifyId: "edb_collect_classify_id",
  40. EdbInfoId: "edb_info_id",
  41. EdbCode: "edb_code",
  42. SysUserId: "sys_user_id",
  43. SysRealName: "sys_real_name",
  44. Sort: "sort",
  45. CreateTime: "create_time",
  46. ModifyTime: "modify_time",
  47. }
  48. }
  49. func (m *EdbCollect) Create() (err error) {
  50. err = global.DmSQL["data"].Create(m).Error
  51. return
  52. }
  53. func (m *EdbCollect) CreateMulti(items []*EdbCollect) (err error) {
  54. if len(items) == 0 {
  55. return
  56. }
  57. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  58. return
  59. }
  60. func (m *EdbCollect) Update(cols []string) (err error) {
  61. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  62. return
  63. }
  64. func (m *EdbCollect) Remove() (err error) {
  65. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  66. err = global.DmSQL["data"].Exec(sql, m.EdbCollectId).Error
  67. return
  68. }
  69. func (m *EdbCollect) MultiRemove(ids []int) (err error) {
  70. if len(ids) == 0 {
  71. return
  72. }
  73. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  74. err = global.DmSQL["data"].Exec(sql, ids).Error
  75. return
  76. }
  77. func (m *EdbCollect) RemoveByCondition(condition string, pars []interface{}) (err error) {
  78. if condition == "" {
  79. return
  80. }
  81. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  82. err = global.DmSQL["data"].Exec(sql, pars...).Error
  83. return
  84. }
  85. func (m *EdbCollect) GetItemById(id int) (item *EdbCollect, err error) {
  86. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  87. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  88. return
  89. }
  90. func (m *EdbCollect) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollect, err error) {
  91. order := ``
  92. if orderRule != "" {
  93. order = ` ORDER BY ` + orderRule
  94. }
  95. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  96. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  97. return
  98. }
  99. func (m *EdbCollect) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  100. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  101. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  102. return
  103. }
  104. func (m *EdbCollect) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollect, err error) {
  105. fields := strings.Join(fieldArr, ",")
  106. if len(fieldArr) == 0 {
  107. fields = `*`
  108. }
  109. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  110. if orderRule != "" {
  111. order = ` ORDER BY ` + orderRule
  112. }
  113. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  114. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  115. return
  116. }
  117. func (m *EdbCollect) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollect, err error) {
  118. fields := strings.Join(fieldArr, ",")
  119. if len(fieldArr) == 0 {
  120. fields = `*`
  121. }
  122. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  123. if orderRule != "" {
  124. order = ` ORDER BY ` + orderRule
  125. }
  126. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  127. pars = append(pars, startSize, pageSize)
  128. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  129. return
  130. }
  131. // GetCollectEdbInfoByClassifyId 获取分类下收藏的指标信息
  132. func GetCollectEdbInfoByClassifyId(classifyId int) (items []*EdbInfo, err error) {
  133. sql := `SELECT b.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE a.edb_collect_classify_id = ? ORDER BY a.sort ASC`
  134. err = global.DmSQL["data"].Raw(sql, classifyId).Find(&items).Error
  135. return
  136. }
  137. // EdbCollectReq 加入/取消收藏
  138. type EdbCollectReq struct {
  139. ClassifyId int `description:"分类ID"`
  140. EdbInfoId int `description:"指标ID"`
  141. }
  142. // GetCollectEdbInfoCount 获取收藏的指标信息总数
  143. func GetCollectEdbInfoCount(condition string, pars []interface{}) (total int64, err error) {
  144. sql := fmt.Sprintf(`SELECT COUNT(1) AS ct FROM (
  145. SELECT b.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  146. WHERE 1=1 %s
  147. ORDER BY a.create_time DESC
  148. ) AS sub`, condition)
  149. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&total).Error
  150. return
  151. }
  152. // GetCollectEdbInfoPageList 获取收藏的指标信息列表-分页
  153. func GetCollectEdbInfoPageList(condition string, pars []interface{}, startSize, pageSize int) (list []*CollectEdbInfoQuery, err error) {
  154. sql := fmt.Sprintf(`SELECT b.*, a.edb_collect_classify_id AS collect_classify_id FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  155. WHERE 1=1 %s
  156. ORDER BY a.create_time DESC LIMIT ?,?`, condition)
  157. pars = append(pars, startSize, pageSize)
  158. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  159. return
  160. }
  161. type CollectEdbInfoQuery struct {
  162. EdbInfo
  163. CollectClassifyId int `gorm:"column:collect_classify_id" description:"收藏分类ID"`
  164. }
  165. // CollectEdbInfoItem 收藏列表指标信息
  166. type CollectEdbInfoItem struct {
  167. EdbInfoId int `description:"指标ID"`
  168. EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
  169. EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
  170. Source int `description:"来源ID"`
  171. SourceName string `description:"来源名称"`
  172. EdbCode string `description:"指标编码"`
  173. EdbName string `description:"指标名称"`
  174. Frequency string `description:"频率"`
  175. Unit string `description:"单位"`
  176. UniqueCode string `description:"唯一编码"`
  177. ChartImage string `description:"图表图片"`
  178. ClassifyId int `description:"指标分类ID"`
  179. CollectClassifyId int `description:"收藏分类ID"`
  180. }
  181. func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
  182. item = new(CollectEdbInfoItem)
  183. item.EdbInfoId = origin.EdbInfoId
  184. item.EdbInfoType = origin.EdbInfoType
  185. item.EdbType = origin.EdbType
  186. item.Source = origin.Source
  187. item.SourceName = origin.SourceName
  188. item.EdbCode = origin.EdbCode
  189. item.EdbName = origin.EdbName
  190. item.Frequency = origin.Frequency
  191. item.Unit = origin.Unit
  192. item.UniqueCode = origin.UniqueCode
  193. item.ChartImage = origin.ChartImage
  194. item.ClassifyId = origin.ClassifyId
  195. item.CollectClassifyId = origin.CollectClassifyId
  196. return
  197. }
  198. // CollectEdbInfoListResp 收藏指标分页列表相应
  199. type CollectEdbInfoListResp struct {
  200. Paging *paging.PagingItem
  201. List []*CollectEdbInfoItem
  202. }