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

public class FrmStuffMoveIn extends DBCommon{

	/***
	 * 查询出库实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryOutList(ArrayList<String> parm ,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.doQueryOutList");
		sql +=  CoreConverter.toSqlConditionString("TAR_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 startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject douQueryInList(ArrayList<String> parm ,String startTim,String endTim,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.douQueryInList");
		if(!startTim.equals(""))
		{
			sql += " AND IN_TIME_F >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND IN_TIME_F <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		sql +=  CoreConverter.toSqlConditionString("STORAGE_NO", arr);
		sql += " group by  GROUD_NO,MAT_ORIGIN_CODE,MAT_ORIGIN_NAME,SOURE_CODE,SOURE_NAME,MATERIAL_NO, MATERIAL_NAME,PRODUCCODE,PRODUCNAME,JUDGE_GRADECODE,JUDGE_GRADENAME,";
		sql += " JUDGE_STD_CODE,JUDGE_STD_NAME,ACT_DIMATER,ACT_HEIGHT,ACT_LEN,ACT_LEN_MIN,ACT_LEN_MAX,ACT_FIX_NUM,ACT_FIX_LEN,BELONG_CODE,BELONG_NAME,STORAGE_NO,MEMO,";
		sql += " CREATE_NAME,INSTOCK_DOC,VEHICLE_NO,PRODUCT_FLAG,MAT_STATUS,STOVE_NO,JUDGE_STOVE_NO,BATCH_NO,CAST_NO,CCMID,SAMPLE_FLAG,CUTTING_FLAG,CUTTING_NUM,PLINE_CODE,PLINE_NAME,PRO_ORDER_NO,PRO_ORDER_ID";
		return this.getData(sql, parm.toArray());
	}
	
	/***
	 * 收料
	 * @param parm
	 * @param inSort
	 * @param userName
	 * @param departId
	 * @param departMent
	 * @return
	 */
	public CoreReturnObject doAddInListData(ArrayList<String> parm,String inSort,String userName,String departId,String departMent,String depart,String departM)
	{
		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 insertInListSql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.doAddInListData");
        	PreparedStatement insertInListpsta = connection.prepareStatement(insertInListSql);
			for(String str:parm)
			{
				YmdGpOutlistEntity ymdOutlist = EntityTool.getEntityByJSONStr(str, YmdGpOutlistEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.selectOutListData");
				psta = connection.prepareStatement(sql);
				psta.setString(1, ymdOutlist.getOutstockDoc());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)>0)
				{
					cro.setV_errMsg("此炉【"+ymdOutlist.getJudgeStoveNo()+"】已进行过收料,不允许重复收料!");
					return cro;
				}
				insertInListpsta.setString(1, inSort);
				insertInListpsta.setString(2, inSort);
				insertInListpsta.setString(3, ymdOutlist.getOutstockDoc());
				insertInListpsta.setString(4, departId);
				insertInListpsta.setString(5, departMent);
				insertInListpsta.setString(6, depart);
				insertInListpsta.setString(7, departM);
				insertInListpsta.setString(8, userName);
				insertInListpsta.setString(9, departId);
				insertInListpsta.setString(10, departMent);
				insertInListpsta.setString(11, depart);
				insertInListpsta.setString(12, departM);
				insertInListpsta.setString(13, userName);
				insertInListpsta.setString(14, inSort);
				insertInListpsta.setString(15, departId);
				insertInListpsta.setString(16, departMent);
				insertInListpsta.setString(17, depart);
				insertInListpsta.setString(18, departM);
				insertInListpsta.addBatch();
			}
			insertInListpsta.executeBatch();
			cro.setV_errMsg("收料成功!");
			connection.commit();
        }
	   	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
	 * @param userName
	 * @param mangeNo
	 * @param mangeMent
	 * @param departId
	 * @param departMent
	 * @return
	 */
	public CoreReturnObject deleteData(ArrayList<String> parm,String userName,String mangeNo,String mangeMent)
	{
		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 deleteInListSql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.deleteInListData");
			PreparedStatement deleteInListpsta = connection.prepareStatement(deleteInListSql);
			for(String str:parm)
			{
				YmdGpInlistEntity ymdInList = EntityTool.getEntityByJSONStr(str, YmdGpInlistEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffMoveIn.selectInListData");
				psta = connection.prepareStatement(sql);
				psta.setString(1, ymdInList.getStoveNo());
				psta.setString(2, ymdInList.getJudgeStoveNo());
				psta.setString(3, ymdInList.getBatchNo());
				psta.setString(4, ymdInList.getGroudNo());
				psta.setString(5, ymdInList.getStorageNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(!acount.equals(ymdInList.getActCount()))
				{
					cro.setV_errMsg("物料已编提单或者已发运,不允许撤销!");
					return cro;
				}
				deleteInListpsta.setString(1, ymdInList.getInstockDoc());
				deleteInListpsta.addBatch();
			}
			deleteInListpsta.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;
	}
}
