package com.zcloud.basic.info.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.basic.info.domain.gateway.CorpFormGateway;
import com.zcloud.basic.info.domain.model.CorpFormE;
import com.zcloud.basic.info.persistence.dataobject.CorpFormDO;
import com.zcloud.basic.info.persistence.repository.CorpFormRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:27
 */
@Service
@AllArgsConstructor
public class CorpFormGatewayImpl implements CorpFormGateway {
    private final CorpFormRepository corpFormRepository;

    @Override
    public Boolean add(CorpFormE corpFormE) {
        CorpFormDO d = new CorpFormDO();
        BeanUtils.copyProperties(corpFormE, d);
        if (StringUtils.isEmpty(d.getCorpFormId())) {
            d.setCorpFormId(Tools.get32UUID());
        }
        corpFormRepository.save(d);
        return true;
    }


    @Override
    public Boolean update(CorpFormE corpFormE) {
        CorpFormDO d = new CorpFormDO();
        BeanUtils.copyProperties(corpFormE, d);
        corpFormRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedCorpFormById(Long id) {
        return corpFormRepository.removeById(id);
    }

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

    @Override
    public void deletedCorpFormByType(Long corpInfoId, List<Integer> typeList) {
        QueryWrapper<CorpFormDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_id", corpInfoId)
                .in("type", typeList);
        corpFormRepository.remove(queryWrapper);
    }

    @Override
    public void deletedCorpFormByType(Long corpInfoId, Integer type) {
        QueryWrapper<CorpFormDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_id", corpInfoId)
                .eq("type", type);
        corpFormRepository.remove(queryWrapper);
    }
}

