package com.ruoyi.cms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.cms.domain.Job;
import com.ruoyi.cms.domain.JobApply;
import com.ruoyi.cms.domain.JobCollection;
import com.ruoyi.cms.mapper.AppUserMapper;
import com.ruoyi.cms.mapper.JobApplyMapper;
import com.ruoyi.cms.mapper.JobCollectionMapper;
import com.ruoyi.cms.service.IJobRecommentService;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 用户岗位收藏Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-03
 */
@Service
public class JobRecommentServiceImpl extends ServiceImpl<JobCollectionMapper,JobCollection> implements IJobRecommentService
{
    @Autowired
    private JobCollectionMapper jobCollectionMapper;
    @Autowired
    private JobApplyMapper jobApplyMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    /**
     * 查询用户岗位收藏
     *
     * @param id 用户岗位收藏主键
     * @return 用户岗位收藏
     */
    @Override
    public JobCollection selectJobCollectionById(Long id)
    {
        return jobCollectionMapper.selectById(id);
    }

    /**
     * 查询用户岗位收藏列表
     *
     * @param jobCollection 用户岗位收藏
     * @return 用户岗位收藏
     */
    @Override
    public List<JobCollection> selectJobCollectionList(JobCollection jobCollection)
    {
        return jobCollectionMapper.selectJobCollectionList(jobCollection);
    }

    /**
     * 新增用户岗位收藏
     *
     * @param jobCollection 用户岗位收藏
     * @return 结果
     */
    @Override
    public int insertJobCollection(JobCollection jobCollection)
    {
        return jobCollectionMapper.insert(jobCollection);
    }

    /**
     * 修改用户岗位收藏
     *
     * @param jobCollection 用户岗位收藏
     * @return 结果
     */
    @Override
    public int updateJobCollection(JobCollection jobCollection)
    {
        return jobCollectionMapper.updateById(jobCollection);
    }

    /**
     * 批量删除用户岗位收藏
     *
     * @param ids 需要删除的用户岗位收藏主键
     * @return 结果
     */
    @Override
    public int deleteJobCollectionByIds(Long[] ids)
    {
        return jobCollectionMapper.deleteBatchIds(Arrays.asList(ids));
    }
    /**
     * 用户收藏岗位
     */
    @Override
    @Transactional
    public int jobCollection(Long jobId) {
        Long userId = SiteSecurityUtils.getUserId();
        List<JobCollection> jobCollections = jobCollectionMapper.selectList(Wrappers.<JobCollection>lambdaQuery().eq(JobCollection::getJobId, jobId).eq(JobCollection::getUserId, userId));
        if(jobCollections.size()>0){
            jobCollectionMapper.deleteBatchIds(jobCollections.stream().map(JobCollection::getId).collect(Collectors.toList()));
        }
        JobCollection jobCollection = new JobCollection();
        jobCollection.setJobId(jobId);
        jobCollection.setUserId(userId);
        jobCollectionMapper.insert(jobCollection);
        return 1;
    }
    /**
     * 用户取消收藏岗位
     */
    @Override
    public int cancel(Long jobId) {
        Long userId = SiteSecurityUtils.getUserId();
        List<JobCollection> jobCollections = jobCollectionMapper.selectList(Wrappers.<JobCollection>lambdaQuery().eq(JobCollection::getJobId, jobId).eq(JobCollection::getUserId, userId));
        if(jobCollections.size()>0){
            jobCollectionMapper.deleteBatchIds(jobCollections.stream().map(JobCollection::getId).collect(Collectors.toList()));
        }
        return 1;
    }

    @Override
    public int apply(Long jobId) {
        Long userId = SiteSecurityUtils.getUserId();
        JobApply jobApply = new JobApply();
        jobApply.setJobId(jobId);
        jobApply.setUserId(userId);
        //todo 计算匹配度
        jobApply.setMatchingDegree(1L);
        List<JobApply> jobApplies = jobApplyMapper.selectList(Wrappers.<JobApply>lambdaQuery().eq(JobApply::getJobId, jobId).eq(JobApply::getUserId, userId));
        if(jobApplies.size()>0){
            List<Long> ids = jobApplies.stream().map(JobApply::getId).collect(Collectors.toList());
            jobApplyMapper.deleteBatchIds(ids);
        }
        jobApplyMapper.insert(jobApply);
        return 1;
    }

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

    @Override
    public String competitiveness(Long jobId) {
        AppUser appUser = appUserMapper.selectById(SiteSecurityUtils.getUserId());
        String education = appUser.getEducation();
        return "90.5";
    }
}
