package org.springblade.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.device.dto.IotDeviceDTO;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.feign.IIotDeviceClient;
import org.springblade.device.vo.IotDeviceVO;
import org.springblade.enterprise.entity.Enterprise;
import org.springblade.enterprise.feign.IEnterpriseClient;
import org.springblade.enterprise.vo.EnterpriseVO;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lidexi
 * @date 2021/5/17 16:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/iotdevice")
public class IotDeviceController {

	private final IIotDeviceClient iotDeviceClient;
	private final IEnterpriseClient enterpriseClient;

	/**
	 * 分页 智能设备
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "分页", notes = "传入smartDevice")
	public R<BladePage<IotDeviceVO>> list(IotDeviceDTO iotDeviceDTO, Query query) {
		iotDeviceDTO.setQuery(query);
		//企业管理员查询
		if(iotDeviceDTO.getCreditCode() != null){
			Enterprise searchEntity = new Enterprise();
			searchEntity.setCreditCode(iotDeviceDTO.getCreditCode());
			EnterpriseVO detail = enterpriseClient.detail(searchEntity);
			Assert.notNull(detail,"找不到企业");
			iotDeviceDTO.setEnterpriseIds(detail.getId().toString());
		}
		return R.data(this.iotDeviceClient.getList(iotDeviceDTO));

	}

}
