package com.zcloud.fire.check.gatewayimpl;

import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordHiddenGateway;
import com.zcloud.fire.check.domain.model.FireCheckRecordDeviceE;
import com.zcloud.fire.check.domain.model.FireCheckRecordHiddenE;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordHiddenDO;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordHiddenRepository;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.gbscommon.zcloudhidden.request.HiddenAddCmd;
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:11:15
 */
@Service
@AllArgsConstructor
public class FireCheckRecordHiddenGatewayImpl implements FireCheckRecordHiddenGateway {
    private final FireCheckRecordHiddenRepository fireCheckRecordHiddenRepository;

    @Override
    public Boolean add(FireCheckRecordHiddenE fireCheckRecordHiddenE) {
        if(StringUtils.isEmpty(fireCheckRecordHiddenE.getFireRecordHiddenId())){
            fireCheckRecordHiddenE.setFireRecordHiddenId(Tools.get32UUID());
        }
        if(fireCheckRecordHiddenE.getCorpinfoId() == null){
            fireCheckRecordHiddenE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckRecordHiddenDO d = new FireCheckRecordHiddenDO();
        BeanUtils.copyProperties(fireCheckRecordHiddenE, d);
        fireCheckRecordHiddenRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(FireCheckRecordHiddenE fireCheckRecordHiddenE) {
        FireCheckRecordHiddenDO d = new FireCheckRecordHiddenDO();
        BeanUtils.copyProperties(fireCheckRecordHiddenE, d);
        fireCheckRecordHiddenRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedFireCheckRecordHiddenById(Long id) {
        return fireCheckRecordHiddenRepository.removeById(id);
    }

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


    @Override
    public void addHidden(FireCheckRecordDeviceE fireCheckRecordDeviceE, HiddenAddCmd hiddenAddCmd) {
        FireCheckRecordHiddenE fireCheckRecordHiddenE = new FireCheckRecordHiddenE();
        fireCheckRecordHiddenE.initFireCheckRecordHidden(fireCheckRecordDeviceE,hiddenAddCmd.getHiddenId(), hiddenAddCmd.getHiddenDesc());
        if(StringUtils.isEmpty(fireCheckRecordHiddenE.getFireRecordHiddenId())){
            fireCheckRecordHiddenE.setFireRecordHiddenId(Tools.get32UUID());
        }
        if(fireCheckRecordHiddenE.getCorpinfoId() == null){
            fireCheckRecordHiddenE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckRecordHiddenDO d = new FireCheckRecordHiddenDO();
        BeanUtils.copyProperties(fireCheckRecordHiddenE, d);
        fireCheckRecordHiddenRepository.add(d, hiddenAddCmd);
    }

    @Override
    public void updateHidden(FireCheckRecordHiddenE fireCheckRecordHiddenE, HiddenAddCmd hiddenAddCmd) {
        FireCheckRecordHiddenDO d = new FireCheckRecordHiddenDO();
        BeanUtils.copyProperties(fireCheckRecordHiddenE, d);
        fireCheckRecordHiddenRepository.updateHidden(d, hiddenAddCmd);

    }

    @Override
    public void addOtherHidden(FireCheckRecordHiddenE fireCheckRecordHiddenE, HiddenAddCmd hiddenAddCmd) {
        if(StringUtils.isEmpty(fireCheckRecordHiddenE.getFireRecordHiddenId())){
            fireCheckRecordHiddenE.setFireRecordHiddenId(Tools.get32UUID());
        }
        if(fireCheckRecordHiddenE.getCorpinfoId() == null){
            fireCheckRecordHiddenE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckRecordHiddenDO d = new FireCheckRecordHiddenDO();
        BeanUtils.copyProperties(fireCheckRecordHiddenE, d);
        hiddenAddCmd.setState(100);
        fireCheckRecordHiddenRepository.add(d, hiddenAddCmd);
    }
}

