package com.njcn.energy.controller;


import cn.hutool.core.util.StrUtil;
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.common.utils.LogUtil;
import com.njcn.energy.pojo.param.NetDevParam;
import com.njcn.energy.pojo.po.NetDev;
import com.njcn.energy.pojo.vo.NetDevInfoVO;
import com.njcn.energy.service.INetDevService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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 com.njcn.web.controller.BaseController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cdf
 * @since 2022-02-22
 */
@Slf4j
@RestController
@RequestMapping("/netDev")
@RequiredArgsConstructor
@Api(tags = "网关控制器")
public class NetDevController extends BaseController {

    private final INetDevService netDevService;

    /**
     * 新增网关
     * @author cdf
     * @date 2022/2/22
     */
    @PostMapping("add")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("新增网关设备")
    @ApiImplicitParam(name = "netDevParam", value = "网关设备", required = true)
    public HttpResult<Object> add(@RequestBody NetDevParam netDevParam){
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，网关设备数据为：{}", methodDescribe, netDevParam);
        boolean result = netDevService.addNetDev(netDevParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }

    }



    /**
     * 分页查询网关设备数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询网关设备")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<NetDev>> list(@RequestBody @Validated NetDevParam.NetDevQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<NetDev> result = netDevService.listNetDev(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 修改网关设备数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改网关设备")
    @ApiImplicitParam(name = "updateParam", value = "网关设备数据", required = true)
    public HttpResult<Object> update(@RequestBody @Validated NetDevParam.NetDevUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，网关设备数据为：{}", methodDescribe, updateParam);
        boolean result = netDevService.updateNetDev(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }

    }

    /**
     * 删除网关设备
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除网关设备")
    @ApiImplicitParam(name = "ids", value = "网关设备索引", required = true)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = netDevService.deleteNetDev(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 查询网关详细信息
     * @author cdf
     * @date 2022/3/28
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getNetDevInfo")
    @ApiOperation("查询网关设备")
    @ApiImplicitParam(name = "id", value = "查询参数", required = true)
    public HttpResult<NetDevInfoVO> getNetDevInfo(@RequestParam("id")String id) {
        String methodDescribe = getMethodDescribe("getNetDevInfo");
        NetDevInfoVO netDevInfo  = netDevService.getNetDevInfo(id);
        if(Objects.nonNull(netDevInfo)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, netDevInfo, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
    }

}

