/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.device.dto.Gb28181DeviceDTO;
import org.springblade.third.videodevice.gb28181.entity.GbDevice;
import org.springblade.third.videodevice.gb28181.feign.IThirdGbDeviceClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.device.entity.Gb28181Device;
import org.springblade.device.vo.Gb28181DeviceVO;
import org.springblade.device.wrapper.GbDeviceWrapper;
import org.springblade.device.service.IGbDeviceService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-12-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("/gbdevice")
@Api(value = "", tags = "接口")
public class GbDeviceController extends BladeController {

	private final IGbDeviceService gbDeviceService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gbDevice")
	public R<Gb28181DeviceVO> detail(Gb28181Device gb28181Device) {
		Gb28181Device detail = gbDeviceService.getOne(Condition.getQueryWrapper(gb28181Device));
		return R.data(GbDeviceWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gbDevice")
	public R<IPage<Gb28181DeviceVO>> list(Gb28181Device gb28181Device, Query query) {
		IPage<Gb28181Device> pages = gbDeviceService.page(Condition.getPage(query), Condition.getQueryWrapper(gb28181Device));
		return R.data(GbDeviceWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gbDevice")
	public R<IPage<Gb28181DeviceVO>> page(Gb28181DeviceVO gbDevice, Query query) {
		IPage<Gb28181DeviceVO> pages = gbDeviceService.selectGbDevicePage(Condition.getPage(query), gbDevice);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入gbDevice")
	public R save(@Valid @RequestBody Gb28181Device gb28181Device) {
		return R.status(gbDeviceService.save(gb28181Device));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gbDevice")
	public R update(@Valid @RequestBody Gb28181Device gb28181Device) {
		return R.status(gbDeviceService.updateById(gb28181Device));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入gbDevice")
	public R submit(@Valid @RequestBody Gb28181Device gb28181Device) {
		return R.status(gbDeviceService.saveOrUpdate(gb28181Device));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return gbDeviceService.deleteDevice(Func.toLongList(ids));
	}

	/**
	 * 设备绑定
	 */
	@PostMapping("/bind")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "设备绑定", notes = "传入gbDevice")
	public R bind(@Valid @RequestBody Gb28181Device gbDevice) {
		LambdaUpdateWrapper<Gb28181Device> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(Gb28181Device::getAgencyId, gbDevice.getAgencyId());
		updateWrapper.set(Gb28181Device::getResidentialId, gbDevice.getResidentialId());
		updateWrapper.eq(Gb28181Device::getDeviceId, gbDevice.getDeviceId());
		return R.status(gbDeviceService.update(updateWrapper));
	}

	/**
	 * 设备刷新
	 */
	@PostMapping("/sync")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "设备刷新", notes = "传入gbDevice")
	public R sync(@RequestBody Gb28181Device gb28181Device) {
		return this.gbDeviceService.sync(gb28181Device);
	}


}
