query.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package sandbox
  2. import (
  3. "hongze/hongze_yb/global"
  4. )
  5. // GetBySandboxId 根据沙盘ID查询详情
  6. func GetBySandboxId(id uint32) (item *Sandbox, err error) {
  7. err = global.MYSQL["data"].Where("sandbox_id = ? and is_delete=0 ", id).Order("sandbox_id desc").First(&item).Error
  8. return
  9. }
  10. // GetPageListByWhere 分页获取活动列表
  11. func GetPageListByWhere(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*Sandbox, err error) {
  12. // 获取查询总数量
  13. sql := "select count(1) from sandbox AS s JOIN sandbox_classify AS c ON c.sandbox_classify_id = s.sandbox_classify_id WHERE " + condition + " order by s.modify_time desc,s.sandbox_id desc"
  14. err = global.MYSQL["data"].Raw(sql, pars...).Scan(&total).Error
  15. if err != nil {
  16. return
  17. }
  18. // 获取列表数据
  19. sql = "select s.* from sandbox AS s JOIN sandbox_classify AS c ON c.sandbox_classify_id = s.sandbox_classify_id WHERE " + condition + " order by s.modify_time desc,s.sandbox_id desc"
  20. sql += " limit ?,? "
  21. pars = append(pars, startSize, pageSize)
  22. err = global.MYSQL["data"].Raw(sql, pars...).Find(&list).Error
  23. return
  24. }
  25. // PermissionSandboxCount 品种下沙盘数
  26. type PermissionSandboxCount struct {
  27. ChartPermissionID uint32 `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0" json:"chart_permission_id"` // 品种id
  28. ChartPermissionName string `gorm:"index:idx_chart_permission_name;column:chart_permission_name;type:varchar(32);not null;default:''" json:"chart_permission_name"` // 品种名称(冗余字段,避免列表页查询时再去关联表查询)
  29. Total int64 `json:"total"` // 汇总数
  30. }
  31. // GetPermissionCountListByWhere 获取各品种的沙盘数
  32. func GetPermissionCountListByWhere(condition string, pars []interface{}) (list []*PermissionSandboxCount, err error) {
  33. //err = global.MYSQL["data"].Model(ResearchReport{}).Where("research_report_id IN (?)", ids).Scan(&list).Error
  34. // 获取列表数据
  35. err = global.MYSQL["data"].Model(Sandbox{}).Select("chart_permission_id,chart_permission_name,count(1) total").Where(condition, pars...).Order("modify_time desc,sandbox_id desc").Group("chart_permission_id").Scan(&list).Error
  36. return
  37. }