package org.springblade.app.controller;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.http.util.Asserts;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.device.dto.UserInfoDto;
import org.springblade.device.entity.DoorDevice;
import org.springblade.device.feign.IDoorDeviceClient;
import org.springblade.device.vo.DoorDeviceVO;

import org.springblade.enterprise.entity.Staff;
import org.springblade.enterprise.feign.IStaffClient;
import org.springblade.enterprise.vo.StaffVO;
import org.springblade.estate.entity.GuestRecord;
import org.springblade.estate.feign.IGuestRecordClient;
import org.springblade.third.doordevice.feign.IDoorDeviceButtClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zwx
 * @create 2021/5/27 17:42
 */
@RestController
@RequestMapping("/doordevice")
@RequiredArgsConstructor
public class DoorDeviceController {

	private final IDoorDeviceClient doorDeviceClient;
	private final IStaffClient enterpriseStaffClient;
	private final IGuestRecordClient guestRecordClient;


	@PostMapping("/remoteOpenDoor")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "远程开门", notes = "传入deviceId, userId")
	public R remoteOpenDoor(@RequestParam(value = "macAddress", required = true) String macAddress,
							@RequestParam(value = "userId", required = true)String userId,
							@RequestParam(value = "userType", required = true) Integer userType){

		DoorDevice doorDevice = doorDeviceClient.getBySerialNum(macAddress);
		Assert.notNull(doorDevice, "设备不存在");
		String deviceUserId = null;
		String userName = null;
		String factory = doorDevice.getDeviceFactory();
		if(userType.intValue() == CommonConstant.UserType.STAFF.getType()){
			Staff staff = enterpriseStaffClient.getById(Long.valueOf(userId));
			Assert.notNull(staff, "员工不存在");
			deviceUserId = CommonConstant.UserFlag.ENTERPRISE_STAFF_FLAG.getValue() + userId;
			userName = staff.getRealName();
		}else if(userType.intValue() == CommonConstant.UserType.GUEST.getType()){
			GuestRecord guestRecord = guestRecordClient.getById(Long.valueOf(userId));
			Assert.notNull(guestRecord, "访客不存在");
			Assert.isTrue(guestRecord.getAuditStatus() == 1, "访客未审核通过");
			deviceUserId = CommonConstant.UserFlag.GUEST_FLAG.getValue() + userId;
			userName = guestRecord.getGuestName();
		}else if(userType.intValue() == CommonConstant.UserType.ADMIN.getType()){
			BladeUser bladeUser = AuthUtil.getUser();
			Assert.notNull(bladeUser, "管理员不存在");
			deviceUserId = CommonConstant.UserFlag.ADMIN_FLAG.getValue() + userId;
			userName = bladeUser.getUserName();
		}
		Map<String,Object> userInfoDto = new HashMap<>();
		userInfoDto.put("id",CommonConstant.UserFlag.ENTERPRISE_STAFF_FLAG + userId);
		return doorDeviceClient.remoteOpenDoor(deviceUserId, userName, Convert.toStr(userType), macAddress, factory, true);
	}
}
