package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.education.entity.MyFavoriteNeeds;

import com.xunwang.education.entity.dto.MyStuNeedsDto;
import com.xunwang.education.mapper.MyFavoriteNeedsMapper;
import com.xunwang.education.service.MyFavoriteNeedsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Long
 * @since 2022-12-30
 */
@Service
public class MyFavoriteNeedsServiceImpl extends ServiceImpl<MyFavoriteNeedsMapper, MyFavoriteNeeds> implements MyFavoriteNeedsService {

    /**
     * 收藏学员需求
     * @param request
     */
    @Override
    public int collect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String requireId = request.getParameter("requireId");
        MyFavoriteNeeds myFavoriteNeeds = new MyFavoriteNeeds();
        myFavoriteNeeds.setRequireId(requireId);
        myFavoriteNeeds.setUid(uid);
        int insert = baseMapper.insert(myFavoriteNeeds);
        return insert;
    }

    /**
     * 取消收藏学员需求
     * @param request
     * @return
     */
    @Override
    public boolean cancelCollect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String requireId = request.getParameter("requireId");
        boolean remove = remove(new QueryWrapper<MyFavoriteNeeds>().eq("uid", uid).eq("require_id", requireId));
        return remove;
    }

    /**
     * 查询个人收藏的学员信息
     * @param request
     */
    @Override
    public List<String> findPersonCollect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<MyFavoriteNeeds> queryWrapper = new QueryWrapper<MyFavoriteNeeds>().select("require_id").eq("uid", uid);
        List<MyFavoriteNeeds> list = list(queryWrapper);
        ArrayList<String> requires = new ArrayList<String>();
        for (MyFavoriteNeeds myFavoriteNeeds : list) {
            requires.add(myFavoriteNeeds.getRequireId());
        }
        return requires;
    }

    /**
     * 查询个人收藏的学员需求全部信息
     * @param request
     * @return
     */
    @Override
    public List<MyStuNeedsDto> findPersonCollectInfo(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        return baseMapper.findPersonCollectInfo(uid);
    }
}
