| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package dao
- import (
- "context"
- "go-template/business/exception"
- "go-template/business/model"
- "gitea.ckfah.com/cjjy/gocommon/pkg/common"
- "gitea.ckfah.com/cjjy/gocommon/pkg/cerror"
- "gitea.ckfah.com/cjjy/gocommon/pkg/database/db"
- "gitea.ckfah.com/cjjy/gocommon/pkg/logger"
- "xorm.io/xorm"
- )
- type reportPeccancyDao struct {
- }
- func NewReportPeccancyDao() *reportPeccancyDao {
- return &reportPeccancyDao{}
- }
- var (
- _ReportPeccancy = new(model.ReportPeccancy)
- )
- func (*reportPeccancyDao) TableName() string {
- return _ReportPeccancy.TableName()
- }
- func (rpd *reportPeccancyDao) GetById(ctx context.Context, id uint64) (*model.ReportPeccancy, cerror.Cerror) {
- result := &model.ReportPeccancy{}
- isExist, err := db.NewSlaveSession(ctx).Where("id=?", id).Cols("*").Get(result)
- if err != nil {
- logger.Errorc(ctx, "[GetById] err,id=%d", id)
- return nil, exception.DbExecError(err)
- }
- if !isExist {
- return nil, nil
- }
- return result, nil
- }
- /**
- 这个方法根据业务去使用,不推荐直接删除,数据库表设计应该为标记删除,可以设置一个is_valid 字段标记是否有效
- */
- func (rpd *reportPeccancyDao) DeleteById(ctx context.Context, session *xorm.Session, id uint64) (int64, cerror.Cerror) {
- effectRow, err := session.Where("id=?", id).Delete(rpd)
- if err != nil {
- logger.Errorc(ctx, "[DeleteById] err,id=%d", id)
- return 0, exception.DbDeleteError(err)
- }
- return effectRow, nil
- }
- func (rpd *reportPeccancyDao) UpdateById(ctx context.Context, session *xorm.Session, id uint64, params map[string]interface{}) (int64, cerror.Cerror) {
- effectRow, err := session.Table(rpd).Where("id=?", id).Update(params)
- if err != nil {
- logger.Errorc(ctx, "[UpdateById] err,id=%d,params=%+v", id, params)
- return 0, exception.DbUpdateError(err)
- }
- return effectRow, nil
- }
- func (rpd *reportPeccancyDao) SaveOne(ctx context.Context, session *xorm.Session, param *model.ReportPeccancy) (int64, cerror.Cerror) {
- effectRow, err := session.InsertOne(param)
- if err != nil {
- logger.Errorc(ctx, "[SaveOne] err,param=%+v", param)
- return 0, exception.DbInsertError(err)
- }
- return effectRow, nil
- }
- func (rpd *reportPeccancyDao) GetByUserId(ctx context.Context, userId uint64, paginator common.Paginator) ([]model.ReportPeccancy, cerror.Cerror) {
- list := make([]model.ReportPeccancy, 0)
- err := db.NewSlaveSession(ctx).Where("user_id=?", userId).Limit(paginator.PageSize, paginator.LimitStart).Find(&list)
- if err != nil {
- logger.Errorc(ctx, "[GetByIds] err,user_id=%v", userId)
- return nil, exception.DbExecError(err)
- }
- return list, nil
- }
- func (rpd *reportPeccancyDao) GetCountByUserId(ctx context.Context, userId uint64) (int64, cerror.Cerror) {
- count, err := db.NewSlaveSession(ctx).Table(rpd.TableName()).Where("user_id=?", userId).Count()
- if err != nil {
- logger.Errorc(ctx, "[GetCountByUserId] err,user_id=%v", userId)
- return 0, exception.DbExecError(err)
- }
- return count, nil
- }
|