package com.qmth.themis.admin.api;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.themis.business.bean.admin.InvigilateListWarningBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.AuthDto;
import com.qmth.themis.business.dto.params.WarnInfoParams;
import com.qmth.themis.business.dto.response.TIeWarningNotifyDto;
import com.qmth.themis.business.entity.TBUser;
import com.qmth.themis.business.entity.TIeInvigilateWarnInfo;
import com.qmth.themis.business.enums.RoleEnum;
import com.qmth.themis.business.service.ThemisCacheService;
import com.qmth.themis.business.service.TIeInvigilateWarnInfoService;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 监考预警信息 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Api(tags = "监考预警信息Controller")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_ADMIN + "/invigilate/warn")
@Validated
public class TIeInvigilateWarnInfoController {

    @Resource
    TIeInvigilateWarnInfoService tIeInvigilateWarnInfoService;

    @Resource
    TOeExamRecordService tOeExamRecordService;

    @Resource
    ThemisCacheService themisCacheService;

    @ApiOperation(value = "预警提醒接口")
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"count\":1}", response = Result.class)})
    public Result notify(@ApiParam(value = "考试批次id", required = false) @RequestParam(required = false) Long examId,
                         @ApiParam(value = "考试场次id", required = false) @RequestParam(required = false) Long examActivityId,
                         @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode) {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = themisCacheService.addAccountAuthCache(tbUser.getId());
        //如果有监考员角色，只能查看自己所监考的考场，巡考员和管理员则可以查看全部考场
        Long userId = null;
        if (authDto.getRoleCodes().toString().contains(RoleEnum.INVIGILATE.name())) {
            userId = tbUser.getId();
        }
        Integer count = tIeInvigilateWarnInfoService.warningCount(examId, examActivityId, roomCode, userId, tbUser.getOrgId());
        return ResultUtil.ok(Collections.singletonMap(SystemConstant.COUNT, count));
    }

    @ApiOperation(value = "预警提醒列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "监考预警信息", response = InvigilateListWarningBean.class)})
    public Result list(@ApiParam(value = "考试批次id", required = false) @RequestParam(required = false) Long examId,
                       @ApiParam(value = "考试场次id", required = false) @RequestParam(required = false) Long examActivityId,
                       @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                       @ApiParam(value = "审阅状态", required = false) @RequestParam(required = false) Integer approveStatus,
                       @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                       @ApiParam(value = "证件号", required = false) @RequestParam(required = false) String identity,
                       @ApiParam(value = "陌生人脸min", required = false) @RequestParam(required = false) Integer minMultipleFaceCount,
                       @ApiParam(value = "陌生人脸max", required = false) @RequestParam(required = false) Integer maxMultipleFaceCount,
                       @ApiParam(value = "异常处理min", required = false) @RequestParam(required = false) Integer minExceptionCount,
                       @ApiParam(value = "异常处理max", required = false) @RequestParam(required = false) Integer maxExceptionCount,
                       @ApiParam(value = "预警量min", required = false) @RequestParam(required = false) Integer minWarningCount,
                       @ApiParam(value = "预警量max", required = false) @RequestParam(required = false) Integer maxWarningCount,
                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = themisCacheService.addAccountAuthCache(tbUser.getId());
        //如果有监考员角色，只能查看自己所监考的考场，巡考员和管理员则可以查看全部考场
        Long userId = null;
        if (authDto.getRoleCodes().toString().contains(RoleEnum.INVIGILATE.name())) {
            userId = tbUser.getId();
        }
        return ResultUtil.ok(tOeExamRecordService.invigilatePageWarningList(new Page<>(pageNumber, pageSize), examId, examActivityId, roomCode, approveStatus, name, identity, minMultipleFaceCount, maxMultipleFaceCount, minExceptionCount, maxExceptionCount, minWarningCount, maxWarningCount, userId, tbUser.getOrgId()));
    }

    @ApiOperation(value = "预警消息接口")
    @RequestMapping(value = "/message", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "预警通知信息", response = TIeWarningNotifyDto.class)})
    public Result warningMessage(@ApiParam(value = "考试批次id", required = false) @RequestParam(required = false) Long examId) {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = themisCacheService.addAccountAuthCache(tbUser.getId());
        //如果有监考员角色，只能查看自己所监考的考场，巡考员和管理员则可以查看全部考场
        Long userId = null;
        if (authDto.getRoleCodes().toString().contains(RoleEnum.INVIGILATE.name())) {
            userId = tbUser.getId();
        }
        return ResultUtil.ok(tIeInvigilateWarnInfoService.warningMessage(examId, userId, tbUser.getOrgId()));
    }

    @ApiOperation(value = "预警修改阅读状态接口")
    @RequestMapping(value = "/save/status", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    @Transactional
    public Result saveWarnStatus(@Validated @RequestBody WarnInfoParams warnInfoParams, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        //2020/11/24新增管理员不清除预警状态
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = themisCacheService.addAccountAuthCache(tbUser.getId());
        if (!authDto.getRoleCodes().toString().contains(RoleEnum.ADMIN.name())) {
            if (CollectionUtils.isEmpty(warnInfoParams.getWarningIds()) && Objects.isNull(warnInfoParams.getExamRecordId())) {
                throw new BusinessException("预警id数组或考试记录id不能同时为空");
            }
            UpdateWrapper<TIeInvigilateWarnInfo> tIeInvigilateWarnInfoUpdateWrapper = new UpdateWrapper<>();
            tIeInvigilateWarnInfoUpdateWrapper.lambda().set(TIeInvigilateWarnInfo::getApproveStatus, warnInfoParams.getApproveStatus());
            if (!CollectionUtils.isEmpty(warnInfoParams.getWarningIds())) {
                List<Long> warningTranIds = warnInfoParams.getWarningIds().stream().map(x -> Long.parseLong(x)).collect(Collectors.toList());
                tIeInvigilateWarnInfoUpdateWrapper.lambda().in(TIeInvigilateWarnInfo::getId, warningTranIds);
            } else if (Objects.nonNull(warnInfoParams.getExamRecordId())) {
                tIeInvigilateWarnInfoUpdateWrapper.lambda().eq(TIeInvigilateWarnInfo::getExamRecordId, warnInfoParams.getExamRecordId());
            }
            tIeInvigilateWarnInfoService.update(tIeInvigilateWarnInfoUpdateWrapper);
        }
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "预警提醒清除未阅列表接口")
    @RequestMapping(value = "/approveStatus/list/update", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "监考预警信息", response = InvigilateListWarningBean.class)})
    public Result approveStatusListUpdate(@ApiParam(value = "考试批次id", required = false) @RequestParam(required = false) Long examId,
                                          @ApiParam(value = "考试场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                          @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                          @ApiParam(value = "审阅状态", required = false) @RequestParam(required = false) Integer approveStatus,
                                          @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                          @ApiParam(value = "证件号", required = false) @RequestParam(required = false) String identity,
                                          @ApiParam(value = "陌生人脸min", required = false) @RequestParam(required = false) Integer minMultipleFaceCount,
                                          @ApiParam(value = "陌生人脸max", required = false) @RequestParam(required = false) Integer maxMultipleFaceCount,
                                          @ApiParam(value = "异常处理min", required = false) @RequestParam(required = false) Integer minExceptionCount,
                                          @ApiParam(value = "异常处理max", required = false) @RequestParam(required = false) Integer maxExceptionCount,
                                          @ApiParam(value = "预警量min", required = false) @RequestParam(required = false) Integer minWarningCount,
                                          @ApiParam(value = "预警量max", required = false) @RequestParam(required = false) Integer maxWarningCount) {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = themisCacheService.addAccountAuthCache(tbUser.getId());
        //如果有监考员角色，只能查看自己所监考的考场，巡考员和管理员则可以查看全部考场
        Long userId = null;
        if (authDto.getRoleCodes().toString().contains(RoleEnum.INVIGILATE.name())) {
            userId = tbUser.getId();
        }
        //2020/11/24新增管理员不清除预警状态
        if (!authDto.getRoleCodes().toString().contains(RoleEnum.ADMIN.name())) {
            tOeExamRecordService.approveStatusListUpdate(examId, examActivityId, roomCode, approveStatus, name, identity, minMultipleFaceCount, maxMultipleFaceCount, minExceptionCount, maxExceptionCount, minWarningCount, maxWarningCount, userId, tbUser.getOrgId());
        }
        return ResultUtil.ok(true);
    }
}