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.MyFavoriteCourses;
import com.xunwang.education.entity.dto.MyTeacherCoursesDto;
import com.xunwang.education.mapper.MyFavoriteCoursesMapper;
import com.xunwang.education.service.MyFavoriteCoursesService;
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 2023-01-04
 */
@Service
public class MyFavoriteCoursesServiceImpl extends ServiceImpl<MyFavoriteCoursesMapper, MyFavoriteCourses> implements MyFavoriteCoursesService {

    @Override
    public List<String> findPersonCollect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<MyFavoriteCourses> queryWrapper = new QueryWrapper<MyFavoriteCourses>().select("course_id").eq("uid", uid);
        List<MyFavoriteCourses> list = list(queryWrapper);
        ArrayList<String> requires = new ArrayList<String>();
        for (MyFavoriteCourses myFavoriteNeeds : list) {
            requires.add(myFavoriteNeeds.getCourseId());
        }
        return requires;
    }

    @Override
    public int collect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String requireId = request.getParameter("courseId");
        MyFavoriteCourses myFavoriteNeeds = new MyFavoriteCourses();
        myFavoriteNeeds.setCourseId(requireId);
        myFavoriteNeeds.setUid(uid);
        return baseMapper.insert(myFavoriteNeeds);
    }

    @Override
    public boolean cancelCollect(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String requireId = request.getParameter("courseId");
        return remove(new QueryWrapper<MyFavoriteCourses>().eq("uid", uid).eq("course_id", requireId));
    }

    @Override
    public List<MyTeacherCoursesDto> findPersonCollection(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        return baseMapper.selectCollectCourse(uid);
    }

}
