package com.njcn.supervision.controller.survey;


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.leaflet.WarningAddParam;
import com.njcn.supervision.pojo.param.survey.SurveyTestParam;
import com.njcn.supervision.pojo.vo.survey.SurveyTestVO;
import com.njcn.supervision.service.survey.ISurveyTestService;
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 java.util.List;

/**
 * <p>
 * 普测测试的管理表 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2024-05-30
 */
@RestController
@RequestMapping("/surveyTest")
@Slf4j
@Api(tags = "普测计划流程管理")
@AllArgsConstructor
public class SurveyTestController extends BaseController {

    private final ISurveyTestService surveyTestService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/surveyTestPage")
    @ApiOperation("分页查询当前用户能看到的普测测试")
    @ApiImplicitParam(name = "surveyTestQueryParam", value = "参数", required = true)
    public HttpResult<Page<SurveyTestVO>> surveyTestPage(@RequestBody @Validated SurveyTestParam.SurveyTestQueryParam surveyTestQueryParam) {
        String methodDescribe = getMethodDescribe("surveyTestPage");
        Page<SurveyTestVO> out = surveyTestService.surveyTestPage(surveyTestQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageProblemSurvey")
    @ApiOperation("分页查询当前用户能看到的普测计划中存在超标问题")
    @ApiImplicitParam(name = "surveyTestQueryParam", value = "参数", required = true)
    public HttpResult<Page<SurveyTestVO>> pageProblemSurvey(@RequestBody @Validated SurveyTestParam.SurveyTestQueryParam surveyTestQueryParam ){
        String methodDescribe = getMethodDescribe("pageProblemSurvey");
        Page<SurveyTestVO> out = surveyTestService.pageProblemSurvey(surveyTestQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增普测测试")
    @ApiImplicitParam(name = "surveyTestParam", value = "实体参数", required = true)
    public HttpResult<String> addSurveyTest(@RequestBody @Validated SurveyTestParam surveyTestParam) {
        String methodDescribe = getMethodDescribe("addSurveyTest");
        String planId = surveyTestService.addSurveyTest(surveyTestParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, planId, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/update")
    @ApiOperation("重新发起普测测试")
    @ApiImplicitParam(name = "surveyTestUpdateParam", value = "实体参数", required = true)
    public HttpResult<String> updateSurveyTest(@RequestBody @Validated SurveyTestParam.SurveyTestUpdateParam surveyTestUpdateParam) {
        String methodDescribe = getMethodDescribe("updateSurveyTest");
        String runningDeviceId = surveyTestService.updateSurveyTest(surveyTestUpdateParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, runningDeviceId, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/cancel")
    @ApiOperation("取消普测测试")
    @ApiImplicitParam(name = "cancelReqVO", value = "取消原因", required = true)
    public HttpResult<String> cancelSurveyTest(@Validated @RequestBody BpmProcessInstanceCancelParam cancelReqVO) {
        String methodDescribe = getMethodDescribe("cancelSurveyTest");
        String runningDeviceId = surveyTestService.cancelSurveyTest(cancelReqVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, runningDeviceId, methodDescribe);
    }

    @GetMapping("/updateSurveyTestStatus")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更新普测测试流程状态")
    public HttpResult<Object> updateSurveyTestStatus(String businessKey, Integer status) {
        String methodDescribe = getMethodDescribe("updateSurveyTestStatus");
        surveyTestService.updateProcessStatus(businessKey, status);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @GetMapping("/getById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id获取普测计划测试的详细数据")
    public HttpResult<SurveyTestVO> getById(String id) {
        String methodDescribe = getMethodDescribe("getById");
        SurveyTestVO surveyTestVO = surveyTestService.getVOById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, surveyTestVO, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/initiateWarningLeaflet")
    @ApiOperation("针对有问题的现场测试发起告警单")
    public HttpResult<Boolean> initiateWarningLeaflet(@RequestBody WarningAddParam warningAddParam){
        String methodDescribe = getMethodDescribe("initiateWarningLeaflet");
        surveyTestService.initiateWarningLeaflet(warningAddParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, Boolean.TRUE, methodDescribe);
    }

    @PostMapping(value = "/deleteSurveyPlanTest")
    @ApiOperation("技术监督测试管理删除流程")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM, operateType = OperateType.DELETE)
    public HttpResult<Boolean> deleteSurveyPlanTest(@RequestBody List<String> supervisionId) {
        String methodDescribe = getMethodDescribe("deleteSurveyPlanTest");
        Boolean b = surveyTestService.deleteSurveyPlanTest(supervisionId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }
}


