package com.steering.pss.ydm.pipemanage;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.DefaultWebServiceRequest;
import com.steering.comm.ServiceToCx;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.plnsaleord.stove.entity.MatGpMEntity;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.QcmGpJudgeApplyEntity;
import com.steering.pss.ydm.entity.YdmGpInlistEntity;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

/***
 * 管坯判定时写入库表所属权入库
 * @author Administrator
 *
 */
public class FrmGpIqInList extends DBCommon{

	/***
	 * 管坯判定时写入库表(所属权转移)
	 * 
	 * @param qcmEntity
	 * @return
	 * @throws Exception
	 */
	public void insertGpInList(Connection con,ArrayList<String> qcmEntity,
			String userName) throws Exception {
		Statement st = null;
		try{
			st = con.createStatement();
			Date now = new Date();
			String bathYear = "";
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
//			SimpleDateFormat dateFormatE = new SimpleDateFormat(
//					"yyyy-MM");

			SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
			String systime = dateFormat.format(now);

//			String endTim = dateFormatE.format(now) + "-25 21:00:00";
//			Date time1 = dateFormat.parse(systime);
//			Date time2 = dateFormat.parse(endTim);
//			if(time1.before(time2))
//			{
//				bathYear = df.format(now);
//			}
//			else
//			{
//				Date dt = dateFormatE.parse(systime);
//				Calendar rightNow = Calendar.getInstance();
//				rightNow.setTime(dt);
//				rightNow.add(Calendar.MONTH, 1);
//				Date dt1 = rightNow.getTime();
//				bathYear = df.format(dt1);
//			}
			String inListDoc = getInListDoc(con, "800709",systime);// 入库单号
			String soureCode = "";
			String soureName = "";
			String storageNo = "";
			String sql = "";
			PreparedStatement ps = null;
			ResultSet rs = null;
			String isDear = "";
			for (String str : qcmEntity) {
				QcmGpJudgeApplyEntity qcmGpApplyEntity = EntityTool
						.getEntityByJSONStr(str, QcmGpJudgeApplyEntity.class);

				bathYear = "120504".equals(qcmGpApplyEntity.getSupplyUnitCode())? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now);

				sql = CoreXmlSqlParsersFactory.getSql("FrmGpIqInList.SelectMatGp");
				sql = EntityTool.fillSqlByEntity(sql,qcmGpApplyEntity);
				ps = con.prepareStatement(sql);
				ps.setString(1,qcmGpApplyEntity.getJugdeApplyCode() + qcmGpApplyEntity.getJugdeApplySqe());
				ps.setString(2,qcmGpApplyEntity.getJugdeApplyCode() + qcmGpApplyEntity.getJugdeApplySqe());
				rs = ps.executeQuery();
				if(rs.next())
				{
					soureCode = rs.getString("SOURE_CODE");
					soureName = rs.getString("SOURE_NAME");
					storageNo = rs.getString("STORAGE_NO");
					isDear = rs.getString("IS_DEALER");
				}
				rs.close();
				ps.close();

				List<double[]> list = MeanValueCalculation.MeanCalculation(Double
						.parseDouble(qcmGpApplyEntity.getActTheoryWeight()), Integer
						.parseInt(qcmGpApplyEntity.getActCount()));
				String matNo = queryMatNoM(con, qcmGpApplyEntity
						.getJudgeStoveNo());
				int m = Integer.parseInt(matNo.substring(matNo.length() - 3, matNo
						.length()));
				String inListSeq = querySendSql(con, inListDoc); // 入库流水号
				int n = Integer.parseInt(inListSeq.substring(
						inListSeq.length() - 4, inListSeq.length()));

				for (int j = 0; j < list.size(); j++) {
					double[] conW = list.get(j);
					if ((int) conW[0] >= 0) {
						for (int z = 0; z < (int) conW[0]; z++) {
							YdmGpInlistEntity ydmGpEntity = new YdmGpInlistEntity();
							ydmGpEntity.setInstockDoc(inListDoc);
							ydmGpEntity.setInstockSqe(inListDoc
									+ StringTool.formatDecimal(n, "0000"));
							ydmGpEntity.setInstockTypeCode("800709");
							ydmGpEntity.setInstockTypeName("产权入库");
							ydmGpEntity.setStoveNo(qcmGpApplyEntity.getStoveNo());
							ydmGpEntity.setJudgeStoveNo(qcmGpApplyEntity
									.getJudgeStoveNo());
							ydmGpEntity.setGroudNo(qcmGpApplyEntity
									.getMultipleNum());
							ydmGpEntity.setProPlanId(qcmGpApplyEntity
									.getProPlanId());
							ydmGpEntity.setGxPlanNo(qcmGpApplyEntity.getGxPlanNo());
							ydmGpEntity.setPlineCode(qcmGpApplyEntity
									.getPlineCode());
							ydmGpEntity.setPlineName(qcmGpApplyEntity
									.getPlineName());
							ydmGpEntity.setBelongCode(qcmGpApplyEntity
									.getBelongCodeJugde());
							ydmGpEntity.setBelongName(qcmGpApplyEntity
									.getBelongNameJugde());
							
							ydmGpEntity.setMaterialNo(qcmGpApplyEntity
									.getMaterialNo());
							ydmGpEntity.setMaterialName(qcmGpApplyEntity
									.getMaterialName());
							ydmGpEntity
									.setDimater(qcmGpApplyEntity.getActDimater());
							ydmGpEntity.setGradecode(qcmGpApplyEntity
									.getGradecode());
							ydmGpEntity.setGradename(qcmGpApplyEntity
									.getGradename());
							ydmGpEntity
									.setFixNum(qcmGpApplyEntity.getMultipleNum());
							ydmGpEntity
									.setFixLen(qcmGpApplyEntity.getLenGpSingle());
							ydmGpEntity.setActLen(qcmGpApplyEntity.getActLen());
							ydmGpEntity.setActCount("1");
							ydmGpEntity.setActWeight(String.valueOf(conW[1]));
							ydmGpEntity.setMatNo((qcmGpApplyEntity
									.getJudgeStoveNo())
									+ StringTool.formatDecimal(m, "0000"));

							
							if (qcmGpApplyEntity.getIsendgx().equals("0")) {
								ydmGpEntity.setProductFlag("801401");
								ydmGpEntity.setMatStatus("80150101");
							} else if (qcmGpApplyEntity.getIsendgx().equals("1")) {
								ydmGpEntity.setProductFlag("801402");
								ydmGpEntity.setMatStatus("80150201");
							}
							ydmGpEntity.setCreateName(userName);
							ydmGpEntity.setCreateTime(systime);
							ydmGpEntity.setInstockTime(systime);
							ydmGpEntity.setBalYearMonth(bathYear);

							ydmGpEntity.setInTimeF(systime);
							ydmGpEntity.setProOrderNo(qcmGpApplyEntity.getProOrderNo());
							ydmGpEntity.setJudgeStoveNoLg(qcmGpApplyEntity.getJudgeStoveNoLg());
							ydmGpEntity.setEfPlineCode(qcmGpApplyEntity.getEfPlineCode());
							ydmGpEntity.setEfPlineName(qcmGpApplyEntity.getEfPlineName());
							ydmGpEntity.setSoureCode(soureCode);
							ydmGpEntity.setSoureName(soureName);
							ydmGpEntity.setStorageNo(storageNo);
							ydmGpEntity.setSupplyUnitCode(qcmGpApplyEntity.getSupplyUnitCode());
							ydmGpEntity.setSupplyUnit(qcmGpApplyEntity.getSupplyUnit());
							ydmGpEntity.setIsDealer(isDear);
							ydmGpEntity.setJugdeApplyCodeSqe(qcmGpApplyEntity.getJugdeApplyCode()+qcmGpApplyEntity.getJugdeApplySqe()+qcmGpApplyEntity.getJugdeApplySequeno());
							String insertInListSql = CoreXmlSqlParsersFactory
									.getSql("FrmGpIqInList.insertGpInList");
							insertInListSql = EntityTool.fillSqlByEntity(
									insertInListSql, ydmGpEntity);
							st.executeUpdate(insertInListSql);
							m++;
							n++;
							
							
						}
					}
				}
			}
		}finally{
			st.close();
		}
	}

	/***
	 * 撤销判定删除产权入库记录
	 * @param con
	 * @param qcmEntity
	 * @return
	 * @throws SQLException 
	 */
	public void cancelInsertGpInList(Connection con,ArrayList<String> qcmEntity) throws SQLException
	{
		Statement st = null;
		try{
			st = con.createStatement();
			for(String str : qcmEntity)
			{
				QcmGpJudgeApplyEntity qcmGpAppEntity = EntityTool.getEntityByJSONStr(str, QcmGpJudgeApplyEntity.class);
				qcmGpAppEntity.setJugdeApplyCodeSqe(qcmGpAppEntity.getJugdeApplyCode()+qcmGpAppEntity.getJugdeApplySqe()+qcmGpAppEntity.getJugdeApplySequeno());
				String deleteInsertInListSql = CoreXmlSqlParsersFactory.getSql("FrmGpIqInList.cancelInsertGpInList");
				deleteInsertInListSql = EntityTool.fillSqlByEntity(deleteInsertInListSql, qcmGpAppEntity);
				st.executeUpdate(deleteInsertInListSql);
			}
		}finally{
			st.close();
		}
	}
	/***
	 * 获取入库单号
	 * 
	 * @param conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound,String systime)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmGpIqInList.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param
	 *
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String inDoc) throws Exception {
		int seq = 0;
		String inListDocSeq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmGpIqInList.querySendSql");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, inDoc);
		psta.setString(2, inDoc);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDocSeq = rs.getString("INSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return inListDocSeq;
	}

	/**
	 * 根据炉号自动生成材料号
	 * 
	 * @param conn
	 *            c
	 * @param
	 *            
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryMatNoM(Connection conn, String judgeNo) throws Exception {
		String matNo = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmGpIqInList.QueryMatNoM");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, judgeNo);
		psta.setString(2, judgeNo);
		psta.setString(3, judgeNo);
		psta.setString(4, judgeNo);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			matNo = rs.getString("MAT_NO");
		}
		rs.close();
		psta.close();
		return matNo;
	}
}
