package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.enums.CheckFlagEnum;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordDeviceGateway;
import com.zcloud.fire.check.domain.model.FireCheckRecordDeviceE;
import com.zcloud.fire.check.dto.FireCheckRecordDeviceUpdateCmd;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDeviceDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordPointDO;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordDeviceRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordPointRepository;
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-12-12 11:10:03
 */
@Component
@AllArgsConstructor
public class FireCheckRecordDeviceUpdateExe {
    private final FireCheckRecordDeviceGateway fireCheckRecordDeviceGateway;
    private final FireCheckRecordDeviceRepository fireCheckRecordDeviceRepository;
    private final FireCheckRecordPointRepository  fireCheckRecordPointRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(FireCheckRecordDeviceUpdateCmd fireCheckRecordDeviceUpdateCmd) {
        FireCheckRecordDeviceDO info = fireCheckRecordDeviceRepository.getById(fireCheckRecordDeviceUpdateCmd.getId());
        if (info == null) {
            throw new BizException("数据不存在");
        }
        FireCheckRecordDeviceE fireCheckRecordDeviceE = new FireCheckRecordDeviceE();
        BeanUtils.copyProperties(fireCheckRecordDeviceUpdateCmd, fireCheckRecordDeviceE);
        boolean res = fireCheckRecordDeviceGateway.update(fireCheckRecordDeviceE);
        if (!res) {
            throw new BizException("修改失败");
        }
        //将对应的point置检查状态改为未检查
        FireCheckRecordPointDO pointInfo = new FireCheckRecordPointDO();
        pointInfo.setFireRecordPointId(info.getFireRecordPointId());
        pointInfo.setCheckedFlag(CheckFlagEnum.CHECK_FLAG_BACK.getCode());

        fireCheckRecordPointRepository.updateCheckFlag(pointInfo);
    }
}

