移动业务逻辑

This commit is contained in:
2025-10-04 00:58:29 +08:00
parent 8bb0a54f18
commit 1bc36f5e10
4 changed files with 73 additions and 66 deletions

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
}