/*
 *      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.smartapplication.devicelog.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.smartapplication.devicelog.entity.DeviceLog;
import org.springblade.smartapplication.devicelog.vo.DeviceLogVO;
import org.springblade.smartapplication.smartsmoke.dto.SmartDeviceDTO;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2020-11-16
 */
public class DeviceLogWrapper extends BaseEntityWrapper<DeviceLog, DeviceLogVO>  {
	private ISmartDeviceService smartDeviceService;

	public static DeviceLogWrapper build( ISmartDeviceService smartDeviceService) {
		return new DeviceLogWrapper(smartDeviceService);
 	}

 	public DeviceLogWrapper(ISmartDeviceService smartDeviceService){
		this.smartDeviceService = smartDeviceService;
	}

	@Override
	public DeviceLogVO entityVO(DeviceLog deviceLog) {
		DeviceLogVO deviceLogVO = Objects.requireNonNull(BeanUtil.copy(deviceLog, DeviceLogVO.class));

		if(deviceLogVO.getDeviceId()!=null){
			SmartDevice deviceDetail = smartDeviceService.getOne(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getDeviceId, deviceLogVO.getDeviceId()));
			if(deviceDetail!=null){
				deviceLogVO.setDeviceName(deviceDetail.getDeviceName());
				deviceLogVO.setDeviceType(deviceDetail.getDeviceType().toString());
				deviceLogVO.setSmartDevice(deviceDetail);
			}
		}

		return deviceLogVO;
	}

}
