package com.njcn.gather.device.types.controller;

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.LogUtil;
import com.njcn.gather.device.types.pojo.param.DeviceTypeParam;
import com.njcn.gather.device.types.pojo.vo.DeviceTypeVO;
import com.njcn.gather.device.types.pojo.vo.PqdifCheckPlaceholderVO;
import com.njcn.gather.device.types.service.DeviceTypeService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 设备类型维护入口。
 */
@Slf4j
@Api(tags = "设备类型管理")
@RestController
@RequestMapping("/api/device-types")
@RequiredArgsConstructor
public class DeviceTypeController extends BaseController {

    private final DeviceTypeService deviceTypeService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询设备类型列表")
    @ApiImplicitParam(name = "keyword", value = "keyword", required = false)
    @GetMapping
    public HttpResult<List<DeviceTypeVO>> listDeviceTypes(@RequestParam(value = "keyword", required = false) String keyword) {
        String methodDescribe = getMethodDescribe("listDeviceTypes");
        LogUtil.njcnDebug(log, "{}，开始查询设备类型列表", methodDescribe);
        List<DeviceTypeVO> result = deviceTypeService.listDeviceTypes(keyword);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("新增设备类型")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated DeviceTypeParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，开始新增设备类型", methodDescribe);
        boolean result = deviceTypeService.addDeviceType(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("编辑设备类型")
    @PostMapping("/update")
    public HttpResult<Boolean> update(@RequestBody @Validated DeviceTypeParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，开始编辑设备类型，devTypeId={}", methodDescribe, param.getId());
        boolean result = deviceTypeService.updateDeviceType(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("删除设备类型")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，开始删除设备类型，ids={}", methodDescribe, ids);
        boolean result = deviceTypeService.deleteDeviceType(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("设备类型PQDIF校验")
    @ApiImplicitParam(name = "id", value = "设备类型ID", required = true)
    @PostMapping("/{id}/pqdif-check")
    public HttpResult<PqdifCheckPlaceholderVO> pqdifCheck(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("pqdifCheck");
        LogUtil.njcnDebug(log, "{}，设备类型PQDIF校验预留入口，devTypeId={}", methodDescribe, id);
        PqdifCheckPlaceholderVO result = deviceTypeService.pqdifCheck(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
