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

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.szpg.db.dao.PgJgDao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgJgDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.plc.message.response.ReadMemoryCommandResponse;
import com.szpg.plc.util.ByteUtil;
import com.szpg.util.Configure;
import com.szpg.util.TimeFormat;

public class ReadJgStatusCommandResponse extends ReadMemoryCommandResponse {

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

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Boolean> jgdk; // 井盖打开/关闭
	private List<Boolean> jgsd; // 井盖锁定/解锁
	private String[] zcList; //井盖的资产列表，从配置文件中获取
	
	public ReadJgStatusCommandResponse() {
		jgdk = new ArrayList<Boolean>();
		jgsd = new ArrayList<Boolean>();
	}
	
	public List<Boolean> getJgdk() {
		return jgdk;
	}

	public void setJgdk(List<Boolean> jgdk) {
		this.jgdk = jgdk;
	}

	public List<Boolean> getJgsd() {
		return jgsd;
	}

	public void setJgsd(List<Boolean> jgsd) {
		this.jgsd = jgsd;
	}

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

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

	@Override
	public void afterAction() {
		// 1将井盖状态数据存入数据库
		PgJgDao jgDao = new PgJgDaoImpl();
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		
		// 遍历资产列表，将井盖状态存入数据库
		for (int i = 0; i < zcList.length; i++) {
			String zcbh = zcList[i];
			
			int id = deviceDao.findDeviceIdByCode(zcbh);
			if (id > 0) {
				jgDao.addSbjzRecord(jgdk.get(i), jgsd.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id);
			}
		}
	}

	@Override
	public void parseData(byte[] messageData) {
		// 获取目标ACU的代码
		String acucode = this.getAcucode();
		
		// 判断数据的长度是否满足要求
		if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".JGSTAT.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 + ".JGSTAT.START"));
		
		// 获取目标ACU井盖相关的资产列表，即井盖设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".JG.ZC.LIST");
		if (StringUtils.isEmpty(zcListStr) == true) {
			this.setValid(false);
			return;
		}
		zcList = zcListStr.split(";");
		
		// 解析井盖状态
		for (int i = 0; i < zcList.length; i++) {
			String zcbh = zcList[i];
			
			// 解析井盖打开/关闭状态
			PgHjsbbl openBlObj = blDao.findBlByBh(zcbh + ".Open");
			if (null == openBlObj) {
				continue;
			}
			
			int okszdz = openBlObj.getKszdz(); //开始字地址
			int ooffset = okszdz - start; //与开始字的偏移量
			int obit = openBlObj.getSzw(); //所在位
			
			char openBt = dataStr.charAt(dataStr.length() - 1 - ooffset * 16 - obit); // 井盖打开/关闭标志位
			
			getJgdk().add(openBt == '0' ? true : false); //0-未关闭;1-关闭
			
			// 解析井盖锁定/解锁状态
			PgHjsbbl lockBlObj = blDao.findBlByBh(zcbh + ".Lock");
			if (null == lockBlObj) {
				continue;
			}
			
			int lkszdz = lockBlObj.getKszdz(); //开始字地址
			int loffset = lkszdz - start; //与开始字的偏移量
			int lbit = lockBlObj.getSzw(); //所在位
			
			char lockBt = dataStr.charAt(dataStr.length() - 1 - loffset * 16 - lbit); // 井盖解锁/锁定标志位
			
			getJgsd().add(lockBt == '0' ? true : false); //0-锁定;1-解锁
		}
	}

}
