package com.njcn.process.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.process.pojo.param.TerminalParam;
import com.njcn.process.pojo.vo.TerminalVO;
import com.njcn.process.service.PmsTerminalDetectionService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
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.io.*;
import java.util.List;

/**
 * <p>
 *  终端检测监督
 * </p>
 *
 * @author wr
 * @since 2023-02-27
 */
@RestController
@RequestMapping("/process/pmsTerminalDetection")
@Api(tags = "终端检测监督管理")
@RequiredArgsConstructor
public class PmsTerminalDetectionController extends BaseController {
    private final PmsTerminalDetectionService pmsTerminalDetectionService;


    @PostMapping("/getTerminalPage")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("终端检测监督管理分页查询")
    public HttpResult<Page<TerminalVO>> getTerminalPage(@RequestBody TerminalParam.TerminalPageParam param) {
        String methodDescribe = getMethodDescribe("getTerminalPage");
        Page<TerminalVO> page = pmsTerminalDetectionService.terminalPage(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @PostMapping("/getStatistics")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("终端入网检测台账统计")
    public HttpResult<TerminalVO.TerminalStatistics> getStatistics(@RequestBody TerminalParam param) {
        String methodDescribe = getMethodDescribe("getStatistics");
        TerminalVO.TerminalStatistics list = pmsTerminalDetectionService.getStatistics(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/getCycleStatistics")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("周期检测统计")
    public HttpResult<TerminalVO.TerminalStatistics> getCycleStatistics(@RequestBody TerminalParam param) {
        String methodDescribe = getMethodDescribe("getCycleStatistics");
        TerminalVO.TerminalStatistics list = pmsTerminalDetectionService.getCycleStatistics(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/insertTerminal")
    @ApiOperation(value = "终端检测监督单条新增")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<Boolean> insertTerminal(@RequestBody TerminalParam.TerminalInsertParam param) {
        String methodDescribe = getMethodDescribe("insertTerminal");
        Boolean flag = pmsTerminalDetectionService.insertTerminal(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @PostMapping(value = "/batchTerminal")
    @ApiOperation("批量导入终端入网检测录入")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<String> batchTerminal(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("batchTerminal");
        pmsTerminalDetectionService.batchTerminal(file, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @PostMapping("/updateTerminal")
    @ApiOperation(value = "终端检测监督修改")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    public HttpResult<Boolean> updateTerminal(@RequestBody @Validated TerminalParam.TerminalUpdateParam param) {
        String methodDescribe = getMethodDescribe("updateTerminal");
        Boolean flag = pmsTerminalDetectionService.updateTerminal(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @PostMapping("/delTerminal")
    @ApiOperation(value = "终端检测监督删除")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    public HttpResult<Boolean> delTerminal(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delTerminal");
        Boolean flag = pmsTerminalDetectionService.delTerminal(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @ApiOperation(value = "导出终端入网检测录入模板")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/export")
    public InputStreamResource export() throws IOException {
        return pmsTerminalDetectionService.exportTemplate();
    }


    @ApiOperation("批量导入检测报告")
    @PostMapping(value = "/importReport")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<String> importReport(@ApiParam(value = "文件", required = true)
                                           @RequestPart("files") MultipartFile[] files,
                                           @RequestParam("type") Integer type, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importReport");
        pmsTerminalDetectionService.importReport(files,type, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @ApiOperation("检测报告下载")
    @GetMapping(value = "/reportDownload")
    public InputStreamResource reportDownload( TerminalParam.DownloadParam param){
        InputStreamResource minIoUploadResDTO = pmsTerminalDetectionService.reportDownload(param);
        return minIoUploadResDTO;
    }


    @ApiOperation("文件上传")
    @PostMapping(value = "/upload")
    public HttpResult<String> upload(MultipartFile file) {
        String methodDescribe = getMethodDescribe("upload");
        String url = pmsTerminalDetectionService.url(file);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, url, methodDescribe);
    }
}

