package com.njcn.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.minioss.bo.MinIoUploadResDTO;
import com.njcn.process.pojo.param.*;
import com.njcn.process.pojo.vo.*;
import com.njcn.process.service.IssuesService;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 电能质量问题
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/11/14 9:18
 */
@Slf4j
@Api(tags = "电能质量问题")
@RestController
@RequestMapping("/electricityQuality")
@RequiredArgsConstructor
public class ElectricityQualityIssuesController extends BaseController {

    private final IssuesService issuesService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addExcessiveIssues")
    @ApiOperation("填报在线监测超标问题")
    @ApiImplicitParam(name = "excessiveParam", value = "参数", required = true)
    public HttpResult<String> addExcessiveIssues(@RequestBody @Validated ExcessiveParam excessiveParam){
        String methodDescribe = getMethodDescribe("addExcessiveIssues");
        issuesService.addExcessiveIssues(excessiveParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addGeneralSurveyIssues")
    @ApiOperation("填报普测超标问题")
    @ApiImplicitParam(name = "generalSurveyParam", value = "参数", required = true)
    public HttpResult<String> addGeneralSurveyIssues(@RequestBody @Validated GeneralSurveyParam generalSurveyParam){
        String methodDescribe = getMethodDescribe("addGeneralSurveyIssues");
        issuesService.addGeneralSurveyIssues(generalSurveyParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addComplaintIssues")
    @ApiOperation("填报用户投诉问题")
    @ApiImplicitParam(name = "complaintParam", value = "参数", required = true)
    public HttpResult<String> addComplaintIssues(@RequestBody @Validated ComplaintParam complaintParam){
        String methodDescribe = getMethodDescribe("addComplaintIssues");
        issuesService.addComplaintIssues(complaintParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addAbnormalIssues")
    @ApiOperation("填报运维监控异常问题")
    @ApiImplicitParam(name = "abnormalParam", value = "参数", required = true)
    public HttpResult<String> addAbnormalIssues(@RequestBody @Validated AbnormalParam abnormalParam){
        String methodDescribe = getMethodDescribe("addAbnormalIssues");
        issuesService.addAbnormalIssues(abnormalParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getIssues")
    @ApiOperation("查询问题列表")
    @ApiImplicitParam(name = "queryIssuesParam", value = "参数", required = true)
    public HttpResult<IPage<IssuesVO>> getIssues(@RequestBody @Validated QueryIssuesParam queryIssuesParam){
        String methodDescribe = getMethodDescribe("getIssues");
        Page<IssuesVO> out = issuesService.getIssues(queryIssuesParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getExcessiveDetail")
    @ApiOperation("查询在线监测超标问题详情")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<ExcessiveDetailVO> getExcessiveDetail(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("getExcessiveDetail");
        ExcessiveDetailVO out = issuesService.getExcessiveDetail(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneralSurveyDetail")
    @ApiOperation("查询普测超标问题详情")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<GeneralSurveyVO> getGeneralSurveyDetail(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("getGeneralSurveyDetail");
        GeneralSurveyVO out = issuesService.getGeneralSurveyDetail(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getComplaintDetail")
    @ApiOperation("查询用户投诉问题详情")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<ComplaintVO> getComplaintDetail(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("getComplaintDetail");
        ComplaintVO out = issuesService.getComplaintDetail(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAbnormalDetail")
    @ApiOperation("查询运维异常问题详情")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<AbnormalVO> getAbnormalDetail(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("getAbnormalDetail");
        AbnormalVO out = issuesService.getAbnormalDetail(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/reasonAnalysis")
    @ApiOperation("原因分析")
    @ApiImplicitParam(name = "processParam", value = "参数", required = true)
    public HttpResult<String> reasonAnalysis(@RequestBody @Validated ProcessParam processParam){
        String methodDescribe = getMethodDescribe("reasonAnalysis");
        issuesService.reasonAnalysis(processParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/correctiveAction")
    @ApiOperation("计划整改措施")
    @ApiImplicitParam(name = "processParam", value = "参数", required = true)
    public HttpResult<String> correctiveAction(@RequestBody @Validated ProcessParam processParam){
        String methodDescribe = getMethodDescribe("correctiveAction");
        issuesService.process(processParam, DicDataEnum.PLAN_MEASURES.getCode());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/takeAction")
    @ApiOperation("实际采取措施")
    @ApiImplicitParam(name = "processParam", value = "参数", required = true)
    public HttpResult<String> takeAction(@RequestBody @Validated ProcessParam processParam){
        String methodDescribe = getMethodDescribe("takeAction");
        issuesService.process(processParam, DicDataEnum.ACTUAL_MEASURES.getCode());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/effectAnalysis")
    @ApiOperation("成效分析")
    @ApiImplicitParam(name = "processParam", value = "参数", required = true)
    public HttpResult<String> effectAnalysis(@RequestBody @Validated ProcessParam processParam){
        String methodDescribe = getMethodDescribe("effectAnalysis");
        issuesService.process(processParam, DicDataEnum.INSIGHTS.getCode());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/archive")
    @ApiOperation("问题归档")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<String> archive(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("archive");
        issuesService.archive(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    @ApiImplicitParam(name = "file", value = "填报进度文件", required = true)
    public HttpResult<MinIoUploadResDTO> uploadFile(@RequestParam("file") MultipartFile issuesFile){
        String methodDescribe = getMethodDescribe("uploadFile");
        String filePath = issuesService.uploadFile(issuesFile);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,new MinIoUploadResDTO(issuesFile.getOriginalFilename(),filePath), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/downloadFile")
    @ApiOperation("下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true),
            @ApiImplicitParam(name = "reportProcess", value = "填报进度", required = true)
    })
    public HttpResult<String> downloadFile(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo,@RequestParam("reportProcess") String reportProcess){
        String methodDescribe = getMethodDescribe("downloadFile");
        String out = issuesService.downloadFile(powerQualityProblemNo, reportProcess);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/deleteIssues")
    @ApiOperation("删除问题")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "电能质量问题编号", required = true)
    public HttpResult<String> deleteIssues(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("deleteIssues");
        issuesService.deleteIssues(powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/checkPowerQuality")
    @ApiOperation("电能质量问题审核")
    @ApiImplicitParam(name = "electricityQualityCheckParam", value = "电能质量问题审核参数", required = true)
    public HttpResult<Boolean> checkPowerQuality(@Validated @RequestBody ElectricityQualityCheckParam electricityQualityCheckParam){
        String methodDescribe = getMethodDescribe("checkPowerQuality");

        Boolean flag  = issuesService.checkPowerQuality (electricityQualityCheckParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }
    /*todo*/
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryCheckTrack")
    @ApiOperation("历史审核记录查询")
    @ApiImplicitParam(name = "powerQualityProblemNo", value = "历史审核记录查询参数", required = true)
    public HttpResult<List<RStatElectricQualityProblemLogVO>> queryCheckTrack(@RequestParam("powerQualityProblemNo") String powerQualityProblemNo){
        String methodDescribe = getMethodDescribe("checkPowerQuality");
        List<RStatElectricQualityProblemLogVO>  rStatElectricQualityProblemLogVOS  = issuesService.queryCheckTrack (powerQualityProblemNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rStatElectricQualityProblemLogVOS, methodDescribe);
    }

}
