package cn.skcks.docking.gb28181.wvp.api.device;

import cn.skcks.docking.gb28181.annotation.web.JsonMapping;
import cn.skcks.docking.gb28181.annotation.web.methods.GetJson;
import cn.skcks.docking.gb28181.annotation.web.methods.PostJson;
import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.common.page.PageWrapper;
import cn.skcks.docking.gb28181.wvp.api.device.convertor.DeviceDTOConvertor;
import cn.skcks.docking.gb28181.wvp.api.device.dto.AddDeviceDTO;
import cn.skcks.docking.gb28181.wvp.api.device.dto.DevicePageDTO;
import cn.skcks.docking.gb28181.wvp.config.SwaggerConfig;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice;
import cn.skcks.docking.gb28181.wvp.service.device.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@Tag(name = "设备信息")
@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
public class DeviceController {
    private final DeviceService deviceService;

    @Bean
    public GroupedOpenApi deviceApi() {
        return SwaggerConfig.api("DeviceApi", "/device");
    }

    @Operation(summary = "分页查询设备列表")
    @GetJson("/page")
    public JsonResponse<PageWrapper<WvpProxyDevice>> getDevicesWithPagePage(@ParameterObject @Validated DevicePageDTO dto) {
        return JsonResponse.success(PageWrapper.of(deviceService.getDevicesWithPage(dto.getPage(), dto.getSize())));
    }

    @Operation(summary = "添加设备")
    @PostJson("/add")
    public JsonResponse<Boolean> addDevice(@RequestBody AddDeviceDTO dto) {
        return JsonResponse.success(deviceService.addDevice(DeviceDTOConvertor.INSTANCE.dto2dao(dto)));
    }

    @Operation(summary = "根据设备编码(21位) 查询指定设备信息")
    @GetJson("/info/deviceCode")
    public JsonResponse<WvpProxyDevice> infoByDeviceCode(@RequestParam String deviceCode) {
        Optional<WvpProxyDevice> wvpProxyDevice = deviceService.getDeviceByDeviceCode(deviceCode);
        return JsonResponse.success(wvpProxyDevice.orElse(null));
    }

    @Operation(summary = "根据国标id(20位) 查询指定设备信息")
    @GetJson("/info/gbDeviceId")
    public JsonResponse<List<WvpProxyDevice>> infoByGbDeviceId(@RequestParam String gbDeviceId) {
        List<WvpProxyDevice> wvpProxyDevice = deviceService.getDeviceByGbDeviceId(gbDeviceId);
        return JsonResponse.success(wvpProxyDevice);
    }

    @Operation(summary = "根据设备编码(21位) 删除指定设备")
    @JsonMapping(value = "/delete/deviceCode",method = {RequestMethod.GET,RequestMethod.DELETE})
    public JsonResponse<Boolean> deleteByDeviceCode(@RequestParam String deviceCode){
        WvpProxyDevice wvpProxyDevice = new WvpProxyDevice();
        wvpProxyDevice.setDeviceCode(deviceCode);
        return JsonResponse.success(deviceService.deleteDevice(wvpProxyDevice));
    }

    @Operation(summary = "根据国标id(20位) 删除指定设备")
    @JsonMapping(value = "/delete/gbDeviceId",method = {RequestMethod.GET,RequestMethod.DELETE})
    public JsonResponse<Boolean> deleteByGbDeviceId(@RequestParam String gbDeviceId){
        WvpProxyDevice wvpProxyDevice = new WvpProxyDevice();
        wvpProxyDevice.setGbDeviceId(gbDeviceId);
        return JsonResponse.success(deviceService.deleteDevice(wvpProxyDevice));
    }

    @Operation(summary = "根据主键 id 删除指定设备")
    @JsonMapping(value = "/delete/id",method = {RequestMethod.GET,RequestMethod.DELETE})
    public JsonResponse<Boolean> deleteById(@RequestParam Long id){
        WvpProxyDevice wvpProxyDevice = new WvpProxyDevice();
        wvpProxyDevice.setId(id);
        return JsonResponse.success(deviceService.deleteDevice(wvpProxyDevice));
    }

    @Operation(summary = "根据主键id修改设备信息")
    @PostJson("/modify")
    public JsonResponse<Boolean> updateById(@RequestBody WvpProxyDevice device){
        return JsonResponse.success(deviceService.modifyDevice(device));
    }
}
