/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.raiseCapitalDesign.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.financial.erpcredit.util.SerializeUtil;
import com.jeeplus.modules.sg.managementcenter.activiti.utils.MyActiviUtils;
import com.jeeplus.modules.sg.picking.activiti.entity.*;
import com.jeeplus.modules.sg.picking.activiti.mapper.PickIngMapper;
import com.jeeplus.modules.sg.picking.activiti.mapper.ShowListMapper;
import com.jeeplus.modules.sg.raiseCapital.entity.RaiseCapital;
import com.jeeplus.modules.sg.raiseCapital.mapper.RaiseCapitalMapper;
import com.jeeplus.modules.sg.raiseCapital.utils.RaiseExportUtil;
import com.jeeplus.modules.sg.raiseCapital.utils.RaiseTemplate;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.*;
import com.jeeplus.modules.sg.raiseCapitalDesign.mapper.RaiseCapitalDesignMapper;
import com.jeeplus.modules.sg.raiseCapitalDesign.utils.RaiseCapitalDesignExportUtil;
import com.jeeplus.modules.sg.raiseCapitalDesign.utils.RaiseCapitalDesignTemplate;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.test.one.dialog.mapper.Leave1Mapper;
import io.swagger.models.auth.In;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.J2Cache;
import net.oschina.j2cache.redis.RedisCacheProvider;
import net.oschina.j2cache.redis.RedisClient;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.BinaryJedisCommands;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 提资模块Service
 */
@Service
@Transactional(readOnly = true)
public class RaiseCapitalDesignService extends CrudService<RaiseCapitalDesignMapper, RaiseCapital> {
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private PickIngMapper pickIngMapper;

	@Autowired
	private ShowListMapper showListMapper;

	public RaiseCapital get(String id) {
		return super.get(id);
	}

	public List<RaiseCapital> findList(RaiseCapital raiseCapital) {
		return super.findList(raiseCapital);
	}

	public Page<RaiseCapital> findPage(Page<RaiseCapital> page, RaiseCapital raiseCapital) {
		return super.findPage(page, raiseCapital);
	}

	@Transactional(readOnly = false)
	public void save(RaiseCapital raiseCapital) {
		super.save(raiseCapital);
	}

	@Transactional(readOnly = false)
	public void delete(RaiseCapital raiseCapital) {
		super.delete(raiseCapital);
	}

	/**
	 * 导出自动决算书
	 * @param request
	 * @param response
	 */
	@Transactional(readOnly = false)
	public void getExport(String projectName,String uuid,HttpServletRequest request, HttpServletResponse response){
		String id = UserUtils.getUser().getId();
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		HashMap<String, Object> newMap = (HashMap<String,Object>) SerializeUtil.deserialize(client.get().get(id.getBytes()));
		//模块表的数量
		List<ShowList> myList = (List<ShowList>)newMap.get("myList");
		//模块表
		List<PickList> newList = newAllList();
		ShowList showList = new ShowList();
		showList.setProjectName(projectName);
		//领料清单-从数据库
		List<ShowList> materialsList = showListMapper.findRaiseList(showList);
		//获取架空导线计算表中的数据
		VoltageData voltageData = new VoltageData();
		//查询10KV数据
		List<VoltageInfo> highVoltageList = this.getAerialConductor(uuid + "highVoltage","highVoltage");
		//查询0.4KV数据
		List<VoltageInfo> lowVoltageList = this.getAerialConductor(uuid + "lowVoltage","lowVoltage");
		voltageData.setHighVoltageList(highVoltageList);
		voltageData.setLowVoltageList(lowVoltageList);
		//获取拆旧材料清单计算表中的数据
		InventoryData inventoryData=new InventoryData();
		List<InventoryInfo> inventoryList = this.getInventory(uuid + "inventory");
		inventoryData.setInventoryList(inventoryList);


		//获取电缆分段表数据信息
		CableSectionData cableSectionData = new CableSectionData();
		BinaryJedisCommands binaryJedisCommands = client.get();
		//获取400的信息
		byte[] bytes = binaryJedisCommands.get((uuid + "cableFourHundred").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableFourHundredList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableFourHundredList){
					cableSectionData.setCableFourHundredList(cableFourHundredList);
				}else{
					cableSectionData.setCableFourHundredList(Lists.newArrayList());
				}
			}else{
				cableSectionData.setCableFourHundredList(Lists.newArrayList());
			}
		}else{
			cableSectionData.setCableFourHundredList(Lists.newArrayList());
		}
		//获取300的信息
		bytes = binaryJedisCommands.get((uuid + "cableThreeHundred").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableThreeHundredList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableThreeHundredList){
					cableSectionData.setCableThreeHundredList(cableThreeHundredList);
				}else{
					cableSectionData.setCableThreeHundredList(Lists.newArrayList());
				}
			}else{
				cableSectionData.setCableThreeHundredList(Lists.newArrayList());
			}
		}else{
			cableSectionData.setCableThreeHundredList(Lists.newArrayList());
		}
		//获取240的信息
		bytes = binaryJedisCommands.get((uuid + "cableTwoHundredForty").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableTwoHundredFortyList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableTwoHundredFortyList){
					cableSectionData.setCableTwoHundredFortyList(cableTwoHundredFortyList);
				}else{
					cableSectionData.setCableTwoHundredFortyList(Lists.newArrayList());
				}
			}else{
				cableSectionData.setCableTwoHundredFortyList(Lists.newArrayList());
			}
		}else{
			cableSectionData.setCableTwoHundredFortyList(Lists.newArrayList());
		}
		//获取120的信息
		bytes = binaryJedisCommands.get((uuid + "cableHundredAndTwenty").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableHundredAndTwentyList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableHundredAndTwentyList){
					cableSectionData.setCableHundredAndTwentyList(cableHundredAndTwentyList);
				}else{
					cableSectionData.setCableHundredAndTwentyList(Lists.newArrayList());
				}
			}else{
				cableSectionData.setCableHundredAndTwentyList(Lists.newArrayList());
			}
		}else{
			cableSectionData.setCableHundredAndTwentyList(Lists.newArrayList());
		}
		//获取70的信息
		bytes = binaryJedisCommands.get((uuid + "cableSeventy").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableSeventyList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableSeventyList){
					cableSectionData.setCableSeventyList(cableSeventyList);
				}else{
					cableSectionData.setCableSeventyList(Lists.newArrayList());
				}
			}else{
				cableSectionData.setCableSeventyList(Lists.newArrayList());
			}
		}else{
			cableSectionData.setCableSeventyList(Lists.newArrayList());
		}

		//获取总长信息
		bytes = binaryJedisCommands.get((uuid + "cableDisposeData").getBytes());
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item)) {
				List<CableSectionInfo> cableDisposeDataList = JSON.parseArray(new String(bytes),CableSectionInfo.class);
				if(null != cableDisposeDataList){
					for (CableSectionInfo info : cableDisposeDataList) {
						if(StringUtils.isNotBlank(info.getNumber()) && "0.0000".equals(info.getNumber())){
							info.setNumber("0");
						}
						switch (info.getKey()){
							case "cableFourHundred":
								cableSectionData.setCableFourHundredListTotal(info.getNumber());
								break;
							case "cableThreeHundred":
								cableSectionData.setCableThreeHundredListTotal(info.getNumber());
								break;
							case "cableTwoHundredForty":
								cableSectionData.setCableTwoHundredFortyListTotal(info.getNumber());
								break;
							case "cableHundredAndTwenty":
								cableSectionData.setCableHundredAndTwentyListTotal(info.getNumber());
								break;
							case "cableSeventy":
								cableSectionData.setCableSeventyListTotal(info.getNumber());
								break;
						}
					}
				}
			}
		}



		RaiseCapital newRaise = (RaiseCapital) newMap.get("raiseCapital");

		RaiseCapitalDesignTemplate raiseTemplate = new RaiseCapitalDesignTemplate();
		File newFile = raiseTemplate.createNewFile();
		InputStream inputStream = null;
		XSSFWorkbook workbook = null;
		XSSFFont font=null;
		//模块表
		XSSFSheet sheetModular = null;
		//领料清单
		XSSFSheet sheetPickingList = null;

		XSSFSheet sheetVoltageList = null;

		XSSFSheet sheetCableList = null;
		//拆旧材料清单
		XSSFSheet sheetInventoryList = null;
		try {
			inputStream = new FileInputStream(newFile);// 将excel文件转为输入流
			workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
			font = workbook.createFont();
			sheetModular =workbook.getSheetAt(0);//模块表
			/*sheetModuleList = workbook.getSheetAt(1);//模块清单*/
			sheetPickingList = workbook.getSheetAt(1);//领料清单

			sheetVoltageList = workbook.getSheetAt(2);//架空导线计算表

			sheetCableList = workbook.getSheetAt(3);//电缆分段表

			sheetInventoryList = workbook.getSheetAt(4);//拆旧材料清单计算表
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			// 写数据
			FileOutputStream fos = new FileOutputStream(newFile);
			RaiseCapitalDesignExportUtil.getSheetModular(sheetModular,myList,newList,newRaise,workbook);//模块表
			/*RaiseExportUtil.getSheetModuleList(sheetModuleList,moduleList,workbook);//模块清单*/
			RaiseCapitalDesignExportUtil.getSheetPickingList(sheetPickingList,materialsList,newRaise,workbook);//领料清单

			RaiseCapitalDesignExportUtil.getSheetVoltageList(sheetVoltageList,voltageData,newRaise,workbook);//架空导线计算表

			RaiseCapitalDesignExportUtil.getSheetCableList(sheetCableList,cableSectionData,newRaise,workbook);//电缆分段表

			RaiseCapitalDesignExportUtil.getSheetInventoryList(sheetInventoryList,inventoryData,newRaise,workbook);//拆旧材料清单计算表
            showListMapper.trunCate(projectName);
			workbook.write(fos);
			fos.flush();
			fos.close();
			InputStream fis = new BufferedInputStream(new FileInputStream(
					newFile));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.reset();
			response.setContentType("text/html;charset=UTF-8");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/x-msdownload");
			String newName = URLEncoder.encode(projectName + "物料清册" + System.currentTimeMillis() + ".xlsx", "UTF-8");
			response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");
			response.addHeader("Content-Length", "" + newFile.length());
			toClient.write(buffer);
			toClient.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 遍历查询结构 大类 中类 小类
	 * 去除重复，记录大类
	 * 根据大类查询它的中类放入list并记录
	 */
	public List<PickList> newAllList() {
		PickIng entity = new PickIng();
		entity.setVersion(MyActiviUtils.findDic().getValue());
		//查询除所有数据
		List<PickIng> allList = pickIngMapper.findAllList(entity);
		//创建返回对象集合
		List<PickList> returnList = new ArrayList<>();

		//循环得到所有大类
		for (PickIng pickIng : allList) {
			boolean flag = false;
			for (PickList list : returnList) {
				if (pickIng.getMdBigClass().equals(list.getName())) {
					flag = true;
					break;
				}
			}
			if (flag == false) {
				//创建返回对象
				PickList pickList = new PickList();
				pickList.setName(pickIng.getMdBigClass());
				returnList.add(pickList);
			}
		}

		//循环所有的大类，与查出的数据做比对，相同的就放入同一个实体类
		for (PickList pList : returnList) {
			List<MdTheClass> mdTheClassList = new ArrayList<>();
			//获得大类
			String name = pList.getName();
			//遍历查询的所有数据，与大类做比对，相同的，将中类记录下来
			for (PickIng pickIng : allList) {
				boolean flag = false;
				//如果查出的数据中和记录下的大类一致，将中类放入大类的
				if (pickIng.getMdBigClass().equals(name)) {
					for (MdTheClass md : mdTheClassList) {
						if (md.getName().equals(pickIng.getMdTheClass())) {
							flag = true;
						}
					}
					if (flag == false) {
						//创建中类对象，包含种类名称及对应的小类集合
						MdTheClass mdTheClass = new MdTheClass();
						mdTheClass.setId(pickIng.getId());
						mdTheClass.setName(pickIng.getMdTheClass());
						mdTheClassList.add(mdTheClass);
					}
				}
			}
			pList.setMdTheClassList(mdTheClassList);
		}

		for (PickList pList : returnList) {
			List<MdTheClass> mdTheClassList = pList.getMdTheClassList();
			for (MdTheClass md : mdTheClassList) {
				//创建
				List<MdSmallClass> mdSmallClassList = new ArrayList<>();
				for (PickIng pc : allList) {
					if (md.getName().equals(pc.getMdTheClass()) && pList.getName().equals(pc.getMdBigClass())) {
						//创建小类对象
						MdSmallClass mdSmallClass = new MdSmallClass();
						mdSmallClass.setName(pc.getMdSmallClass());
						mdSmallClass.setId(pc.getId());
						mdSmallClass.setMdUnit(pc.getMdUnit());
						if (Global.SYS_TYPE.equals("2")) {
							if (pc.getMdImg() != null && !"".equals(pc.getMdImg())) {
								mdSmallClass.setMdImg("http://" + request.getServerName() + pc.getMdImg());
							}
						} else {
							mdSmallClass.setMdImg(pc.getMdImg());
						}
						mdSmallClassList.add(mdSmallClass);
					}
					md.setMdSmallClassList(mdSmallClassList);
				}
			}
		}
		return returnList;
	}

	/**
	 * 遍历查询结构 大类 中类 小类
	 * 去除重复，记录大类
	 * 根据大类查询它的中类放入list并记录
	 */
	public List<DisposeData> getDisposeFormData(String uuid) {
		//创建通道，查询redis中数据
		List<DisposeData> voltageDisposeDataList = Lists.newArrayList();
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		try {
			BinaryJedisCommands binaryJedisCommands = client.get();
			byte[] voltageDisposeBytes = binaryJedisCommands.get((uuid+"voltageDisposeData").getBytes());
			String voltageDisposeItem = null;
			if(null != voltageDisposeBytes){
				voltageDisposeItem = new String(voltageDisposeBytes);
				if(StringUtils.isNotBlank(voltageDisposeItem))
					voltageDisposeDataList = JSON.parseArray(voltageDisposeItem,DisposeData.class);
			}
		} finally {
			client.release();
		}
		return voltageDisposeDataList;
	}
	/**
	 * 遍历查询结构 大类 中类 小类
	 * 去除重复，记录大类
	 * 根据大类查询它的中类放入list并记录
	 */
	public List<DisposeCableData> disposeFormCableDataList(String uuid) {
		//创建通道，查询redis中数据
		List<DisposeCableData> voltageDisposeDataList = Lists.newArrayList();
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		BinaryJedisCommands binaryJedisCommands = client.get();
		byte[] voltageDisposeBytes = binaryJedisCommands.get((uuid+"cableDisposeData").getBytes());
		String voltageDisposeItem = null;
		if(null != voltageDisposeBytes){
			voltageDisposeItem = new String(voltageDisposeBytes);
			if(StringUtils.isNotBlank(voltageDisposeItem))
				voltageDisposeDataList = JSON.parseArray(voltageDisposeItem,DisposeCableData.class);
		}
		return voltageDisposeDataList;
	}

	/**
	 * 将页面数据保存到redis中
	 * @param uuid
	 * @param item
	 */
	public void disposeFormData(String uuid,String item){
		//创建通道，查询redis中数据
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		BinaryJedisCommands binaryJedisCommands = client.get();
		binaryJedisCommands.set((uuid+"item").getBytes(),item.getBytes());
	}

	/**
	 * 根据UUID查询架空导线再redis中的数据
	 * @return
	 */
	public List<VoltageInfo> getAerialConductor(String uuidKey, String type) {
		List<VoltageInfo> list = Lists.newArrayList();
		//创建通道，查询redis中数据
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		BinaryJedisCommands binaryJedisCommands = client.get();
		byte[] bytes = binaryJedisCommands.get(uuidKey.getBytes());
		//判定是否存在这些数据的备份，有则进行处理并返回
		if(null != bytes){
			String item = new String(bytes);
			if(StringUtils.isNotBlank(item))
				list = JSON.parseArray(new String(bytes),VoltageInfo.class);
		}else{
			PickIng entity = new PickIng();
			entity.setVersion(MyActiviUtils.findDic().getValue());
			if("highVoltage".equals(type)){
				//查询4.1高压架空线路数据
				List<String> mdSmallNameList = Lists.newArrayList();
				mdSmallNameList.add("JKLYJ-240绝缘导线");
				mdSmallNameList.add("JKLYJ-150绝缘导线");
				mdSmallNameList.add("JKLYJ-50绝缘导线");
				entity.setMdSmallName(mdSmallNameList);
				entity.setType(type);
				list = pickIngMapper.getListByTheClassName(entity);

			}else{
				//查询4.1低压架空线路数据
				List<String> mdSmallNameList = Lists.newArrayList();
				mdSmallNameList.add("JKYJ-120");
				mdSmallNameList.add("JKLYJ-120");
				mdSmallNameList.add("JKLYJ-70");
				mdSmallNameList.add("JKYJ-70");
				mdSmallNameList.add("JKYJ-16");
				mdSmallNameList.add("JKYJ-35");
				mdSmallNameList.add("JKLGYJ-120/20");
				entity.setType(type);
				entity.setMdSmallName(mdSmallNameList);
				list = pickIngMapper.getListByTheClassName(entity);
			}
		}

		return list;
	}
	/**
	 * 根据UUID查询电缆分段再redis中的数据
	 * @return
	 */
	public List<CableSectionInfo> getCableSection(String uuidKey, String type) {
		List<CableSectionInfo> list = Lists.newArrayList();
		//创建通道，查询redis中数据
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		BinaryJedisCommands binaryJedisCommands = client.get();
		byte[] bytes = binaryJedisCommands.get(uuidKey.getBytes());
		//判定是否存在这些数据的备份，有则进行处理并返回
		if(null != bytes){
			list = JSON.parseArray(new String(bytes),CableSectionInfo.class);
		}
		return list;
	}
	/**
	 * 根据UUID查询再拆旧材料清册redis中的数据
	 * @return
	 */
	public List<InventoryInfo> getInventory(String uuidKey) {
		List<InventoryInfo> list = Lists.newArrayList();
		//创建通道，查询redis中数据
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		BinaryJedisCommands binaryJedisCommands = client.get();
		byte[] bytes = binaryJedisCommands.get(uuidKey.getBytes());
		//判定是否存在这些数据的备份，有则进行处理并返回
		if(null != bytes){
			list = JSON.parseArray(new String(bytes),InventoryInfo.class);
		}else{
			list=inventoryListData();
		}
		return list;
	}
	/**
	 * 根据UUID查询电缆分段再redis中的数据
	 * @return
	 */
	public List<CableSectionData> getCableSectionData() {
		List<CableSectionData> list = Lists.newArrayList();
		PickIng entity = new PickIng();
		entity.setVersion(MyActiviUtils.findDic().getValue());
		//查询4.2高压电缆线路数据
		List<String> mdSmallNameList = Lists.newArrayList();
		mdSmallNameList.add("3×400电缆");
		mdSmallNameList.add("3×120电缆");
		mdSmallNameList.add("3×300电缆");
		mdSmallNameList.add("3×240电缆");
		mdSmallNameList.add("3×70电缆");
		entity.setMdSmallName(mdSmallNameList);
		list = pickIngMapper.getListCableByTheClassName(entity);
		return list;
	}

	/**
	 * 架空导线计算处理
	 * @param voltageData
	 */
	@Transactional(readOnly = false)
	public void saveVlatage(VoltageData voltageData) {
		List<DisposeData> dataList = Lists.newArrayList();
		List<VoltageInfo> highVoltageList = voltageData.getHighVoltageList();
		List<VoltageInfo> lowVoltageList = voltageData.getLowVoltageList();
		DisposeData data = null;
		for (VoltageInfo highVoltage: highVoltageList) {
			data = new DisposeData();
			data.setId(highVoltage.getId());
			data.setNumber(highVoltage.getSingleLong());
			dataList.add(data);
		}
		for (VoltageInfo lowVoltage: lowVoltageList) {
			data = new DisposeData();
			data.setId(lowVoltage.getId());
			data.setNumber(lowVoltage.getSingleLong());
			dataList.add(data);
		}
		String voltageDisposeDataJson = JSON.toJSONString(dataList);
		String highVoltageJson = JSON.toJSONString(highVoltageList);
		String lowVoltageJson = JSON.toJSONString(lowVoltageList);

		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		try {
			BinaryJedisCommands binaryJedisCommands = client.get();
			binaryJedisCommands.set((voltageData.getUuid()+"voltageDisposeData").getBytes(),voltageDisposeDataJson.getBytes());
			binaryJedisCommands.set((voltageData.getUuid()+"highVoltage").getBytes(),highVoltageJson.getBytes());
			binaryJedisCommands.set((voltageData.getUuid()+"lowVoltage").getBytes(),lowVoltageJson.getBytes());
			binaryJedisCommands.expire((voltageData.getUuid()+"voltageDisposeData").getBytes(),86400);
			binaryJedisCommands.expire((voltageData.getUuid()+"highVoltage").getBytes(),86400);
			binaryJedisCommands.expire((voltageData.getUuid()+"lowVoltage").getBytes(),86400);
		} finally {
			client.release();
		}
	}
	/**
	 * 电缆分段计算处理
	 * @param cableSectionData
	 */
	@Transactional(readOnly = false)
	public void saveCable(CableSectionData cableSectionData) {
		List<DisposeCableData> dataList = this.CableDataList(cableSectionData);
		List<CableSectionInfo> cableFourHundredList=cableSectionData.getCableFourHundredList();
		List<CableSectionInfo> cableHundredAndTwentyList=cableSectionData.getCableHundredAndTwentyList();
		List<CableSectionInfo> cableThreeHundredList=cableSectionData.getCableThreeHundredList();
		List<CableSectionInfo> cableTwoHundredFortyList=cableSectionData.getCableTwoHundredFortyList();
		List<CableSectionInfo> cableSeventyList=cableSectionData.getCableSeventyList();
		String cableDisposeDataJson = JSON.toJSONString(dataList);
		String cableFourHundredListJson = JSON.toJSONString(cableFourHundredList);
		String cableHundredAndTwentyListJson = JSON.toJSONString(cableHundredAndTwentyList);
		String cableThreeHundredListJson = JSON.toJSONString(cableThreeHundredList);
		String cableTwoHundredFortyListJson = JSON.toJSONString(cableTwoHundredFortyList);
		String cableSeventyListJson = JSON.toJSONString(cableSeventyList);
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		try {
			BinaryJedisCommands binaryJedisCommands = client.get();
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableDisposeData").getBytes(),cableDisposeDataJson.getBytes());
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableFourHundred").getBytes(),cableFourHundredListJson.getBytes());
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableHundredAndTwenty").getBytes(),cableHundredAndTwentyListJson.getBytes());
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableThreeHundred").getBytes(),cableThreeHundredListJson.getBytes());
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableTwoHundredForty").getBytes(),cableTwoHundredFortyListJson.getBytes());
			binaryJedisCommands.set((cableSectionData.getUuid()+"cableSeventy").getBytes(),cableSeventyListJson.getBytes());
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableDisposeData").getBytes(),86400);
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableFourHundred").getBytes(),86400);
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableHundredAndTwenty").getBytes(),86400);
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableThreeHundred").getBytes(),86400);
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableTwoHundredForty").getBytes(),86400);
			binaryJedisCommands.expire((cableSectionData.getUuid()+"cableSeventy").getBytes(),86400);
		}finally {
			client.release();
		}


	}
	/**
	 * 拆旧材料清单计算处理
	 * @param inventoryData
	 */
	@Transactional(readOnly = false)
	public void saveInventory(InventoryData inventoryData) {
		List<InventoryInfo> inventoryListData = inventoryData.getInventoryList();
		String inventoryJson = JSON.toJSONString(inventoryListData);
		CacheChannel channel = J2Cache.getChannel();
		RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
		try {
			BinaryJedisCommands binaryJedisCommands = client.get();
			binaryJedisCommands.set((inventoryData.getUuid()+"inventory").getBytes(),inventoryJson.getBytes());
			binaryJedisCommands.expire((inventoryData.getUuid()+"inventory").getBytes(),86400);
		} finally {
			client.release();
		}
	}
	/**
	 * 页面需要的数据
	 * @param cableSectionData
	 * @return
	 */
	public List<DisposeCableData> CableDataList(CableSectionData cableSectionData){
		List<DisposeCableData> dataList = Lists.newArrayList();

		DisposeCableData data = new DisposeCableData();
		data.setId(cableSectionData.getCableFourHundredId());
		data.setKey("cableFourHundred");
		data.setNumber(cableSectionData.getCableFourHundredListTotal());
		dataList.add(data);

		DisposeCableData dataCableHundredAndTwenty = new DisposeCableData();
		dataCableHundredAndTwenty.setId(cableSectionData.getCableHundredAndTwentyId());
		dataCableHundredAndTwenty.setKey("cableHundredAndTwenty");
		dataCableHundredAndTwenty.setNumber(cableSectionData.getCableHundredAndTwentyListTotal());
		dataList.add(dataCableHundredAndTwenty);

		DisposeCableData dataCableThreeHundred = new DisposeCableData();
		dataCableThreeHundred.setId(cableSectionData.getCableThreeHundredId());
		dataCableThreeHundred.setKey("cableThreeHundred");
		dataCableThreeHundred.setNumber(cableSectionData.getCableThreeHundredListTotal());
		dataList.add(dataCableThreeHundred);

		DisposeCableData dataCableTwoHundredForty = new DisposeCableData();
		dataCableTwoHundredForty.setId(cableSectionData.getCableTwoHundredFortyId());
		dataCableTwoHundredForty.setKey("cableTwoHundredForty");
		dataCableTwoHundredForty.setNumber(cableSectionData.getCableTwoHundredFortyListTotal());
		dataList.add(dataCableTwoHundredForty);

		DisposeCableData dataCableSeventy = new DisposeCableData();
		dataCableSeventy.setId(cableSectionData.getCableSeventyId());
		dataCableSeventy.setKey("cableSeventy");
		dataCableSeventy.setNumber(cableSectionData.getCableSeventyListTotal());
		dataList.add(dataCableSeventy);
		return dataList;
	}

	public List<InventoryInfo> inventoryListData(){
		List<InventoryInfo> list = Lists.newArrayList();
		InventoryInfo inventoryYi=new InventoryInfo();
		inventoryYi.setNum("1");
		inventoryYi.setName("10kV电力电缆");
		inventoryYi.setSpecifications("YJV22-8.7/15-3×400");
		inventoryYi.setCompany("米");
		list.add(inventoryYi);

		InventoryInfo inventoryEr=new InventoryInfo();
		inventoryEr.setNum("1");
		inventoryEr.setName("10kV电力电缆");
		inventoryEr.setSpecifications("YJV22-8.7/15-3×300");
		inventoryEr.setCompany("米");
		list.add(inventoryEr);

		InventoryInfo inventorySan=new InventoryInfo();
		inventorySan.setNum("1");
		inventorySan.setName("10kV电力电缆");
		inventorySan.setSpecifications("YJV22-8.7/15-3×95");
		inventorySan.setCompany("米");
		list.add(inventorySan);

		InventoryInfo inventorySi=new InventoryInfo();
		inventorySi.setNum("1");
		inventorySi.setName("10kV电力电缆");
		inventorySi.setSpecifications("YJV22-8.7/15-3×35");
		inventorySi.setCompany("米");
		list.add(inventorySi);

		InventoryInfo inventoryWu=new InventoryInfo();
		inventoryWu.setNum("2");
		inventoryWu.setName("10kV架空导线");
		inventoryWu.setSpecifications("JKLYJ-240");
		inventoryWu.setCompany("米");
		inventoryWu.setRemarks("折单长度(单回路径)");
		list.add(inventoryWu);

		InventoryInfo inventoryLiu=new InventoryInfo();
		inventoryLiu.setNum("2");
		inventoryLiu.setName("10kV架空导线");
		inventoryLiu.setSpecifications("JKLYJ-185");
		inventoryLiu.setCompany("米");
		list.add(inventoryLiu);

		InventoryInfo inventoryQi=new InventoryInfo();
		inventoryQi.setNum("2");
		inventoryQi.setName("10kV架空导线");
		inventoryQi.setSpecifications("JKLYJ-150");
		inventoryQi.setCompany("米");
		list.add(inventoryQi);

		InventoryInfo inventoryBa=new InventoryInfo();
		inventoryBa.setNum("2");
		inventoryBa.setName("10kV架空导线");
		inventoryBa.setSpecifications("LGJ-240");
		inventoryBa.setCompany("米");
		list.add(inventoryBa);

		InventoryInfo inventoryJiu=new InventoryInfo();
		inventoryJiu.setNum("2");
		inventoryJiu.setName("10kV架空导线");
		inventoryJiu.setSpecifications("LGJ-185");
		inventoryJiu.setCompany("米");
		list.add(inventoryJiu);

		InventoryInfo inventoryShi=new InventoryInfo();
		inventoryShi.setNum("2");
		inventoryShi.setName("10kV架空导线");
		inventoryShi.setSpecifications("LGJ-150");
		inventoryShi.setCompany("米");
		list.add(inventoryShi);

		InventoryInfo inventoryShiYi=new InventoryInfo();
		inventoryShiYi.setNum("3");
		inventoryShiYi.setName("10kV电杆");
		inventoryShiYi.setSpecifications("钢管杆");
		inventoryShiYi.setCompany("基");
		list.add(inventoryShiYi);

		InventoryInfo inventoryShiEr=new InventoryInfo();
		inventoryShiEr.setNum("3");
		inventoryShiEr.setName("10kV电杆");
		inventoryShiEr.setSpecifications("宅基塔");
		inventoryShiEr.setCompany("基");
		list.add(inventoryShiEr);

		InventoryInfo inventoryShiSan=new InventoryInfo();
		inventoryShiSan.setNum("3");
		inventoryShiSan.setName("10kV电杆");
		inventoryShiSan.setSpecifications("Φ190×15");
		inventoryShiSan.setCompany("基");
		list.add(inventoryShiSan);

		InventoryInfo inventoryShiSi=new InventoryInfo();
		inventoryShiSi.setNum("3");
		inventoryShiSi.setName("10kV电杆");
		inventoryShiSi.setSpecifications("Φ190×12");
		inventoryShiSi.setCompany("基");
		list.add(inventoryShiSi);

		InventoryInfo inventoryShiWu=new InventoryInfo();
		inventoryShiWu.setNum("4");
		inventoryShiWu.setName("10kV电缆分支箱");
		inventoryShiWu.setSpecifications("1进1出");
		inventoryShiWu.setCompany("台");
		list.add(inventoryShiWu);

		InventoryInfo inventoryShiLiu=new InventoryInfo();
		inventoryShiLiu.setNum("4");
		inventoryShiLiu.setName("10kV电缆分支箱");
		inventoryShiLiu.setSpecifications("1进2出");
		inventoryShiLiu.setCompany("台");
		list.add(inventoryShiLiu);

		InventoryInfo inventoryShiQi=new InventoryInfo();
		inventoryShiQi.setNum("4");
		inventoryShiQi.setName("10kV电缆分支箱");
		inventoryShiQi.setSpecifications("1进3出");
		inventoryShiQi.setCompany("台");
		list.add(inventoryShiQi);

		InventoryInfo inventoryShiBa=new InventoryInfo();
		inventoryShiBa.setNum("5");
		inventoryShiBa.setName("10kV电缆环网柜");
		inventoryShiBa.setSpecifications("2进2出");
		inventoryShiBa.setCompany("台");
		list.add(inventoryShiBa);

		InventoryInfo inventoryShiJiu=new InventoryInfo();
		inventoryShiJiu.setNum("5");
		inventoryShiJiu.setName("10kV电缆环网柜");
		inventoryShiJiu.setSpecifications("2进3出");
		inventoryShiJiu.setCompany("台");
		list.add(inventoryShiJiu);

		InventoryInfo inventoryErYi=new InventoryInfo();
		inventoryErYi.setNum("6");
		inventoryErYi.setName("10kV变压器");
		inventoryErYi.setSpecifications("50kVA");
		inventoryErYi.setCompany("台");
		list.add(inventoryErYi);

		InventoryInfo inventoryErEr=new InventoryInfo();
		inventoryErEr.setNum("6");
		inventoryErEr.setName("10kV变压器");
		inventoryErEr.setSpecifications("315kVA");
		inventoryErEr.setCompany("台");
		list.add(inventoryErEr);

		InventoryInfo inventoryErSan=new InventoryInfo();
		inventoryErSan.setNum("7");
		inventoryErSan.setName("10kV柱上设备");
		inventoryErSan.setSpecifications("断路器开关");
		inventoryErSan.setCompany("台");
		list.add(inventoryErSan);

		InventoryInfo inventoryErSi=new InventoryInfo();
		inventoryErSi.setNum("7");
		inventoryErSi.setName("10kV柱上设备");
		inventoryErSi.setSpecifications("SF6开关");
		inventoryErSi.setCompany("台");
		list.add(inventoryErSi);

		InventoryInfo inventoryErWu=new InventoryInfo();
		inventoryErWu.setNum("7");
		inventoryErWu.setName("10kV柱上设备");
		inventoryErWu.setSpecifications("熔断器");
		inventoryErWu.setCompany("只");
		list.add(inventoryErWu);

		InventoryInfo inventoryErLiu=new InventoryInfo();
		inventoryErLiu.setNum("8");
		inventoryErLiu.setName("0.4kV电气设备");
		inventoryErLiu.setSpecifications("低压综合配电箱");
		inventoryErLiu.setCompany("套");
		list.add(inventoryErLiu);

		InventoryInfo inventoryErQi=new InventoryInfo();
		inventoryErQi.setNum("8");
		inventoryErQi.setName("0.4kV电气设备");
		inventoryErQi.setSpecifications("低压综合配电箱");
		inventoryErQi.setCompany("面");
		list.add(inventoryErQi);

		InventoryInfo inventoryErBa=new InventoryInfo();
		inventoryErBa.setNum("9");
		inventoryErBa.setName("0.4kV电缆");
		inventoryErBa.setSpecifications("");
		inventoryErBa.setCompany("米");
		list.add(inventoryErBa);

		InventoryInfo inventoryErJiu=new InventoryInfo();
		inventoryErJiu.setNum("10");
		inventoryErJiu.setName("铁附件");
		inventoryErJiu.setSpecifications("变压器全套框架等");
		inventoryErJiu.setCompany("吨");
		list.add(inventoryErJiu);

		InventoryInfo inventorySanYi=new InventoryInfo();
		inventorySanYi.setNum("11");
		inventorySanYi.setName("10kV箱变");
		inventorySanYi.setSpecifications("");
		inventorySanYi.setCompany("台");
		list.add(inventorySanYi);

		InventoryInfo inventorySanEr=new InventoryInfo();
		inventorySanEr.setNum("12");
		inventorySanEr.setName("10kV箱变");
		inventorySanEr.setSpecifications("");
		inventorySanEr.setCompany("台");
		list.add(inventorySanEr);
		return list;
	}
}
