package models import ( "eta_gn/eta_task/global" "eta_gn/eta_task/utils" "fmt" "strings" "time" ) const ( OaReportMessageSourceRemind = 1 OaReportMessageStatusSuccess = 1 OaReportMessageStatusFail = 2 ) // OaReportMessage OA报告消息 type OaReportMessage struct { Id int `gorm:"primaryKey;autoIncrement;column:id"` ReceiveUserId int `gorm:"column:receive_user_id" description:"接收人ID"` ReportType int `gorm:"column:report_type" description:"报告类型:1-研报;2-PPT"` ReportId int `gorm:"column:report_id" description:"报告/PPT-ID"` MessageParams string `gorm:"column:message_params" description:"消息请求"` MessageResult string `gorm:"column:message_result" description:"响应结果"` TaskId string `gorm:"column:task_id" description:"待办任务ID"` Source int `gorm:"column:source" description:"来源:1-报告提醒"` SendStatus int `gorm:"column:send_status" description:"发送状态:1-发送成功;2-发送失败"` CreateTime time.Time `gorm:"column:create_time" description:"创建时间"` ModifyTime time.Time `gorm:"column:modify_time" description:"更新时间"` } func (m *OaReportMessage) TableName() string { return "oa_report_message" } type OaReportMessageCols struct { PrimaryId string ReceiveUserId string ReportType string ReportId string MessageParams string MessageResult string TaskId string Source string SendStatus string CreateTime string ModifyTime string } func (m *OaReportMessage) Cols() OaReportMessageCols { return OaReportMessageCols{ PrimaryId: "id", ReceiveUserId: "receive_user_id", ReportType: "report_type", ReportId: "report_id", MessageParams: "message_params", MessageResult: "message_result", TaskId: "task_id", Source: "source", SendStatus: "send_status", CreateTime: "create_time", ModifyTime: "modify_time", } } func (m *OaReportMessage) Create() (err error) { err = global.DmSQL["eta"].Create(m).Error return } func (m *OaReportMessage) CreateMulti(items []*OaReportMessage) (err error) { if len(items) == 0 { return } err = global.DmSQL["eta"].CreateInBatches(items, utils.MultiAddNum).Error return } func (m *OaReportMessage) Update(cols []string) (err error) { err = global.DmSQL["eta"].Select(cols).Updates(m).Error return } func (m *OaReportMessage) Remove() (err error) { sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DmSQL["eta"].Exec(sql, m.Id).Error return } func (m *OaReportMessage) 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.DmSQL["eta"].Exec(sql, ids).Error return } func (m *OaReportMessage) RemoveByCondition(condition string, pars []interface{}) (err error) { if condition == "" { return } sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition) err = global.DmSQL["eta"].Exec(sql, pars...).Error return } func (m *OaReportMessage) GetItemById(id int) (item *OaReportMessage, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DmSQL["eta"].Raw(sql, id).First(&item).Error return } func (m *OaReportMessage) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *OaReportMessage, 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.DmSQL["eta"].Raw(sql, pars...).First(&item).Error return } func (m *OaReportMessage) 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.DmSQL["eta"].Raw(sql, pars...).Scan(&count).Error return } func (m *OaReportMessage) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*OaReportMessage, 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.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error return } func (m *OaReportMessage) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*OaReportMessage, 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 LIMIT ?,?`, fields, m.TableName(), condition, order) pars = append(pars, startSize, pageSize) err = global.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error return }