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.QuitRunningDeviceParam;
import com.njcn.supervision.pojo.vo.device.QuitRunningDeviceVO;
import com.njcn.supervision.service.device.IQuitRunningDeviceService;
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-13
 */
@Slf4j
@RestController
@RequestMapping("/quitRunningDevice")
@Api(tags = "设备退运管理")
@AllArgsConstructor
public class QuitRunningDeviceController extends BaseController {

    private final IQuitRunningDeviceService quitRunningDeviceService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("分页查询当前用户能看到的退运设备数据")
    @ApiImplicitParam(name = "quitRunningDeviceQueryParam", value = "参数", required = true)
    public HttpResult<Page<QuitRunningDeviceVO>> getQuitRunningDevice(@RequestBody @Validated QuitRunningDeviceParam.QuitRunningDeviceQueryParam quitRunningDeviceQueryParam) {
        String methodDescribe = getMethodDescribe("getQuitRunningDevice");
        Page<QuitRunningDeviceVO> out = quitRunningDeviceService.getQuitRunningDevice(quitRunningDeviceQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增设备退运")
    @ApiImplicitParam(name = "quitRunningDeviceParam", value = "实体参数", required = true)
    public HttpResult<String> addRunningDevice(@RequestBody @Validated QuitRunningDeviceParam quitRunningDeviceParam) {
        String methodDescribe = getMethodDescribe("addRunningDevice");
        String runningDeviceId = quitRunningDeviceService.addRunningDevice(quitRunningDeviceParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, runningDeviceId, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/update")
    @ApiOperation("重新发起设备退运")
    @ApiImplicitParam(name = "quitRunningDeviceUpdateParam", value = "实体参数", required = true)
    public HttpResult<String> updateQuitRunningDevice(@RequestBody @Validated QuitRunningDeviceParam.QuitRunningDeviceUpdateParam quitRunningDeviceUpdateParam) {
        String methodDescribe = getMethodDescribe("quitRunningDeviceUpdateParam");
        String runningDeviceId = quitRunningDeviceService.updateQuitRunningDevice(quitRunningDeviceUpdateParam);
        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> cancelQuitRunningDevice(@Validated @RequestBody BpmProcessInstanceCancelParam cancelReqVO) {
        String methodDescribe = getMethodDescribe("cancelQuitRunningDevice");
        String runningDeviceId = quitRunningDeviceService.cancelQuitRunningDevice(cancelReqVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, runningDeviceId, methodDescribe);
    }

    @GetMapping("/updateDeviceStatus")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更新退运装置流程状态")
    public HttpResult<Object> updateDeviceStatus(String businessKey, Integer status) {
        String methodDescribe = getMethodDescribe("updateDeviceStatus");
        quitRunningDeviceService.updateProcessStatus(businessKey, status);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @GetMapping("/getById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id获取设备的详细数据")
    public HttpResult<QuitRunningDeviceVO> getById(String id) {
        String methodDescribe = getMethodDescribe("getById");
        QuitRunningDeviceVO quitRunningDeviceVO = quitRunningDeviceService.getVOById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, quitRunningDeviceVO, methodDescribe);
    }

    @PostMapping(value = "/deleteQuitRunningDevice")
    @ApiOperation("设备退运管理删除流程")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM, operateType = OperateType.DELETE)
    public HttpResult<Boolean> deleteQuitRunningDevice(@RequestBody List<String> supervisionId) {
        String methodDescribe = getMethodDescribe("deleteQuitRunningDevice");
        Boolean b = quitRunningDeviceService.deleteQuitRunningDevice(supervisionId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

}

