package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <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", "上架").eq("deleted", 0));
        return count;
    }

    /**
     * 个人发布的需求
     * @param request
     * @return
     */
    @Override
    public IPage<StuNeeds> getPersonCourse(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        Page<StuNeeds> stuNeedsPage = new Page<StuNeeds>(Long.parseLong(pageNum), Long.parseLong(pageSize));
        IPage<StuNeeds> stuNeedsIPage = baseMapper.selectPage(stuNeedsPage, new QueryWrapper<StuNeeds>().eq("uid", uid).eq("deleted", 0).orderByDesc("datetime"));
        return stuNeedsIPage;
    }

    /**
     * 更新个人需求上下架
     * @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;
    }

    /**
     * 查询所有需求
     * @return
     */
    @Override
    public Map getAllRequirements(HttpServletRequest request) {

        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        Page<StuNeeds> page = new Page<>(Long.parseLong(pageNum), Long.parseLong(pageSize));
        IPage<StuNeeds> stuNeedsIPage = baseMapper.selectPage(page, new QueryWrapper<StuNeeds>().eq("deleted", 0).eq("deal", "未成交").eq("display", "上架"));

        List<StuNeeds> stuNeeds = stuNeedsIPage.getRecords();
        for (StuNeeds stuNeed : stuNeeds) {
            stuNeed.setWxid(null);
            stuNeed.setPhone(null);
            stuNeed.setId(null);
        }

        long total = stuNeedsIPage.getTotal();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("list", stuNeeds);
        return hashMap;
    }

}
