package com.xunwang.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.dto.MyTeacherCourseDetailDto;
import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.servicebase.utils.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);
    }

    @ApiOperation("查看需求手机号和微信号")
    @GetMapping("/getStuNeedPhoneAndWeixin")
    public R getStuNeedPhoneAndWeixin(HttpServletRequest request) {
        String uid = request.getParameter("uid");
        LambdaQueryWrapper<StuNeeds> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuNeeds::getUid, uid);
        StuNeeds one = requirementsService.getOne(wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", one.getPhone());
        map.put("weixin", one.getWxid());
        return R.ok().data(map);
    }

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/18 11:40
     *
     */
    @ApiOperation("查询 全部 审核中 的 需求 信息")
    @GetMapping("getAllCheckingNeedInfo")
    public R getAllCheckingNeedInfo(HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        LambdaQueryWrapper<StuNeeds> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuNeeds::getVerifyStatus, "审核中");
        wrapper.eq(StuNeeds::getDeleted, "0");

        List<StuNeeds> list = requirementsService.list(wrapper);
        return R.ok().data("list", list);
    }

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/18 11:40
     *
     */
    @ApiOperation("审核 学员需求 信息")
    @GetMapping("updateStuNeedInfo")
    public R updateStuNeedInfo(HttpServletRequest request) {
        String requireid = request.getParameter("requireid");
        String checkstatus = request.getParameter("checkstatus");
        String checktext = request.getParameter("checktext");
        String display = request.getParameter("display");

        LambdaQueryWrapper<StuNeeds> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuNeeds::getRequireId, requireid);
        StuNeeds one = requirementsService.getOne(wrapper);

        one.setVerifyStatus(checkstatus);
        one.setVerifyRefuseReason(checktext);
        one.setDisplay(display);

        boolean uid1 = requirementsService.update(one, new QueryWrapper<StuNeeds>().eq("require_id", requireid));
        if (uid1) {
            return R.ok().message("学员需求审核成功");
        } else {
            return R.ok().message("学员需求审核失败");
        }
    }

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/25 16:10
     */
    @ApiOperation("删除已发布的需求,不是真删除 ")
    @GetMapping("deleteNeedById")
    public R deleteNeedById(HttpServletRequest request) {
        String requireId = request.getParameter("requireId");

        LambdaQueryWrapper<StuNeeds> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuNeeds::getRequireId, requireId);
        StuNeeds one = requirementsService.getOne(wrapper);
        one.setDeleted("1");
        one.setDisplay("下架");

        boolean uid1 = requirementsService.update(one, new QueryWrapper<StuNeeds>().eq("require_id", requireId));
        if (uid1) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("删除失败！");
        }
    }

}

