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

public class FrmInListRead extends DBCommon{

	/***
	 * 查询库存
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param departId
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parm,String startTim,String endTim,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.doQueryInList");
		if(!startTim.equals(""))
		{
			sql += " AND T1.INSTOCK_TIME>=TO_DATE('" + startTim
			+ "','YYYY-MM-DD HH24:MI:SS') AND T1.INSTOCK_TIME<= TO_DATE('"
			+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(0).equals(""))
		{
			sql += " AND t1.JUDGE_STOVE_NO = '"+parm.get(0)+"'";
		}
		if(!parm.get(1).equals(""))
		{
			sql += " AND T1.PRO_ORDER_NO LIKE '"+parm.get(1)+"%'";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND T2.STORAGE_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 doQueryInListRead(ArrayList<String> parm,String startTim,String endTim,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.doQueryInListRead");
		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')";
		}
		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 T.STORAGE_NO = '"+parm.get(2)+"'";
		}
		sql += " AND T.BELONG_CODE LIKE '"+departId+"%'";
		sql += " group by t.LOT_NO,t.PRO_ORDER_NO,t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO,t.INSTOCK_DOC,t.MATERIAL_NO,t.MATERIAL_NAME,t.PRODUCCODE,t.PRODUCNAME,t.STD_CODE,t.STD_NAME,t.STEELCODE,t.STEELNAME,t.STD_STYLE,t.STD_STYLE_DESC,t.SPEC_CODE,t.SPEC_NAME,t.JUDGE_STD_CODE,t.JUDGE_STD_NAME,t.GRADECODE,t.GRADENAME,t.BAL_YEAR_MONTH,t.ORD_LN_PK,t.ORD_LN_DLY_PK,";
		sql += " t.MODEL_DESC,t.ACT_DIMATER,t.ACT_HEIGHT,t.ACT_LEN_MIN,t.ACT_LEN_MAX,t.BELONG_CODE,t.BELONG_NAME,s.STORAGE_NAME,t.MEMO,t.CREATE_NAME,t.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,t.MODEL_CODE,t.PRODUCT_FLAG,t.ZB_RCDID,t.PLINE_CODE,t.PLINE_NAME,d.SALE_AREA_DESC,d.create_name,t.mat_status";
		return this.getData(sql);
	}
	/***
	 * 入库实绩进行红冲(所属权)
	 * @return
	 */
	public CoreReturnObject doPipeRedDased(String ydmInListEntity,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 inListReadDasSql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.doPipeRedDased");
            
            MatZcMEntity1 ydmZcInListTity = EntityTool.getEntityByJSONStr(ydmInListEntity, MatZcMEntity1.class);
            //判断支数是否超出入库支数
            sql = CoreXmlSqlParsersFactory.getSql("FrmInListRead.SelectInListCount");
            sql = EntityTool.fillSqlByEntity(sql, ydmZcInListTity);
            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(ydmZcInListTity.getActCount()) > actCount)
            {
            	cro.setV_errMsg("红冲支数已超出入库支数, 不允许进行红冲!");
            	return cro;
            }
            inListReadDasSql = EntityTool.fillSqlByEntity(inListReadDasSql, ydmZcInListTity);
            PreparedStatement inListReadDasPsta = connection.prepareStatement(inListReadDasSql);
            inListReadDasPsta.setString(1, matList.get(1));
            inListReadDasPsta.setString(2, matList.get(0));
            inListReadDasPsta.setString(3, depart);
            inListReadDasPsta.setString(4, matList.get(3));
            inListReadDasPsta.setString(5, matList.get(4));
            inListReadDasPsta.setString(6, matList.get(0));
            inListReadDasPsta.setString(7, matList.get(2));
            inListReadDasPsta.setString(8, systime);
            inListReadDasPsta.setString(9, systime);
            inListReadDasPsta.setString(10, matList.get(5));
            inListReadDasPsta.setString(11, matList.get(6));
            inListReadDasPsta.setString(12, bathYear);
            inListReadDasPsta.setString(13, systime);
            inListReadDasPsta.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 cancelDoPipeRedDased(String ydmInListEntity,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("FrmInListRead.cancelDoPipeRedDased");
            
            YdmZcInlistEntity ydmZcEntity = EntityTool.getEntityByJSONStr(ydmInListEntity, YdmZcInlistEntity.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;
            }
            ydmZcEntity.setCreateName(list.get(1));
            ydmZcEntity.setCreateTime(systime);
            ydmZcEntity.setBalYearMonth(bathYear);
            ydmZcEntity.setManagementNo(list.get(2));
            ydmZcEntity.setManagementName(list.get(3));
            ydmZcEntity.setDepartmentCode(list.get(4));
            ydmZcEntity.setDepartmentDesc(list.get(5));
            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 ydmZcInListTity,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("FrmInListRead.adjustBalYearMoth");
            PreparedStatement updateInListBalYearMothPsta = connection.prepareStatement(updateInListBalYearMothSql);
            YdmZcInlistEntity ydmEntity = EntityTool.getEntityByJSONStr(ydmZcInListTity, YdmZcInlistEntity.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.getInstockDoc());
            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;
	}
}
