package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.enums.FireRegionStateEnum;
import com.zcloud.fire.check.domain.gateway.FireRegionGateway;
import com.zcloud.fire.check.domain.model.FireRegionE;
import com.zcloud.fire.check.dto.FireRegionUpdateCmd;
import com.zcloud.fire.check.dto.FireRegionUpdateDepartmentCmd;
import com.zcloud.fire.check.dto.FireRegionUpdateStatusCmd;
import com.zcloud.fire.check.persistence.dataobject.FireRegionDO;
import com.zcloud.fire.check.persistence.repository.FireRegionRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:13:10
 */
@Component
@AllArgsConstructor
public class FireRegionUpdateExe {
    private final FireRegionGateway fireRegionGateway;
    private final FireRegionRepository fireRegionRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(FireRegionUpdateCmd fireRegionUpdateCmd) {

        //区域编码唯一
        FireRegionDO fireRegionDO =fireRegionRepository.getInfoByfireRefionCode(fireRegionUpdateCmd.getFireRegionCode());
        if (fireRegionDO != null && !fireRegionDO.getId().equals(fireRegionUpdateCmd.getId())) {
            throw new BizException("区域编码已存在");
        }

        FireRegionE fireRegionE = new FireRegionE();
        BeanUtils.copyProperties(fireRegionUpdateCmd, fireRegionE);
        boolean res = fireRegionGateway.update(fireRegionE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    public void editStatus(FireRegionUpdateStatusCmd fireRegionUpdateStatusCmd) {
        FireRegionE listManagerE = new FireRegionE();
        if(!FireRegionStateEnum.contains(fireRegionUpdateStatusCmd.getState())){
            throw new BizException("状态错误");
        }
        BeanUtils.copyProperties(fireRegionUpdateStatusCmd, listManagerE);
        boolean res = fireRegionGateway.update(listManagerE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    public void editAllDepartment(FireRegionUpdateDepartmentCmd fireRegionUpdateDepartmentCmd) {

        fireRegionRepository.editAllDepartment(fireRegionUpdateDepartmentCmd.getIds(),fireRegionUpdateDepartmentCmd.getDepartmentId());
    }
}

