package com.unis.detectionnum.server.domain;

import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.*;
import com.unis.detectionnum.server.dao.DKdxxsjyDao;
import com.unis.detectionnum.server.dao.DeviceAlarmDao;
import com.unis.detectionnum.server.entity.DKdxxsjyDO;
import com.unis.detectionnum.server.entity.DeviceAlarmDO;
import com.unis.detectionnum.server.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DeviceAlarmDomain extends BaseDomain{

	@Autowired
	private DKdxxsjyDao kdxxsjyDao;
	
	@Autowired
    private DeviceAlarmDao deviceAlarmDao;

	/** 
	 * @Title: storehouseStatistic 
	 * @Description: TODO
	 * @param zskdm
	 * @return Map<String,Object>
	 * @throws 
	 */
	public Map<String,Object> storehouseStatistic(String userCode) {
		
		Map<String,Object> resultMap = new HashMap<String, Object>();
		List<DeviceAlarmStatisticVO> resultList = new ArrayList<>();
		List<DKdxxsjyDO> kdList = new ArrayList<DKdxxsjyDO>();
		if(userCode.equals("U0000777")){
            userCode = "";
			 kdList = kdxxsjyDao.getDKdxxsjyInfoBytyshxydms(userCode);
		}else{
             kdList =kdxxsjyDao.getDKdxxsjyInfoBytyshxydm(userCode);
        }
		Integer disconnectsTotal = 0;
		Integer timesTotal = 0;
		Integer totals = 0;
		Integer monthsTotal = 0;
		for(DKdxxsjyDO kddo : kdList) {
			DeviceAlarmStatisticVO statisticVO = deviceAlarmDao.storehouseStatistic(kddo.getKddm());
			
			disconnectsTotal += statisticVO.getDisconnects();
			timesTotal += statisticVO.getTimes();
			totals += statisticVO.getTotals();
			monthsTotal += statisticVO.getMonths();
			resultList.add(statisticVO);
		}
		//总数统计
		Map<String,Object> totalMap = new HashMap<String, Object>();
		totalMap.put("name", "总数");
		totalMap.put("disconnectsTotal",disconnectsTotal);
		totalMap.put("timesTotal",timesTotal);
		totalMap.put("totals",totals);
		totalMap.put("monthsTotal",monthsTotal);
		
		resultMap.put("list", resultList);
		resultMap.put("total", totalMap);
		return resultMap;
	}

	/** 
	 * @Title: alarmRecordList 
	 * @Description: TODO
	 * @param deviceAlarmQueryVO
	 * @return List<DeviceAlarmRecodVO>
	 * @throws 
	 */
	public List<DeviceAlarmRecodVO> alarmRecordList(DeviceAlarmQueryVO deviceAlarmQueryVO) {
		// TODO Auto-generated method stub
		return deviceAlarmDao.selectByParam(deviceAlarmQueryVO);
	}

	/** 
	 * @Title: saveDeviceAlarm 
	 * @Description: 保存告警记录
	 * @param deviceAlarmVO
	 * @return void
	 * @throws 
	 */
	public String saveDeviceAlarm(DeviceAlarmVO deviceAlarmVO) {
		DeviceAlarmDO deviceAlarmDO = new DeviceAlarmDO();
		LsBeanUtils.copyAllProperties(deviceAlarmVO, deviceAlarmDO);
		deviceAlarmDao.insertSelective(deviceAlarmDO);
		return deviceAlarmDO.getId();
	}


    public String getAlarmStatus(String sbCode) {
	    return deviceAlarmDao.getAlarmStatus(sbCode);
    }

    public CallAlarmQueryVO getCallAlarm(String cfBH) {
        return  deviceAlarmDao.getCallAlarm(cfBH);
    }
}
