package com.zcloud.zcGbsServicer.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.zcGbsServicer.command.convertor.ResourceDeviceCoConvertor;
import com.zcloud.zcGbsServicer.command.convertor.ResourcePersonCoConvertor;
import com.zcloud.zcGbsServicer.domain.enums.DeviceTypeEnum;
import com.zcloud.zcGbsServicer.domain.enums.PersonTypeEnum;
import com.zcloud.zcGbsServicer.domain.gateway.ControlRoomGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourceDeviceGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourcePersonGateway;
import com.zcloud.zcGbsServicer.domain.model.ControlRoomE;
import com.zcloud.zcGbsServicer.domain.model.ResourceDeviceE;
import com.zcloud.zcGbsServicer.domain.model.ResourcePersonE;
import com.zcloud.zcGbsServicer.dto.ControlRoomUpdateCmd;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:21:51
*/
@Component
@AllArgsConstructor
public class ControlRoomUpdateExe {
    private final ControlRoomGateway controlRoomGateway;
    private final ResourcePersonGateway resourcePersonGateway;
    private final ResourceDeviceGateway resourceDeviceGateway;

    private final ResourcePersonCoConvertor resourcePersonCoConvertor;
    private final ResourceDeviceCoConvertor resourceDeviceCoConvertor;

    @Transactional(rollbackFor = Exception.class)
    public void execute(ControlRoomUpdateCmd controlRoomUpdateCmd) {
        ControlRoomE controlRoomE = new ControlRoomE();
        BeanUtils.copyProperties(controlRoomUpdateCmd, controlRoomE);
        boolean res = controlRoomGateway.update(controlRoomE);
        boolean relUpdate = executePersonAndDevice(controlRoomUpdateCmd);
        if (!res || !relUpdate) {
            throw new BizException("修改失败");
        }
    }

    /**
     * 处理人员和设备（全删再增）
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean executePersonAndDevice(ControlRoomUpdateCmd cmd) {
        Integer personBizType = PersonTypeEnum.CONTROL_ROOM.getCode();
        Integer deviceBizType = DeviceTypeEnum.CONTROL_ROOM.getCode();
        String bizId = cmd.getRoomId();

        resourcePersonGateway.deletedResourcePersonByBizId(bizId, personBizType);
        resourceDeviceGateway.deletedResourceDeviceByBizId(bizId, deviceBizType);

        if (CollUtil.isEmpty(cmd.getPersons()) && CollUtil.isEmpty(cmd.getDevices())) {
            return true;
        }
        Boolean resPer = true;
        Boolean resDev = true;
        if (CollUtil.isNotEmpty(cmd.getPersons())) {
            List<ResourcePersonE> persons = resourcePersonCoConvertor.converAddToEOs(cmd.getPersons());
            persons.forEach(p -> {
                p.relInfo(bizId, personBizType);
            });
            resPer = resourcePersonGateway.addBatch(persons);
        }

        if (CollUtil.isNotEmpty(cmd.getDevices())) {
            List<ResourceDeviceE> devices = resourceDeviceCoConvertor.converAddToE(cmd.getDevices());
            devices.forEach(d -> {
                d.relInfo(bizId, deviceBizType);
            });
            resDev = resourceDeviceGateway.addBatch(devices);
        }
        return resPer && resDev;
    }
}

