package com.ruoyi.cms.service.impl;

import com.ruoyi.common.core.domain.entity.CompanyContact;
import com.ruoyi.cms.mapper.CompanyContactMapper;
import com.ruoyi.cms.service.CompanyContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service
public class CompanyContactServiceImpl extends ServiceImpl<CompanyContactMapper, CompanyContact> implements CompanyContactService {

    @Autowired
    CompanyContactMapper companyContactMapper;

    public List<CompanyContact> getSelectList(CompanyContact companyContact){
        return companyContactMapper.getSelectList(companyContact);
    }

    @Override
    @Transactional
    public int insertUpadteCompanyContact(List<CompanyContact> list) {
        Map<Boolean, List<CompanyContact>> partitioned = list.stream()
                .collect(Collectors.partitioningBy(it -> it.getId() != null));
        List<CompanyContact> add=partitioned.get(false);
        List<CompanyContact> udp=partitioned.get(true);
        if(add.size()>0){
            companyContactMapper.batchInsert(add);
        }
        if(udp.size()>0){
            companyContactMapper.batchUpdate(udp);
        }
        return 0;
    }

    @Override
    public int insertContact(CompanyContact contact) {
        return companyContactMapper.insert(contact);
    }
}
