package com.ruoyi.cms.service.impl;

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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.cms.domain.Job;
import com.ruoyi.cms.mapper.JobMapper;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CompanyCollectionMapper;
import com.ruoyi.cms.domain.CompanyCollection;
import com.ruoyi.cms.service.ICompanyCollectionService;

/**
 * 用户公司收藏Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-04
 */
@Service
public class CompanyCollectionServiceImpl extends ServiceImpl<CompanyCollectionMapper,CompanyCollection> implements ICompanyCollectionService
{
    @Autowired
    private CompanyCollectionMapper companyCollectionMapper;
    @Autowired
    private JobMapper jobMapper;
    /**
     * 查询用户公司收藏
     *
     * @param id 用户公司收藏主键
     * @return 用户公司收藏
     */
    @Override
    public CompanyCollection selectCompanyCollectionById(Long id)
    {
        return companyCollectionMapper.selectById(id);
    }

    /**
     * 查询用户公司收藏列表
     *
     * @param companyCollection 用户公司收藏
     * @return 用户公司收藏
     */
    @Override
    public List<CompanyCollection> selectCompanyCollectionList(CompanyCollection companyCollection)
    {
        return companyCollectionMapper.selectCompanyCollectionList(companyCollection);
    }

    /**
     * 新增用户公司收藏
     *
     * @param companyCollection 用户公司收藏
     * @return 结果
     */
    @Override
    public int insertCompanyCollection(CompanyCollection companyCollection)
    {
        return companyCollectionMapper.insert(companyCollection);
    }

    /**
     * 修改用户公司收藏
     *
     * @param companyCollection 用户公司收藏
     * @return 结果
     */
    @Override
    public int updateCompanyCollection(CompanyCollection companyCollection)
    {
        return companyCollectionMapper.updateById(companyCollection);
    }

    /**
     * 批量删除用户公司收藏
     *
     * @param ids 需要删除的用户公司收藏主键
     * @return 结果
     */
    @Override
    public int deleteCompanyCollectionByIds(Long[] ids)
    {
        return companyCollectionMapper.deleteBatchIds(Arrays.asList(ids));
    }
    /**
     * 用户取消收藏公司
     */
    @Override
    public int companyCancel(Long companyId) {
        List<CompanyCollection> companyCollections = companyCollectionMapper.selectList(Wrappers.<CompanyCollection>lambdaQuery().eq(CompanyCollection::getCompanyId, companyId).eq(CompanyCollection::getUserId, SiteSecurityUtils.getUserId()));
        if(companyCollections.size()>0){
            companyCollectionMapper.deleteBatchIds(companyCollections.stream().map(CompanyCollection::getId).collect(Collectors.toList()));
        }
        return 1;
    }
    /**
     * 用户收藏公司
     */
    @Override
    public int companyCollection(Long companyId) {
        Long userId = SiteSecurityUtils.getUserId();
        List<CompanyCollection> companyCollections = companyCollectionMapper.selectList(Wrappers.<CompanyCollection>lambdaQuery().eq(CompanyCollection::getCompanyId, companyId).eq(CompanyCollection::getUserId, userId));
        if(companyCollections.size()>0){
            companyCollectionMapper.deleteBatchIds(companyCollections.stream().map(CompanyCollection::getId).collect(Collectors.toList()));
        }
        CompanyCollection companyCollection = new CompanyCollection();
        companyCollection.setCompanyId(companyId);
        companyCollection.setUserId(userId);
        companyCollectionMapper.insert(companyCollection);
        return 1;
    }

    @Override
    public List<Company> collectionCompany() {
        List<Company> companies = companyCollectionMapper.collectionJob(SiteSecurityUtils.getUserId());
        return companies;
    }

    @Override
    public List<Job> jobCompany(Long companyId) {
        Job job = new Job();
        job.setCompanyId(companyId);
        job.setIsPublish(1);
        List<Job> jobs = jobMapper.selectJobList(job);
        return jobs;
    }

}
