package com.securitycontrol.background.controller;

import com.securitycontrol.background.service.EmergencyService;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.utils.WordUtils;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.background.dto.DeviceDto;
import com.securitycontrol.entity.background.vo.AreaVo;
import com.securitycontrol.entity.background.vo.DeviceVo;
import com.securitycontrol.entity.background.vo.EmergencyPlanVo;
import com.securitycontrol.entity.file.FileExportVo;
import com.securitycontrol.entity.system.SystemGlobal;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 应急响应
 * @author 黑子
 */

@RestController
@RequestMapping("/back/emerge/")
@Slf4j
public class EmergencyController extends BaseController {
     @Autowired
     private EmergencyService service;

    @ApiOperation(value = "查询预案")
    @GetMapping("getDhList")
    @Log(title = "应急响应", menu = "风险值班管理->应急电话", grade = OperationType.QUERY_BUSINESS, details = "查询应急电话列表", type = "业务日志")
    public TableDataInfo getDhList(EmergencyPlanVo dto) {
        try{
            dto.setEmergencyType("1");
            startPage();
            List<EmergencyPlanVo> list = service.getList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }


    @ApiOperation(value = "查询预案")
    @GetMapping("getYaList")
    @Log(title = "应急响应", menu = "风险值班管理->应急预案", grade = OperationType.QUERY_BUSINESS, details = "查询应急预案列表", type = "业务日志")
    public TableDataInfo getYaList(EmergencyPlanVo dto) {
        try{
            dto.setEmergencyType("2");
            dto.setSourceType("应急预案");
            startPage();
            List<EmergencyPlanVo> list = service.getList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }


    @ApiOperation(value = "查询预案")
    @GetMapping("getLcList")
    @Log(title = "应急响应", menu = "风险值班管理->应急流程", grade = OperationType.QUERY_BUSINESS, details = "查询应急流程列表", type = "业务日志")
    public TableDataInfo getLcList(EmergencyPlanVo dto) {
        try{
            dto.setSourceType("应急流程");
            dto.setEmergencyType("3");
            startPage();
            List<EmergencyPlanVo> list = service.getList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }

    /**
     * 新增应急响应
     * @param dto
     * @return
     */
    @PostMapping("addTelData")
    @Log(title = "应急响应", menu = "应急响应->应急电话", grade = OperationType.ADD_BUSINESS, details = "新增应急电话", type = "业务日志")
    public Result<String> addTelData(@RequestBody @Valid EmergencyPlanVo dto) {
        dto.setEmergencyType("1");
        return service.addTelData(dto);
    }


    /**
     * 新增应急响应
     *
     * @param dto
     * @return
     */
    @PostMapping("updTelData")
    @Log(title = "应急响应", menu = "应急响应->应急电话", grade = OperationType.UPDATE_BUSINESS, details = "修改应急电话", type = "业务日志")
    public Result<String> updTelData(@RequestBody @Valid EmergencyPlanVo dto) {
        return service.updTelData(dto);
    }
    /**
     * 新增区域
     * @param dto
     * @return
     */
    @PostMapping("deleteData")
    @Log(title = "应急响应", menu = "应急响应->应急电话", grade = OperationType.UPDATE_BUSINESS, details = "删除应急电话", type = "业务日志")
    public Result<String> deleteData(@RequestBody @Valid EmergencyPlanVo dto) {
        return service.deleteData(dto);
    }




    @ApiOperation(value = "新增预案")
    @PostMapping("addYaFile")
    @Log(title = "应急响应", menu = "应急响应->应急预案", grade = OperationType.ADD_BUSINESS, details = "新增预案", type = "业务日志")
    public  Result<String> addYaFile(@RequestParam(value = "file[]", required = false) MultipartFile[] files,EmergencyPlanVo dto ) {
              dto.setEmergencyType("2");
              dto.setSourceType(SystemGlobal.YA_NAME);
             return service.addYaFile(files, dto);
    }


    @ApiOperation(value = "修改预案")
    @PostMapping("updateYaFile")
    @Log(title = "应急响应", menu = "应急响应->应急预案", grade = OperationType.ADD_BUSINESS, details = "新增预案", type = "业务日志")
    public  Result<String> updateYaFile(@RequestParam(value = "file[]", required = false) MultipartFile[] files,EmergencyPlanVo dto ) {
        dto.setSourceType(SystemGlobal.YA_NAME);
        return service.updateYaFile(files, dto);
    }



    @ApiOperation(value = "新增流程预案")
    @PostMapping("addLcFile")
    @Log(title = "应急响应", menu = "应急响应->流程预案", grade = OperationType.ADD_BUSINESS, details = "新增流程预案", type = "业务日志")
    public  Result<String> addLcFile(@RequestParam(value = "file[]",required = false) MultipartFile[] files,EmergencyPlanVo dto ) {
        dto.setEmergencyType("3");
        dto.setSourceType(SystemGlobal.LC_NAME);
        return service.addYaFile(files, dto);
    }

    /**
     * 流程
     * @param files
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改流程预案预案")
    @PostMapping("updateLcFile")
    @Log(title = "应急响应", menu = "应急响应->流程预案", grade = OperationType.ADD_BUSINESS, details = "修改流程预案", type = "业务日志")
    public  Result<String> updateLcFile(@RequestParam(value = "file[]", required = false) MultipartFile[] files,EmergencyPlanVo dto ) {
        dto.setSourceType(SystemGlobal.LC_NAME);
        return service.updateYaFile(files, dto);
    }

    @ApiOperation(value = "文件预览")
    @PostMapping("getFileDetails")
    @Log(title = "应急响应", menu = "应急响应->应急预案", grade = OperationType.ADD_BUSINESS, details = "修改流程预案", type = "业务日志")
    public  Result<List<FileExportVo>> getFileDetails(@RequestBody EmergencyPlanVo dto ) {
        return service.getFileDetails(dto);
    }

    @GetMapping("downLoadFile")
    public void downLoadSgFile(HttpServletRequest request, HttpServletResponse response, String fileId, String fileName) {
        try {
            byte[] decode = service.downLoadSgFile(fileId);
            WordUtils.exportFile(request, response, fileName, decode);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

}
