package com.steering.pss.plnsaleord.processOrder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import net.sf.json.JSONObject;
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.processOrder.entity.SlmBaseMaterialHPlEntity;

/**
 * 接箍产线能力
 * 
 * @author peng
 * 
 */
public class FrmBaseMaterialHPl extends DBCommon {
	public CoreReturnObject getBaseMaterialH(String inMaterialNo,
			String outMaterialNo) {
		Object[] params = new Object[] { inMaterialNo, outMaterialNo };
		return this.getDataBySqlID("FrmBaseMaterialHPl.getBaseMaterialH",
				params);
	}

	public CoreReturnObject getBaseMaterialHPl(String inMaterialNo,
			String outMaterialNo) {
		Object[] params = new Object[] { inMaterialNo, outMaterialNo };
		return this.getDataBySqlID("FrmBaseMaterialHPl.getBaseMaterialHPl",
				params);
	}

	/**
	 * 修改
	 * 
	 * @param jsonStrList
	 * @return
	 */
	public CoreReturnObject updateBaseMaterialHPl(ArrayList<String> jsonStrList) {
		ArrayList<String> sqlList = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmBaseMaterialHPl.updateBaseMaterialHPl");
		for (String jsonStr : jsonStrList) {
			JSONObject obj = JSONObject.fromObject(jsonStr);
			SlmBaseMaterialHPlEntity entity = (SlmBaseMaterialHPlEntity) JSONObject
					.toBean(obj, SlmBaseMaterialHPlEntity.class);
			if (entity.getDisableFl().equals("True")) {
				entity.setDisableFl("1");
			} else {
				entity.setDisableFl("0");
			}
			sql = EntityTool.fillSqlByEntity(sql, entity);
			sqlList.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqlList);
	}

	/**
	 * 修改产线
	 * 
	 * @throws ParseException
	 */
	public CoreReturnObject updateBasePlineJgM(ArrayList<String> jsonStrList,
			String proPlanId, String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs=null;
			String sql = CoreXmlSqlParsersFactory
					.getSql("FrmBaseMaterialHPl.updateBaseMaterialHPl");
			for (String jsonStr : jsonStrList) {
				JSONObject obj = JSONObject.fromObject(jsonStr);
				SlmBaseMaterialHPlEntity entity = (SlmBaseMaterialHPlEntity) JSONObject
						.toBean(obj, SlmBaseMaterialHPlEntity.class);
				if (entity.getDisableFl().equals("True")) {
					entity.setDisableFl("1");
				} else {
					entity.setDisableFl("0");
				}
				sql = EntityTool.fillSqlByEntity(sql, entity);
				ps = conn.prepareStatement(sql);
				ps.executeUpdate();
				ps.close();
				
				String isFinish="";
				int jgwtBlreq=0;
				String sqlQuery = CoreXmlSqlParsersFactory
						.getSql("FrmBaseMaterialHPl.QueryPlineJgM");
				ps = conn.prepareStatement(sqlQuery);
				ps.setObject(1, proPlanId);
				rs=ps.executeQuery();
				if(rs.next()){
					isFinish=rs.getString("IS_FINISH");
					jgwtBlreq=rs.getInt("JGWT_BLREQ");
				}
				rs.close();
				ps.close();
				if(isFinish.equals("完成配料")){
					cro.setV_errMsg("接箍订单已完成配料,不能修改产线!");
					return cro;
				}
				if(jgwtBlreq!=0){
					cro.setV_errMsg("接箍订单已开始配料,不能修改产线!");
					return cro;
				}
				String sqlgXJgM = CoreXmlSqlParsersFactory
						.getSql("FrmBaseMaterialHPl.updateBasePlineJgM");
				ps = conn.prepareStatement(sqlgXJgM);
				ps.setObject(1, entity.getPlineCode());
				ps.setObject(2, entity.getPlineName());
				ps.setObject(3, userName);
				ps.setObject(4, proPlanId);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("修改成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}
