package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.DeviceInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import com.jpsoft.shinestar.modules.base.service.DeviceInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.sys.dao.DataDictionaryDAO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@Component(value="deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService {
	@Resource(name="deviceInfoDAO")
	private DeviceInfoDAO deviceInfoDAO;

	@Autowired
	private DataDictionaryDAO dataDictionaryDAO;

	@Override
	public DeviceInfo get(String id) {
		// TODO Auto-generated method stub
		return deviceInfoDAO.get(id);
	}

	@Override
	public int insert(DeviceInfo model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());

		return deviceInfoDAO.insert(model);
	}

	@Override
	public int update(DeviceInfo model) {
		// TODO Auto-generated method stub
		return deviceInfoDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return deviceInfoDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = deviceInfoDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public List<DeviceInfo> list() {
		// TODO Auto-generated method stub
		return deviceInfoDAO.list();
	}

	@Override
	public List<DeviceInfo> findByDeviceIdList(List<String> deviceIdList){
		return deviceInfoDAO.findByDeviceIdList(deviceIdList);
	}


	@Override
	public List<DeviceInfo> findByScene(String scene){
		return deviceInfoDAO.findByScene(scene);
	}

	@Override
	public DeviceInfo getByDeviceNo(String deviceNo){
		return deviceInfoDAO.getByDeviceNo(deviceNo);
	}

	@Override
	public List<DeviceInfo> findByCompanyId(String companyId){
		//去掉了删除判断
		//需要查出已删除的设备
		return deviceInfoDAO.findByCompanyId(companyId);
	}

	@Override
	public List<DeviceInfo> findByCompanyIdRelation(String companyId){
		//去掉了删除判断
		//需要查出已删除的设备
		return deviceInfoDAO.findByCompanyIdRelation(companyId);
	}
	@Override
	public Page<DeviceInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<DeviceInfo> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            deviceInfoDAO.search(searchParams,sortList);
        });

        return page;
	}

	@Override
	public void updateByDeviceNo(String deviceNo, boolean online) {
		DeviceInfo deviceInfo= deviceInfoDAO.findByDeviceNo(deviceNo);

		if (deviceInfo != null){
			deviceInfo.setIsOnline(online);
			deviceInfo.setAbnormalReason(online ? "设备上线" : "设备离线");

			deviceInfo.setHeartbeatUpdateTime(new Date());
			deviceInfoDAO.update(deviceInfo);
		}
	}

	@Override
	public List<DeviceInfo> listOnline() {
		return deviceInfoDAO.listOnline();
	}

	@Override
	public List<String> findSceneListByDeviceNo(String deviceNo) {
		List<String> sceneNameList = new ArrayList<>();

		DeviceInfo deviceInfo = getByDeviceNo(deviceNo);

		if (deviceInfo!=null){
			String scenes = deviceInfo.getScene();

			if (StringUtils.isNotEmpty(scenes)){
				String[] sceneArr = scenes.split(",");

				for (String sceneValue : sceneArr) {
					String sceneName = dataDictionaryDAO.findNameByCatalogNameAndValue("设备用途",sceneValue);

					if (StringUtils.isNotEmpty(sceneName)){
						sceneNameList.add(sceneName);
					}
				}
			}
		}

		return sceneNameList;
	}
}