package com.ynfy.buss.practice.userpractice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.buss.practice.userpractice.entity.UserPractice;
import com.ynfy.buss.practice.userpractice.entity.dto.UserPracticeDTO;
import com.ynfy.buss.practice.userpractice.service.IUserPracticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
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-11-19
 * @Version: V1.0
 */
@Api(tags = "用户练习表")
@RestController
@RequestMapping("/practice/userPractice")
@Slf4j
public class UserPracticeController extends JeecgController<UserPractice, IUserPracticeService> {
    @Autowired
    private IUserPracticeService userPracticeService;

    /**
     * 分页列表查询
     *
     * @param userPractice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户练习表-分页列表查询")
    @ApiOperation(value = "用户练习表-分页列表查询", notes = "用户练习表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<UserPractice>> queryPageList(UserPractice userPractice,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<UserPractice> page = new Page<>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        userPractice.setUserId(user.getId());
        IPage<UserPractice> pageList = userPracticeService.selectPageList(page, userPractice);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param userPractice
     * @return
     */
    @AutoLog(value = "用户练习表-添加")
    @ApiOperation(value = "用户练习表-添加", notes = "用户练习表-添加")
    //@RequiresPermissions("practice.userpractice:user_practice:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody UserPractice userPractice) {
        userPracticeService.save(userPractice);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param userPractice
     * @return
     */
    @AutoLog(value = "用户练习表-编辑")
    @ApiOperation(value = "用户练习表-编辑", notes = "用户练习表-编辑")
    //@RequiresPermissions("practice.userpractice:user_practice:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody UserPractice userPractice) {
        userPracticeService.updateById(userPractice);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户练习表-通过id删除")
    @ApiOperation(value = "用户练习表-通过id删除", notes = "用户练习表-通过id删除")
    //@RequiresPermissions("practice.userpractice:user_practice:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        userPracticeService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户练习表-批量删除")
    @ApiOperation(value = "用户练习表-批量删除", notes = "用户练习表-批量删除")
    //@RequiresPermissions("practice.userpractice:user_practice:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.userPracticeService.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<UserPractice> queryById(@RequestParam(name = "id", required = true) String id) {
        UserPractice userPractice = userPracticeService.getById(id);
        if (userPractice == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(userPractice);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param userPractice
     */
    //@RequiresPermissions("practice.userpractice:user_practice:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, UserPractice userPractice) {
        return super.exportXls(request, userPractice, UserPractice.class, "用户练习表");
    }

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

    /**
     * 获取用户最近的练习
     *
     * @return
     */
    @ApiOperation(value = "获取用户最近的练习", notes = "获取用户最近的练习")
    @GetMapping(value = "/getRecentPractice")
    public Result<?> getRecentPractice(@RequestParam String repositoryId) {
        return Result.OK(userPracticeService.getRecentPractice(repositoryId));
    }


    /**
     * 提交练习
     *
     * @return
     */
    @ApiOperation(value = "提交练习")
    @PostMapping(value = "/submitPractice")
    public Result<?> submitPractice(@RequestBody UserPracticeDTO dto) {
        return Result.OK("提交练习成功！", userPracticeService.submitPractice(dto));
    }

    /**
     * 获取练习结果
     *
     * @return
     */
    @ApiOperation(value = "获取练习结果", notes = "获取练习结果")
    @GetMapping(value = "/getPracticeResult")
    public Result<?> getPracticeResult(@RequestParam String id) {
        return Result.OK(userPracticeService.getPracticeResult(id));
    }

}
