package com.steering.pss.ydm.Stuffmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

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.pss.ydm.Stuffmanage.Entity.YmdGpOutlistEntity;
import com.steering.pss.ydm.Stuffmanage.Entity.MatGpMEntity;

public class FrmStuffMoveOut extends DBCommon{

	/***
	 * 查询库存实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQuery(ArrayList<String> parm ,String startTim,String endTim,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.doQuery");
		if(!startTim.equals(""))
		{
			sql += " AND T.INSTOCK_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND T.INSTOCK_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		//sql += CoreConverter.toSqlConditionString("S.STORAGE_NO", arr);
		sql += " group by T.MEMO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.CAST_NO,T.CCMID,T.SAMPLE_FLAG,T.CUTTING_FLAG,T.CUTTING_NUM,T.MATERIAL_NO,T.MATERIAL_NAME,T.GROUD_NO,";
		sql += " T.PRODUCCODE,T.PRODUCNAME,T.JUDGE_GRADECODE,T.JUDGE_GRADENAME,T.JUDGE_STD_CODE,T.JUDGE_STD_NAME,T.PRO_ORDER_NO,T.PRO_ORDER_ID,T.PLINE_CODE,T.PLINE_NAME,";
		sql += " T.ACT_DIMATER,T.ACT_LEN,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.ACT_FIX_NUM,T.ACT_FIX_LEN,T.PRODUCT_FLAG,T.MAT_STATUS,S.STORAGE_NO,T.BELONG_CODE,T.BELONG_NAME";
		return this.getData(sql, parm.toArray());
	}

	/***
	 * 查询出库实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryOutList(ArrayList<String> parm ,String startTim,String endTim,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.doQueryOutList");
		if(!startTim.equals(""))
		{
			sql += " AND OUTSTOCK_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		sql +=  CoreConverter.toSqlConditionString("STORAGE_NO", arr);
		sql += " group by OUTSTOCK_PLAN_NO,OUTSTOCK_DOC,OUTSTOCK_TYPE_CODE,OUTSTOCK_TYPE_NAME, VEHICLE_NO,PRODUCT_FLAG,MAT_STATUS,STOVE_NO,FLAG_STOVE_NO,JUDGE_STOVE_NO,";
		sql += " BATCH_NO,CAST_NO,CCMID,SAMPLE_FLAG,CUTTING_FLAG,CUTTING_NUM,MATERIAL_NO,MATERIAL_NAME,PRODUCCODE,PRODUCNAME,JUDGE_GRADECODE,JUDGE_GRADENAME,JUDGE_STD_CODE,";
		sql += " JUDGE_STD_NAME,ACT_DIMATER,ACT_LEN,ACT_LEN_MIN,ACT_LEN_MAX,ACT_FIX_NUM,ACT_FIX_LEN,ACT_COUNT,ACT_WEIGHT,PLINE_CODE,PLINE_NAME,PRO_ORDER_NO,PRO_ORDER_ID,";
		sql += " BELONG_CODE,BELONG_NAME,STORAGE_NO,TAR_STORAGE_NO,TAR_REC_FLAG,MEMO,CREATE_NAME";
		return this.getData(sql, parm.toArray());
	}
	
	
	/***
	 * 新增倒库实绩
	 * @param parm
	 * @param storeNo
	 * @return
	 */
	public CoreReturnObject doAddData(ArrayList<String> parm,String storeNo,String outSort,String userName,String carNo,String mangeNo,String mangeMent,String departId,String departMent,String cardNo)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertOutListsql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.doAddData");//插入出库表
			PreparedStatement insertOutListpsta = connection.prepareStatement(insertOutListsql);
			for (String str : parm) {
				MatGpMEntity matGpList = EntityTool
						.getEntityByJSONStr(str, MatGpMEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.selectMatData");
				psta = connection.prepareStatement(sql);
				psta.setString(1, matGpList.getStoveNo());
				psta.setString(2, matGpList.getJudgeStoveNo());
				psta.setString(3, matGpList.getBatchNo());
				psta.setString(4, matGpList.getGroudNo());
				psta.setString(5, matGpList.getProductFlag());
				psta.setString(6, matGpList.getMatStatus());
				psta.setString(7, matGpList.getStorageNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)< Math.abs(Integer.parseInt(matGpList.getActCount())))
				{
					cro.setV_errMsg("炉号【"+matGpList.getJudgeStoveNo()+"】的库存信息已发生改变,请刷新页面!");
					return cro;
				}
				insertOutListpsta.setString(1, outSort);
				insertOutListpsta.setString(2, outSort);
				insertOutListpsta.setString(3, matGpList.getStoveNo());
				insertOutListpsta.setString(4, matGpList.getJudgeStoveNo());
				insertOutListpsta.setString(5, matGpList.getBatchNo());
				insertOutListpsta.setString(6, matGpList.getGroudNo());
				insertOutListpsta.setString(7, matGpList.getProductFlag());
				insertOutListpsta.setString(8, matGpList.getMatStatus());
				insertOutListpsta.setString(9, matGpList.getStorageNo());
				insertOutListpsta.setInt(10, Math.abs(Integer.parseInt(matGpList.getActCount()))+1);
				insertOutListpsta.setString(11, outSort);
				insertOutListpsta.setString(12, carNo);
				insertOutListpsta.setString(13, departId);
				insertOutListpsta.setString(14, departMent);
				insertOutListpsta.setString(15, mangeNo);
				insertOutListpsta.setString(16, mangeMent);
				insertOutListpsta.setString(17, storeNo);
				insertOutListpsta.setString(18, storeNo+"100101");
				insertOutListpsta.setString(19, userName);
				insertOutListpsta.setString(20, cardNo);
				insertOutListpsta.addBatch();
			}
			insertOutListpsta.executeBatch();
			connection.commit();
			cro.setV_errMsg("倒库成功!");
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	/***
	 * 撤销倒库
	 * @param parm
	 * @return
	 */
	public CoreReturnObject deleteData(ArrayList<String> parm,String userName,String mangeNo,String mangeMent,String departId,String departMent)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deleteOutListSql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.deleteOutListData");
			PreparedStatement deleteOutListpsta = connection.prepareStatement(deleteOutListSql);
			for(String str:parm)
			{
				YmdGpOutlistEntity ydmOutlist = EntityTool.getEntityByJSONStr(str, YmdGpOutlistEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveOut.SelectOutListData");
				psta = connection.prepareStatement(sql);
				psta.setString(1, ydmOutlist.getStoveNo());
				psta.setString(2, ydmOutlist.getJudgeStoveNo());
				psta.setString(3, ydmOutlist.getBatchNo());
				psta.setString(4, ydmOutlist.getGroudNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)>0)
				{
					cro.setV_errMsg("此炉【"+ydmOutlist.getJudgeStoveNo()+"】倒库信息已被接收,不允许撤销!");
					return cro;
				}
				deleteOutListpsta.setString(1, ydmOutlist.getStoveNo());
				deleteOutListpsta.setString(2, ydmOutlist.getJudgeStoveNo());
				deleteOutListpsta.setString(3, ydmOutlist.getBatchNo());
				deleteOutListpsta.setString(4, ydmOutlist.getGroudNo());
				deleteOutListpsta.setString(5, departId);
				deleteOutListpsta.setString(6, departMent);
				deleteOutListpsta.setString(7, mangeNo);
				deleteOutListpsta.setString(8, mangeMent);
				deleteOutListpsta.setString(9, userName);
				deleteOutListpsta.addBatch();
			}
			deleteOutListpsta.executeBatch();
			connection.commit();
			cro.setV_errMsg("撤销成功!");
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
   /***
    * 生成倒库单号
    * @param outStort
    * @return
    */
	public CoreReturnObject doQueryDoc(String outStort)
	{
	     return this.getDataBySqlID("FrmStuffMoveOut.doQueryDoc", new Object[]{outStort});
	}
}
