package models import ( "eta_gn/eta_report/global" "eta_gn/eta_report/utils" "fmt" "strings" "time" ) // ReportGrant 报告授权表 type ReportGrant struct { GrantId int `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"` // 授权id ReportId int `gorm:"column:report_id;type:int(9) unsigned;not null;default:0"` // 报告id AdminId int `gorm:"column:admin_id;type:int(9) unsigned;default:0"` // 授权的用户id CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 授权时间 } func (m *ReportGrant) TableName() string { return "report_grant" } type ReportGrantCols struct { PrimaryId string ReportId string AdminId string CreateTime string } func (m *ReportGrant) Cols() ReportGrantCols { return ReportGrantCols{ PrimaryId: "grant_id", ReportId: "report_id", AdminId: "admin_id", CreateTime: "create_time", } } func (m *ReportGrant) Create() (err error) { err = global.DEFAULT_DmSQL.Create(m).Error return } func (m *ReportGrant) CreateMulti(items []*ReportGrant) (err error) { if len(items) == 0 { return } err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error return } func (m *ReportGrant) Update(cols []string) (err error) { err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error return } func (m *ReportGrant) Remove() (err error) { sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DEFAULT_DmSQL.Exec(sql, m.GrantId).Error return } func (m *ReportGrant) MultiRemove(ids []int) (err error) { if len(ids) == 0 { return } sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids))) err = global.DEFAULT_DmSQL.Exec(sql, ids).Error return } func (m *ReportGrant) RemoveByCondition(condition string, pars []interface{}) (err error) { if condition == "" { return } sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition) err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error return } func (m *ReportGrant) GetItemById(id int) (item *ReportGrant, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error return } func (m *ReportGrant) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportGrant, err error) { order := `` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error return } func (m *ReportGrant) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error return } func (m *ReportGrant) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportGrant, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime) if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error return }