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.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.servicebase.utils.RedisIdWorker;
import com.xunwang.education.entity.TeacherCertifications;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.dto.MyTeacherCourseDetailDto;
import com.xunwang.education.entity.dto.MyTeacherCoursesDto;
import com.xunwang.education.entity.vo.TeacherCoursesVo;
import com.xunwang.education.entity.vo.TeacherListQuery;
import com.xunwang.education.service.TeacherCertificationsService;
import com.xunwang.education.service.TeacherCoursesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 老师课程发布 前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-12-04
 */
@ApiSupport(author = "Long")
@Api("老师课程发布")
@RestController
@RequestMapping("/education/teacher-courses")
public class TeacherCoursesController {

    @Autowired
    private TeacherCoursesService teacherCoursesService;

    @Autowired
    private TeacherCertificationsService teacherCertificationsService;

    @Resource
    private RedisIdWorker redisIdWorker;

    @ApiOperation("课程发布")
    @PostMapping("publishCourse")
    public R publishCourse(@RequestBody TeacherCoursesVo teacherCoursesVo, HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int i = teacherCoursesService.addTeacherCourses(teacherCoursesVo, uid);
        if (i > 0) {
            return R.ok().message("发布成功,请耐心等待审核");
        } else {
            return R.ok().message("发布失败！");
        }
    }

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

    @ApiOperation("删除已发布的课程")
    @GetMapping("deletePersonCourse")
    public R deletePersonCourse(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");
        Integer id = Integer.valueOf(courseId);
        int i = teacherCoursesService.deletePersonCourse(id);
        if (i > 0) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("删除失败！");
        }
    }

    @ApiOperation("查询个人是否有正在审核的课程")
    @GetMapping("queryPersonVerifyCourse")
    public R queryPersonVerifyCourse(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        boolean flag = teacherCoursesService.queryPersonVerifyCourse(uid);
        return R.ok().data("flag", flag);
    }

    @ApiOperation("查询个人未成交的课程数")
    @GetMapping("queryPersonNoDeal")
    public R queryPersonNoDeal(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int count = teacherCoursesService.queryPersonNoDeal(uid);
        return R.ok().data("count", count);
    }

    @ApiOperation("查询全部课程")
    @PostMapping("showCourse")
    public R showCourse(@RequestBody TeacherListQuery queryObj) {
        List<MyTeacherCoursesDto> teacherLists = teacherCoursesService.queryAllByConditions(queryObj);
        int count = teacherCoursesService.count(new QueryWrapper<TeacherCourses>().eq("display", "上架").eq("locked", "未锁"));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("course", teacherLists);
        hashMap.put("count", count);
        return R.ok().data(hashMap);
    }

    @ApiOperation("查询个人的课程号和辅导科目")
    @GetMapping("queryPersonSomeInfo")
    public R queryPersonSomeInfo(HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        String name = "";
        QueryWrapper<TeacherCourses> queryWrapper = new QueryWrapper<TeacherCourses>().eq("uid", uid).eq("locked", "未锁").eq("display", "上架");
        List<TeacherCourses> list = teacherCoursesService.list(queryWrapper);
        TeacherCertifications one = teacherCertificationsService.getOne(new LambdaQueryWrapper<TeacherCertifications>().eq(TeacherCertifications::getUid, uid));
        if (one != null) {
            name = one.getName();
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", list);
        hashMap.put("name", name);
        return R.ok().data(hashMap);
    }

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

    @ApiOperation("查询课程详情信息")
    @GetMapping("/getCourseDetailById")
    public R getCourseDetailById(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");
        TeacherCourses one = teacherCoursesService.getById(courseId);
        Double count = redisIdWorker.recordViewCount("course", courseId);
        one.setViewedCount(count.intValue());
        return R.ok().data("one", one);
    }

    @ApiOperation("查询个人课程详情信息")
    @GetMapping("/getPersonCourseDetailById")
    public R getPersonCourseDetailById(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");
        MyTeacherCourseDetailDto courseDetail = teacherCoursesService.getPersonCourseDetailById(courseId);
        Double count = redisIdWorker.recordViewCount("course", courseId);
        courseDetail.setViewedCount(count.intValue());
        return R.ok().data("one", courseDetail);
    }

/**
 * TODO
 *
 * @Description
 * @Author Paul  Email:8203193@qq.com
 * @Version 1.0
 * @Date 2023/6/15 15:42
 *
 */
    @ApiOperation("查询 全部 审核中 的 课程 信息")
    @GetMapping("getAllCheckingCourseInfo")
    public R getAllCheckingCourseInfo(HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        List<MyTeacherCourseDetailDto> list = teacherCoursesService.getAllCourseDetailListByVerifyStatus("审核中");
        return R.ok().data("list", list);
    }

/**
 * TODO
 *
 * @Description
 * @Author Paul  Email:8203193@qq.com
 * @Version 1.0
 * @Date 2023/6/15 17:06
 *
 */
    @ApiOperation("审核 教员课程 信息")
    @GetMapping("updateTeacherCourseInfo")
    public R updateTeacherQualificationInfo(HttpServletRequest request) {
        String courseid = request.getParameter("courseid");
        String checkstatus = request.getParameter("checkstatus");
        String checktext = request.getParameter("checktext");
        String display = request.getParameter("display");

        LambdaQueryWrapper<TeacherCourses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherCourses::getCourseId, courseid);
        TeacherCourses one = teacherCoursesService.getOne(wrapper);

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

        boolean uid1 = teacherCoursesService.update(one, new QueryWrapper<TeacherCourses>().eq("course_id", courseid));
        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/24 0:55
     */
    @ApiOperation("删除已发布的课程,不是真删除 ")
    @GetMapping("deleteCourseByCourseId")
    public R deleteCourseByCourseId(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");

        LambdaQueryWrapper<TeacherCourses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherCourses::getCourseId, courseId);
        TeacherCourses one = teacherCoursesService.getOne(wrapper);
        one.setDeleted("1");
        one.setDisplay("下架");

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


}

