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

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.springblade.community.enterprise.entity.Enterprise;
import org.springblade.community.enterprise.service.IEnterpriseService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.smartapplication.facedevice.entity.FaceDevice;
import org.springblade.smartapplication.facedevice.vo.FaceDeviceVO;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.vo.SmartDeviceVO;
import org.springblade.statistics.vo.SmartDeviceStaticVO;
import org.springblade.system.deviceicon.entity.DeviceTypeIcon;
import org.springblade.system.deviceicon.service.IDeviceTypeIconService;

import java.util.IdentityHashMap;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2020-12-04
 */
public class SmartDeviceWrapper extends BaseEntityWrapper<SmartDevice, SmartDeviceVO> {
	private IDictBizService dictBizService;
	private IDeviceTypeIconService deviceTypeIconService;
	private IEnterpriseService enterpriseService;

	public static SmartDeviceWrapper build(IDictBizService dictBizService, IDeviceTypeIconService deviceTypeIconService, IEnterpriseService enterpriseService) {
		return new SmartDeviceWrapper(dictBizService, deviceTypeIconService, enterpriseService);
	}

	public SmartDeviceWrapper(IDictBizService dictBizService, IDeviceTypeIconService deviceTypeIconService, IEnterpriseService enterpriseService) {
		this.dictBizService = dictBizService;
		this.deviceTypeIconService = deviceTypeIconService;
		this.enterpriseService = enterpriseService;
	}

	@Override
	public SmartDeviceVO entityVO(SmartDevice smartDevice) {
		SmartDeviceVO smartDeviceVO = Objects.requireNonNull(BeanUtil.copy(smartDevice, SmartDeviceVO.class));

		//获取设备类型名称
		String deviceType = smartDeviceVO.getDeviceType();
		if (deviceType != null) {
			DictBiz dictBiz = dictBizService.getBaseMapper().selectOne(new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "device_type").eq(DictBiz::getDictKey, deviceType));
			if (dictBiz != null) {
				smartDeviceVO.setDeviceTypeName(dictBiz.getDictValue());
			}
		}
		//获取设备类型图标
		DeviceTypeIcon deviceTypeIcon = deviceTypeIconService.getBaseMapper().selectOne(new QueryWrapper<>(new DeviceTypeIcon()).lambda().eq(DeviceTypeIcon::getDeviceTypeName, smartDeviceVO.getDeviceTypeName()));
		if (deviceTypeIcon != null) {
			smartDeviceVO.setIconUrl(deviceTypeIcon.getIcon());
		}

		//所属设备名称
		String enterpriseIds1 = smartDevice.getEnterpriseIds();
		if (enterpriseIds1 != null && "".equals(enterpriseIds1)){
			String[] enterpriseIds = enterpriseIds1.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < enterpriseIds.length; i++) {
				if(null != enterpriseIds[i]&&!enterpriseIds[i].trim().equals("")){
					Enterprise enterprise = enterpriseService.getById(enterpriseIds[i]);
					if(enterprise != null){
						if(i ==0){
							sb.append(enterprise.getEnterpriseName());
						}else {
							sb.append(","+enterprise.getEnterpriseName());
						}
					}
				}
			}
			smartDeviceVO.setEnterpriseNames(sb.toString());
		}

		return smartDeviceVO;
	}

}
