issue_29 #32

Merged
huang merged 65 commits from issue_29 into main 2025-10-07 13:33:25 +08:00
4 changed files with 73 additions and 66 deletions
Showing only changes of commit 1bc36f5e10 - Show all commits

View File

@@ -133,11 +133,15 @@ func (s *pigBatchService) UpdatePigBatch(id uint, dto *dto.PigBatchUpdateDTO) (*
existingBatch.Status = *dto.Status
}
updatedBatch, err := s.pigBatchRepo.UpdatePigBatch(existingBatch)
updatedBatch, rowsAffected, err := s.pigBatchRepo.UpdatePigBatch(existingBatch)
if err != nil {
s.logger.Errorf("更新猪批次失败ID: %d, 错误: %v", id, err)
return nil, err
}
// 如果没有行受影响,则认为猪批次不存在
if rowsAffected == 0 {
return nil, ErrPigBatchNotFound
}
return s.toPigBatchResponseDTO(updatedBatch), nil
}
@@ -160,14 +164,16 @@ func (s *pigBatchService) DeletePigBatch(id uint) error {
}
// 3. 执行删除操作
err = s.pigBatchRepo.DeletePigBatch(id)
rowsAffected, err := s.pigBatchRepo.DeletePigBatch(id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) || errors.New("未找到要删除的猪批次").Error() == err.Error() {
return ErrPigBatchNotFound
}
s.logger.Errorf("删除猪批次失败ID: %d, 错误: %v", id, err)
return err
}
// 如果没有行受影响,则认为猪批次不存在
if rowsAffected == 0 {
return ErrPigBatchNotFound
}
return nil
}

View File

@@ -71,10 +71,13 @@ func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*mod
Name: name,
Description: description,
}
err := s.repo.UpdatePigHouse(house)
rowsAffected, err := s.repo.UpdatePigHouse(house)
if err != nil {
return nil, err
}
if rowsAffected == 0 {
return nil, ErrHouseNotFound
}
// 返回更新后的完整信息
return s.repo.GetPigHouseByID(id)
}
@@ -90,11 +93,14 @@ func (s *pigFarmService) DeletePigHouse(id uint) error {
}
// 调用仓库层进行删除
err = s.repo.DeletePigHouse(id)
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrHouseNotFound // 或者直接返回 gorm.ErrRecordNotFound取决于业务需求
rowsAffected, err := s.repo.DeletePigHouse(id)
if err != nil {
return err
}
return err
if rowsAffected == 0 {
return ErrHouseNotFound
}
return nil
}
// --- Pen Implementation ---
@@ -144,10 +150,13 @@ func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capa
Capacity: capacity,
Status: status,
}
err = s.repo.UpdatePen(pen)
rowsAffected, err := s.repo.UpdatePen(pen)
if err != nil {
return nil, err
}
if rowsAffected == 0 {
return nil, ErrPenNotFound
}
// 返回更新后的完整信息
return s.repo.GetPenByID(id)
}
@@ -157,27 +166,31 @@ func (s *pigFarmService) DeletePen(id uint) error {
pen, err := s.repo.GetPenByID(id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return gorm.ErrRecordNotFound // 猪栏不存在
return ErrPenNotFound // 猪栏不存在
}
return err
}
// 检查猪栏是否关联了活跃批次
if *pen.PigBatchID != 0 {
// 注意pen.PigBatchID 是指针类型,需要检查是否为 nil
if pen.PigBatchID != nil && *pen.PigBatchID != 0 {
pigBatch, err := s.repo.GetPigBatchByID(*pen.PigBatchID)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
// 如果批次活跃,则不能删除猪栏
if pigBatch.IsActive() {
if pigBatch != nil && pigBatch.IsActive() {
return ErrPenInUse
}
}
// 调用仓库层进行删除
err = s.repo.DeletePen(id)
if errors.Is(err, gorm.ErrRecordNotFound) {
return gorm.ErrRecordNotFound // 猪栏不存在
rowsAffected, err := s.repo.DeletePen(id)
if err != nil {
return err
}
return err
if rowsAffected == 0 {
return ErrPenNotFound
}
return nil
}

View File

@@ -1,8 +1,6 @@
package repository
import (
"errors"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
@@ -11,8 +9,10 @@ import (
type PigBatchRepository interface {
CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error)
GetPigBatchByID(id uint) (*models.PigBatch, error)
UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, error)
DeletePigBatch(id uint) error
// UpdatePigBatch 更新一个猪批次,返回更新后的批次、受影响的行数和错误
UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error)
// DeletePigBatch 根据ID删除一个猪批次返回受影响的行数和错误
DeletePigBatch(id uint) (int64, error)
ListPigBatches(isActive *bool) ([]*models.PigBatch, error)
}
@@ -44,27 +44,23 @@ func (r *gormPigBatchRepository) GetPigBatchByID(id uint) (*models.PigBatch, err
}
// UpdatePigBatch 更新一个猪批次
func (r *gormPigBatchRepository) UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, error) {
func (r *gormPigBatchRepository) UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error) {
result := r.db.Model(&models.PigBatch{}).Where("id = ?", batch.ID).Updates(batch)
if result.Error != nil {
return nil, result.Error
return nil, 0, result.Error
}
if result.RowsAffected == 0 {
return nil, errors.New("未找到要更新的猪批次或数据未改变") // 明确返回错误,而不是 gorm.ErrRecordNotFound
}
return batch, nil
// 返回更新后的批次、受影响的行数和错误
return batch, result.RowsAffected, nil
}
// DeletePigBatch 根据ID删除一个猪批次 (GORM 会执行软删除)
func (r *gormPigBatchRepository) DeletePigBatch(id uint) error {
func (r *gormPigBatchRepository) DeletePigBatch(id uint) (int64, error) {
result := r.db.Delete(&models.PigBatch{}, id)
if result.Error != nil {
return result.Error
return 0, result.Error
}
if result.RowsAffected == 0 {
return errors.New("未找到要删除的猪批次") // 明确返回错误
}
return nil
// 返回受影响的行数和错误
return result.RowsAffected, nil
}
// ListPigBatches 批量查询猪批次,支持根据 IsActive 筛选

View File

@@ -11,8 +11,10 @@ type PigFarmRepository interface {
CreatePigHouse(house *models.PigHouse) error
GetPigHouseByID(id uint) (*models.PigHouse, error)
ListPigHouses() ([]models.PigHouse, error)
UpdatePigHouse(house *models.PigHouse) error
DeletePigHouse(id uint) error
// UpdatePigHouse 更新一个猪舍,返回受影响的行数和错误
UpdatePigHouse(house *models.PigHouse) (int64, error)
// DeletePigHouse 根据ID删除一个猪舍返回受影响的行数和错误
DeletePigHouse(id uint) (int64, error)
CountPensInHouse(houseID uint) (int64, error)
// Pen methods
@@ -22,8 +24,10 @@ type PigFarmRepository interface {
// GetPenByIDTx 根据ID获取单个猪栏 (事务性)
GetPenByIDTx(tx *gorm.DB, id uint) (*models.Pen, error)
ListPens() ([]models.Pen, error)
UpdatePen(pen *models.Pen) error
DeletePen(id uint) error
// UpdatePen 更新一个猪栏,返回受影响的行数和错误
UpdatePen(pen *models.Pen) (int64, error)
// DeletePen 根据ID删除一个猪栏返回受影响的行数和错误
DeletePen(id uint) (int64, error)
// GetPensByBatchID 根据批次ID获取所有关联的猪栏 (事务性)
GetPensByBatchID(tx *gorm.DB, batchID uint) ([]models.Pen, error)
// UpdatePenFields 更新猪栏的指定字段 (事务性)
@@ -71,28 +75,22 @@ func (r *gormPigFarmRepository) ListPigHouses() ([]models.PigHouse, error) {
return houses, nil
}
// UpdatePigHouse 更新一个猪舍
func (r *gormPigFarmRepository) UpdatePigHouse(house *models.PigHouse) error {
// UpdatePigHouse 更新一个猪舍,返回受影响的行数和错误
func (r *gormPigFarmRepository) UpdatePigHouse(house *models.PigHouse) (int64, error) {
result := r.db.Model(&models.PigHouse{}).Where("id = ?", house.ID).Updates(house)
if result.Error != nil {
return result.Error
return 0, result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 未找到要更新的猪舍或数据未改变
}
return nil
return result.RowsAffected, nil
}
// DeletePigHouse 根据ID删除一个猪舍
func (r *gormPigFarmRepository) DeletePigHouse(id uint) error {
// DeletePigHouse 根据ID删除一个猪舍,返回受影响的行数和错误
func (r *gormPigFarmRepository) DeletePigHouse(id uint) (int64, error) {
result := r.db.Delete(&models.PigHouse{}, id)
if result.Error != nil {
return result.Error
return 0, result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 未找到要删除的猪舍
}
return nil
return result.RowsAffected, nil
}
// CountPensInHouse 统计猪舍中的猪栏数量
@@ -136,28 +134,22 @@ func (r *gormPigFarmRepository) ListPens() ([]models.Pen, error) {
return pens, nil
}
// UpdatePen 更新一个猪栏
func (r *gormPigFarmRepository) UpdatePen(pen *models.Pen) error {
// UpdatePen 更新一个猪栏,返回受影响的行数和错误
func (r *gormPigFarmRepository) UpdatePen(pen *models.Pen) (int64, error) {
result := r.db.Model(&models.Pen{}).Where("id = ?", pen.ID).Updates(pen)
if result.Error != nil {
return result.Error
return 0, result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 未找到要更新的猪栏或数据未改变
}
return nil
return result.RowsAffected, nil
}
// DeletePen 根据ID删除一个猪栏
func (r *gormPigFarmRepository) DeletePen(id uint) error {
// DeletePen 根据ID删除一个猪栏,返回受影响的行数和错误
func (r *gormPigFarmRepository) DeletePen(id uint) (int64, error) {
result := r.db.Delete(&models.Pen{}, id)
if result.Error != nil {
return result.Error
return 0, result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 未找到要删除的猪栏
}
return nil
return result.RowsAffected, nil
}
// GetPensByBatchID 根据批次ID获取所有关联的猪栏 (事务性)