package com.njcn.csdevice.controller.equipment;

import com.njcn.common.pojo.annotation.OperateInfo;
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.pojo.po.CsDeviceRegistry;
import com.njcn.csdevice.service.ICsDeviceRegistryService;
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.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徐扬
 */
@RestController
@RequestMapping("/csDeviceRegistry")
@Api(tags = "设备注册台账记录表")
@AllArgsConstructor
public class CsDeviceRegistryController extends BaseController {

    private final ICsDeviceRegistryService csDeviceRegistryService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("批量新增设备注册记录")
    @ApiImplicitParam(name = "list", value = "设备注册信息列表", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated List<CsDeviceRegistry> list) {
        String methodDescribe = getMethodDescribe("add");
        boolean result = csDeviceRegistryService.add(list);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/update")
    @ApiOperation("修改设备注册记录")
    @ApiImplicitParam(name = "csDeviceRegistry", value = "设备注册信息", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated CsDeviceRegistry csDeviceRegistry) {
        String methodDescribe = getMethodDescribe("update");
        boolean result = csDeviceRegistryService.update(csDeviceRegistry);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateBatch")
    @ApiOperation("批量修改设备注册记录")
    @ApiImplicitParam(name = "list", value = "设备注册信息列表", required = true)
    public HttpResult<Boolean> updateBatch(@RequestBody @Validated List<CsDeviceRegistry> list) {
        String methodDescribe = getMethodDescribe("updateBatch");
        boolean result = csDeviceRegistryService.updateBatch(list);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/delete")
    @ApiOperation("删除设备注册记录")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<Boolean> delete(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = csDeviceRegistryService.delete(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateIsAccessByCurrentNdid")
    @ApiOperation("根据currentNdid修改isAccess")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentNdid", value = "当前设备NDID", required = true),
            @ApiImplicitParam(name = "isAccess", value = "接入状态(0:未接入 1:已接入)", required = true)
    })
    public HttpResult<Boolean> updateIsAccessByCurrentNdid(
            @RequestParam("currentNdid") String currentNdid,
            @RequestParam("isAccess") Integer isAccess) {
        String methodDescribe = getMethodDescribe("updateIsAccessByCurrentNdid");
        boolean result = csDeviceRegistryService.updateIsAccessByCurrentNdid(currentNdid, isAccess);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deleteByCurrentNdid")
    @ApiOperation("根据currentNdid删除设备注册记录")
    @ApiImplicitParam(name = "currentNdid", value = "当前设备NDID", required = true)
    public HttpResult<List<CsDeviceRegistry>> deleteByCurrentNdid(@RequestParam("currentNdid") String currentNdid) {
        String methodDescribe = getMethodDescribe("deleteByCurrentNdid");
        csDeviceRegistryService.deleteByCurrentNdid(currentNdid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryByCurrentNdid")
    @ApiOperation("根据currentNdid查询设备注册记录")
    @ApiImplicitParam(name = "currentNdid", value = "当前设备NDID", required = true)
    public HttpResult<List<CsDeviceRegistry>> queryByCurrentNdid(@RequestParam("currentNdid") String currentNdid) {
        String methodDescribe = getMethodDescribe("queryByCurrentNdid");
        List<CsDeviceRegistry> list = csDeviceRegistryService.queryByCurrentNdid(currentNdid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getById")
    @ApiOperation("根据id查询设备注册记录")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<CsDeviceRegistry> getById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        CsDeviceRegistry csDeviceRegistry = csDeviceRegistryService.getById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, csDeviceRegistry, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryByCurrentNdidAndClDid")
    @ApiOperation("根据currentNdid和clDid查询设备注册记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentNdid", value = "当前设备NDID", required = true),
            @ApiImplicitParam(name = "clDid", value = "逻辑子设备编号", required = true)
    })
    public HttpResult<CsDeviceRegistry> queryByCurrentNdidAndClDid(
            @RequestParam("currentNdid") String currentNdid,
            @RequestParam("clDid") Integer clDid) {
        String methodDescribe = getMethodDescribe("queryByCurrentNdidAndClDid");
        CsDeviceRegistry pojo = csDeviceRegistryService.queryByCurrentNdidAndClDid(currentNdid, clDid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pojo, methodDescribe);
    }

}
