package com.ynfy.buss.exam.userexamresult.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.buss.exam.userexamresult.entity.UserExamResult;
import com.ynfy.buss.exam.userexamresult.service.IUserExamResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 用户考试成绩
 * @Author: jeecg-boot
 * @Date:   2023-03-09
 * @Version: V1.0
 */
@Api(tags="用户考试成绩")
@RestController
@RequestMapping("/userExamResult")
@Slf4j
public class UserExamResultController extends JeecgController<UserExamResult, IUserExamResultService> {
	@Autowired
	private IUserExamResultService userExamResultService;
	
	/**
	 * 分页列表查询
	 *
	 * @param userExamResult
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="用户考试成绩-分页列表查询", notes="用户考试成绩-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<UserExamResult>> queryPageList(UserExamResult userExamResult,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Page<UserExamResult> page = new Page<UserExamResult>(pageNo, pageSize);
		IPage<UserExamResult> pageList = userExamResultService.selectPageList(page, userExamResult);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param userExamResult
	 * @return
	 */
	@AutoLog(value = "用户考试成绩-添加")
	@ApiOperation(value="用户考试成绩-添加", notes="用户考试成绩-添加")
	//@RequiresPermissions("userExamResult:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody UserExamResult userExamResult) {
		userExamResultService.save(userExamResult);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param userExamResult
	 * @return
	 */
	@AutoLog(value = "用户考试成绩-编辑")
	@ApiOperation(value="用户考试成绩-编辑", notes="用户考试成绩-编辑")
	//@RequiresPermissions("userExamResult:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody UserExamResult userExamResult) {
		userExamResultService.updateById(userExamResult);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户考试成绩-通过id删除")
	@ApiOperation(value="用户考试成绩-通过id删除", notes="用户考试成绩-通过id删除")
	//@RequiresPermissions("userExamResult:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		userExamResultService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户考试成绩-批量删除")
	@ApiOperation(value="用户考试成绩-批量删除", notes="用户考试成绩-批量删除")
	//@RequiresPermissions("userExamResult:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.userExamResultService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户考试成绩-通过id查询")
	@ApiOperation(value="用户考试成绩-通过id查询", notes="用户考试成绩-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<UserExamResult> queryById(@RequestParam(name="id",required=true) String id) {
		UserExamResult userExamResult = userExamResultService.getById(id);
		if(userExamResult==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(userExamResult);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param userExamResult
    */
    //@RequiresPermissions("userExamResult:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, UserExamResult userExamResult) {
        return super.exportXls(request, userExamResult, UserExamResult.class, "用户考试成绩");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("userExamResult:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, UserExamResult.class);
    }

	 /**
	  * 获取用户考试成绩
	  *
	  * @param examId
	  * @return
	  */
	 @GetMapping(value = "/examScore")
	 public Result<?> examScore(@RequestParam(name = "examId") String examId) {
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 return Result.ok(userExamResultService.examScore(user.getId(), examId));
	 }

}
