package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.gateway.FirePointGateway;
import com.zcloud.fire.check.domain.model.FirePointE;
import com.zcloud.fire.check.dto.FirePointUpdateCmd;
import com.zcloud.fire.check.dto.FirePointUpdateDepartmentCmd;
import com.zcloud.fire.check.persistence.dataobject.FirePointDO;
import com.zcloud.fire.check.persistence.repository.FirePointRepository;
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:12:55
 */
@Component
@AllArgsConstructor
public class FirePointUpdateExe {
    private final FirePointGateway firePointGateway;
    private final FirePointRepository firePointRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(FirePointUpdateCmd firePointUpdateCmd) {
        //点位编码唯一
        FirePointDO firePointDO = firePointRepository.getInfoByCode(firePointUpdateCmd.getFirePointCode());
        if (firePointDO != null && !firePointDO.getId().equals(firePointUpdateCmd.getId())) {
            throw new BizException("点位编码已存在");
        }

        FirePointE firePointE = new FirePointE();
        BeanUtils.copyProperties(firePointUpdateCmd, firePointE);
        boolean res = firePointGateway.update(firePointE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    public void editAllDepartment(FirePointUpdateDepartmentCmd firePointUpdateDepartmentCmd) {
        firePointRepository.editAllDepartment(firePointUpdateDepartmentCmd.getIds(),firePointUpdateDepartmentCmd.getDepartmentId());
    }
}

