package com.zd.laboratory.controller;

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

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabViolation;
import com.zd.laboratory.domain.dto.LabApprovalAndDetailDTO;
import com.zd.laboratory.domain.vo.LabApprovalVO;
import com.zd.laboratory.domain.vo.LabSecurityGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.LabApproval;
import com.zd.laboratory.service.ILabApprovalService;
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 com.zd.common.core.web.page.TableDataInfo;

/**
 * 安全准入审批记录Controller
 *
 * @author zd
 * @date 2021-08-31
 */
@RestController
@RequestMapping("/approval")
public class LabApprovalController extends BaseController {
    @Autowired
    private ILabApprovalService labApprovalService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询安全准入审批记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_APPROVAL + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabApproval labApproval) {
        startPage();
        List<LabApprovalVO> list = labApprovalService.selectLabApprovalList(labApproval);
        return getDataTable(list);
    }

    /**
     * 查询当前登录人安全准入审批记录列表(个人PC 列表)
     */
//    @PreAuthorize(hasPermi = "laboratory:approval:list")
    @GetMapping("/userAppRovallist")
    public TableDataInfo userAppRovallist(LabApproval labApproval) {
        labApproval.setJoinUserId(SecurityUtils.getUserId());
        startPage();
        List<LabApprovalVO> list = labApprovalService.selectApprovalListByUser(labApproval);
        return getDataTable(list);
    }

    /**
     * 导出安全准入审批记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_APPROVAL + PerFun.EXPORT)
    @Log(title = "安全准入审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabApproval labApproval) throws IOException {
        List<LabApprovalVO> list = labApprovalService.selectLabApprovalList(labApproval);
        ExcelUtil<LabApprovalVO> util = new ExcelUtil<LabApprovalVO>(LabApprovalVO.class);
        util.exportExcel(response, list, "安全准入审批记录数据");
    }

    /**
     * 获取安全准入审批记录详细信息
     */
//    @PreAuthorize(hasPermi = "laboratory:approval:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LabApprovalAndDetailDTO dto = labApprovalService.selectLabApprovalById(id);
        return AjaxResult.success(dto);
    }

    /**
     * 新增安全准入审批记录
     */
//    @PreAuthorize(hasPermi = "laboratory:approval:add")
    @Log(title = "安全准入审批记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated LabApprovalAndDetailDTO labApproval) {
        return toAjax(labApprovalService.insertLabApproval(labApproval));
    }

    /**
     * 修改安全准入审批记录
     */
//    @PreAuthorize(hasPermi = "laboratory:approval:edit")
    @Log(title = "安全准入审批记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated LabApprovalAndDetailDTO labApproval) {
        return toAjax(labApprovalService.updateLabApproval(labApproval));
    }

    /**
     * 安全准入审批操作
     */
    @Log(title = "安全准入审批操作", businessType = BusinessType.UPDATE)
    @PutMapping("/appOperation")
    public AjaxResult appOperation(@RequestBody LabApproval labApproval) {
        return toAjax(labApprovalService.appOperation(labApproval));
    }

    /**
     * 安全准入拒绝操作
     */
    @Log(title = "安全准入拒绝操作", businessType = BusinessType.UPDATE)
    @PutMapping("/failedOperation")
    public AjaxResult failedOperation(@RequestBody LabApproval labApproval) {
        return toAjax(labApprovalService.failedOperation(labApproval));
    }

    /**
     * 删除安全准入审批记录
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_APPROVAL + PerFun.REMOVE)
    @Log(title = "安全准入审批记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labApprovalService.deleteLabApprovalByIds(ids));
    }

    /**
     * 通过实验室 查询对应的安全准入规则 1V1
     */
    @PostMapping("/querySecurityBySubjectId")
    public AjaxResult add(@RequestBody LabApproval labApproval) {
        return AjaxResult.success(labApprovalService.querySecurityBySubjectId(labApproval.getSubjectId()));
    }


    /**
     * 统计安全准入审批不同列表信息
     */
    @RequestMapping(value = "/getApprovalCount")
    public AjaxResult getApprovalCount(@RequestBody LabApproval labApproval) {
        return AjaxResult.success(labApprovalService.getApprovalCount(labApproval));
    }

    /**
     * 查询安全准入审批记录列表
     */
    @RequestMapping("/getApprovalList")
    public TableDataInfo<LabApprovalVO> getApprovalList(@RequestBody LabApprovalVO labApproval) {
        startPage(labApproval.getAppPageNum(),labApproval.getAppPageSize());
        List<LabApprovalVO> list = labApprovalService.getApprovalList(labApproval);
        return getDataTable(list);
    }


    @PostMapping("/accessReview")
    public AjaxResult accessReview(@RequestBody LabApprovalAndDetailDTO labApproval) {
        if(labApproval.getId()!=null && !labApproval.getId().equals("")){
            return toAjax(labApprovalService.updateLabApproval(labApproval));
        }else{
            return toAjax(labApprovalService.insertLabApproval(labApproval));
        }
    }

    /**
     * 大屏 安全考试与安全准入统计
     */
    @PostMapping("/bigData/statistics")
    public AjaxResult statistics(@RequestBody LabApproval labApproval) {
        List<Map<String, Object>> data = labApprovalService.statistics(labApproval);
        return AjaxResult.success(data);
    }
}
