package com.steering.pss.ydm.pipeleavefactory;

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.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.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.entity.MatInfoEntity;
import com.steering.pss.ydm.entity.MatZcMInfoEntity1;
import com.steering.pss.ydm.entity.YdmZcSendplanCEntity;
import com.steering.pss.ydm.entity.YdmZcSendplanMEntity;

/**
 * 汽运出厂计划
 * @author Administrator TJG 2015-06-10
 */
public class FrmLocInfo extends DBCommon {


    /**
     * 查询可发资源主信息
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject queryLocInfo(String LoadNo,String judgeNo,
    		String bathNo,String groupNo,String ordLnDlyPk) {

        Object[] params = new Object[] { LoadNo, judgeNo, bathNo,groupNo,ordLnDlyPk};
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmLocInfo.queryLocInfo");
        return this.getData(sql, params);
    }

    
    /**
     * 查询可替换库位
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject queryMatInfo(String judgeNo,String bathNo,
    		String groupNo,String ordLnDlyPk) {
        Object[] params = new Object[] { judgeNo, bathNo,groupNo,ordLnDlyPk,judgeNo, bathNo,groupNo,ordLnDlyPk};
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmLocInfo.queryMatInfo");
        return this.getData(sql, params);
    }



	/**
	 * 替换料位
	 * @param parms
	 * @param billNo
	 * @return
	 */
	public CoreReturnObject doConfirm(ArrayList<String> parms1,
			ArrayList<String> parms2) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try {
			con.setAutoCommit(false);
			List<MatInfoEntity> matZcMInfos1 = queryLocMatInfo(con, parms1.get(0).toString(),parms1.get(1).toString(),parms1.get(2).toString(),
			parms1.get(3).toString(),parms1.get(4).toString(),parms1.get(5).toString(),parms1.get(6).toString(),parms1.get(7).toString());
			List<MatInfoEntity> matZcMInfos2 = queryMatInfoByNum(con, parms2.get(0).toString(),parms2.get(1).toString(),parms2.get(2).toString(),
					parms2.get(3).toString(),parms2.get(4).toString(),parms2.get(5).toString(),parms2.get(6).toString(),
					parms2.get(7).toString(),parms2.get(8).toString());
			if(parms1.get(6).toString().equals(matZcMInfos1.size())){
				core.setV_errMsg("移库支数与库存不一致，请刷新后再进行操作！");
				core.setV_errCode(-1);
                con.rollback();
                return core;
			}
			if(parms2.get(6).toString().equals(matZcMInfos2.size())){
				core.setV_errMsg("移库支数与库存不一致，请刷新后再进行操作！");
				core.setV_errCode(-1);
                con.rollback();
                return core;
			}
			for(MatInfoEntity mat:matZcMInfos1){
				//修改装车表
				String sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateLoadMatInfo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms2.get(5).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
	            //修改理货表
	            sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateTallInfo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms2.get(5).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
	            //修改提单表
	            sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateBillInfo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms2.get(5).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
	            //修改库存表
				sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateStoge");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms2.get(5).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
			}
			for(MatInfoEntity mat:matZcMInfos2){
				//修改库存表
				String sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateStoge");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms1.get(6).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
	           //修改理货表
	            sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateTallInfo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms1.get(6).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
	            //修改提单表
	            sql = CoreXmlSqlParsersFactory
	                    .getSql("FrmLocInfo.updateBillInfo");
				ps = con.prepareStatement(sql);
				ps.setObject(1, parms1.get(6).toString());
				ps.setObject(2, mat.getMatNo());
	            ps.executeUpdate();
	            ps.close();
			}
			core.setV_errMsg("保存成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
				core.setV_errMsg(e.getMessage());
				core.setV_errCode(-1);
                e.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}
	
	/**
	 * 查询库存可编制资源信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param matInfoEntity
	 *            资源类
	 * @return List<MatZcMInfoEntity>
	 * @throws SQLException
	 */
	public static List<MatInfoEntity> queryLocMatInfo(Connection con,
			String LoadNo,String judgeNo,String bathNo,String groupNo,String ordDlyLnPk,String storgeNo,String locationNo,String count) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmLocInfo.queryLocMatInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, LoadNo);
		ps.setObject(2, judgeNo);
		ps.setObject(3, bathNo);
		ps.setObject(4, groupNo);
		ps.setObject(5, ordDlyLnPk);
		ps.setObject(6, storgeNo);
		ps.setObject(7, locationNo);
		ps.setObject(8, count);
		ResultSet rs = ps.executeQuery();
		List<MatInfoEntity> matZcMInfoEntitys = EntityTool.getEntitysByRs(
				rs, MatInfoEntity.class);
		ps.close();
		rs.close();
		if (matZcMInfoEntitys == null || matZcMInfoEntitys.size() == 0) {
			return null;
		} else {
			return matZcMInfoEntitys;
		}
	}
	
	/**
	 * 查询库存可编制资源信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param matInfoEntity
	 *            资源类
	 * @return List<MatZcMInfoEntity>
	 * @throws SQLException
	 */
	public static List<MatInfoEntity> queryMatInfoByNum(Connection con,
			String judgeNo,String bathNo,String groupNo,String ordLnDlyPk,String storgeNo,String locationNo,
			String count,String billNo,String tallNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmLocInfo.queryMatInfoByNum");
		if(!"".endsWith(billNo)){
			sql+=" AND EXISTS (SELECT 1 FROM YDM_ZC_BILL_C T2 WHERE T.MAT_NO=T2.MAT_NO AND T2.BILL_NO='" + billNo +"')";
		}
		if(!"".endsWith(tallNo)){
			sql+=" AND EXISTS (SELECT 1 FROM YDM_ZC_TALLYSHEET_C T3 WHERE T.MAT_NO=T3.MAT_NO AND T3.TALLYSHEET_NO='" + tallNo +"')";
		} 
		if(!"".endsWith(count)){
			sql+=" AND ROWNUM <= " + count ;
		}		
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeNo);
		ps.setObject(2, bathNo);
		ps.setObject(3, groupNo);
		ps.setObject(4, ordLnDlyPk);
		ps.setObject(5, judgeNo);
		ps.setObject(6, bathNo);
		ps.setObject(7, groupNo);
		ps.setObject(8, ordLnDlyPk);
		ps.setObject(9, storgeNo);
		ps.setObject(10, locationNo);
		ResultSet rs = ps.executeQuery();
		List<MatInfoEntity> matZcMInfoEntitys = EntityTool.getEntitysByRs(
				rs, MatInfoEntity.class);
		ps.close();
		rs.close();
		if (matZcMInfoEntitys == null || matZcMInfoEntitys.size() == 0) {
			return null;
		} else {
			return matZcMInfoEntitys;
		}
	}

}
