package com.xunwang.education.controller;

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.education.entity.StuNeeds;
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;

    @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("查询所有学员需求信息")
    @GetMapping("getAllRequirements")
    public R getAllRequirements(HttpServletRequest request) {
        Map map = requirementsService.getAllRequirements(request);
        return R.ok().data(map);
    }

    @ApiOperation("查询个人上架了需求数量 以及 个人全部的需求信息")
    @GetMapping("getPublishCounts")
    public R getPublishCounts(HttpServletRequest request) {
        int publishCounts = requirementsService.getPublishCounts(request);
        IPage<StuNeeds> personCourse = requirementsService.getPersonCourse(request);
        List<StuNeeds> records = personCourse.getRecords();
        long total = personCourse.getTotal();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("count", total);
        hashMap.put("publishCounts", publishCounts);
        hashMap.put("list", records);
        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();
        }
    }
}

