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

import org.springblade.core.tool.api.R;
import org.springblade.device.entity.DoorDevice;
import org.springblade.device.feign.IDoorDeviceClient;
import org.springblade.estate.dto.SnapRecordDTO;
import org.springblade.estate.entity.SnapRecord;
import org.springblade.estate.vo.SnapRecordVO;
import org.springblade.estate.mapper.SnapRecordMapper;
import org.springblade.estate.service.ISnapRecordService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2022-01-05
 */
@Service
public class SnapRecordServiceImpl extends BaseServiceImpl<SnapRecordMapper, SnapRecord> implements ISnapRecordService {

	@Autowired
	private IDoorDeviceClient doorDeviceClient;

	@Override
	public IPage<SnapRecordVO> selectSnapRecordPage(IPage<SnapRecordVO> page, SnapRecordVO snapRecord) {
		return page.setRecords(baseMapper.selectSnapRecordPage(page, snapRecord));
	}

	@Override
	public R generateSnapRecord(SnapRecordDTO snapRecordDTO) {
		DoorDevice doorDevice = doorDeviceClient.getBySerialNum(snapRecordDTO.getSerialNum());
		snapRecordDTO.setDeviceName(doorDevice.getName());
		snapRecordDTO.setDeviceAddress(doorDevice.getAddress());
		snapRecordDTO.setIsOut(doorDevice.getIsOut());
		snapRecordDTO.setAgencyId(doorDevice.getAgencyId());
		snapRecordDTO.setResidentialId(doorDevice.getResidentialId());
		snapRecordDTO.setBuildingId(doorDevice.getBuildingId());
		snapRecordDTO.setUnitId(doorDevice.getUnitId());
		snapRecordDTO.setRoomId(doorDevice.getRoomId());
		snapRecordDTO.setEnterpriseId(doorDevice.getEnterpriseId());
		return R.status(save(snapRecordDTO));
	}

}
