package com.steering.pss.ydm.pipemanage;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.ydm.entity.QcmZgJugdeApplyEntity;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class FrmBcIqInList {

	/***
	 * 管材判定写入库表(产权转移)
	 * @return
	 * @throws SQLException 
	 */
	public void insertBcInList(Connection con, ArrayList<QcmZgJugdeApplyEntity> qcmEntitys,String userName) throws SQLException
	{
		Statement st = null;
		try{
			st = con.createStatement();
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			//SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
			String systime = dateFormat.format(now);
			//String bathYear = df.format(now);
			String inListDoc = getInListDoc(con, "800709",systime);// 入库单号
			for (QcmZgJugdeApplyEntity qcmBcApplyEntity : qcmEntitys) {
				qcmBcApplyEntity.setApplyName(userName);
				qcmBcApplyEntity.setApplyTime(systime);
				qcmBcApplyEntity.setInstockDoc(inListDoc);
				qcmBcApplyEntity.setBalYearMonth("120504".equals(qcmBcApplyEntity.getSupplyUnit())? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
				if(!qcmBcApplyEntity.getTarget().equals("A"))
				{
					String insertBcInistSql = CoreXmlSqlParsersFactory.getSql("FrmBcIqInList.insertBcIqInList");
					insertBcInistSql = EntityTool.fillSqlByEntity(insertBcInistSql, qcmBcApplyEntity);
					st.executeUpdate(insertBcInistSql);
				}
				
			}
		}finally{
			st.close();
		}
	}
	
	/***
	 * 删除产权转移入库数据
	 * @throws SQLException 
	 */
	public void cancelInsertBcInList(Connection con,ArrayList<QcmZgJugdeApplyEntity> qcmEntitys) throws SQLException
	{
		Statement st = null;
		try{
			st = con.createStatement();
			for(QcmZgJugdeApplyEntity qcmGpAppEntity : qcmEntitys)
			{
				String deleteInsertInListSql = CoreXmlSqlParsersFactory.getSql("FrmBcIqInList.cancelInsertBcInList");
				deleteInsertInListSql = EntityTool.fillSqlByEntity(deleteInsertInListSql, qcmGpAppEntity);
				st.executeUpdate(deleteInsertInListSql);
			}
		}finally{
			st.close();
		}
	}
	
	/***
	 * 获取入库单号
	 * 
	 * @param conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound,String systime)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmBcIqInList.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		psta.setString(2, systime);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}
	/***
	 * 产销成品判定成2时转移半成品表至历史表
	 * @param con
	 * @param qcmEntitys
	 * @param
	 * @throws SQLException 
	 */
	public void deleteMatBcA(Connection con, ArrayList<QcmZgJugdeApplyEntity> qcmEntitys) throws SQLException
	{
		Statement st = null;
		try{
			
			try {
				st = con.createStatement();
				for (QcmZgJugdeApplyEntity qcmBcApplyEntity : qcmEntitys) {
					String deleteBcInistSql = CoreXmlSqlParsersFactory.getSql("FrmBcIqInList.deleteMatBcM");
					deleteBcInistSql = EntityTool.fillSqlByEntity(deleteBcInistSql, qcmBcApplyEntity);
					st.executeUpdate(deleteBcInistSql);
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}finally{
			st.close();
		}
	}
	/***
	 * 撤销成品判定恢复半成品表
	 * @throws SQLException 
	 */
	public void insertMatBcA(Connection con, ArrayList<QcmZgJugdeApplyEntity> qcmEntitys) throws SQLException
	{
		Statement st = null;
		try{
			
			try {
				st = con.createStatement();
				for (QcmZgJugdeApplyEntity qcmBcApplyEntity : qcmEntitys) {
					String insertBcInistSql = CoreXmlSqlParsersFactory.getSql("FrmBcIqInList.insertMatBcM");
					insertBcInistSql = EntityTool.fillSqlByEntity(insertBcInistSql, qcmBcApplyEntity);
					st.executeUpdate(insertBcInistSql);
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}finally{
			st.close();
		}
	}
}
