/*
 *      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.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.device.entity.DoorDevice;
import org.springblade.device.vo.GbDeviceChannelTreeVO;
import org.springblade.system.vo.DeptVO;
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.GbDeviceChannel;
import org.springblade.device.vo.GbDeviceChannelVO;
import org.springblade.device.wrapper.GbDeviceChannelWrapper;
import org.springblade.device.service.IGbDeviceChannelService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

	private final IGbDeviceChannelService gbDeviceChannelService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gbDeviceChannel")
	public R<GbDeviceChannelVO> detail(GbDeviceChannel gbDeviceChannel) {
		GbDeviceChannel detail = gbDeviceChannelService.getOne(Condition.getQueryWrapper(gbDeviceChannel));
		return R.data(GbDeviceChannelWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gbDeviceChannel")
	public R<IPage<GbDeviceChannelVO>> list(GbDeviceChannel gbDeviceChannel, Query query) {
		LambdaQueryWrapper<GbDeviceChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.like(StringUtil.isNotBlank(gbDeviceChannel.getName()), GbDeviceChannel::getName, gbDeviceChannel.getName());
		lambdaQueryWrapper.eq(StringUtil.isNotBlank(gbDeviceChannel.getChannelId()), GbDeviceChannel::getChannelId, gbDeviceChannel.getChannelId());
		lambdaQueryWrapper.eq(StringUtil.isNotBlank(gbDeviceChannel.getDeviceId()), GbDeviceChannel::getDeviceId, gbDeviceChannel.getDeviceId());
		IPage<GbDeviceChannel> pages = gbDeviceChannelService.page(Condition.getPage(query), lambdaQueryWrapper);
		return R.data(GbDeviceChannelWrapper.build().pageVO(pages));
	}


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

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

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

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

	/**
	 * 批量更新
	 */
	@PostMapping("/updateBatch")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "批量更新", notes = "传入doorDevice")
	public R update(@Valid @RequestBody List<GbDeviceChannel> gbDeviceChannels) {
		return R.status(gbDeviceChannelService.saveOrUpdateBatch(gbDeviceChannels));
	}

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

	@PostMapping("/play")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "播放", notes = "deviceId,channelId")
	public R<JSONObject> play(@RequestParam(value = "deviceId", required = true) String deviceId, @RequestParam(value = "channelId", required = true) String channelId){
		return gbDeviceChannelService.play(deviceId, channelId);
	}

	@PostMapping("/stop")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "播放", notes = "deviceId,channelId")
	public R<String> stop(@RequestParam(value = "deviceId", required = true) String deviceId, @RequestParam(value = "channelId", required = true) String channelId){
		return gbDeviceChannelService.stop(deviceId, channelId);
	}

	/**
	 * 懒加载获取视频通道树形结构
	 */
	@GetMapping("/lazy-tree")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "懒加载树形结构", notes = "树形结构")
	public R<List<GbDeviceChannelTreeVO>> lazyTree(@RequestParam(value = "parentColumn", required = true) String parentColumn, @RequestParam(value = "parentId", required = false) Long parentId) {
		List<GbDeviceChannelTreeVO> tree = gbDeviceChannelService.lazyTree(parentColumn, parentId);
		return R.data(tree);
	}

	/**
	 * 点位绑定
	 * @param deviceId
	 * @param channelId
	 * @param residentialId
	 * @param buildingId
	 * @param floorId
	 * @return
	 */
	@PostMapping("/bind")
	@ApiOperationSupport(order = 9)
	public R bind(@RequestParam(value = "deviceId", required = true) String deviceId,
				  @RequestParam(value = "channelId", required = true) String channelId,
				  @RequestParam(value = "residentialId", required = false) Long residentialId,
				  @RequestParam(value = "buildingId", required = false) Long buildingId,
				  @RequestParam(value = "floorId", required = false) Long floorId,
				  @RequestParam(value = "installAddress", required = false) String installAddress){
		return gbDeviceChannelService.bind(deviceId, channelId, residentialId, buildingId, floorId, installAddress);
	}


	/**
	 * 获取视频设备的经纬度坐标集合
	 */
	@GetMapping("/getVideoDeviceCoordinates")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "详情", notes = "传入doorDevice")
	public R getVideoDeviceCoordinates() {
		List<GbDeviceChannel> list = gbDeviceChannelService.list(Condition.getQueryWrapper(new GbDeviceChannel()).lambda()
			.ne(GbDeviceChannel::getLatitude, "")
			.ne(GbDeviceChannel::getLongitude, ""));
		return R.data(list);
	}

	/**
	 * 编辑
	 */
	@PostMapping("/edit")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "修改", notes = "传入gbDeviceChannel")
	public R edit(@RequestBody GbDeviceChannel gbDeviceChannel) {
		return R.status(gbDeviceChannelService.updateById(gbDeviceChannel));
	}

}
