还原改动, bmad真难用

This commit is contained in:
2025-11-02 15:07:19 +08:00
parent 548d3eae00
commit 8e97922012
453 changed files with 4 additions and 81231 deletions

View File

@@ -38,8 +38,7 @@ type deviceService struct {
deviceRepo repository.DeviceRepository
areaControllerRepo repository.AreaControllerRepository
deviceTemplateRepo repository.DeviceTemplateRepository
planRepo repository.PlanRepository // 新增计划仓库依赖
deviceDomainSvc device.Service // 依赖领域服务
deviceDomainSvc device.Service // 依赖领域服务
}
// NewDeviceService 创建一个新的 DeviceService 实例。
@@ -47,48 +46,16 @@ func NewDeviceService(
deviceRepo repository.DeviceRepository,
areaControllerRepo repository.AreaControllerRepository,
deviceTemplateRepo repository.DeviceTemplateRepository,
planRepo repository.PlanRepository,
deviceDomainSvc device.Service,
) DeviceService {
return &deviceService{
deviceRepo: deviceRepo,
areaControllerRepo: areaControllerRepo,
deviceTemplateRepo: deviceTemplateRepo,
planRepo: planRepo,
deviceDomainSvc: deviceDomainSvc,
}
}
func (s *deviceService) isDeviceInUseByActivePlan(deviceID uint) (bool, error) {
plans, err := s.planRepo.FindPlansWithPendingTasks()
if err != nil {
return false, fmt.Errorf("查询活动计划失败: %w", err)
}
for _, plan := range plans {
tasks, err := s.planRepo.FlattenPlanTasks(plan.ID)
if err != nil {
return false, fmt.Errorf("展开计划 %d 的任务失败: %w", plan.ID, err)
}
for _, task := range tasks {
// 假设任务参数中设备ID的键是 "device_id"
var params map[string]interface{}
if err := json.Unmarshal(task.Parameters, &params); err != nil {
// 无法解析参数,跳过此任务
continue
}
if paramDeviceID, ok := params["device_id"]; ok {
if floatDeviceID, ok := paramDeviceID.(float64); ok && uint(floatDeviceID) == deviceID {
return true, nil
}
}
}
}
return false, nil
}
// --- Devices ---
func (s *deviceService) CreateDevice(req *dto.CreateDeviceRequest) (*dto.DeviceResponse, error) {
@@ -182,13 +149,6 @@ func (s *deviceService) DeleteDevice(id string) error {
return err
}
// 检查设备是否被活动计划使用
if inUse, err := s.isDeviceInUseByActivePlan(uint(idUint)); err != nil {
return fmt.Errorf("检查设备是否被计划使用失败: %w", err)
} else if inUse {
return errors.New("设备正在被活动计划使用,无法删除")
}
return s.deviceRepo.Delete(uint(idUint))
}
@@ -303,15 +263,6 @@ func (s *deviceService) DeleteAreaController(id string) error {
return err
}
// 检查是否有设备正在使用此区域主控
devices, err := s.deviceRepo.ListByAreaControllerID(uint(idUint))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if len(devices) > 0 {
return errors.New("区域主控正在被设备使用,无法删除")
}
return s.areaControllerRepo.Delete(uint(idUint))
}
@@ -418,14 +369,5 @@ func (s *deviceService) DeleteDeviceTemplate(id string) error {
return err
}
// 检查是否有设备正在使用此模板
devices, err := s.deviceRepo.FindByDeviceTemplateID(uint(idUint))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if len(devices) > 0 {
return errors.New("设备模板正在被设备使用,无法删除")
}
return s.deviceTemplateRepo.Delete(uint(idUint))
}