移动业务逻辑
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user