package com.zcloud.fire.check.gatewayimpl;

import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.fire.check.domain.gateway.FireCheckListUserGateway;
import com.zcloud.fire.check.domain.model.FireCheckListUserE;
import com.zcloud.fire.check.persistence.dataobject.FireCheckListUserDO;
import com.zcloud.fire.check.persistence.repository.FireCheckListUserRepository;
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;
import java.util.List;
import java.util.stream.Collectors;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-11 15:32:49
 */
@Service
@AllArgsConstructor
public class FireCheckListUserGatewayImpl implements FireCheckListUserGateway {
    private final FireCheckListUserRepository fireCheckListUserRepository;

    @Override
    public Boolean add(FireCheckListUserE fireCheckListUserE) {
        if(StringUtils.isEmpty(fireCheckListUserE.getFireCheckListUserId())){
            fireCheckListUserE.setFireCheckListUserId(Tools.get32UUID());
        }
        if(fireCheckListUserE.getCorpinfoId() == null){
            fireCheckListUserE.setCorpinfoId(AuthContext.getTenantId());
        }
        FireCheckListUserDO d = new FireCheckListUserDO();
        BeanUtils.copyProperties(fireCheckListUserE, d);
        fireCheckListUserRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(FireCheckListUserE fireCheckListUserE) {
        FireCheckListUserDO d = new FireCheckListUserDO();
        BeanUtils.copyProperties(fireCheckListUserE, d);
        fireCheckListUserRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedFireCheckListUserById(Long id) {
        return fireCheckListUserRepository.removeById(id);
    }

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

    @Override
    public void addBath(List<FireCheckListUserE> fireCheckListUserEList) {
        List<FireCheckListUserDO> dList = fireCheckListUserEList.stream().map(info -> {
            if(StringUtils.isEmpty(info.getFireCheckListUserId())){
                info.setFireCheckListUserId(Tools.get32UUID());
            }
            if(info.getCorpinfoId() == null){
                info.setCorpinfoId(AuthContext.getTenantId());
            }
            FireCheckListUserDO d = new FireCheckListUserDO();
            BeanUtils.copyProperties(info, d);
            return d;
        }).collect(Collectors.toList());
        fireCheckListUserRepository.saveBatch(dList);
    }
}

