report_peccancy_dao.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "context"
  4. "go-template/business/exception"
  5. "go-template/business/model"
  6. "gitea.ckfah.com/cjjy/gocommon/pkg/common"
  7. "gitea.ckfah.com/cjjy/gocommon/pkg/cerror"
  8. "gitea.ckfah.com/cjjy/gocommon/pkg/database/db"
  9. "gitea.ckfah.com/cjjy/gocommon/pkg/logger"
  10. "xorm.io/xorm"
  11. )
  12. type reportPeccancyDao struct {
  13. }
  14. func NewReportPeccancyDao() *reportPeccancyDao {
  15. return &reportPeccancyDao{}
  16. }
  17. var (
  18. _ReportPeccancy = new(model.ReportPeccancy)
  19. )
  20. func (*reportPeccancyDao) TableName() string {
  21. return _ReportPeccancy.TableName()
  22. }
  23. func (rpd *reportPeccancyDao) GetById(ctx context.Context, id uint64) (*model.ReportPeccancy, cerror.Cerror) {
  24. result := &model.ReportPeccancy{}
  25. isExist, err := db.NewSlaveSession(ctx).Where("id=?", id).Cols("*").Get(result)
  26. if err != nil {
  27. logger.Errorc(ctx, "[GetById] err,id=%d", id)
  28. return nil, exception.DbExecError(err)
  29. }
  30. if !isExist {
  31. return nil, nil
  32. }
  33. return result, nil
  34. }
  35. /**
  36. 这个方法根据业务去使用,不推荐直接删除,数据库表设计应该为标记删除,可以设置一个is_valid 字段标记是否有效
  37. */
  38. func (rpd *reportPeccancyDao) DeleteById(ctx context.Context, session *xorm.Session, id uint64) (int64, cerror.Cerror) {
  39. effectRow, err := session.Where("id=?", id).Delete(rpd)
  40. if err != nil {
  41. logger.Errorc(ctx, "[DeleteById] err,id=%d", id)
  42. return 0, exception.DbDeleteError(err)
  43. }
  44. return effectRow, nil
  45. }
  46. func (rpd *reportPeccancyDao) UpdateById(ctx context.Context, session *xorm.Session, id uint64, params map[string]interface{}) (int64, cerror.Cerror) {
  47. effectRow, err := session.Table(rpd).Where("id=?", id).Update(params)
  48. if err != nil {
  49. logger.Errorc(ctx, "[UpdateById] err,id=%d,params=%+v", id, params)
  50. return 0, exception.DbUpdateError(err)
  51. }
  52. return effectRow, nil
  53. }
  54. func (rpd *reportPeccancyDao) SaveOne(ctx context.Context, session *xorm.Session, param *model.ReportPeccancy) (int64, cerror.Cerror) {
  55. effectRow, err := session.InsertOne(param)
  56. if err != nil {
  57. logger.Errorc(ctx, "[SaveOne] err,param=%+v", param)
  58. return 0, exception.DbInsertError(err)
  59. }
  60. return effectRow, nil
  61. }
  62. func (rpd *reportPeccancyDao) GetByUserId(ctx context.Context, userId uint64, paginator common.Paginator) ([]model.ReportPeccancy, cerror.Cerror) {
  63. list := make([]model.ReportPeccancy, 0)
  64. err := db.NewSlaveSession(ctx).Where("user_id=?", userId).Limit(paginator.PageSize, paginator.LimitStart).Find(&list)
  65. if err != nil {
  66. logger.Errorc(ctx, "[GetByIds] err,user_id=%v", userId)
  67. return nil, exception.DbExecError(err)
  68. }
  69. return list, nil
  70. }
  71. func (rpd *reportPeccancyDao) GetCountByUserId(ctx context.Context, userId uint64) (int64, cerror.Cerror) {
  72. count, err := db.NewSlaveSession(ctx).Table(rpd.TableName()).Where("user_id=?", userId).Count()
  73. if err != nil {
  74. logger.Errorc(ctx, "[GetCountByUserId] err,user_id=%v", userId)
  75. return 0, exception.DbExecError(err)
  76. }
  77. return count, nil
  78. }