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.JobFair;
import com.ruoyi.cms.mapper.FairCollectionMapper;
import com.ruoyi.cms.mapper.JobFairMapper;
import com.ruoyi.cms.service.IFairCollectionService;
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.*;
import java.util.stream.Collectors;

/**
 * 用户招聘会收藏Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-03
 */
@Service
public class FairCollectionServiceImpl extends ServiceImpl<FairCollectionMapper, FairCollection> implements IFairCollectionService
{
    @Autowired
    private FairCollectionMapper fairCollectionMapper;
    @Autowired
    private JobFairMapper jobFairMapper;
    /**
     * 查询用户招聘会收藏
     *
     * @param id 用户招聘会收藏主键
     * @return 用户招聘会收藏
     */
    @Override
    public FairCollection selectFairCollectionById(Long id)
    {
        return fairCollectionMapper.selectById(id);
    }

    /**
     * 查询用户招聘会收藏列表
     *
     * @param fairCollection 用户招聘会收藏
     * @return 用户招聘会收藏
     */
    @Override
    public List<FairCollection> selectFairCollectionList(FairCollection fairCollection)
    {
        List<FairCollection> fairCollections = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getUserId, SiteSecurityUtils.getUserId()));
        return fairCollections;
    }

    /**
     * 新增用户招聘会收藏
     *
     * @param fairCollection 用户招聘会收藏
     * @return 结果
     */
    @Override
    public int insertFairCollection(FairCollection fairCollection)
    {
        return fairCollectionMapper.insert(fairCollection);
    }

    /**
     * 修改用户招聘会收藏
     *
     * @param fairCollection 用户招聘会收藏
     * @return 结果
     */
    @Override
    public int updateFairCollection(FairCollection fairCollection)
    {
        return fairCollectionMapper.updateById(fairCollection);
    }

    /**
     * 批量删除用户招聘会收藏
     *
     * @param ids 需要删除的用户招聘会收藏主键
     * @return 结果
     */
    @Override
    public int deleteFairCollectionByIds(Long[] ids)
    {
        return fairCollectionMapper.deleteBatchIds(Arrays.asList(ids));
    }
    /**
     * 用户收藏招聘会
     */
    @Override
    @Transactional
    public int fairCollection(Long fairId) {
        Long userId = SiteSecurityUtils.getUserId();
        List<FairCollection> fairCollections = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getFairId, fairId).eq(FairCollection::getUserId, userId));
        if(fairCollections.size()>0){
            fairCollectionMapper.deleteBatchIds(fairCollections.stream().map(FairCollection::getId).collect(Collectors.toList()));
        }
        FairCollection fairCollection = new FairCollection();
        fairCollection.setFairId(fairId);
        fairCollection.setUserId(userId);
        fairCollectionMapper.insert(fairCollection);
        return 1;
    }
    /**
     * 用户取消收藏招聘会
     */
    @Override
    public int cancel(Long fairId) {
        Long userId = SiteSecurityUtils.getUserId();
        List<FairCollection> fairCollections = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getFairId, fairId).eq(FairCollection::getUserId, userId));
        if(fairCollections.size()>0){
            fairCollectionMapper.deleteBatchIds(fairCollections.stream().map(FairCollection::getId).collect(Collectors.toList()));
        }
        return 1;
    }
    /**
     * 用户收藏招聘会列表
     */
    @Override
    public List<JobFair> appCollectionFair(Integer type) {
        return jobFairMapper.selectAppList(SiteSecurityUtils.getUserId(),type);
    }
}
