/*
 *      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.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.codehaus.janino.Access;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.DoorDevice;
import org.springblade.device.vo.DoorDeviceVO;
import org.springblade.device.mapper.DoorDeviceMapper;
import org.springblade.device.service.IDoorDeviceService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.estate.dto.AccessRecordDTO;
import org.springblade.estate.entity.AccessRecord;
import org.springblade.estate.feign.IAccessRecordClient;
import org.springblade.third.doordevice.feign.IDoorDeviceButtClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Date;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-04-19
 */
@Service
public class DoorDeviceServiceImpl extends BaseServiceImpl<DoorDeviceMapper, DoorDevice> implements IDoorDeviceService {

	@Autowired
	private IDoorDeviceButtClient doorDeviceButtClient;
	@Autowired
	private IAccessRecordClient accessRecordClient;

	@Override
	public IPage<DoorDeviceVO> selectDoorDevicePage(IPage<DoorDeviceVO> page, DoorDeviceVO doorDevice) {
		return page.setRecords(baseMapper.selectDoorDevicePage(page, doorDevice));
	}

	@Override
	public DoorDevice getBySerialNum(String serialNum) {
		LambdaQueryWrapper<DoorDevice> lambda = new LambdaQueryWrapper<>();
		lambda.eq(DoorDevice::getSerialNum, serialNum);
		return baseMapper.selectOne(lambda);
	}

	@Override
	public R heartbeat(String serialNum) {
		LambdaUpdateWrapper<DoorDevice> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(DoorDevice::getHeartbeatTime, new Date());
		updateWrapper.eq(DoorDevice::getSerialNum, serialNum);
		int update = this.baseMapper.update(new DoorDevice(), updateWrapper);
		if(update > 0){
			return R.status(true);
		}
		return R.status(false);
	}

	@Override
	public R remoteOpenDoor(String userId, String userName, String userType, String serialNum, String factory, boolean isOpen) {
		//创建开门记录
		AccessRecordDTO accessRecord = new AccessRecordDTO();
		accessRecord.setUserId(userId);
		accessRecord.setUserName(userName);
		accessRecord.setSerialNum(serialNum);
		accessRecord.setUserType(userType);
		accessRecord.setOpenType(2);
		accessRecord.setOpenTime(new Date());
		accessRecordClient.generateAccessRecord(accessRecord);
		return this.doorDeviceButtClient.remoteOpenDoor(serialNum, factory, isOpen);
	}

}
