package models import ( "eta_gn/eta_report/global" "eta_gn/eta_report/utils" "fmt" "strings" "time" ) // ReportChapterGrant 报告授权表 type ReportChapterGrant struct { GrantId int `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"` // 授权id ReportChapterId int `gorm:"column:report_chapter_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 *ReportChapterGrant) TableName() string { return "report_chapter_grant" } type ReportChapterGrantCols struct { PrimaryId string ReportChapterId string AdminId string CreateTime string } func (m *ReportChapterGrant) Cols() ReportChapterGrantCols { return ReportChapterGrantCols{ PrimaryId: "grant_id", ReportChapterId: "report_chapter_id", AdminId: "admin_id", CreateTime: "create_time", } } func (m *ReportChapterGrant) Create() (err error) { err = global.DEFAULT_DmSQL.Create(m).Error return } func (m *ReportChapterGrant) CreateMulti(items []*ReportChapterGrant) (err error) { if len(items) == 0 { return } err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error return } func (m *ReportChapterGrant) Update(cols []string) (err error) { err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error return } func (m *ReportChapterGrant) 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 *ReportChapterGrant) 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 *ReportChapterGrant) 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 *ReportChapterGrant) GetItemById(id int) (item *ReportChapterGrant, 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 *ReportChapterGrant) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportChapterGrant, 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 *ReportChapterGrant) 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 *ReportChapterGrant) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportChapterGrant, 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 }