package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.RedisIdWorker;
import com.xunwang.education.entity.StuNeeds;
import com.xunwang.education.mapper.StudentRequirementsMapper;
import com.xunwang.education.service.StudentRequirementsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 需求表 服务实现类
 * </p>
 *
 * @author Long
 * @since 2022-11-16
 */
@Service
public class StudentRequirementsServiceImpl extends ServiceImpl<StudentRequirementsMapper, StuNeeds> implements StudentRequirementsService {

    @Resource
    private RedisIdWorker redisIdWorker;

    /**
     * 发布学生需求
     * @param requirements
     * @param request
     * @return
     */
    @Override
    public boolean publishRequirements(StuNeeds requirements, HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        long next = redisIdWorker.nextId("requirements");
        String requireId = String.valueOf(next);
        StuNeeds stuNeeds = requirements.setUid(uid).setRequireId(requireId);

        boolean save = save(stuNeeds);
        return save;
    }

    /**
     * 查询个人上架了需求数量
     * @param request
     */
    @Override
    public int getPublishCounts(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int count = count(new QueryWrapper<StuNeeds>().eq("uid", uid).eq("deal", "未成交").eq("display", "上架"));
        return count;
    }

    /**
     * 个人发布的需求
     * @param request
     * @return
     */
    @Override
    public List<StuNeeds> getPersonCourse(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        List<StuNeeds> list = baseMapper.selectList(new QueryWrapper<StuNeeds>().eq("uid", uid).eq("deleted", 0));
        return list;
    }

    /**
     * 更新个人需求上下架
     * @param request
     */
    @Override
    public boolean updatePersonDisplay(HttpServletRequest request) {
        String display = request.getParameter("display");
        String requireId = request.getParameter("requireId");
        StuNeeds stuNeeds = baseMapper.selectOne(new QueryWrapper<StuNeeds>().eq("require_id", requireId));
        stuNeeds.setDisplay(display);
        boolean flag = saveOrUpdate(stuNeeds);
        return flag;
    }
}
