package com.steering.ydm.bc.report;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.mes.mcp.entity.QcmZgJugdeApplyEntity;
import com.steering.mes.mcp.entity.YdmBcInitialEntity;
import com.steering.mes.mcp.entity.YdmBcInlistEntity;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;

public class FrmBcRunTimeReport extends DBCommon{

	/***
	 * 查询
	 * @param bathYear
	 * @param arr
	 * @return
	 */
	public CoreReturnObject queryDataRun(String bathYear,String[] arr,String custom)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.queryData");
		//String str = CoreConverter.toSqlConditionString("BELONG_CODE", arr);
		String str = "";
//		if(custom.equals("120501"))
//		{
//			str = " AND BELONG_CODE = '002001001'";
//		}else if(custom.equals("120504"))
//		{
//			str = " AND BELONG_CODE = '002023005'";
//		}
		str = " AND BELONG_CODE in ('002001001','002023005') ";
		sql = sql.replace("#CONDTIONS#", str);
		return this.getData(sql, new Object[]{bathYear,bathYear,bathYear,bathYear,bathYear,bathYear});
	}
	
	/***
	 * 查询是否存在下月期初
	 * @return
	 * @throws ParseException 
	 */
	public CoreReturnObject doQueryInialData(String bathYear,String custom) throws ParseException
	{
		SimpleDateFormat simple = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat nextSimple = new SimpleDateFormat("yyyy-MM");
		String sql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.doQueryInialData");
		if(custom.equals("120501"))
		{
			sql += " AND T.BELONG_CODE = '002001001'";
		}else if(custom.equals("120504"))
		{
			sql += " AND T.BELONG_CODE = '002023005'";
		}
		String nextMoth = getBalYearMoth(nextSimple.format(simple.parse(bathYear)));
		return this.getData(sql, new Object[]{nextMoth});
	}
	
	/***
	 * 结转
	 * @return
	 */
	public CoreReturnObject insertInital(ArrayList<ArrayList<String>> parm,String bathYear,String userName,String custom)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		ResultSet rs = null;
		
		SimpleDateFormat simple = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat dtSt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat nextSimple = new SimpleDateFormat("yyyy-MM");
		int m = 0;
		String judgeNo = "";
		String actCount ="";
		String belongCode = "";
		String belongName = "";
		try {
			//connection.setAutoCommit(false);
			ArrayList<String> sqllist = new ArrayList<String>();
			String mothYear = getBalYearMoth(nextSimple.format(simple.parse(bathYear)));
			//String mothYear = getBalYearMoth(nextSimple.format(bathYear));
			if(custom.equals("120501"))
			{
				belongCode = "002001001";
				belongName = "生产计划部";
			}else if(custom.equals("120504"))
			{
				belongCode = "002023005";
				belongName = "产品制造部";
			}
			for(int i = 0;i<parm.size();i++)
			{
				ArrayList<String> list = parm.get(i);
				YdmBcInitialEntity ydmBcInitialTiy = new YdmBcInitialEntity();

				ydmBcInitialTiy.setJudgeStoveNo(list.get(0));
				ydmBcInitialTiy.setBatchNo(list.get(1));
				ydmBcInitialTiy.setPlineCode(list.get(5));
				ydmBcInitialTiy.setMaterialName(list.get(6));
				ydmBcInitialTiy.setBelongCode(belongCode);
				ydmBcInitialTiy.setBelongName(belongName);
				ydmBcInitialTiy.setProcessCdoe(list.get(4));
				sql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.selectMatInformation");
				sql = EntityTool.fillSqlByEntity(sql, ydmBcInitialTiy);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				//List<MatBcMEntity> matEntitys = EntityTool.getEntitysByRs(rs, MatBcMEntity.class);
				MatBcMEntity matEntity = EntityTool.getEntityByRs(rs,MatBcMEntity.class);
				rs.close();
				psta.close();
				
				//if(matEntitys.size() == 0)
				if(matEntity == null)
				{
					sql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.selectOutList");
					sql = EntityTool.fillSqlByEntity(sql, ydmBcInitialTiy);
					psta = connection.prepareStatement(sql);
					rs = psta.executeQuery();
					YdmBcOutlistEntity ydmOut = EntityTool.getEntityByRs(rs, YdmBcOutlistEntity.class);
					rs.close();
					psta.close();
					if(ydmOut == null)
					{
						sql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.selectInList");
						sql = EntityTool.fillSqlByEntity(sql, ydmBcInitialTiy);
						psta = connection.prepareStatement(sql);
						rs = psta.executeQuery();
						YdmBcInlistEntity ydmIn = EntityTool.getEntityByRs(rs, YdmBcInlistEntity.class);
						rs.close();
						psta.close();
						
						if(ydmIn == null)
						{
							cro.setV_errMsg(ydmBcInitialTiy.getJudgeStoveNo()+"入库记录为空!");
							return cro;
						}
						ydmBcInitialTiy.setStoveNo(ydmIn.getStoveNo());
						ydmBcInitialTiy.setLotNo(ydmIn.getLotNo());
						ydmBcInitialTiy.setMaterialNo(ydmIn.getMaterialNo());
						ydmBcInitialTiy.setMaterialName(ydmIn.getMaterialName());
						ydmBcInitialTiy.setProduccode(ydmIn.getProduccode());
						ydmBcInitialTiy.setProducname(ydmIn.getProducname());
						ydmBcInitialTiy.setStdStyle(ydmIn.getStdStyle());
						ydmBcInitialTiy.setStdStyleDesc(ydmIn.getStdStyleDesc());
						ydmBcInitialTiy.setSteelcode(ydmIn.getSteelcode());
						ydmBcInitialTiy.setSteelname(ydmIn.getSteelname());
						ydmBcInitialTiy.setSpecCode(ydmIn.getSpecCode());
						ydmBcInitialTiy.setSpecName(ydmIn.getSpecName());
						ydmBcInitialTiy.setModelCode(ydmIn.getModelCode());
						ydmBcInitialTiy.setModelDesc(ydmIn.getModelDesc());
						ydmBcInitialTiy.setStdCode(ydmIn.getStdCode());
						ydmBcInitialTiy.setStdName(ydmIn.getStdName());
						ydmBcInitialTiy.setGradecode(ydmIn.getGradecode());
						ydmBcInitialTiy.setGradename(ydmIn.getGradename());
						ydmBcInitialTiy.setDimater(ydmIn.getActDimater());
						ydmBcInitialTiy.setHeight(ydmIn.getActHeight());
						ydmBcInitialTiy.setLenMin(ydmIn.getActLenMin());
						ydmBcInitialTiy.setLenMax(ydmIn.getActLenMax());
						ydmBcInitialTiy.setLen(ydmIn.getActLen());
						ydmBcInitialTiy.setActCount(list.get(2));
						ydmBcInitialTiy.setActWeight(list.get(3));
						ydmBcInitialTiy.setPlineCode(ydmIn.getPlineCode());
						ydmBcInitialTiy.setPlineName(ydmIn.getPlineName());
						
						ydmBcInitialTiy.setInstockTime(ydmIn.getInstockTime());
						ydmBcInitialTiy.setBalYearMonth(mothYear);
						ydmBcInitialTiy.setProductFlag(ydmIn.getProductFlag());
						ydmBcInitialTiy.setMatStatus(ydmIn.getMatStatus());
						ydmBcInitialTiy.setJudgeStdDes("");
						
						ydmBcInitialTiy.setCreateName(userName);
						ydmBcInitialTiy.setProcessCdoe(ydmIn.getProcessCdoe());
						ydmBcInitialTiy.setProcessDesc(ydmIn.getProcessDesc());

						String insertInialSql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.insertInital");
						insertInialSql = EntityTool.fillSqlByEntity(insertInialSql, ydmBcInitialTiy);
						sqllist.add(insertInialSql);
					}else
					{
						ydmBcInitialTiy.setStoveNo(ydmOut.getStoveNo());
						ydmBcInitialTiy.setLotNo(ydmOut.getLotNo());
						ydmBcInitialTiy.setMaterialNo(ydmOut.getMaterialNo());
						ydmBcInitialTiy.setMaterialName(ydmOut.getMaterialName());
						ydmBcInitialTiy.setProduccode(ydmOut.getProduccode());
						ydmBcInitialTiy.setProducname(ydmOut.getProducname());
						ydmBcInitialTiy.setStdStyle(ydmOut.getStdStyle());
						ydmBcInitialTiy.setStdStyleDesc(ydmOut.getStdStyleDesc());
						ydmBcInitialTiy.setSteelcode(ydmOut.getSteelcode());
						ydmBcInitialTiy.setSteelname(ydmOut.getSteelname());
						ydmBcInitialTiy.setSpecCode(ydmOut.getSpecCode());
						ydmBcInitialTiy.setSpecName(ydmOut.getSpecName());
						ydmBcInitialTiy.setModelCode(ydmOut.getModelCode());
						ydmBcInitialTiy.setModelDesc(ydmOut.getModelDesc());
						ydmBcInitialTiy.setStdCode(ydmOut.getStdCode());
						ydmBcInitialTiy.setStdName(ydmOut.getStdName());
						ydmBcInitialTiy.setGradecode(ydmOut.getGradecode());
						ydmBcInitialTiy.setGradename(ydmOut.getGradename());
						ydmBcInitialTiy.setDimater(ydmOut.getActDimater());
						ydmBcInitialTiy.setHeight(ydmOut.getActHeight());
						ydmBcInitialTiy.setLenMin(ydmOut.getActLenMin());
						ydmBcInitialTiy.setLenMax(ydmOut.getActLenMax());
						ydmBcInitialTiy.setLen(ydmOut.getActLen());
						ydmBcInitialTiy.setActCount(list.get(2));
						ydmBcInitialTiy.setActWeight(list.get(3));
						ydmBcInitialTiy.setPlineCode(ydmOut.getPlineCode());
						ydmBcInitialTiy.setPlineName(ydmOut.getPlineName());
						
						ydmBcInitialTiy.setInstockTime(ydmOut.getInstockTime());
						ydmBcInitialTiy.setBalYearMonth(mothYear);
						ydmBcInitialTiy.setProductFlag(ydmOut.getProductFlag());
						ydmBcInitialTiy.setMatStatus(ydmOut.getMatStatus());
						ydmBcInitialTiy.setJudgeStdDes("");
						
						ydmBcInitialTiy.setCreateName(userName);
						ydmBcInitialTiy.setProcessCdoe(ydmOut.getProcessCdoe());
						ydmBcInitialTiy.setProcessDesc(ydmOut.getProcessDesc());

						String insertInialSql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.insertInital");
						insertInialSql = EntityTool.fillSqlByEntity(insertInialSql, ydmBcInitialTiy);
						sqllist.add(insertInialSql);
					}
					
				}
				else
				{
//					for (MatBcMEntity matEntity:matEntitys )
//					{
						ydmBcInitialTiy.setStoveNo(matEntity.getStoveNo());
						ydmBcInitialTiy.setLotNo(matEntity.getLotNo());
						ydmBcInitialTiy.setMaterialNo(matEntity.getMaterialNo());
						ydmBcInitialTiy.setMaterialName(matEntity.getMaterialName());
						ydmBcInitialTiy.setProduccode(matEntity.getProduccode());
						ydmBcInitialTiy.setProducname(matEntity.getProducname());
						ydmBcInitialTiy.setStdStyle(matEntity.getStdStyle());
						ydmBcInitialTiy.setStdStyleDesc(matEntity.getStdStyleDesc());
						ydmBcInitialTiy.setSteelcode(matEntity.getSteelcode());
						ydmBcInitialTiy.setSteelname(matEntity.getSteelname());
						ydmBcInitialTiy.setSpecCode(matEntity.getSpecCode());
						ydmBcInitialTiy.setSpecName(matEntity.getSpecName());
						ydmBcInitialTiy.setModelCode(matEntity.getModelCode());
						ydmBcInitialTiy.setModelDesc(matEntity.getModelDesc());
						ydmBcInitialTiy.setStdCode(matEntity.getStdCode());
						ydmBcInitialTiy.setStdName(matEntity.getStdName());
						ydmBcInitialTiy.setGradecode(matEntity.getGradecode());
						ydmBcInitialTiy.setGradename(matEntity.getGradename());
						ydmBcInitialTiy.setDimater(matEntity.getActDimater());
						ydmBcInitialTiy.setHeight(matEntity.getActHeight());
						ydmBcInitialTiy.setLenMin(matEntity.getActLenMin());
						ydmBcInitialTiy.setLenMax(matEntity.getActLenMax());
						ydmBcInitialTiy.setLen(matEntity.getActLen());
						ydmBcInitialTiy.setActCount(list.get(2));
						ydmBcInitialTiy.setActWeight(list.get(3));
//						ydmBcInitialTiy.setActCount(matEntity.getActCount());
//						ydmBcInitialTiy.setActWeight(matEntity.getActWeight());
						ydmBcInitialTiy.setPlineCode(matEntity.getPlineCode());
						ydmBcInitialTiy.setPlineName(matEntity.getPlineName());

						ydmBcInitialTiy.setInstockTime(matEntity.getInstockTime());
						ydmBcInitialTiy.setBalYearMonth(mothYear);
						//ydmBcInitialTiy.setBalYearMonth("202010");
						ydmBcInitialTiy.setProductFlag(matEntity.getProductFlag());
						ydmBcInitialTiy.setMatStatus(matEntity.getMatStatus());
						ydmBcInitialTiy.setJudgeStdDes(matEntity.getJudgeStdDes());

						ydmBcInitialTiy.setCreateName(userName);

						ydmBcInitialTiy.setStorageNo(matEntity.getStorageNo());
						ydmBcInitialTiy.setStorageName(matEntity.getStorageName());
						String insertInialSql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.insertInital");
						insertInialSql = EntityTool.fillSqlByEntity(insertInialSql, ydmBcInitialTiy);
						sqllist.add(insertInialSql);
					//}

				}
				

				
				
				m++;
				judgeNo = ydmBcInitialTiy.getJudgeStoveNo();
				actCount = ydmBcInitialTiy.getActCount();
			}
			QcmZgJugdeApplyEntity qcmEntity = new QcmZgJugdeApplyEntity();
			qcmEntity.setBalYearMonth(bathYear);
			qcmEntity.setBelongCodeJugde(belongCode);
			String updateQcmGpJudgeSql = CoreXmlSqlParsersFactory.getSql("FrmBcRunTimeReport.updateCarry");//修改申请判定表结转标识
			updateQcmGpJudgeSql = EntityTool.fillSqlByEntity(updateQcmGpJudgeSql, qcmEntity);
			sqllist.add(updateQcmGpJudgeSql);
			
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("期初数据结转成功!");
				return cro;
			}
		} catch (Exception e) {
			System.out.println(m + "/" + judgeNo + "/" + actCount +"/"+e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/**
	 * 生成最大年月下一年月
	 * 
	 * @param yearMoth
	 * @return
	 */
	public static String getBalYearMoth(String yearMoth) {
		String year = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdfS = new SimpleDateFormat("yyyyMM");
		try {
			Date date = sdf.parse(yearMoth);
			Calendar calender = Calendar.getInstance();
			calender.setTime(date);
			calender.add(Calendar.MONTH, 1);
			year = sdfS.format(calender.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return year;

	}
}
