package com.zd.app.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.LabStudentsInfo;
import com.zd.system.api.domain.SysFile;
import com.zd.system.api.laboratory.RemoteLaboratoryService;
import com.zd.system.api.laboratory.domain.LabSubjectControllerVO;
import com.zd.system.api.students.RemoteStudentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/lab/api")
@Api(tags = {"实验室系统相关"})
public class LabApi {

    @Autowired
    private RemoteLaboratoryService laboratoryService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RemoteStudentsService studentsService;


    @ApiOperation(value = "新增随手拍记录")
    @PostMapping("/addPhotoNote")
    public R<Object> addPhotoNote(@RequestBody Map<String, Object> labPhotoNote) {

//        if (StringUtils.isBlank((String) labPhotoNote.get("imgUrls"))) {
//            return R.fail("图片不能为空");
//        }
        if (labPhotoNote.get("subjectId") == null) {
            return R.fail("实验室ID不能为空");
        }
        // describe 随手拍描述

        return laboratoryService.addPhotoNote(labPhotoNote);
    }

    @ApiOperation(value = "小程序查询登录人接收随手拍统计")
    @GetMapping("/appReceivePhotoNote")
    public R<Object> appReceivePhotoNote() {
        Map<String, Object> photoNote = new HashMap <>();
        return laboratoryService.appReceivePhotoNote(photoNote);
    }

    @ApiOperation(value = "获取随手拍记录列表信息（管理端）")
    @GetMapping("/photonote/getAppList")
    public TableDataInfo getAppList(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam Integer handleStatus) {
        Map map = new HashMap(3);
        map.put("handleUser", tokenService.getLoginUser().getUserid());
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        map.put("handleStatus", handleStatus);
        TableDataInfo list = laboratoryService.getAppList(map);
        return list;
    }

    @ApiOperation(value = "获取随手拍记录未处理列表信息（管理端）")
    @GetMapping("/photonote/getUntreatedAppList")
    public TableDataInfo getUntreatedAppList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Map map = new HashMap(3);
        map.put("handleUser", tokenService.getLoginUser().getUserid());
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        map.put("handleStatus", "0");
        TableDataInfo list = laboratoryService.getAppList(map);
        return list;
    }

    @ApiOperation(value = "查询登录用户是否是实验室管理员（数据权限）")
    @GetMapping("/my/isadmin")
    public R<Object> isadmin() {
        Map<String, Object> map = new HashMap <>();
        map.put("adminId", tokenService.getLoginUser().getUserid());
        return laboratoryService.isadmin(map);
    }

    /**
     * 获取随手拍记录详细信息
     */
    @ApiOperation(value = "获取随手拍记录详细信息")
    @GetMapping(value = "/photonote/{id}")
    public R<Object> getPhotonoteInfo(@PathVariable("id") Long id) {
        return laboratoryService.getPhotonoteInfo(id);
    }

    @ApiOperation(value = "处理随手拍记录")
    @PostMapping("/editPhotoNote")
    public R<Object> editPhotoNote(@RequestParam Long id,@RequestParam String handleDescribe,@RequestParam String handleImg) {
        Map map = new HashMap(3);
        map.put("id",id);
        map.put("handleDescribe",handleDescribe);
        map.put("handleImg",handleImg);
        return laboratoryService.editPhotoNote(map);
    }


    @ApiOperation(value = "离开实验室拍照附件-当前无需要上报则数据data 为false,已上报还未关闭则返回上报数据,未上报则返回null")
    @GetMapping("/outSubjectPhoto")
    public R<Object> outSubjectPhoto() {
        return laboratoryService.outSubjectPhoto();
    }


    @ApiOperation(value = "获取签到离开的实验室列表")
    @GetMapping("/outSubjectList")
    public R<Object> outSubjectList() {
        return laboratoryService.outSubjectList();
    }


    @ApiOperation(value = "新增拍照检查附件")
    @PostMapping("/addPhotoInspection")
    public R<Object> addPhotoInspection(@RequestBody Map<String,Object> labPhotoInspection) {
//        List <LabPhotoInspection> inspecList  = labPhotoInspection.getLabPhotoInspection();
//        List <Map<String,Object>> labPhotoInspectionList = new ArrayList <>();
//        for(LabPhotoInspection inspec:inspecList){
//            Map<String,Object> inspecMap = new HashMap<>();
//            inspecMap.put("accessId",inspec.getAccessId());
//            inspecMap.put("annexTitle",inspec.getAnnexTitle());
//            inspecMap.put("annexUrl",inspec.getAnnexUrl());
//            labPhotoInspectionList.add(inspecMap);
//        }
        return laboratoryService.addPhotoInspection(labPhotoInspection);
    }

    @ApiOperation(value = "统计安全准入审批不同信息(管理端)")
    @GetMapping("/getApprovalCount")
    public R<Object> getApprovalCount() {
        Map<String, Object> approval = new HashMap <>();
        approval.put("deptId", tokenService.getLoginUser().getSysUser().getDeptId());
        return laboratoryService.getApprovalCount(approval);
    }

    @ApiOperation(value = "查询安全准入审批记录列表（用户端,管理端）")
    @GetMapping("/getApprovalList")
    public TableDataInfo getApprovalList(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam Integer userId,@RequestParam Integer recordContent) {
        Map map = new HashMap(3);
        if(!userId.equals(-1) && userId!=-1){
            map.put("joinUserId", userId);
        }
        if(!recordContent.equals(-1) && recordContent!=-1){
            map.put("recordContent", recordContent);
            map.put("deptId", tokenService.getLoginUser().getSysUser().getDeptId());
        }
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        TableDataInfo list = laboratoryService.getApprovalList(map);
        return list;
    }

    @ApiOperation(value = "查询违规记录列表 （用户端）")
    @GetMapping("/getViolationList")
    public TableDataInfo getViolationList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Map map = new HashMap(3);
        map.put("joinUserId", tokenService.getLoginUser().getUserid());
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        map.put("isHandle", 0);
        TableDataInfo list = laboratoryService.getViolationList(map);
        return list;
    }

    @ApiOperation(value = "查询我的违规记录统计（用户端）")
    @GetMapping("/myViolationCount")
    public R<Object> myViolationCount() {
        Map map = new HashMap(3);
        map.put("joinUserId", tokenService.getLoginUser().getUserid());
        map.put("isHandle", 0);
        return laboratoryService.myViolationCount(map);
    }

    @ApiOperation(value = "查询我的实验室列表")
    @GetMapping("/mySubjectList")
    public R<LabSubjectControllerVO> mySubjectList() {
        return laboratoryService.queryMyAadminSubCListVO();
    }



    @ApiOperation(value = "获取当前用户学生卡和人脸信息")
    @GetMapping(value = "/studentinfo/my")
    public R<LabStudentsInfo> getMyCardInfo() {
        R info = laboratoryService.getMyCardInfo();
        return info;
    }

    @ApiOperation(value = "查询安全准入申请实验室列表（用户端）")
    @GetMapping("/mySecuritySubjectList")
    public R<Object> mySecuritySubjectList(@RequestParam("deptId") Integer deptId) {
        Map<String, Object> subjectMap = new HashMap();
        subjectMap.put("deptId", deptId);
        subjectMap.put("userId", tokenService.getLoginUser().getUserid());
        return laboratoryService.mySubjectList(subjectMap);
    }

    @ApiOperation(value = "查询实验室下的安全准入资格材料列表（用户端）")
    @GetMapping(value = "/subjectMaterialList")
    public R<Object> subjectMaterialList(@RequestParam Long subjectId) {
        Map<String, Object> material = new HashMap <>();
        material.put("subjectId", subjectId);
        return laboratoryService.subjectMaterialList(material);
    }

    @ApiOperation(value = "小程序安全准入资格提交(用户端)")
    @PostMapping(value = "/accessReview")
    public R<Object> accessReview(@RequestBody Map<String, Object> labApprovalMap) {
        return laboratoryService.accessReview(labApprovalMap);
    }

    @ApiOperation(value = "获取安全准入审批记录详细信息(用户端)")
    @GetMapping(value = "/approvalDetail")
    public R<Object> approvalDetail(@RequestParam String ids) {
        return laboratoryService.approvalDetail(ids);
    }

    @ApiOperation(value = "小程序安全准入资格审批(管理端)")
    @PutMapping(value = "/appOperation")
    public R<Object> appOperation(@RequestBody Map<String, Object> labApprovalMap) {
        return laboratoryService.appOperation(labApprovalMap);
    }


    @ApiOperation(value = "小程序安全准入资格拒绝审批(管理端)")
    @PutMapping(value = "/failedOperation")
    public R<Object> failedOperation(@RequestBody Map<String, Object> labApprovalMap) {
        return laboratoryService.failedOperation(labApprovalMap);
    }

    @ApiOperation(value = "小程序查询实验室列表（用户端,管理端）")
    @GetMapping(value = "/getSubjectDict")
    public R<Object> getSubjectDict() {
        return laboratoryService.getSubjectDict();
    }


    @ApiOperation(value = "小程序人脸识别提交接口")
    @PostMapping (value = "/commit/face")
    public R<Object> uploadFace(@RequestPart(value = "file") MultipartFile file){
        return laboratoryService.uploadFace(file);
    }
    @ApiOperation(value = "小程序学生卡提交接口")
    @PostMapping (value = "/commit/crad")
    public R commitCard(@RequestPart(value = "file") MultipartFile file){
        return studentsService.commitCard(file);
    }


    @ApiOperation(value = "查询安全预警列表 （管理端）")
    @GetMapping("/getSafeWarnList")
    public TableDataInfo getSafeWarnList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam Integer count, @RequestParam String groupStatus) {
        Map map = new HashMap(3);
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        map.put("count", count);
        map.put("groupStatus", groupStatus);

        TableDataInfo list = laboratoryService.getSafeWarnList(map);
        return list;
    }

    @ApiOperation(value = "查询安全预警统计 （管理端）")
    @GetMapping(value = "/getSafeWarnCount")
    public R<Object> getSafeWarnCount() {
        return laboratoryService.getSafeWarnCount();
    }

    @ApiOperation(value = "查询实验室下的预案列表 （管理端）")
    @GetMapping("/getSubByRiskPlan")
    public TableDataInfo getSubByRiskPlan(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String deptId, @RequestParam String subDept, @RequestParam String typeId, @RequestParam String startDate, @RequestParam String endDate) {
        Map map = new HashMap(3);
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        map.put("deptId", deptId);
        map.put("subDept", subDept);
        map.put("typeId", typeId);
        map.put("strStartDate", startDate);
        map.put("strEndDate", endDate);
        TableDataInfo list = laboratoryService.getSubByRiskPlan(map);
        return list;
    }

    @ApiOperation(value = "查询实验室下的预案详情 （管理端）")
    @GetMapping("/getSubByRiskPlanDetail")
    public TableDataInfo getSubByRiskPlanDetail(@RequestParam Integer id) {
        Map map = new HashMap(3);
        map.put("appPageNum", 1);
        map.put("appPageSize", 1);
        map.put("id", id);
        TableDataInfo list = laboratoryService.getSubByRiskPlan(map);
        return list;
    }

//    @ApiOperation(value = "获取小程序人脸识别接口")
//    @GetMapping(value = "/studentinfo/facemy")
//    public R<LabStudentsInfo> facemy(){
//        Long userId = SecurityUtils.getUserIdEx();
//        R <LabStudentsInfo> info = studentsService.getInfo(userId);
//        if(info.getData()!=null&&info.getData().getFaceFeature()!=null&&info.getData().getFaceFeature().length>1)
//        {
//            info.getData().setIfFaceFeature(true);
//        }
//        return info;
//    }

    @ApiOperation(value = "小程序安全检查记录-按时间分组")
    @GetMapping(value = "/listGroupBy")
    public R<Object> listGroupBy(@RequestBody Map<String, Object> map) {
        return laboratoryService.listGroupBy(map);
    }

    @ApiOperation(value = "小程序安全检查（根据房间号查询实验室信息）")
    @GetMapping(value = "/selectInfoByRoom")
    public R<Object> selectInfoByRoom(@RequestBody Map<String, Object> map) {
        return laboratoryService.selectInfoByRoom(map);
    }

    @ApiOperation(value = "程序安全检查 草稿保存")
    @PostMapping(value = "/addCg")
    public R<Object> addCg(@RequestBody Map<String, Object> map) {
        return laboratoryService.addCg(map);
    }

    @ApiOperation(value = "小程序安全检查保存")
    @PostMapping(value = "/addTj")
    public R<Object> addTj(@RequestBody Map<String, Object> map) {
        return laboratoryService.addTj(map);
    }

    @ApiOperation(value = "根据检查项获取 隐患整改集合")
    @GetMapping(value = "/recordDettailsList")
    public R<Object> recordDettailsList(@RequestParam Integer id) {
        return laboratoryService.recordDettailsList(id);
    }

    @ApiOperation(value = "安全检查修改")
    @PostMapping(value = "/checkRecordEdit")
    public R<Object> recordDettailsList(@RequestBody Map<String, Object> map) {
        return laboratoryService.editCheckRecord(map);
    }

    @ApiOperation(value = "根据检查项获取 隐患整改集合")
    @PostMapping(value = "/checkRecordDetailsEdit")
    public R<Object> editList(@RequestBody Map<String, Object> map) {
        return laboratoryService.editCheckRecordDetailsList(map);
    }

    @ApiOperation(value = "根据用户id获取用户详情")
    @GetMapping(value = "/selectUserInfoById")
    public R<Object> selectUserInfoById(@PathVariable("id") Long id) {
        return laboratoryService.selectUserInfoById(id);
    }

    /**
     * 小程序单个 生成整改通知书
     */
    @ApiOperation(value = "生成整改通知书")
    @GetMapping("/genNotice/{id}")
    public void genNotice(@PathVariable("id") Long id, HttpServletResponse response) {
        laboratoryService.genNotice(id, response);
    }

    /**
     * 小程序单个 生成整改报告
     */
    @ApiOperation(value = "生成整改报告")
    @GetMapping("/genReport/{id}")
    public void genReport(@PathVariable("id") Long id, HttpServletResponse response) {
        laboratoryService.genReport(id, response);
    }
}
