package com.zcloud.fire.check.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordUserGateway;
import com.zcloud.fire.check.domain.model.FireCheckRecordUserE;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordUserDO;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordUserRepository;
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:12:04
 */
@Service
@AllArgsConstructor
public class FireCheckRecordUserGatewayImpl implements FireCheckRecordUserGateway {
    private final FireCheckRecordUserRepository fireCheckRecordUserRepository;

    @Override
    public Boolean add(FireCheckRecordUserE fireCheckRecordUserE) {
        if(StringUtils.isEmpty(fireCheckRecordUserE.getFireRecordUserId())){
            fireCheckRecordUserE.setFireRecordUserId(Tools.get32UUID());
        }
        if(fireCheckRecordUserE.getCorpinfoId() == null){
            fireCheckRecordUserE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckRecordUserDO d = new FireCheckRecordUserDO();
        BeanUtils.copyProperties(fireCheckRecordUserE, d);
        fireCheckRecordUserRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(FireCheckRecordUserE fireCheckRecordUserE) {
        FireCheckRecordUserDO d = new FireCheckRecordUserDO();
        BeanUtils.copyProperties(fireCheckRecordUserE, d);
        fireCheckRecordUserRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedFireCheckRecordUserById(Long id) {
        return fireCheckRecordUserRepository.removeById(id);
    }

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

    @Override
    public void deletedByCheckRecordId(String fireRecordId) {
        QueryWrapper<FireCheckRecordUserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_id", fireRecordId);
        fireCheckRecordUserRepository.remove(queryWrapper);
    }
}

