package com.ruoyi.cms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cms.domain.FairCollection;
import com.ruoyi.cms.domain.Job;
import com.ruoyi.cms.domain.JobApply;
import com.ruoyi.cms.domain.query.MineJobQuery;
import com.ruoyi.cms.domain.vo.CandidateVO;
import com.ruoyi.cms.mapper.*;
import com.ruoyi.cms.service.IJobApplyService;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 岗位申请Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-04
 */
@Service
public class JobApplyServiceImpl extends ServiceImpl<JobApplyMapper,JobApply> implements IJobApplyService
{
    @Autowired
    private JobApplyMapper jobApplyMapper;
    @Autowired
    private JobCollectionMapper jobCollectionMapper;
    @Autowired
    private AppReviewJobMapper appReviewJobMapper;
    @Autowired
    private FairCollectionMapper fairCollectionMapper;
    @Autowired
    private CompanyCollectionMapper companyCollectionMapper;

    /**
     * 查询岗位申请
     *
     * @param id 岗位申请主键
     * @return 岗位申请
     */
    @Override
    public JobApply selectJobApplyById(Long id)
    {
        return jobApplyMapper.selectById(id);
    }

    /**
     * 查询岗位申请列表
     *
     * @param jobApply 岗位申请
     * @return 岗位申请
     */
    @Override
    public List<JobApply> selectJobApplyList(JobApply jobApply)
    {
        return jobApplyMapper.selectJobApplyList(jobApply);
    }

    /**
     * 新增岗位申请
     *
     * @param jobApply 岗位申请
     * @return 结果
     */
    @Override
    public int insertJobApply(JobApply jobApply)
    {
        return jobApplyMapper.insert(jobApply);
    }

    /**
     * 修改岗位申请
     *
     * @param jobApply 岗位申请
     * @return 结果
     */
    @Override
    public int updateJobApply(JobApply jobApply)
    {
        return jobApplyMapper.updateById(jobApply);
    }

    /**
     * 批量删除岗位申请
     *
     * @param ids 需要删除的岗位申请主键
     * @return 结果
     */
    @Override
    public int deleteJobApplyByIds(Long[] ids)
    {
        return jobApplyMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<Job> applyJob() {
        List<Job> jobs = jobApplyMapper.applyJob(SiteSecurityUtils.getUserId());
        return jobs;
    }

    @Override
    public HashMap<String, Integer> statistics() {
        Integer applyCount = jobApplyMapper.applyJob(SiteSecurityUtils.getUserId()).size();
        Integer collectionJobCount = jobCollectionMapper.collectionJob(SiteSecurityUtils.getUserId()).size();
        Integer collectionCompanyCount = companyCollectionMapper.collectionJob(SiteSecurityUtils.getUserId()).size();
        Integer jobReviewCount = appReviewJobMapper.review(SiteSecurityUtils.getUserId(),new MineJobQuery()).size();
        Integer fairCollecitonCount = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getUserId, SiteSecurityUtils.getUserId())).size();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("applyCount", applyCount);
        map.put("collectionCount", collectionJobCount+collectionCompanyCount);
        map.put("jobReviewCount", jobReviewCount);
        map.put("fairCollecitonCount", fairCollecitonCount);
        return map;
    }

    @Override
    public HashMap<String, Integer> trendChart(JobApply jobApply) {
        return jobApplyMapper.trendChart(jobApply);
    }

    @Override
    public List<CandidateVO> selectAppUserList(Job job) {
        return jobApplyMapper.selectAppUserList(job);
    }

    @Override
    public List<CandidateVO> selectApplyJobUserList(AppUser appUser) {
        return jobApplyMapper.selectApplyJobUserList(appUser);
    }

    @Override
    public int applyComJob(JobApply jobApply) {
        //todo 计算匹配度
        jobApply.setMatchingDegree(1L);
        List<JobApply> jobApplies = jobApplyMapper.selectList(Wrappers.<JobApply>lambdaQuery().eq(JobApply::getJobId, jobApply.getJobId()).eq(JobApply::getUserId, jobApply.getUserId()));
        if(jobApplies.size()>0){
            List<Long> ids = jobApplies.stream().map(JobApply::getId).collect(Collectors.toList());
            jobApplyMapper.deleteBatchIds(ids);
        }
        jobApplyMapper.insert(jobApply);
        //添加消息说明

        return 0;
    }

    @Override
    public List<Job> selectJobApplyListJob(JobApply jobApply) {
        return jobApplyMapper.selectJobApplyListJob(jobApply);
    }

    @Override
    public int updateJobZphApply(JobApply jobApply) {
        return jobApplyMapper.updateJobZphApply(jobApply);
    }
}
