package com.njcn.csdevice.controller.icd;


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.csdevice.param.IcdNodeParam;
import com.njcn.csdevice.pojo.param.NodeParam;
import com.njcn.csdevice.pojo.po.Node;
import com.njcn.csdevice.pojo.vo.NodeProcessDeviceVo;
import com.njcn.csdevice.service.INodeService;
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 javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 前置程序控制器
 * @author xy
 */
@Slf4j
@Api(tags = "前置机管理")
@RestController
@RequestMapping("node")
@RequiredArgsConstructor
public class NodeController extends BaseController {

    private final INodeService iNodeService;

    /**
     * 新增前置机
     * @author cdf
     * @date 2021/6/23
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @ApiOperation("新增前置机")
    @ApiImplicitParam(value = "前置机信息",name = "nodeParam",required = true)
    @PostMapping("addNode")
    public HttpResult<Boolean> addNode(@Validated @RequestBody NodeParam nodeParam){
        String methodDescribe = getMethodDescribe("addNode");
        boolean result = iNodeService.addNode(nodeParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 修改前置机
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation("修改前置机")
    @ApiImplicitParam(value = "前置机信息",name = "updateNodeParam",required = true)
    @PutMapping("updateNode")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    public HttpResult<Boolean> updateNode(@Validated @RequestBody NodeParam.NodeUpdateParam updateNodeParam){
        String methodDescribe = getMethodDescribe("updateNode");
        boolean result = iNodeService.updateNode(updateNodeParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 删除前置
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation(value = "删除前置机")
    @ApiImplicitParam(value = "前置机id",name = "id",required = true)
    @PostMapping("delNode")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    public HttpResult<Boolean> delNode(@Validated @NotNull(message = "id不可为空") @RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("delNode");
        boolean result = iNodeService.delNode(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 变更前置机状态
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation(value = "变更前置机状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "前置机id",name = "id",required = true ),
            @ApiImplicitParam(value = "前置机状态",name = "state",required = true )
    })
    @PostMapping("updateNodeState")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    public HttpResult<Boolean> updateNodeState(@Validated @NotNull(message = "id不可为空") @RequestParam("id")String id,@NotNull(message = "状态不为空") @RequestParam("state")Integer state){
        String methodDescribe = getMethodDescribe("updateNodeState");
        boolean result = iNodeService.updateNodeState(id,state);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    /**
     * 获取全部前置机分页
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation("获取全部前置机")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    @PostMapping("nodeList")
    public HttpResult<Page<Node>> nodeList(@RequestBody NodeParam.NodeQueryParam nodeQueryParam){
        String methodDescribe = getMethodDescribe("nodeList");
        Page<Node> page =  iNodeService.nodeList(nodeQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 获取全部前置机不分页
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation("获取全部前置机")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    @GetMapping("/nodeAllList")
    public HttpResult<List<Node>> nodeAllList(){
        String methodDescribe = getMethodDescribe("nodeAllList");
        List<Node> resList =  iNodeService.nodeAllList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    /**
     * 根据id获取前置机
     * @author cdf
     * @date 2021/6/23
     */
    @ApiOperation("根据id获取前置机")
    @ApiImplicitParam(value = "前置机id",name = "id",required = true)
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    @GetMapping("/getNodeById")
    public HttpResult<Node> getNodeById(@Validated @NotNull(message = "id不可为空") @RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("getNodeById");
        Node node = iNodeService.getNodeById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, node, methodDescribe);
    }

    /**
     * 根据前置机id获取下面的进程和设备
     * @author xy
     */
    @ApiOperation("根据前置机id获取下面的进程和设备")
    @ApiImplicitParam(value = "前置机id",name = "id",required = true)
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    @GetMapping("/getProcessNoAndDeviceById")
    public HttpResult<NodeProcessDeviceVo> getProcessNoAndDeviceById(@Validated @NotNull(message = "id不可为空") @RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("getProcessNoAndDeviceById");
        NodeProcessDeviceVo vo = iNodeService.getProcessNoAndDeviceById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/restartProcess")
    @ApiOperation("重启前置机进程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nodeId", value = "前置服务器id", required = true),
            @ApiImplicitParam(name = "processNo", value = "进程号", required = true)
    })
    public HttpResult<String> restartProcess(@RequestParam String nodeId,@RequestParam Integer processNo){
        String methodDescribe = getMethodDescribe("restartProcess");
        iNodeService.restartProcess(nodeId,processNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateDevProcessNo")
    @ApiOperation("更新设备进程号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备id", required = true),
            @ApiImplicitParam(name = "processNo", value = "进程号", required = true)
    })
    public HttpResult<String> updateDevProcessNo(@RequestParam String id,@RequestParam Integer processNo){
        String methodDescribe = getMethodDescribe("updateDevProcessNo");
        String result = iNodeService.updateDevProcessNo(id,processNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateDevNode")
    @ApiOperation("更新设备前置信息")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<String> updateDevNode(@RequestBody IcdNodeParam param){
        String methodDescribe = getMethodDescribe("updateDevNode");
        String result = iNodeService.updateDevNode(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

