package com.steering.pss.plnsaleord.planStove;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.GpOptimize.model.PlnOrderZgSEntity;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.jggxpln.model.CouplingMatButtressEntity;
import com.steering.pss.plnsaleord.planStove.entity.CouplingMatMEntity;
import com.steering.pss.plnsaleord.planStove.entity.MatBcTrackEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.plnsaleord.stove.corePlanFurnProM;
import com.steering.pss.plnsaleord.stove.entity.MatBcMEntityPln;
import com.steering.pss.plnsaleord.stove.entity.PlnOrderDbkSEntity;
import com.steering.pss.plnsaleord.stove.entity.PlnZyDbkCEntity;
import com.steering.pss.plnsaleord.stove.entity.PlnZyDbkMEntity;

/**
 *接箍库存导入
 * 
 * @author Administrator
 * 
 */
public class FrmImportSetMatJg extends DBCommon {

	/**
	 * 查询管坯库存信息
	 * 
	 * @return
	 */
	public CoreReturnObject queryMaterialMat(String all) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmImportSetMatJg.queryMaterial");
		return this.getData(sql);
	}

	/**
	 * 导入库存
	 * 
	 * @throws ParseException
	 */
	public CoreReturnObject saveOrderSetlog(ArrayList<String> jslist,
			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;
			List<CouplingMatMEntity> list = PlnHelper.jsonsToEntityList(
					CouplingMatMEntity.class, jslist);
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);

			// 循环库存信息
			for (CouplingMatMEntity zgSentity : list) {
				CouplingMatMEntity matM = new CouplingMatMEntity();
				matM.setBatchNo(zgSentity.getBatchNo());
				matM.setJudgeStoveNo(zgSentity.getJudgeStoveNo());
//				matM.setJgOrderno(zgSentity.getJgOrderno());
				matM.setMemo(zgSentity.getMemo());
				matM.setMinD(zgSentity.getMinD());
				matM.setMinH(zgSentity.getMinH());
				matM.setOutdiameter(zgSentity.getOutdiameter());
				matM.setWallthick(zgSentity.getWallthick());
//				matM.setFinalUserDesc(zgSentity.getFinalUserDesc());
				matM.setFinTime(zgSentity.getFinTime());
				matM.setSpecJgDesc(zgSentity.getSpecJgDesc());
				matM.setModelDesc(zgSentity.getModelDesc());
				matM.setSteelname(zgSentity.getSteelname());
				matM.setGradename(zgSentity.getGradename());
				matM.setProNum(zgSentity.getProNum());
				matM.setProWt(zgSentity.getProWt());
				matM.setSingleweight(zgSentity.getSingleweight());
				matM.setSoureName(zgSentity.getSoureName());
				if (zgSentity.getCoatType().equals("")) {
					zgSentity.getCoatType();
				} else {
					if (zgSentity.getCoatType().equals("镀铜")) {
						matM.setCoatType("1");
						matM.setDemandBonderizing("40840202");
					} else if (zgSentity.getCoatType().equals("磷化")) {
						matM.setCoatType("2");
						matM.setDemandBonderizing("40840201");
					} else if (zgSentity.getCoatType().equals("钝化")) {
						matM.setCoatType("4");
					}
				}
				matM.setCreateTime(dateTime);
				matM.setCreateName(userName);
				matM.setJudgeStdName(zgSentity.getJudgeStdName());
				// 获取最大材料号
				String matNoMaX = "";
				String sql = CoreXmlSqlParsersFactory
						.getSql("FrmImportSetMatJg.getMaxMatNo");
				ps = conn.prepareStatement(sql);
				ps.setObject(1, matM.getJudgeStoveNo());
				ps.setObject(2, matM.getJudgeStoveNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					matNoMaX = rs.getString("MAT_NO");
				}
				rs.close();
				ps.close();
				matM.setMatNo(matNoMaX);
				//查询接箍规格描述代码
				sql = "SELECT T.SPEC_CODE FROM Com_Base_Spec T WHERE T.VALIDFLAG='1'AND T.SPEC_NAME=?";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, matM.getSpecJgDesc());
				rs = ps.executeQuery();
				if (rs.next()) {
					matM.setSpecJg(rs.getString("SPEC_CODE"));
				}
				rs.close();
				ps.close();
				//查询扣型描述代码
				sql = "SELECT T.MODEL_CODE FROM Com_Base_Model T WHERE T.VALIDFLAG='1'AND T.MODEL_DESC=?";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, matM.getModelDesc());
				rs = ps.executeQuery();
				if (rs.next()) {
					matM.setModelCode(rs.getString("MODEL_CODE"));
				}
				rs.close();
				ps.close();
				//查询钢级描述代码
				sql = "SELECT T.STEELCODE FROM COM_BASE_STEEL T WHERE T.VALIDFLAG='1'AND T.STEELNAME=?";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, matM.getSteelname());
				rs = ps.executeQuery();
				if (rs.next()) {
					matM.setSteelcode(rs.getString("STEELCODE"));
				}
				rs.close();
				ps.close();
				//查询钢种描述代码
				sql = "SELECT T.GRADECODE FROM Com_Base_Grade T WHERE T.VALIDFLAG='1'AND T.GRADENAME=?";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, matM.getGradename());
				rs = ps.executeQuery();
				if (rs.next()) {
					matM.setGradecode(rs.getString("GRADECODE"));
				}
				rs.close();
				ps.close();
				// 新增接箍库存信息
				String sqlAddMat = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmImportSetMatJg.getAddMatJgM"), matM);
				ps = conn.prepareStatement(sqlAddMat);
				ps.executeUpdate();
				ps.close();
				
				CouplingMatButtressEntity matBut=new CouplingMatButtressEntity();
				matBut.setMatNo(matNoMaX);
				matBut.setMemo(zgSentity.getMemo());
				matBut.setPieces(zgSentity.getProNum());
				matBut.setWeight(zgSentity.getProWt());
				matBut.setCreateTime(dateTime);
				matBut.setCreateName(userName);
				
				// 新增接箍库存位置信息
				String sqlAddMatBut = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmImportSetMatJg.getAddMatButt"), matBut);
				ps = conn.prepareStatement(sqlAddMatBut);
				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;
	}

	/**
	 * 删除导入库存
	 * 
	 * @throws ParseException
	 */
	public CoreReturnObject deleteOrderStatus(ArrayList<ArrayList<String>> jslist)
			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;
//			List<CouplingMatMEntity> list = PlnHelper.jsonsToEntityList(
//					CouplingMatMEntity.class, jslist);
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);

			// 循环库存信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String matNo=list.get(0);
				// 删除接箍库存
				String deleteMat ="DELETE COUPLING_MAT_M WHERE MAT_NO = ?";
				ps = conn.prepareStatement(deleteMat);
				ps.setObject(1, matNo);
				ps.executeUpdate();
				ps.close();
				
				// 删除接箍位置信息
				String deleteMatBut ="DELETE Coupling_Mat_Buttress WHERE MAT_NO = ?";
				ps = conn.prepareStatement(deleteMatBut);
				ps.setObject(1, matNo);
				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;
	}

}
