package com.zd.laboratory.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabBlacklist;
import com.zd.laboratory.domain.LabNegativelistHistory;
import com.zd.laboratory.domain.LabViolation;
import com.zd.laboratory.domain.vo.LabBlackdetailVO;
import com.zd.laboratory.domain.vo.LabNegativeListRecordsVO;
import com.zd.laboratory.domain.vo.LabNegativelistVO;
import com.zd.laboratory.service.ILabBlacklistService;
import com.zd.laboratory.service.ILabNegativelistHistoryService;
import com.zd.laboratory.service.ILabViolationService;
import com.zd.system.api.exam.RemoteExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabNegativeListRecords;
import com.zd.laboratory.service.ILabNegativeListRecordsService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 负面清单列表Controller
 *
 * @author zd
 * @date 2021-12-15
 */
@RestController
@Api(tags = "【负面清单列表】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
@RequestMapping("/negativeListRecords")
public class LabNegativeListRecordsController extends BaseController
{
    @Autowired
    private ILabNegativeListRecordsService labNegativeListRecordsService;

    @Autowired
    private ILabViolationService labViolationService;

    @Autowired
    private ILabBlacklistService labBlacklistService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ILabNegativelistHistoryService negativelistHistoryService;

    @Autowired
    private RemoteExamService remoteExamService;

    /**
     * 查询负面清单列表列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询负面清单列表列表")
    public TableDataInfo<LabNegativeListRecords> list(LabNegativeListRecordsVO vo)
    {
        startPage();
        vo.setCreact_id(tokenService.getLoginUser().getUserid());
        List<LabNegativeListRecords> list = labNegativeListRecordsService.selectLabNegativeListRecordsList(vo);
        return getDataTable(list);
    }

    /**
     * 导出负面清单列表列表
     */
   /* @ApiOperation(value = "导出负面清单列表列表")
    @PreAuthorize(hasPermi = "laboratory:negativeListRecords:export")
    @Log(title = "负面清单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabNegativeListRecords labNegativeListRecords) throws IOException
    {
        List<LabNegativeListRecords> list = labNegativeListRecordsService.selectLabNegativeListRecordsList(labNegativeListRecords);
        ExcelUtil<LabNegativeListRecords> util = new ExcelUtil<LabNegativeListRecords>(LabNegativeListRecords.class);
        util.exportExcel(response, list, "负面清单列表数据");
    }*/

    /**
     * 获取负面清单列表详细信息
     */
    @ApiOperation(value = "获取负面清单列表详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<LabNegativeListRecords> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(labNegativeListRecordsService.selectLabNegativeListRecordsById(id));
    }

    /**
     * 新增负面清单列表
     */
    @ApiOperation(value = "新增负面清单列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.ADD)
    @Log(title = "负面清单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody LabNegativelistHistory history)
    {
        history.setUserId(tokenService.getLoginUser().getUserid());
        return labNegativeListRecordsService.insertLabNegativeListRecords(history);
    }

    /**
     * 修改负面清单列表
     */
    @ApiOperation(value = "修改负面清单列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.EDIT)
    @Log(title = "负面清单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabNegativeListRecords labNegativeListRecords)
    {
        return  ResultData.result(labNegativeListRecordsService.updateLabNegativeListRecords(labNegativeListRecords));
    }

    /**
     * 删除负面清单列表
     */
    @ApiOperation(value = "删除负面清单列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.REMOVE)
    @Log(title = "负面清单列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(labNegativeListRecordsService.deleteLabNegativeListRecordsByIds(ids));
    }

    /**
     * 加入黑名单
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.EDIT)
    @Log(title = "黑名单记录", businessType = BusinessType.UPDATE)
    @PostMapping("/joinBlackList/{ids}")
    public AjaxResult joinBlackList(@RequestParam String reason,
                                    @RequestParam String creditScore,
                                    @PathVariable("ids") Long... ids) {
        LabViolation violation = labViolationService.selectLabViolationById(ids[0]);
        LabBlacklist blackList = new LabBlacklist();
        blackList.setJoinUserId(violation.getJoinUserId());
        blackList.setBlacklistStatus(1);
        blackList.setDeptId(violation.getDeptId());
        blackList.setDeptName(violation.getDeptName());
        blackList.setUserId(tokenService.getLoginUser().getUserid());
        blackList.setCreateBy(tokenService.getLoginUser().getSysUser().getNickName());
        blackList.setReason(reason);
        blackList.setCreditScore(creditScore);
        int i = labBlacklistService.insertLabBlacklist(blackList);
        //加入黑名单后处理负面清单
        labNegativeListRecordsService.updateStatus(violation.getId());
        return toAjax(i);
    }



  /**
     * 移除负面清单
     */
    @ApiOperation(value = "移除负面清单")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NEGATIVELISTRECORDS + PerFun.REMOVE)
    @Log(title = "负面清单列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/delnegativelist")
    public ResultData delnegativelist(LabNegativelistHistory negativelistHistory)
    {
        negativelistHistoryService.insertLabNegativelistHistory(negativelistHistory);
//        return ResultData.result(labNegativeListRecordsService.deleteLabNegativeListRecordsByIds(ids));
        return null;
    }

    /**
     * 移除黑名单
     * @param labBlacklist
     * @return
     */
    @PostMapping("/delBlacklist")
    public R delBlacklist(@RequestBody LabBlacklist labBlacklist){
        labBlacklist.setJoinUserId(labBlacklist.getUserId());
        int row = labBlacklistService.removeBlacklist(labBlacklist);
        labNegativeListRecordsService.updateIsNegative(labBlacklist.getUserId(),0L);
        if (row > 0) return R.ok();
        return R.fail();
    }

    /**
     * 移除负面清单
     * @param vo
     * @return
     */
    @PostMapping("/delNegivelist")
    public R delNegivelist(@RequestBody LabNegativeListRecordsVO vo){

        return labNegativeListRecordsService.delNegivelist(vo);
    }


 /*   *//**
     * 完成考试后移除负面清单
     * @param vo
     * @return
     *//*
    @PostMapping("/delexam")
    public R delexam(@RequestBody LabNegativeListRecordsVO vo){

        return labNegativeListRecordsService.delexam(vo);
    }*/


    /**
     * 负面清单完成学习更改状态
     */
    @Log(title = "面清单完成学习更改状态", businessType = BusinessType.UPDATE)
    @GetMapping("/LearningRules")
    public R negativelistLearningRules(@RequestParam Long violationId,
                                       @RequestParam int status)  {
           return negativelistHistoryService.updateLabHistory(violationId,status);
    }

    /**
     * 根据违规记录id查询当前违规起始、截至时间
     * @param violationId
     * @return
     */
    @GetMapping("/getViolationTime")
    public R getViolationTime(Long violationId){
        LabNegativelistHistory history = negativelistHistoryService.selectLabNegativelistHistoryById(violationId);
        if (history!=null){
            Map result = new HashMap();
            result.put("startTime",history.getCreateTime());
            result.put("endTime",history.getHandleDay());
            result.put("ztsl",history.getNumberTopics());
            result.put("zql",history.getNumberAccuracy());
            //result.put("siteLearnTime",history)
            return R.ok(result);
        }
        return R.fail();
    }

    /**
     * 根据违规id获取做题数量和合格率
     * @return
     */
    @GetMapping("/getpracticenum")
    public R getPracticeNum(Long violationId){
        LabNegativelistHistory history = negativelistHistoryService.selectLabNegativelistHistoryById(violationId);
        if (history!=null){
            Map result = new HashMap();
            result.put("ztsl",history.getNumberTopics());
            result.put("zql",history.getNumberAccuracy());
            //result.put("siteLearnTime",history)
            return R.ok(result);
        }
        return R.fail();
    }








}
