package com.steering.pss.plnsaleord;

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.pss.sale.order.bomGeneration.BOMGeneration;

/**
 * 物料操作界面
 * 
 * @author Administrator
 * 
 */
public class FrmMaterialBom extends DBCommon {

	public CoreReturnObject queryOrderInfo(String beginTime, String endTime,
			String orderNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMaterialBom.queryOrderInfo");
		List<String> params = new ArrayList<String>();
		if (!beginTime.equals("") && !endTime.equals("")) {
			sql += "   AND B.CREATE_TIME BETWEEN TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND  TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')";
			params.add(beginTime);
			params.add(endTime);
		}
		if (!orderNo.equals("")) {
			sql += "   AND UPPER(A.ORDER_NO) || '&' LIKE UPPER('%' || ? || '%')";
			params.add(orderNo);
		}
		sql += " order by  A.ORDER_NO,B.ORDER_SEQ";
		return this.getData(sql, params.toArray());
	}

	/**
	 * 按条件查询BOM
	 * 
	 * @param msc
	 * @param mscPline
	 * @param gradename
	 * @param rollspec
	 * @return
	 */
	public CoreReturnObject queryBomByCondition(String msc, String mscPline,
			String gradename, String rollspec, String flag) {
		List<String> params = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMaterialBom.queryBomByCondition");
		if (!msc.equals("")) {
			sql += "AND T.MSC_DESC  LIKE '%'||?||'%' ";
			params.add(msc);
		}
		if (!mscPline.equals("")) {
			sql += " AND T.MSC_PLINE=?";
			params.add(mscPline);
		}
		if (!gradename.equals("")) {
			sql += " AND T.GRADENAME=?";
			params.add(gradename);
		}
		if (!rollspec.equals("")) {
			sql += "AND T.SPEC_NAME_R LIKE '%'||?||'%' ";
			params.add(rollspec);
		}
		if (flag.equals("0")) {
			// 不加sql条件
		} else if (flag.equals("1")) {
			sql += "    and exists (select t1.bom from PLN_SALEORD_PROC t1 where t1.bom = t.bom and t1.planstatus in ('2', '3'))";
		} else if (flag.equals("2")) {
			sql += "    and  not exists (select t1.bom from PLN_SALEORD_PROC t1 where t1.bom = t.bom and t1.planstatus in ('2', '3'))";
		} else {
			sql += " and 1!=1";
		}
		sql += " order by t.MATERIAL_NO,t.msc,t.msc_pline,t.SPEC_CODE_R,t.GRADECODE";
		return this.getData(sql, params.toArray());
	}

	public CoreReturnObject queryBom(String ordlnpk) {
		Object[] params = new Object[] { ordlnpk, ordlnpk };
		return this.getDataBySqlID("FrmMaterialBom.queryBom", params);
	}

	public CoreReturnObject queryBomA(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomA", params);
	}

	public CoreReturnObject queryBomB(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomB", params);
	}

	public CoreReturnObject queryBomC(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomC", params);
	}

	public CoreReturnObject queryBomD(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomD", params);
	}

	public CoreReturnObject queryBomE(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomE", params);
	}

	public CoreReturnObject queryBomF(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomF", params);
	}

	public CoreReturnObject queryBomK(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomK", params);
	}

	public CoreReturnObject queryBomJ(String bom) {
		Object[] params = new Object[] { bom };
		return this.getDataBySqlID("FrmMaterialBom.queryBomJ", params);
	}

	/**
	 * 删除Bom
	 * 
	 * @param bomList
	 * @return
	 */
	public CoreReturnObject deleteBom(ArrayList<String> bomList) {
		CoreReturnObject core = new CoreReturnObject();
		core.setResult(false);
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			String slmOrdLineBomSql = "delete from slm_ord_line_bom t where t.bom=?";
			String craftOrdLineBomSql = "delete from craft_ord_line_bom t where t.bom=?";
			String bomSql = "delete from SLM_ORD_DESIGN_BOM t where t.bom=?";
			String bomASql = "delete from SLM_ORD_DESIGN_BOM_A t where t.bom=?";
			String bomBSql = "delete from SLM_ORD_DESIGN_BOM_B t where t.bom=?";
			String bomCSql = "delete from SLM_ORD_DESIGN_BOM_C t where t.bom=?";
			String bomDSql = "delete from SLM_ORD_DESIGN_BOM_D t where t.bom=?";
			String bomESql = "delete from SLM_ORD_DESIGN_BOM_E t where t.bom=?";
			String bomFSql = "delete from SLM_ORD_DESIGN_BOM_F t where t.bom=?";
			String bomKSql = "delete from SLM_ORD_DESIGN_BOM_K t where t.bom=?";
			String bomJSql = "delete from SLM_ORD_DESIGN_BOM_H t where t.bom=?";
			PreparedStatement ps = null;
			for (String bom : bomList) {
				// 删除技术评审与Bom关系
				ps = con.prepareStatement(slmOrdLineBomSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// 删除工艺评审与Bom关系
				ps = con.prepareStatement(craftOrdLineBomSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// 删除Bom
				ps = con.prepareStatement(bomSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// bomA
				ps = con.prepareStatement(bomASql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomB
				ps = con.prepareStatement(bomBSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomC
				ps = con.prepareStatement(bomCSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomD
				ps = con.prepareStatement(bomDSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomE
				ps = con.prepareStatement(bomESql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomF
				ps = con.prepareStatement(bomFSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomJ
				ps = con.prepareStatement(bomJSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
				// BomK
				ps = con.prepareStatement(bomKSql);
				ps.setObject(1, bom);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("删除成功");
			core.setResult(true);
			con.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.toString());
			e.printStackTrace();
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return core;
	}

	/**
	 * 生成Bom
	 * 
	 * @param ordlnpks
	 * @return
	 */
	public CoreReturnObject greaBom(ArrayList<String> ordlnpks, String flag) {
		CoreReturnObject core = new CoreReturnObject();
		core.setResult(false);
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			String qualitySql = "SELECT T1.DESGIN_KEY FROM SLM_ORDER_LINE T, SLM_ORD_DESIGN_MSC T1 WHERE T.ORD_LN_PK = T1.ORD_LN_PK AND T.ORD_LN_PK = ?  AND T.VALIDFLAG = '1'  AND T1.VALIDFLAG = '1' AND T.ORDER_LN_STATUS >= '12020202'";
			String craftSql = "SELECT T.DESGIN_KEY\n" +
							"  FROM CRAFT_ORD_DESIGN T\n" + 
							" WHERE T.ORD_LN_PK = ?\n" + 
							"   AND T.CRAFT_SEQ = (SELECT MAX(A.CRAFT_SEQ)\n" + 
							"                        FROM CRAFT_ORD_DESIGN A\n" + 
							"                       WHERE A.ORD_LN_PK = T.ORD_LN_PK)";
			PreparedStatement ps = null;
			ResultSet rs = null;
			for (String ordlnpk : ordlnpks) {
				String designKey = "";
				if (flag.equals("Q")) {
					ps = con.prepareStatement(qualitySql);
					ps.setObject(1, ordlnpk);
					rs = ps.executeQuery();
					if (rs.next()) {
						designKey = rs.getString("DESGIN_KEY");
					}
					rs.close();
					ps.close();
					if (designKey == null || designKey.equals("")) {
						core.setV_errMsg("合同没有技术评审结果,不能生成Bom!");
						return core;
					} else {
						BOMGeneration.generationBomMain(con, designKey, "0",
								"0");
					}
				} else {
					ps = con.prepareStatement(craftSql);
					ps.setObject(1, ordlnpk);
					rs = ps.executeQuery();
					if (rs.next()) {
						designKey = rs.getString("DESGIN_KEY");
					}
					rs.close();
					ps.close();
					if (designKey == null || designKey.equals("")) {
						core.setV_errMsg("合同没有工艺评审结果,不能生成Bom!");
						return core;
					} else {
						BOMGeneration.generationBomMain(con, designKey, "2",
								"1");
					}
				}
			}
			core.setResult(true);
			core.setV_errMsg("生成成功");
			con.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.toString());
			e.printStackTrace();
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return core;
	}
}
