package com.steering.pss.plnsaleord.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.order.baseMethod.CoreOrdBase;
import com.steering.pss.plnsaleord.order.model.CraftOrdDesignMscPlPrcEntity;
import com.steering.pss.plnsaleord.order.model.PlnSaleordProcEntity;

/**
 * 合同制程更改
 * 
 * @author xiaohai
 */
public class CoreChangeIndex extends DBCommon {
	/**
	 * 查询合同拆分
	 * 
	 * @param ordLnDlyPk
	 * @return
	 */
	public CoreReturnObject queryPlnOrderProc(String ordLnDlyPk, String rNumId,
			String plnDivideId) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreChangeIndex.queryPlnOrderProc");
		// if (rNumId.equals("")) {
		// sql += " AND T.ORDER_SOURCE IN ('销售合同')";
		// }
		// else {
		// sql += " AND T.ORDER_SOURCE = '补量需求' " + "AND T.R_NUM_ID = '"
		// + rNumId + "'";
		// }
		cro = this.getData(sql, new Object[] { ordLnDlyPk, plnDivideId });
		return cro;
	}

	/**
	 * 查询交货行子记录
	 * 
	 * @param ordLnDlyPk
	 * @return
	 */
	public CoreReturnObject queryPlnSaleOrderProc(String ordLnDlyPk) {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreChangeIndex.queryPlnSaleOrderProc",
				new Object[] { ordLnDlyPk });
		return cro;
	}

	/**
	 * 查询交货行子记录状态
	 * 
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @return
	 */
	public CoreReturnObject queryPlnOrdProcById(String ordLnDlyPk,
			String plnDivideId) {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreChangeIndex.queryPlnOrdProcById",
				new Object[] { ordLnDlyPk, plnDivideId });
		return cro;
	}

	/**
	 * 新增交货行子记录
	 * 
	 * @param ordProc
	 * @param list
	 * @return
	 */
	public CoreReturnObject newPlnOrdProc(PlnSaleordProcEntity ordProc,
			ArrayList<Object> list) {
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

		Object orderQtyD = list.get(0);
		String ordLnDlyPk = list.get(1).toString();
		String plnDivideId = list.get(2).toString();

		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreChangeIndex.newPlnOrdProc");
		String sqlUp = CoreXmlSqlParsersFactory
				.getSql("CoreChangeIndex.updateProcOrdQtyD");
		String sqlAdd = EntityTool.fillSqlByEntity(sql, ordProc);

		sqllist.add(sqlAdd);
		parmlist.add(new Object[] {});
		sqllist.add(sqlUp);
		parmlist.add(new Object[] { orderQtyD, orderQtyD, orderQtyD, orderQtyD,
				orderQtyD, orderQtyD, ordLnDlyPk, plnDivideId });

		cro = this.setData(sqllist, parmlist);
		return cro;
	}

	/**
	 * 更新交货行子记录拆分量
	 * 
	 * @param ordProc
	 * @return
	 */
	public CoreReturnObject updatePlnOrdProc(
			ArrayList<PlnSaleordProcEntity> list) {
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreChangeIndex.updatePlnOrdProc");
		String sqle = CoreXmlSqlParsersFactory
				.getSql("CoreChangeIndex.updatePlnOrdProcOutPi");
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getOrderQtyD().trim().equals("0")) {
				String sqlDe = CoreXmlSqlParsersFactory
						.getSql("CoreChangeIndex.deletePlnOrdProc");
				sqllist.add(sqlDe);
				parmlist.add(new Object[] { list.get(i).getOrdLnDlyPk(),
						list.get(i).getPlnDivideId() });
			} else if (list.get(i).getWeightPerM().trim().equals("")) {
				String sqlUp = EntityTool.fillSqlByEntity(sqle, list.get(i));
				sqllist.add(sqlUp);
				parmlist.add(new Object[] {});
			} else {
				String sqlUp = EntityTool.fillSqlByEntity(sql, list.get(i));
				sqllist.add(sqlUp);
				parmlist.add(new Object[] {});
			}
		}
		cro = this.setData(sqllist, parmlist);
		return cro;
	}

	/**
	 * 更新交货行子记录制程工艺
	 * 
	 * @param ordProcEntity
	 * @return
	 */
	public CoreReturnObject updatePlnOrdProcIndex(
			PlnSaleordProcEntity ordProcEntity, String desginKey) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);

			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.updatePlnOrdProcAmout");
			String sqlCraft = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.updatePlnOrdProcCraft");
			String sqlMscPline = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.updatePlnOrdProcMscPline");
			String sqlDPline = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.updatePlnOrdProcDPline");
			String sqlNewCraft = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.queryNewCaft");
			String sqlUpNew = CoreXmlSqlParsersFactory
					.getSql("CoreChangeIndex.updateNewCaft");
			psta = conn.prepareStatement(sqlNewCraft);
			psta.setObject(1, ordProcEntity.getOrdLnDlyPk());
			psta.setObject(2, ordProcEntity.getPlnDivideId());
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				PreparedStatement ps = null;
				String sqlUpe = EntityTool.fillSqlByEntity(sqlCraft,
						ordProcEntity);
				ps = conn.prepareStatement(sqlUpe);
				ps.executeUpdate();
				ps.close();
				if (!ordProcEntity.getProcessSeq().equals("")) {
					String sqlUp = EntityTool.fillSqlByEntity(sqlMscPline,
							ordProcEntity);
					ps = conn.prepareStatement(sqlUp);
					ps.executeUpdate();
					ps.close();
					CoreOrdLinePrdcrLst.updateOrdPrdcrproPk(conn,
							ordProcEntity.getOrdLnDlyPk(),
							ordProcEntity.getPlnDivideId());
				}
				if (!ordProcEntity.getGroupRoll().equals("")) {
					String sqlUp = EntityTool.fillSqlByEntity(sqlDPline,
							ordProcEntity);
					ps = conn.prepareStatement(sqlUp);
					ps.executeUpdate();
					ps.close();
				}
				if (!ordProcEntity.getPlanWtD().equals("")) {
					String sqlUp = EntityTool.fillSqlByEntity(sql,
							ordProcEntity);
					ps = conn.prepareStatement(sqlUp);
					ps.executeUpdate();
					ps.close();
				}

				if (rs.getString("CRAFT_SEQ") != null
						&& rs.getString("CRAFT_SEQ").equals(
								ordProcEntity.getCraftSeq())) {
					ps = conn.prepareStatement(sqlUpNew);
					ps.setObject(1, ordProcEntity.getOrdLnDlyPk());
					ps.setObject(2, ordProcEntity.getPlnDivideId());
					ps.executeUpdate();
					ps.close();
				}
			}
			rs.close();
			psta.close();

			// 查询二级工序
			List<CraftOrdDesignMscPlPrcEntity> mscPrcList = PlnHelper
					.getEntityDatas(conn, new String[] { desginKey,
							ordProcEntity.getIndexSeq() },
							"CorePlnSaleOrder.queryMscplPrc",
							CraftOrdDesignMscPlPrcEntity.class);
			if (mscPrcList != null) {

				String wholeBacklog = ordProcEntity.getProcessSeq();
				String endgxCode = wholeBacklog
						.substring(wholeBacklog.length() - 1);
				String gprocessC = ""; // 加工二级工序
				String fprocessC = ""; // 热处理二级工序
				for (CraftOrdDesignMscPlPrcEntity prcEntity : mscPrcList) {
					if (prcEntity.getProcessCode().trim().equals("G")) {
						gprocessC = prcEntity.getProcessCodeC();
					} else if (prcEntity.getProcessCode().trim().equals("F")) {
						fprocessC = prcEntity.getProcessCodeC();
					}
				}
				String prdCls = "";
				String sqlPrd = "SELECT T.PRD_CLS FROM PLN_SALEORD T  WHERE T.ORD_LN_DLY_PK=?";
				psta = conn.prepareStatement(sqlPrd);
				psta.setObject(1, ordProcEntity.getOrdLnDlyPk());
				rs = psta.executeQuery();
				if (rs.next()) {
					prdCls = rs.getString("PRD_CLS");
				}
				rs.close();
				psta.close();
				String produceSort = CoreOrdBase.produceSort(endgxCode, prdCls,
						gprocessC, fprocessC);

				String update = "UPDATE PLN_SALEORD T SET T.PRODUCE_SORT=? WHERE T.ORD_LN_DLY_PK=?";
				psta = conn.prepareStatement(update);
				psta.setObject(1, produceSort);
				psta.setObject(2, ordProcEntity.getOrdLnDlyPk());
				psta.executeUpdate();
				psta.close();
			}
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 删除交货行子记录
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject deletePlnOrdProc(ArrayList<ArrayList<Object>> list) {
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			sqllist.add("CoreChangeIndex.deletePlnOrdProc");
			parmlist.add(list.get(i).toArray());
		}
		cro = this.setDataById(sqllist, parmlist);
		return cro;
	}

}