package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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.ydm.entity.YdmZcInlistEntity;
import com.steering.pss.ydm.entity.YdmZcOutlistEntity;

public class FrmOutListRead extends DBCommon{

	/***
	 * 查询出库记录
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param departId
	 * @return
	 */
	public CoreReturnObject doQueryOutList(ArrayList<String> parm,String startTim,String endTim,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.doQueryOutList");
		if(!startTim.equals(""))
		{
			sql += " AND T.OUTSTOCK_TIME>=TO_DATE('" + startTim
			+ "','YYYY-MM-DD HH24:MI:SS') AND T.OUTSTOCK_TIME<= TO_DATE('"
			+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(0).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parm.get(0)+"'";
		}
		if(!parm.get(1).equals(""))
		{
			sql += " AND T.PRO_ORDER_NO LIKE '"+parm.get(1)+"%'";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND S.STORAGE_NO = '"+parm.get(2)+"'";
		}
		sql += "  AND T.BELONG_CODE LIKE '"+ departId +"%'";
		sql += "  GROUP BY T.OUTSTOCK_PLAN_NO,T.OUTSTOCK_DOC,T.OUTSTOCK_TYPE_CODE,T.OUTSTOCK_TYPE_NAME,T.LOT_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.MATERIAL_NO,T.MATERIAL_NAME,T.PRODUCCODE,T.PRODUCNAME,T.STD_CODE,T.STD_NAME,T.GRADECODE,T.GRADENAME,";
		sql += "  T.STEELCODE,T.STEELNAME,T.STD_STYLE,T.STD_STYLE_DESC,T.SPEC_CODE,T.SPEC_NAME,T.MODEL_CODE,T.MODEL_DESC,T.PRO_ORDER_NO,T.PLINE_CODE,T.PLINE_NAME,T.ACT_DIMATER,T.ACT_HEIGHT,T.ACT_LEN,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.BELONG_CODE,T.BELONG_NAME,";
		sql += "  T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.PRODUCT_FLAG,T.MAT_STATUS,T.JUDGE_STD_NAME,T.JUDGE_STD_CODE,T.STORAGE_NO,S.STORAGE_NAME";
		return this.getData(sql);
	}
	/***
	 * 查询出库红冲实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param departId
	 * @return
	 */
	public CoreReturnObject doQueryOutListRead(ArrayList<String> parm,String startTim,String endTim,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.doQueryOutListRead");
		if(!startTim.equals(""))
		{
			sql += " AND T.OUTSTOCK_TIME>=TO_DATE('" + startTim
			+ "','YYYY-MM-DD HH24:MI:SS') AND T.OUTSTOCK_TIME<= TO_DATE('"
			+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(0).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parm.get(0)+"'";
		}
		if(!parm.get(1).equals(""))
		{
			sql += " AND T.PRO_ORDER_NO LIKE '"+parm.get(1)+"%'";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND S.STORAGE_NO = '"+parm.get(2)+"'";
		}
		sql += "  AND T.BELONG_CODE LIKE '"+ departId +"%'";
		sql += "  GROUP BY T.OUTSTOCK_PLAN_NO,T.OUTSTOCK_DOC,T.OUTSTOCK_TYPE_CODE,T.OUTSTOCK_TYPE_NAME,T.LOT_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.MATERIAL_NO,T.MATERIAL_NAME,T.PRODUCCODE,T.PRODUCNAME,T.STD_CODE,T.STD_NAME,T.GRADECODE,T.GRADENAME,";
		sql += "  T.STEELCODE,T.STEELNAME,T.STD_STYLE,T.STD_STYLE_DESC,T.SPEC_CODE,T.SPEC_NAME,T.MODEL_CODE,T.MODEL_DESC,T.PRO_ORDER_NO,T.PLINE_CODE,T.PLINE_NAME,T.ACT_DIMATER,T.ACT_HEIGHT,T.ACT_LEN,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.BELONG_CODE,T.BELONG_NAME,T.BAL_YEAR_MONTH,";
		sql += "  T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.PRODUCT_FLAG,T.MAT_STATUS,T.JUDGE_STD_NAME,T.JUDGE_STD_CODE,T.STORAGE_NO,S.STORAGE_NAME";
		return this.getData(sql);
	}
	
	/***
	 * 出库红冲
	 * @param ydmInListEntity
	 * @param depart
	 * @param matList
	 * @return
	 */
	public CoreReturnObject doPipeRedDased(String ydmOutListEntity,String depart,ArrayList<String> matList)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        SimpleDateFormat df = new SimpleDateFormat(
        "yyyyMM");
        String bathYear = df.format(now);
        String sql = "";
        String inListDoc = "";
        int actCount = 0;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String outListSql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.doPipeRedDased"); 
            
            YdmZcOutlistEntity ydmOutListTity = EntityTool.getEntityByJSONStr(ydmOutListEntity, YdmZcOutlistEntity.class);
          //判断支数是否超出库支数
            sql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.SelectInListCount");
            sql = EntityTool.fillSqlByEntity(sql, ydmOutListTity);
            psta = connection.prepareStatement(sql);
            psta.setString(1, depart);
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = Integer.parseInt(rs.getString("ACT_COUNT"));
            }
            rs.close();
            psta.close();
            if(Integer.parseInt(ydmOutListTity.getActCount()) > actCount)
            {
            	cro.setV_errMsg("红冲支数已超出出库支数, 不允许进行红冲!");
            	return cro;
            }
            outListSql = EntityTool.fillSqlByEntity(outListSql, ydmOutListTity);
            PreparedStatement outListPsta = connection.prepareStatement(outListSql);
            outListPsta.setString(1, matList.get(1));
            outListPsta.setString(2, matList.get(0));
            outListPsta.setString(3, depart);
            outListPsta.setString(4, matList.get(2));
            
            outListPsta.setString(5, matList.get(3));
            outListPsta.setString(6, matList.get(4));
            outListPsta.setString(7, matList.get(5));
            outListPsta.setString(8, matList.get(6));
            outListPsta.setString(9, matList.get(2));
            outListPsta.executeUpdate();
            
            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;
	}
	
	
	public CoreReturnObject cancelDoPipeRedDased(String ydmOutEntity,String depart,ArrayList<String> list)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        SimpleDateFormat df = new SimpleDateFormat(
        "yyyyMM");
        String bathYear = df.format(now);
        String sql = "";
        String inListDoc = "";
        int actCount = 0;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String cancelPipeReadDasSql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.cancelDoPipeRedDased");
            
            YdmZcOutlistEntity ydmZcEntity = EntityTool.getEntityByJSONStr(ydmOutEntity, YdmZcOutlistEntity.class);
            sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.SelectInital");//查询是否存在当月
            psta = connection.prepareStatement(sql);
            psta.setString(1, list.get(0));
            psta.setString(2, depart);
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = Integer.parseInt(rs.getString("ACOUNT"));
            }
            rs.close();
            psta.close();
            if(actCount >0)
            {
            	cro.setV_errMsg("此数据已进行结转,不允许进行撤销!");
            	return cro;
            }
            cancelPipeReadDasSql = EntityTool.fillSqlByEntity(cancelPipeReadDasSql, ydmZcEntity);
            PreparedStatement cancelPipeReadDasPsta = connection.prepareStatement(cancelPipeReadDasSql);
            cancelPipeReadDasPsta.setString(1, depart);
            cancelPipeReadDasPsta.executeUpdate();
            
            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;
	}
	
	/***
	 * 调整结转年月
	 * @return
	 */
	public CoreReturnObject adjustBalYearMoth(String ydmZcOutListTity,String depart,ArrayList<String> list)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        String sql = "";
        int actCount = 0;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateInListBalYearMothSql = CoreXmlSqlParsersFactory.getSql("FrmOutListRead.adjustBalYearMoth");
            PreparedStatement updateInListBalYearMothPsta = connection.prepareStatement(updateInListBalYearMothSql);
            YdmZcOutlistEntity ydmEntity = EntityTool.getEntityByJSONStr(ydmZcOutListTity, YdmZcOutlistEntity.class);
            sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.SelectInital");//查询是否存在当月
            psta = connection.prepareStatement(sql);
            psta.setString(1, list.get(0));
            psta.setString(2, depart);
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = Integer.parseInt(rs.getString("ACOUNT"));
            }
            rs.close();
            psta.close();
            if(actCount >0)
            {
            	cro.setV_errMsg("此数据已存在调整月下月的期初,不允许进行结转年月的调整!");
            	return cro;
            }
            updateInListBalYearMothPsta.setString(1, list.get(1));
            updateInListBalYearMothPsta.setString(2, list.get(2));
            updateInListBalYearMothPsta.setString(3, ydmEntity.getOutstockDoc());
            updateInListBalYearMothPsta.executeUpdate();
            
            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;
	}
}
