package com.szpg.plc.message.response.read;

import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.PgZmDao;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.dao.impl.PgZmDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.db.data.PgZmStat;
import com.szpg.plc.message.response.ReadMemoryCommandResponse;
import com.szpg.plc.util.ByteUtil;
import com.szpg.util.Configure;
import com.szpg.util.TimeFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class ReadZmStatCommandResponse extends ReadMemoryCommandResponse {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3887112876994787055L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Boolean> zmRun;
	private String[] zcList; //资产列表，从配置文件中获取

	public ReadZmStatCommandResponse() {
		zmRun = new ArrayList<Boolean>();
	}

	public List<Boolean> getZmRun() {
		return zmRun;
	}

	public void setZmRun(List<Boolean> zmRun) {
		this.zmRun = zmRun;
	}

	public String[] getZcList() {
		return zcList;
	}

	public void setZcList(String[] zcList) {
		this.zcList = zcList;
	}

	@Override
	public void afterAction() {
		// 1将照明运行状态数据存入数据库
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		PgZmDao zmDao = new PgZmDaoImpl();
		
		if (null != zcList && zcList.length > 0) {
			// 遍历设备列表，将照明运行状态存入数据库
			for (int i = 0; i < zcList.length; i++) {
				String zcbh = zcList[i];
				
				int id = deviceDao.findDeviceIdByCode(zcbh);
				if (id > 0) {
					logger.info("照明[" + zcbh + "][运行状态" + zmRun.get(i) + "]");
					deviceDao.updateDeviceStatus(id, zmRun.get(i) == true ? "照明打开" : "照明关闭");

					// 更新照明状态现状表
					PgZmStat temp = zmDao.findLatestStatByDevice(id);
					if (null == temp) {
						zmDao.addStatRecord(zmRun.get(i), TimeFormat.formatTimestamp(Calendar.getInstance().getTime()), id);
					} else {
						zmDao.updateStatRecord(zmRun.get(i), TimeFormat.formatTimestamp(Calendar.getInstance().getTime()), id);
					}
				}
			}
		}
	}

	@Override
	public void parseData() {
		// 获取目标ACU的代码
		String acucode = this.getAcucode();
		byte[] messageData = this.getResponseData();
		
		// 判断数据的长度是否满足要求
		if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".ZMSTAT.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
		
		// 将数据转换为字符串
		// 按照高字在前，低字在后；高字节在前，低字节在后的顺序
		String dataStr = "";
		for (int c = 0; c < messageData.length; c = c + 2) {
			dataStr = ByteUtil.binToBinString(new byte[] {messageData[c], messageData[c + 1]}) + dataStr;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU照明运行状态内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".ZMSTAT.START"));
		
		// 获取目标ACU照明相关的资产列表，即风机列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".ZM.ZC.LIST");
		if (StringUtils.isEmpty(zcListStr) == true) {
			this.setValid(false);
			return;
		}
		zcList = zcListStr.split(";");

		// 初始化结果集合
		zmRun = Arrays.asList(new Boolean[zcList.length]);
		
		// 解析照明运行状态
		for (int i = 0; i < zcList.length; i++) {
			String zcbh = zcList[i];
			
			// 解析照明运行状态
			PgHjsbbl runBlObj = blDao.findBlByBh(zcbh + ".Run");
			if (null != runBlObj) {
				int rkszdz = runBlObj.getKszdz(); //开始字地址
				int roffset = rkszdz - start; //与开始字的偏移量
				int rbit = runBlObj.getSzw(); //所在位

				int index = dataStr.length() - 1 - roffset * 16 - rbit;
				if (index >= 0) {
					char runBt = dataStr.charAt(index); // 照明运行状态标志位
					getZmRun().set(i, runBt == '1' ? true : false);
				}
			}
		}
	}

}
