package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;

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 net.sf.json.JSONObject;

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

import com.steering.comm.db.DBCommon;

import com.steering.pss.sale.order.model.SlmOrderLineEntity;

public class CoreOrderPriceDetails extends DBCommon {
	// -----------2015/06/22------------
	/**
	 * 查询合同行信息
	 * 
	 * @param ordPk
	 *            合同号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getOrdLineByOrdPk(String ordPk) {
		Object[] params = new Object[] { ordPk };
		return this.getDataBySqlID("CoreOrderPriceDetails.getOrdLineByOrdPk",
				params);
	}

	/**
	 * 获取定价说明
	 * 
	 * @param priceNo
	 * @return
	 */
	public CoreReturnObject getPriceBase(String ordPk) {
		Object[] params = new Object[] { ordPk };
		return this
				.getDataBySqlID("CoreOrderPriceDetails.getPriceBase", params);
	}

	/**
	 * 查询合同头定价编号
	 * 
	 * @param ordPk
	 * @return
	 */
	public CoreReturnObject getOrdHeadPriceNo(String ordPk) {
		Object[] params = new Object[] { ordPk };
		return this.getDataBySqlID("CoreOrderPriceDetails.getOrdHeadPriceNo",
				params);
	}

	/**
	 * 保存定价信息
	 * 
	 * @param strJsons
	 * @return
	 */
	public CoreReturnObject savePrice(ArrayList<String> strJsons,
			String orderPk, String priceBase, String userName, String saleOrg) {
		CoreReturnObject core = new CoreReturnObject();
		if(saleOrg.equals("100102")){
			core.setV_errMsg("国际贸易部的合同不需要维护定价！");
			core.setV_errCode(-1);
			return core;
		}
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false);
			List<SlmOrderLineEntity> orderInfos = new ArrayList<SlmOrderLineEntity>();
			for (String strJson : strJsons) {
				JSONObject obj = JSONObject.fromObject(strJson);
				SlmOrderLineEntity orderInfo = (SlmOrderLineEntity) JSONObject
						.toBean(obj, SlmOrderLineEntity.class);
				orderInfos.add(orderInfo);
			}
			String sql = "";
			PreparedStatement ps = null;
			ResultSet rs = null;
			for (SlmOrderLineEntity orderInfo : orderInfos) {
				sql = CoreXmlSqlParsersFactory
						.getSql("CoreOrderPriceDetails.checkPriceNo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, orderPk);
				rs = ps.executeQuery();
				String priceNo = "";
				if (rs.next()) {
					priceNo = rs.getString("PRICE_NO");
				}
				// 如同编号为空,新增定价信息,否则修改定价信息
				if (priceNo.equals("")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.addPriceA");
					ps = con.prepareStatement(sql);
					ps.setObject(1, orderInfo.getOrdLnPk());
					ps.setObject(2, orderInfo.getOutPrice());
					ps.setObject(3, orderInfo.getBlaPrice());
					ps.setObject(4, orderInfo.getTrsPrice());
					ps.setObject(5, orderInfo.getPakPrice());
					ps.setObject(6, userName);
					ps.executeUpdate();
				} else {
					// 如果定价明细没有此合同行的记录,则新增
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.checkPriceListByOldLnPk");
					ps = con.prepareStatement(sql);
					ps.setObject(1, orderInfo.getOrdLnPk());
					rs = ps.executeQuery();
					int count = 0;
					if (rs.next()) {
						count = Integer.parseInt(rs.getString("COUNT"));
					}
					if (count != 0) {
						sql = CoreXmlSqlParsersFactory
								.getSql("CoreOrderPriceDetails.updatePrice");
						ps = con.prepareStatement(sql);
						ps.setObject(1, orderInfo.getOutPrice());
						ps.setObject(2, orderInfo.getBlaPrice());
						ps.setObject(3, orderInfo.getTrsPrice());
						ps.setObject(4, orderInfo.getPakPrice());
						ps.setObject(5, userName);
						ps.setObject(6, priceNo);
						ps.setObject(7, orderInfo.getOrdLnPk());
						ps.executeUpdate();
					} else {
						sql = CoreXmlSqlParsersFactory
								.getSql("CoreOrderPriceDetails.addPriceB");
						ps = con.prepareStatement(sql);
						ps.setObject(1, priceNo);
						ps.setObject(2, orderInfo.getOrdLnPk());
						ps.setObject(3, orderInfo.getOutPrice());
						ps.setObject(4, orderInfo.getBlaPrice());
						ps.setObject(5, orderInfo.getTrsPrice());
						ps.setObject(6, orderInfo.getPakPrice());
						ps.setObject(7, userName);
						ps.executeUpdate();
					}

				}
			}
			// 定价说明
			sql = CoreXmlSqlParsersFactory
					.getSql("CoreOrderPriceDetails.checkPriceNo");
			ps = con.prepareStatement(sql);
			ps.setObject(1, orderPk);
			rs = ps.executeQuery();
			String priceNoOne = "";
			if (rs.next()) {
				priceNoOne = rs.getString("PRICE_NO");
			}
			if (!priceNoOne.equals("")) {
				sql = CoreXmlSqlParsersFactory
						.getSql("CoreOrderPriceDetails.checkOrderPrice");
				ps = con.prepareStatement(sql);
				ps.setObject(1, priceNoOne);
				rs = ps.executeQuery();
				if (rs.next()) {
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.updateOrderPrice");
					ps = con.prepareStatement(sql);
					ps.setObject(1, priceBase);
					ps.setObject(2, userName);
					ps.setObject(3, priceNoOne);
					ps.executeUpdate();
				} else {
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.addOrderPrice");
					ps = con.prepareStatement(sql);
					ps.setObject(1, priceNoOne);
					ps.setObject(2, priceBase);
					ps.setObject(3, userName);
					ps.setObject(4, orderPk);
					ps.executeUpdate();
				}
			}
			// 验证合同下的所有合同行是否定价
			sql = CoreXmlSqlParsersFactory
					.getSql("CoreOrderPriceDetails.getPriceNoByOrdLnPk");
			ps = con.prepareStatement(sql);
			ps.setObject(1, orderPk);
			rs = ps.executeQuery();
			boolean flag = true;
			String priceNo = "";
			while (rs.next()) {
				if (rs.getString("PRICE_NO") == null) {
					priceNo = "";
				} else {
					priceNo = rs.getString("PRICE_NO");
				}
				if (priceNo.length() == 0) {
					flag = false;
				}
			}
			if (flag) {
				sql = CoreXmlSqlParsersFactory
						.getSql("CoreOrderPriceDetails.updateHeadPriceNo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, priceNo);
				ps.setObject(2, orderPk);
				ps.executeUpdate();
				// 修改合同状态
				sql = CoreXmlSqlParsersFactory
						.getSql("CoreOrderApproval.getOrdPkStatus");
				ps = con.prepareStatement(sql);
				ps.setObject(1, orderPk);
				rs = ps.executeQuery();
				String status = "";
				if (rs.next()) {
					status = rs.getString("ORDER_STS");
				}
				List<String> statuses = new ArrayList<String>();
				statuses.add("12020221");
				statuses.add("12020223");
				statuses.add("12020241");
				statuses.add("12020231");
				if (statuses.contains(status)) {
					// 修改合同头状态
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderApproval.updateOrderHeadStatus");
					ps = con.prepareStatement(sql);
					ps.setObject(1, "12020212");
					ps.setObject(2, "交货期评审通过");
					ps.setObject(3, orderPk);
					ps.executeUpdate();
					// 修改合同行状态
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderApproval.updateOrderLineStatus");
					ps = con.prepareStatement(sql);

					ps.setObject(1, "12020212");
					ps.setObject(2, orderPk);
					ps.executeUpdate();
					// 修改交货行状态
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderApproval.updateOrderDeliveryStatus");
					ps = con.prepareStatement(sql);
					ps.setObject(1, "12020212");
					ps.setObject(2, orderPk);
					ps.executeUpdate();
					// 修改评审明细中价格评审（标识为“1”）的标识为“0”，评审项目中价格评审的状态为“封锁”
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.updateItemListP");
					ps = con.prepareStatement(sql);
					ps.setObject(1, orderPk);
					ps.executeUpdate();
					// 修改评审项目表价格状态为封锁
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.updateItemP");
					ps = con.prepareStatement(sql);
					ps.setObject(1, orderPk);
					ps.executeUpdate();
					// 新增评审明细中价格评审明细记录，状态为“封锁”，标识为“1”
					sql = CoreXmlSqlParsersFactory
							.getSql("CoreOrderPriceDetails.addItemListP");
					ps = con.prepareStatement(sql);
					ps.setObject(1, orderPk);
					ps.setObject(2, orderPk);
					ps.setObject(3, orderPk);
					ps.setObject(4, orderPk);
					ps.setObject(5, userName);
					ps.executeUpdate();
				}
			}
			con.commit();

		} catch (SQLException e) {
			try {
				core.setResult("-1");
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

		return core;
	}

	public CoreReturnObject cancelPriceNo(ArrayList<String> strJsons,
			String ordPk) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false);
			String sql = "";
			PreparedStatement ps = null;
			for (String strJson : strJsons) {
				JSONObject obj = JSONObject.fromObject(strJson);
				SlmOrderLineEntity orderInfo = (SlmOrderLineEntity) JSONObject
						.toBean(obj, SlmOrderLineEntity.class);
				sql = CoreXmlSqlParsersFactory
						.getSql("CoreOrderPriceDetails.deleteListPriceNo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, orderInfo.getOrdLnPk());
				ps.executeUpdate();
			}
			sql = CoreXmlSqlParsersFactory
					.getSql("CoreOrderPriceDetails.deleteHeadPriceNo");
			ps = con.prepareStatement(sql);
			ps.setObject(1, ordPk);
			ps.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return core;
	}

}
