package com.zcloud.fire.check.gatewayimpl;

import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.fire.check.domain.enums.FireCheckListStateEnum;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordGateway;
import com.zcloud.fire.check.domain.model.FireCheckListE;
import com.zcloud.fire.check.domain.model.FireCheckRecordE;
import com.zcloud.fire.check.persistence.dataobject.FireCheckListDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO;
import com.zcloud.fire.check.persistence.repository.FireCheckListRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-12 11:10:58
 */
@Service
@AllArgsConstructor
public class FireCheckRecordGatewayImpl implements FireCheckRecordGateway {
    private final FireCheckRecordRepository fireCheckRecordRepository;
    private final FireCheckListRepository fireCheckListRepository;

    @Override
    public Boolean add(FireCheckRecordE fireCheckRecordE) {
        if(StringUtils.isEmpty(fireCheckRecordE.getFireRecordId())){
            fireCheckRecordE.setFireRecordId(Tools.get32UUID());
        }
        if(fireCheckRecordE.getCorpinfoId() == null){
            fireCheckRecordE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckRecordDO d = new FireCheckRecordDO();
        BeanUtils.copyProperties(fireCheckRecordE, d);
        fireCheckRecordRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(FireCheckRecordE fireCheckRecordE) {
        FireCheckRecordDO d = new FireCheckRecordDO();
        BeanUtils.copyProperties(fireCheckRecordE, d);
        fireCheckRecordRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedFireCheckRecordById(Long id) {
        return fireCheckRecordRepository.removeById(id);
    }

    @Override
    public Boolean deletedFireCheckRecordByIds(Long[] ids) {
        return fireCheckRecordRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public FireCheckRecordE initCheckRecord(String fireCheckId, String fireRecordId) {

        FireCheckRecordDO fireCheckRecordDO = fireCheckRecordRepository.getCheckRecordInfoByFireRecordId(fireRecordId);
        FireCheckRecordE fireCheckRecordE = new FireCheckRecordE();
        if(fireCheckRecordDO == null){
            FireCheckListDO fireCheckListDO = fireCheckListRepository.getInfoByUUIdState(fireCheckId, FireCheckListStateEnum.ENABLE.getCode());
            if(fireCheckListDO == null){
                throw new BizException("检查表不存在");
            }
            FireCheckListE fireCheckListE = new FireCheckListE();
            BeanUtils.copyProperties(fireCheckListDO, fireCheckListE);
            fireCheckRecordE.initCheckRecord(fireCheckListE);
            add(fireCheckRecordE);
            return fireCheckRecordE;
        }
        BeanUtils.copyProperties(fireCheckRecordDO, fireCheckRecordE);
        return fireCheckRecordE;
    }
}

