package com.steering.mes.mcp.common;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.coup.CoupLoadingService;
import com.steering.mes.mcp.entity.*;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgWorkbillEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class CoupMatCollarShar extends DBCommon {

	/***
	 * 查询材料号
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static String doAddMatNo(String judNo, Connection con)
			throws SQLException {
		String matNo = "";
		PreparedStatement ps = null;
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.queryMaxMatNo");// 查询最大材料号
		ps = con.prepareStatement(sqlloading);
		ps.setObject(1, judNo);
		ps.setObject(2, judNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			matNo = rs.getString("MAT_NO");
		}
		rs.close();
		ps.close();
		return matNo;
	}

	/**
	 * 查询交易表信息
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static <T> T getPlanDeal(Connection con, String deal, Class<T> cls)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupMatCollarShar.QueryDeal");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, deal);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 领用拧接废品回库/撤销回库
	 * 
	 * @return
	 */
	public static String addMatFaill(ArrayList<String> list1, Connection con,
			String flag) throws Exception {
		String mess = "";
		PreparedStatement ps = null;
		ResultSet rs = null;

		String dealOrde = list1.get(0);
		String name = list1.get(1);
		String bc = list1.get(2);
		String bz = list1.get(3);
		int num = Integer.parseInt(list1.get(4));

		CouplingMatDealEntity deal = getPlanDeal(con, dealOrde,
				CouplingMatDealEntity.class);
		if (deal == null) {
			mess = "未查询领用信息,请确认!";
			return mess;
		}
		String matNo = "";
		int proNum = 0;
		// 查询接箍库存是否存在同工单，同炉批的废品记录
		String sql = "SELECT T.MAT_NO,T.SINGLEWEIGHT,nvl(T.PRO_NUM,0) PRO_NUM  FROM COUPLING_MAT_M T WHERE T.QCM_STAUS='22' AND T.WO_ID=? AND T.JUDGE_STOVE_NO=? AND T.BATCH_NO=?";
		ps = con.prepareStatement(sql);
		ps.setObject(1, deal.getWoId());
		ps.setObject(2, deal.getJudgeStoveNo());
		ps.setObject(3, deal.getBatchNo());
		rs = ps.executeQuery();
		if (rs.next()) {
			matNo = rs.getString("MAT_NO");
			proNum = rs.getInt("PRO_NUM");
		}
		rs.close();
		ps.close();
		if (flag.equals("0")) {
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(con,
					deal.getWoId(), PlnZyJgWorkbillEntity.class);
			if (work == null) {
				mess = "未查询到工单信息,请确认!";
				return mess;
			}

			if (matNo.equals("")) {
				// 新增接箍库存废品记录
				String storageNo = "XJG1";
				String storageAtte = "";
				String storageAtteName = "";
				String storageType = "";
				String storageTypeName = "";
				String managNo = "";
				String managName = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.queryStorageType");
				ps = con.prepareStatement(sqlloading);
				ps.setObject(1, work.getPlineCode());
				rs = ps.executeQuery();
				if (rs.next()) {
					storageAtte = rs.getString("STORAGE_ATTR");
					storageAtteName = rs.getString("STORAGE_ATTR_NAME");
					storageType = rs.getString("STORAGE_TYPE_NO");
					storageTypeName = rs.getString("STORAGE_TYPE_NAME");
					managNo = rs.getString("MANAGEMENT_NO");
					managName = rs.getString("MANAGEMENT_NAME");
				}
				rs.close();
				ps.close();

				String belongCode = "";
				String belongName = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.selectBelong");
				ps = con.prepareStatement(sqlloading);
				ps.setObject(1, work.getPlineCode());
				rs = ps.executeQuery();
				if (rs.next()) {
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				ps.close();
				CouplingMatMEntity matM = new CouplingMatMEntity();
				// 获取材料号
				matNo = doAddMatNo(work.getWoId(), con);
				// 新增材料表
				matM.setMatNo(matNo);
				matM.setResultNo("");
				matM.setWoId(deal.getWoId());
				matM.setPlineCode(work.getPlineCode());
				matM.setPlineName(work.getPlineName());
				matM.setJudgeStoveNo(work.getJudgeStoveNo());
				matM.setBatchNo(work.getBatchNo());
				matM.setBlWlId(work.getBlWlId());
				matM.setBlWlDesc(work.getBlWlDesc());
				matM.setMinD(work.getMinD());
				matM.setMinH(work.getMinH());
				matM.setCodeJg(work.getCodeJg());
				matM.setGradecode(work.getGradecode());
				matM.setGradename(work.getGradename());
				matM.setSteelcode(work.getSteelcode());
				matM.setSteelname(work.getSteelname());
				matM.setModelCode(work.getModelCode());
				matM.setModelDesc(work.getModelDesc());
				matM.setOutdiameter(work.getDiameterJg());
				matM.setWallthick(work.getHeightJg());
				matM.setSpecJg(work.getSpecJg());
				matM.setSpecJgDesc(work.getSpecJgDesc());
				matM.setStyleJg(work.getStyleJg());
				matM.setDiameter(work.getDiameter());
				matM.setCheckJVal(work.getCheckJVal());
				matM.setAimlength(work.getLengthJg());
				matM.setSingleweight(work.getSingleCutNun());
				matM.setMonitordemand("");
				matM.setIfMonitorJg(work.getIfMonitorJg());
				matM.setPrdcrNo(work.getPrdcrNo());
				matM.setProducer("");
				matM.setProNum(String.valueOf(num));
				matM.setProWt(String.valueOf(num
						* (Double.parseDouble(work.getSingleweight()) / 1000)));
				matM.setColOperator(name);
				matM.setProShift(bc);
				matM.setProGroup(bz);
				matM.setCreateName(name);
				matM.setMemo("");
				matM.setDemandBlast(work.getDemandBlast());
				matM.setDemandBonderizing(work.getDemandBonderizing());
				matM.setDemandCopper(work.getDemandCopper());
				matM.setDemandDetection(work.getDemandDetection());
				if (work.getDemandDetection().equals("40840201")) {
					matM.setCoatType("2");
				} else if (work.getDemandDetection().equals("40840202")) {
					matM.setCoatType("1");
				} else {
					matM.setCoatType("0");
				}
				matM.setBelongCode(belongCode);
				matM.setBelongName(belongName);
				matM.setCodeJgBl(work.getCodeJgBl());
				matM.setCraftFileNo(work.getCraftFileNo());
				matM.setCraftSeq(work.getCraftSeq());
				matM.setFinalUserDesc("");
				matM.setMatOriginCode("");
				matM.setMatOriginName("");
				matM.setSoureCode("");
				matM.setSoureName("");
				matM.setJudgeStdName("");
				matM.setQcmStaus("22");
				String strMatM = CoreXmlSqlParsersFactory
						.getSql("CoupMatCollarShar.AddMatJgFaill");// 新增接箍材料表
				strMatM = com.steering.common.EntityTool.fillSqlByEntity(strMatM, matM);
				ps = con.prepareStatement(strMatM);
				ps.executeUpdate();
				ps.close();

				CouplingMatButtressEntity matBut = new CouplingMatButtressEntity();
				// 新增材料从表
				matBut.setMatNo(matNo);
				matBut.setMatNoXh("0001");
				matBut.setProductFlag("801402");
				matBut.setMatStatus("80150201");
				matBut.setMatOriginCode("");
				matBut.setMatOriginName("");
				matBut.setSoureCode("");
				matBut.setSoureName("");
				matBut.setPieces(String.valueOf(num));
				matBut.setWeight(String.valueOf(num
						* (Double.parseDouble(work.getSingleweight()) / 1000)));

				matBut.setCustAlphaNo("");
				matBut.setOrderSpecialDesc("");
				matBut.setAlphaCode("");
				matBut.setKepNo("");
				matBut.setStorageNo(storageNo);
				matBut.setLocationNo(storageNo + "10101");
				matBut.setStorageAttr(storageAtte);
				matBut.setStorageAttrName(storageAtteName);
				matBut.setStorageTypeNo(storageType);
				matBut.setStorageTypeName(storageTypeName);
				matBut.setManagementNo(managNo);
				matBut.setManagementName(managName);
				matBut.setMemo("");
				matBut.setCreateName(name);
				String strMatButters = CoreXmlSqlParsersFactory
						.getSql("CoupMatCollarShar.AddMatButJgFaill");// 新增位置表
				strMatButters = com.steering.common.EntityTool.fillSqlByEntity(strMatButters, matBut);
				ps = con.prepareStatement(strMatButters);
				ps.executeUpdate();
				ps.close();
			} else {
				// 修改接箍库存废品记录

				// 修改材料表支数
				String strMat = " UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)+TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
				ps = con.prepareStatement(strMat);
				ps.setObject(1, String.valueOf(num));
				if (work.getSingleweight().equals("")) {
					ps.setObject(2, 0);
				} else {
					ps.setObject(2,
							num * Double.parseDouble(work.getSingleweight())
									/ 1000);// 重量
				}
				ps.setObject(3, matNo);
				ps.executeUpdate();
				ps.close();
				// 修改位置表支数
				String strMatBut = " UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)+TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
				ps = con.prepareStatement(strMatBut);
				ps.setObject(1, String.valueOf(num));
				if (work.getSingleweight().equals("")) {
					ps.setObject(2, 0);
				} else {
					ps.setObject(2,
							num * Double.parseDouble(work.getSingleweight())
									/ 1000);// 重量
				}
				ps.setObject(3, matNo);
				ps.executeUpdate();
				ps.close();

			}
		} else {
			// 修改库存废品信息
			if (proNum <= num) {
				//删除废品材料
				/*String deleteMat = " DELETE COUPLING_MAT_M WHERE MAT_NO = ?";
				ps = con.prepareStatement(deleteMat);
				ps.setObject(1, deal.getMatNo());
				ps.executeUpdate();
				ps.close();
				// 删除废品位置
				String deleteMatBut = " DELETE COUPLING_MAT_BUTTRESS WHERE MAT_NO = ?";
				ps = con.prepareStatement(deleteMatBut);
				ps.setObject(1, deal.getMatNo());
				ps.executeUpdate();
				ps.close();*/
			} else {
				// 修改材料表支数
				String strMat = " UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)-TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)-TO_NUMBER(?) WHERE T.MAT_NO=?";
				ps = con.prepareStatement(strMat);
				ps.setObject(1, String.valueOf(num));
				if (deal.getSingleweight().equals("")) {
					ps.setObject(2, 0);
				} else {
					ps.setObject(2,
							num * Double.parseDouble(deal.getSingleweight())
									/ 1000);// 重量
				}
				ps.setObject(3, deal.getMatNo());
				ps.executeUpdate();
				ps.close();
				// 修改位置表支数
				String strMatBut = " UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)-TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)-TO_NUMBER(?) WHERE T.MAT_NO=?";
				ps = con.prepareStatement(strMatBut);
				ps.setObject(1, String.valueOf(num));
				if (deal.getSingleweight().equals("")) {
					ps.setObject(2, 0);
				} else {
					ps.setObject(2,
							num * Double.parseDouble(deal.getSingleweight())
									/ 1000);// 重量
				}
				ps.setObject(3, deal.getMatNo());
				ps.executeUpdate();
				ps.close();

				// 查询
			}
		}
		return mess;
	}
}
