package com.njcn.supervision.controller.device;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.bpm.pojo.param.instance.BpmProcessInstanceCancelParam;
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.supervision.pojo.param.device.SupervisionDevMainReportParam;
import com.njcn.supervision.pojo.po.device.SupervisionTempDeviceReport;
import com.njcn.supervision.pojo.vo.device.SupervisionDevMainReportVO;
import com.njcn.supervision.service.device.SupervisionDevMainReportPOService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.List;

/**
 * 干扰源用户管理
 *
 * @author qijian
 * @version 1.0.0
 * @createTime 2022/11/11 - 9:20
 */
@Slf4j
@RestController
@RequestMapping("/deVReport")
@Api(tags = "电能质量监测装置设备管理")
@AllArgsConstructor
public class DeVReportManageController extends BaseController {

    private final SupervisionDevMainReportPOService supervisionDevMainReportPOService;

    /**
     * 装置新增
     * @param supervisionDevMainReportParam 参数
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType= OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增电能质量监测装置设备")
    @ApiImplicitParam(name = "supervisionDevMainReportParam", value = "实体参数", required = true)
    public HttpResult<String> addDevReport(@RequestBody @Validated SupervisionDevMainReportParam supervisionDevMainReportParam){
        String methodDescribe = getMethodDescribe("addDevReport");
        String devReportId = supervisionDevMainReportPOService.addDevReport(supervisionDevMainReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, devReportId, methodDescribe);

    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType= OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改电能质量监测装置设备")
    @ApiImplicitParam(name = "supervisionDevMainReportParam", value = "实体参数", required = true)
    public HttpResult<String> updateDevReport(@RequestBody @Validated SupervisionDevMainReportParam.SupervisionDevMainReportParamUpdate supervisionDevMainReportParam){
        String methodDescribe = getMethodDescribe("updateDevReport");
        String devReportId = supervisionDevMainReportPOService.updateDevReport(supervisionDevMainReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, devReportId, methodDescribe);

    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType= OperateType.ADD)
    @PostMapping("/auditDevReport")
    @ApiOperation("修改电能质量监测装置设备")
    @ApiImplicitParam(name = "supervisionDevMainReportParamUpdate", value = "实体参数", required = true)
    public HttpResult<Boolean> auditDevReport(@RequestBody @Validated SupervisionDevMainReportParam.SupervisionDevMainReportParamUpdate supervisionDevMainReportParamUpdate){
        String methodDescribe = getMethodDescribe("auditDevReport");
        boolean res = supervisionDevMainReportPOService.auditDevReport(supervisionDevMainReportParamUpdate);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);

    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/removeDevReport")
    @ApiOperation("移除电能质量监测装置设备")
    public HttpResult<Boolean> removeDevReport(@RequestParam("ids") List<String> ids){
        String methodDescribe = getMethodDescribe("removeDevReport");
        Boolean flag = supervisionDevMainReportPOService.removeDevReport(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("分页查询当前用户能看到的接入监测装置设备")
    @ApiImplicitParam(name = "supervisionDevMainReportQuery", value = "参数", required = true)
    public HttpResult<Page<SupervisionDevMainReportVO>> getDevReport(@RequestBody @Validated SupervisionDevMainReportParam.SupervisionDevMainReportQuery supervisionDevMainReportQuery) {
        String methodDescribe = getMethodDescribe("getDevReport");
        Page<SupervisionDevMainReportVO> out = supervisionDevMainReportPOService.getDevReport(supervisionDevMainReportQuery);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @GetMapping("/updateStatus")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更改表单状态")
    public HttpResult<Object> updateStatus(String businessKey,Integer status) {
        String methodDescribe = getMethodDescribe("updateStatus");
        supervisionDevMainReportPOService.updateProcessStatus(businessKey,status);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/cancel")
    @ApiOperation("取消设备录入")
    @ApiImplicitParam(name = "cancelReqVO", value = "取消原因", required = true)
    public HttpResult<String> cancelDevMainReport(@Validated @RequestBody BpmProcessInstanceCancelParam cancelReqVO) {
        String methodDescribe = getMethodDescribe("cancelDevMainReport");
        String id = supervisionDevMainReportPOService.cancelDevMainReport(cancelReqVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, id, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/getDeviceList")
    @ApiOperation("获取监测点绑定设备列表")
    public HttpResult<List<SupervisionTempDeviceReport>> getDeviceList() {
        String methodDescribe = getMethodDescribe("getDeviceList");
        List<SupervisionTempDeviceReport> id = supervisionDevMainReportPOService.getDeviceList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, id, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryDeviceDetail")
    @ApiOperation("查询设备详情")
    public HttpResult<SupervisionDevMainReportVO> queryDeviceDetail(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("queryDeviceDetail");
        SupervisionDevMainReportVO vo = supervisionDevMainReportPOService.querySurveyDetail(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping(value = "/downloadDevTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation("下载终端台账入网模板")
    public void downloadDevTemplate() {
        supervisionDevMainReportPOService.downloadDevTemplate();
    }


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

    @PostMapping(value = "/deleteDevReport")
    @ApiOperation("终端入网检测删除流程")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM, operateType = OperateType.DELETE)
    public HttpResult<Boolean> deleteDevReport(@RequestBody List<String> supervisionId) {
        String methodDescribe = getMethodDescribe("deleteDevReport");
        Boolean b = supervisionDevMainReportPOService.deleteDevReport(supervisionId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }
}
