package com.xunwang.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.commonutils.RedisIdWorker;
import com.xunwang.education.entity.StuNeeds;
import com.xunwang.education.entity.vo.StuNeedsVo;
import com.xunwang.education.service.StudentRequirementsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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
 */
@RestController
@RequestMapping("/education/student-requirements")
@Api("学生需求表")
@ApiSupport(author = "Long")
public class StudentRequirementsController {

    @Resource
    private StudentRequirementsService requirementsService;

    @Resource
    private RedisIdWorker redisIdWorker;

    @ApiOperation("发布学生需求")
    @PostMapping("publishRequirements")
    public R publishRequirements(@RequestBody StuNeeds requirements, HttpServletRequest request) {
        boolean b = requirementsService.publishRequirements(requirements, request);
        if (b) {
            return R.ok().message("提交成功,请等待审核");
        } else {
            return R.ok().message("提交失败,请稍后再试");
        }
    }

    @ApiOperation("分页查询所有学员需求信息")
    @PostMapping("getAllRequirements")
    public R getAllRequirements(@RequestBody StuNeedsVo stuNeedsVo) {
        Map<String, Object> map = requirementsService.getAllRequirements(stuNeedsVo);
        return R.ok().data(map);
    }

    @ApiOperation("查询个人上架了需求数量 以及 个人全部的需求信息")
    @GetMapping("getPublishCounts")
    public R getPublishCounts(HttpServletRequest request) {
        HashMap<String, Object> hashMap = requirementsService.getPersonCourse(request);
        return R.ok().data(hashMap);
    }

    @ApiOperation("更新个人需求上下架")
    @GetMapping("updatePersonDisplay")
    public R updatePersonDisplay(HttpServletRequest request) {
        boolean b = requirementsService.updatePersonDisplay(request);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("查询个人的需求号和需求科目")
    @GetMapping("queryPersonSomeInfo")
    public R queryPersonSomeInfo(HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<StuNeeds> queryWrapper = new QueryWrapper<StuNeeds>().eq("uid", uid).eq("display", "上架").eq("locked", "未锁");
        List<StuNeeds> list = requirementsService.list(queryWrapper);
        return R.ok().data("list", list);
    }

    @ApiOperation("获取需求详情")
    @GetMapping("/getStuNeedsDetailById")
    public R getStuNeedsDetailById(HttpServletRequest request) {
        String requireId = request.getParameter("requireId");
        StuNeeds one = requirementsService.getStuNeedDetail(request);
        Double count = redisIdWorker.recordViewCount("stuNeed", requireId);

        one.setWxid(null).setPhone(null).setId(null).setViewedCount(count.intValue());
        return R.ok().data("one", one);
    }

    @ApiOperation("获取个人需求详情")
    @GetMapping("/getPublishStuNeedsDetailById")
    public R getPublishStuNeedsDetailById(HttpServletRequest request) {
        StuNeeds one = requirementsService.getStuNeedDetail(request);
        one.setId(null);
        return R.ok().data("one", one);
    }
}

