package com.njcn.event.controller.majornetwork;

import cn.hutool.core.util.ObjectUtil;
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.device.pms.pojo.param.MonitorTerminalParam;
import com.njcn.event.file.pojo.dto.WaveDataDTO;
import com.njcn.event.pojo.param.EventVerifyReasonParam;
import com.njcn.event.pojo.param.TransientParam;
import com.njcn.event.pojo.po.EventDetailNew;
import com.njcn.event.pojo.vo.TransientVO;
import com.njcn.event.service.majornetwork.TransientService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * @author: chenchao
 * @date: 2022/03/29 10:19
 * @Description: <描述>
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/transient")
@Api(tags = "暂态事件列表")
@AllArgsConstructor
public class TransientController extends BaseController {

    private final TransientService transientService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTransientData")
    @ApiOperation("暂态事件信息列表")
    @ApiImplicitParam(name = "transientParam", value = "暂态列表参数", required = true)
    public HttpResult<Page<TransientVO>> getTransientData(@RequestBody @Validated TransientParam transientParam){
        String methodDescribe = getMethodDescribe("getTransientData");
        Page<TransientVO> list = transientService.getTransientData(transientParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTransientAnalyseWave")
    @ApiOperation("暂态事件波形分析")
    public HttpResult<WaveDataDTO> getTransientAnalyseWave(@RequestBody MonitorTerminalParam param){
        String methodDescribe = getMethodDescribe("getTransientAnalyseWave");
        WaveDataDTO wave = transientService.getTransientAnalyseWave(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, wave, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTransientAnalyseWaveToByteArray")
    @ApiOperation("暂态事件波形分析(转byte数组)")
    public HttpResult<byte[]> getTransientAnalyseWaveToByteArray(@RequestBody MonitorTerminalParam param){
        byte[] bytes = null;
        String methodDescribe = getMethodDescribe("getTransientAnalyseWaveToByteArray");
        WaveDataDTO wave = transientService.getTransientAnalyseWave(param);
        if(ObjectUtil.isNotNull(wave)){
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(wave);
                bytes = baos.toByteArray();
                baos.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, bytes, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/downloadWaveFile")
    @ApiOperation("暂态波形下载")
    @ApiImplicitParam(name = "eventIds", value = "波形下载参数", required = true)
    public void downloadWaveFile(@RequestBody List<String> eventIds, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("downloadWaveFile");
        transientService.downloadWaveFile(eventIds, response);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTransientValue")
    @ApiOperation("根据条件查询暂态事件")
    @ApiImplicitParam(name = "transientParam", value = "暂态列表参数", required = true)
    public HttpResult<Page<EventDetailNew>> getTransientValue(@RequestBody @Validated TransientParam transientParam){
        String methodDescribe = getMethodDescribe("getTransientValue");
        Page<EventDetailNew> list = transientService.getTransientValue(transientParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/updateEventReason")
    @ApiOperation("更新暂降核实原因")
    public HttpResult<String> updateEventReason(@RequestBody EventVerifyReasonParam eventVerifyReasonParam) {
        String methodDescribe = getMethodDescribe("updateEventReason");
        transientService.updateEventReason(eventVerifyReasonParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }



}
