package com.ruoyi.cms.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.mapper.CompanyMapper;
import com.ruoyi.cms.mapper.FairCollectionMapper;
import com.ruoyi.cms.mapper.FairCompanyMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.common.exception.ServiceException;
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.JobFairMapper;
import com.ruoyi.cms.service.IJobFairService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 招聘会信息Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-04
 */
@Service
public class JobFairServiceImpl extends ServiceImpl<JobFairMapper,JobFair> implements IJobFairService
{
    @Autowired
    private JobFairMapper jobFairMapper;
    @Autowired
    private FairCompanyMapper fairCompanyMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private FairCollectionMapper fairCollectionMapper;
    /**
     * 查询招聘会信息
     *
     * @param jobFairId 招聘会信息主键
     * @return 招聘会信息
     */
    @Override
    public JobFair selectJobFairByJobFairId(Long jobFairId)
    {
        JobFair jobFair = jobFairMapper.selectById(jobFairId);
        if(Objects.isNull(jobFair)){
            throw new ServiceException("招聘会不存在");
        }
        //查询公司信息
        List<FairCompany> fairCompanies = fairCompanyMapper.selectList(Wrappers.<FairCompany>lambdaQuery().eq(FairCompany::getJobFairId, jobFairId));
        if(fairCompanies.size()>0){
            List<Long> companyIds = fairCompanies.stream().map(FairCompany::getCompanyId).collect(Collectors.toList());
            List<Company> companyList = companyMapper.selectBatchIds(companyIds);
            jobFair.setCompanyList(companyList);
        }
        return jobFair;
    }

    /**
     * 查询招聘会信息列表
     *
     * @param jobFair 招聘会信息
     * @return 招聘会信息
     */
    @Override
    public List<JobFair> selectJobFairList(JobFair jobFair)
    {
        return jobFairMapper.selectJobFairList(jobFair);
    }
    /**
     * 新增招聘会信息
     *
     * @param jobFair 招聘会信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertJobFair(JobFair jobFair)
    {
        int insert = jobFairMapper.insert(jobFair);
        if(insert>0){
            jobFair.getCompanyList().forEach(x -> {
                FairCompany fairCompany = new FairCompany();
                fairCompany.setCompanyId(x.getCompanyId());
                fairCompany.setJobFairId(jobFair.getJobFairId());
                fairCompanyMapper.insert(fairCompany);
            });
        }
        return insert;
    }

    /**
     * 修改招聘会信息
     *
     * @param jobFair 招聘会信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateJobFair(JobFair jobFair)
    {
        int i = jobFairMapper.updateById(jobFair);
        if(i>0){
            fairCompanyMapper.update(null,Wrappers.<FairCompany>lambdaUpdate()
                    .eq(FairCompany::getJobFairId, jobFair.getJobFairId())
                    .set(FairCompany::getDelFlag, Constants.Del_FLAG_DELETE));
            if(Objects.isNull(jobFair.getCompanyList())){return i;}
            jobFair.getCompanyList().forEach(x -> {
                FairCompany fairCompany = new FairCompany();
                fairCompany.setCompanyId(x.getCompanyId());
                fairCompany.setJobFairId(jobFair.getJobFairId());
                fairCompanyMapper.insert(fairCompany);
            });
        }
        return i;
    }

    /**
     * 批量删除招聘会信息
     *
     * @param jobFairIds 需要删除的招聘会信息主键
     * @return 结果
     */
    @Override
    public int deleteJobFairByJobFairIds(Long[] jobFairIds)
    {
        int i = jobFairMapper.deleteBatchIds(Arrays.asList(jobFairIds));
        if(i>0){
            fairCompanyMapper.update(null,Wrappers.<FairCompany>lambdaUpdate()
                    .in(FairCompany::getJobFairId, Arrays.asList(jobFairIds))
                    .set(FairCompany::getDelFlag, Constants.Del_FLAG_DELETE));
        }
        return i;
    }

    @Override
    public JobFair appDetail(Long fairId) {
        JobFair jobFair = jobFairMapper.selectById(fairId);
        if(Objects.isNull(jobFair)){
            throw new ServiceException("招聘会不存在");
        }
        //查询公司信息
        List<FairCompany> fairCompanies = fairCompanyMapper.selectList(Wrappers.<FairCompany>lambdaQuery().eq(FairCompany::getJobFairId, fairId));
        if(fairCompanies.size()>0){
            List<Long> companyIds = fairCompanies.stream().map(FairCompany::getCompanyId).collect(Collectors.toList());
            List<Company> companyList = companyMapper.selectBatchIds(companyIds);
            jobFair.setCompanyList(companyList);
        }
        if(SiteSecurityUtils.isLogin()){
            Long count = fairCollectionMapper.selectCount(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getFairId, fairId).eq(FairCollection::getUserId, SiteSecurityUtils.getUserId()));
            jobFair.setIsCollection(count>0?1:null);
        }
        return jobFair;
    }

    @Override
    public List<JobFair> appList(JobFair jobFair) {
        List<JobFair> fairCompanies = jobFairMapper.appList(jobFair);
        if(fairCompanies.isEmpty()){return new ArrayList<>();}
        if(SiteSecurityUtils.isLogin()){
            //收藏
            Set<Long> collectionIds = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery()
                            .eq(FairCollection::getUserId, SiteSecurityUtils.getUserId())
                            .in(FairCollection::getFairId, fairCompanies.stream().map(JobFair::getJobFairId).collect(Collectors.toList())))
                    .stream().map(FairCollection::getFairId).collect(Collectors.toSet());
            for (JobFair j : fairCompanies) {
                if (collectionIds.contains(j.getJobFairId())) {
                    j.setIsCollection(1);
                }
            }
        }
        return fairCompanies;
    }
}
