package com.xunwang.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.vo.TeacherCoursesVo;
import com.xunwang.education.entity.vo.TeacherList;
import com.xunwang.education.entity.vo.TeacherListQuery;
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.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private TeacherCoursesService teacherCoursesService;

    @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) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        List<TeacherCourses> teacherCourses = teacherCoursesService.showPersonCourse(uid);
        return R.ok().data("courses", teacherCourses);
    }

    @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<TeacherList> teacherLists = teacherCoursesService.queryAllByConditions(queryObj);
        int count = teacherCoursesService.count(new QueryWrapper<TeacherCourses>().eq("verify_status", "已通过").eq("display", "上架").eq("deal", "未成交").eq("deleted", "0"));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("course", teacherLists);
        hashMap.put("count", count);
        return R.ok().data(hashMap);
    }

    @ApiOperation("查询老师详情总成交次数和最近成交时间")
    @GetMapping("queryCountAndLastTime")
    public R queryCountAndLastTime(HttpServletRequest request) {
        String uid = request.getParameter("uid");
        System.out.println(uid);
        Map map = teacherCoursesService.queryCountAndLastTime(uid);
        return R.ok().data(map);
    }

}

