package com.miaxis.pc.controller.question;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.SysRole;
import com.miaxis.common.core.domain.entity.SysUser;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.score.domain.ScoreInfo;
import com.miaxis.score.dto.ScoreInfoAllDTO;
import com.miaxis.score.dto.ScoreInfoDTO;
import com.miaxis.score.dto.ScoreInfoPcDTO;
import com.miaxis.score.dto.ScoreInfoUserIdDTO;
import com.miaxis.score.service.IScoreInfoService;
import com.miaxis.score.vo.ScoreIStudentInfoPcVo;
import com.miaxis.user.service.IUserInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 【模拟考成绩】Controller
 *
 * @author miaxis
 * @date 2021-08-23
 */
@RestController
@RequestMapping("score/info")
@Api(tags = {"【PC-模拟考成绩】"})
public class PcScoreInfoController extends BaseController {
    @Autowired
    private IScoreInfoService scoreInfoService;


    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 根据当前用户查询模拟考成绩列表
     */
    @GetMapping("/list")
    @ApiOperation("查询模拟考成绩列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<ScoreInfo> list(){
        startPage();
        ScoreInfoUserIdDTO  scoreInfoUserIdDTO = new ScoreInfoUserIdDTO();
        scoreInfoUserIdDTO.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        scoreInfoUserIdDTO.setDeviceType(2);
        List<ScoreInfo> list = scoreInfoService.selectScoreInfoList(scoreInfoUserIdDTO);
        return toResponsePageInfo(list);
    }


    /**
     * 新增模拟考成绩
     */
    @PostMapping
    @ApiOperation("新增模拟考成绩")
    public Response<Integer> add(@RequestBody ScoreInfoDTO scoreInfoDTO) {
        ScoreInfo scoreInfo = new ScoreInfo();
        BeanUtils.copyProperties(scoreInfoDTO, scoreInfo);
        scoreInfo.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        scoreInfo.setDeviceType(2);
        return toResponse(scoreInfoService.save(scoreInfo) ? 1 : 0);
    }


    /**
     * 查询学员列表
     */
    @GetMapping("/studentlist")
    @ApiOperation("查询驾校学员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<ScoreIStudentInfoPcVo> studentList(@ModelAttribute ScoreInfoPcDTO scoreInfoPcDTO) {
        startPage();
        //判断该用户是否拥有amin或者manager角色
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        Boolean hasAdminAuthority = false;
        for (SysRole sysRole : roles) {
            if (sysRole.getRoleKey().equals("admin") || sysRole.getRoleKey().equals("manager")) {
                hasAdminAuthority = true;
            }
        }
        if (!hasAdminAuthority) {
            scoreInfoPcDTO.setSchoolName(user.getNickName());
        }
        List<ScoreIStudentInfoPcVo> list = scoreInfoService.selectStudentList(scoreInfoPcDTO);
        return toResponsePageInfo(list);
    }

    /**
     * 查询学员成绩
     */
    @GetMapping("/studentscore/{userId}")
    @ApiOperation("查询学员成绩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<ScoreInfo> studentscore(@PathVariable("userId") Long userId) {
        startPage();
        List<ScoreInfo> list = scoreInfoService.list(new QueryWrapper<ScoreInfo>().eq("user_id", userId)
                                                        .eq("device_type",2).orderByDesc("create_time"));
        return toResponsePageInfo(list);
    }


    /**
     * 删除模拟考成绩
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除模拟考成绩")
    public Response<Integer> remove(
            @ApiParam(name = "ids", value = "模拟考成绩ids参数", required = true)
            @PathVariable Long[] ids
    ) {
        return toResponse(scoreInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * 根据userID获取最大成绩，平均成绩，预测成绩
     */
    @GetMapping(value = "/getScoreInfoAllByUserId")
    @ApiOperation("根据userID获取最大成绩，平均成绩，预测成绩")
    public Response<ScoreInfoAllDTO> getScoreInfoAllByUserId(ScoreInfoUserIdDTO scoreInfoUserIdDTO) {
        ScoreInfo scoreInfo = new ScoreInfo();
        scoreInfo.setUserId(scoreInfoUserIdDTO.getUserId());
        ScoreInfoAllDTO scoreInfoAllDTO = scoreInfoService.getScoreInfoAll(scoreInfo);
        return Response.success(scoreInfoAllDTO);
    }


}
