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

public class FrmLoadMaterialOut extends DBCommon {

	public CoreReturnObject doQueryMatData(ArrayList<String> parm,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmLoadMaterialOut.doQueryMatData");
		if(!parm.get(0).equals(""))
		{
			sql += " AND T1.LOT_NO = '"+parm.get(0)+"'";
		}
		if(!parm.get(1).equals(""))
		{
			sql += " AND T1.JUDGE_STOVE_NO = '"+parm.get(1)+"'";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND T1.PRO_ORDER_NO = '"+parm.get(2)+"'";
		}
		sql += "  AND T1.BELONG_CODE LIKE '"+ departId +"%'";
		sql += " GROUP BY T1.LOT_NO,T1.PRO_ORDER_NO,T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.ORD_LN_PK,T1.ORD_PK,T1.ORD_LN_DLY_PK,T1.MAT_STATUS,T1.MATERIAL_NO,T1.MATERIAL_NAME,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.LEN_NO,T1.LEN_DESC,T1.LEN_UNIT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,T1.SOURE_CODE,T1.SOURE_NAME,T1.MAT_ORIGIN_CODE,T1.MAT_ORIGIN_NAME,T1.ACT_LEN,";
		sql += " T2.STORAGE_NO,S.STORAGE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME";
		return this.getData(sql);
	}
	/***
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryOutList(ArrayList<String> parm,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmLoadMaterialOut.doQueryOutList");
		if(!parm.get(0).equals(""))
		{
			sql += " AND T.LOT_NO = '"+parm.get(0)+"'";
		}
		if(!parm.get(1).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parm.get(1)+"'";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND T.PRO_ORDER_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);
	}
	/***
	 * 投料出库
	 * @return
	 */
	public CoreReturnObject loadMaterial(String matZcTity,String departId,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 loadOutSql = CoreXmlSqlParsersFactory.getSql("FrmLoadMaterialOut.loadMaterial");
            
            MatZcMEntity1 matZcEntity = EntityTool.getEntityByJSONStr(matZcTity, MatZcMEntity1.class);
            
           //判断支数是否超出库存支数
            sql = CoreXmlSqlParsersFactory.getSql("FrmLoadMaterialOut.SelectMatListCount");
            sql = EntityTool.fillSqlByEntity(sql, matZcEntity);
            psta = connection.prepareStatement(sql);
            psta.setString(1, departId);
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = Integer.parseInt(rs.getString("ACT_COUNT"));
            }
            rs.close();
            psta.close();
            if(Integer.parseInt(matZcEntity.getActCount()) > actCount)
            {
            	cro.setV_errMsg("红冲支数已超出出库支数, 不允许进行红冲!");
            	return cro;
            }
            loadOutSql = EntityTool.fillSqlByEntity(loadOutSql, matZcEntity);
            PreparedStatement loadOutPsta = connection.prepareStatement(loadOutSql);
            loadOutPsta.setString(1, matList.get(1));
            loadOutPsta.setString(2, matList.get(0));
            loadOutPsta.setString(3, departId);
            loadOutPsta.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;
	}
	
	/***
	 * 撤销
	 * @param ydmOutListTity
	 * @param dePartId
	 * @param list
	 * @return
	 */
	public CoreReturnObject cancelLoadMaterial(String ydmOutListTity,String dePartId,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 cancelLoadOutSql = CoreXmlSqlParsersFactory.getSql("FrmLoadMaterialOut.cancelLoadMaterial");
            
            YdmZcOutlistEntity ydmZcEntity = EntityTool.getEntityByJSONStr(ydmOutListTity, YdmZcOutlistEntity.class);
            sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.SelectInital");//查询是否存在当月
            psta = connection.prepareStatement(sql);
            psta.setString(1, list.get(0));
            psta.setString(2, dePartId);
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = Integer.parseInt(rs.getString("ACOUNT"));
            }
            rs.close();
            psta.close();
            if(actCount >0)
            {
            	cro.setV_errMsg("此数据已进行结转,不允许进行撤销!");
            	return cro;
            }
            ydmZcEntity.setCreateName(list.get(1));
            ydmZcEntity.setManagementNo(list.get(2));
            ydmZcEntity.setManagementName(list.get(3));
            ydmZcEntity.setDepartmentCode(list.get(4));
            ydmZcEntity.setDepartmentDesc(list.get(5));
            cancelLoadOutSql = EntityTool.fillSqlByEntity(cancelLoadOutSql, ydmZcEntity);
            PreparedStatement cancelLoadOutPsta = connection.prepareStatement(cancelLoadOutSql);
            cancelLoadOutPsta.setString(1, dePartId);
            cancelLoadOutPsta.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;
	}
}
