package com.steering.pss.ydm.Report;

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

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.entity.MatZcMEntity1;

public class FrmRunTimeRptZg extends DBCommon{

	/**
     * 获取期初重量
     * @param strWhere
     * @return
     */
    public CoreReturnObject getOriginalLast(String time,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getOriginalLast").replace("#condtions#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.STORAGE_NO", arr);
    	sql += " AND T.BELONG_CODE NOT IN('100101','100102','100103','002001001')";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql,new Object[]{time});
    }
    
    /**
     * 获取期初重量(所属权)
     * @param strWhere
     * @return
     */
    public CoreReturnObject getOriginalLastB(String time,String strWhere,String belogCode)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getOriginalLast").replace("#condtions#", strWhere);
    	sql += " AND t.BELONG_CODE LIKE '"+belogCode+"%'";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql,new Object[]{time});
    }
    
    /***
     * 本期入库量
     * @param strOriginal
     * @param strDateFrom
     * @param strDateTo
     * @param strWhere
     * @return
     */
    public CoreReturnObject getThisIn(String strOriginal,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getThisIn").replace("#condtions#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.STORAGE_NO", arr);
    	sql += " AND T.BELONG_CODE NOT IN('100101','100102','100103','002001001')";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }
    
    
    public CoreReturnObject getThisInB(String strOriginal,String strWhere,String belogCode)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getThisInB").replace("#condtions#", strWhere);
    	sql += " AND t.BELONG_CODE LIKE '"+belogCode+"%'";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }
    
    /***
     * 本期出库量
     * @param strOriginal
     * @param strDateFrom
     * @param strDateTo
     * @param strWhere
     * @return
     */
    public CoreReturnObject getThisOut(String strOriginal,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getThisOut").replace("#condtions#", strWhere);
    	sql = sql.replace("#condtions1#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.STORAGE_NO", arr);
    	sql += " AND T.BELONG_CODE NOT IN('100101','100102','100103','002001001')";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }
    
    
    public CoreReturnObject getThisOutB(String strOriginal,String strWhere,String belogCode)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getThisOutB").replace("#condtions#", strWhere);
    	sql = sql.replace("#condtions1#", strWhere);
    	sql += " AND t.BELONG_CODE LIKE '"+belogCode+"%'";
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }
    
    /***
     * 结转期初数据(仓库)(还需整改)
     * @param list
     * @return
     */
    public CoreReturnObject insertInital(ArrayList<ArrayList<String>> list,String[] arr,String bathYear)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
 	    SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyyMMdd");
         String systime = dateFormat.format(now);
         String ss = "";
         try {
             connection.setAutoCommit(false); // 取消自动提交事务
             String insertInitalsql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.insertInital_");
             PreparedStatement insertInitalpsta = connection.prepareStatement(insertInitalsql);
             String str = CoreConverter.toSqlConditionString("T.STORAGE_NO", arr);
             String insertInOutListSql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.insertInOutHistory");//转移入库出库历史表
             insertInOutListSql = insertInOutListSql.replace("#CONDITIONS#", str);
             PreparedStatement insertInOutpsta = connection.prepareStatement(insertInOutListSql);
             for(int i=0;i<list.size();i++)
             {
             	ArrayList<String> parm = list.get(i);
             	ss = parm.get(0);
				insertInitalpsta.setString(1, parm.get(0));
				insertInitalpsta.setString(2, parm.get(1));
				insertInitalpsta.setString(3, parm.get(2));
				insertInitalpsta.setString(4, parm.get(0));
				insertInitalpsta.setString(5, parm.get(1));
				insertInitalpsta.setString(6, parm.get(2));
				insertInitalpsta.setString(7, parm.get(0));
				insertInitalpsta.setString(8, parm.get(1));
				insertInitalpsta.setString(9, parm.get(2));
				
				
				insertInitalpsta.setString(10, parm.get(0));
				insertInitalpsta.setString(11, parm.get(1));
				insertInitalpsta.setString(12, parm.get(2));
				insertInitalpsta.setString(13, parm.get(3));
				insertInitalpsta.setString(14, parm.get(4));
				insertInitalpsta.setString(15, parm.get(6));
				insertInitalpsta.setString(16, systime);
				insertInitalpsta.setString(17, parm.get(5));
				insertInitalpsta.setString(18, "1");
				insertInitalpsta.addBatch();

				
             }
             insertInOutpsta.setString(1, bathYear);
             insertInOutpsta.addBatch();
             insertInOutpsta.executeBatch();
             insertInitalpsta.executeBatch();
             connection.commit();
             cro.setV_errMsg("期初数据结转成功!");
         }
         catch (Exception e) {
             System.out.println(e.getMessage() + ss);
             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 departId
     * @param bathYear
     * @return
     */
    public CoreReturnObject insertInitalBelog(ArrayList<ArrayList<String>> parm,String departId,String bathYear)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        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);
         int count = 0;
         String ss = "";
         try {
             connection.setAutoCommit(false); // 取消自动提交事务
             PreparedStatement insertInitalpsta = null;
             String insertInitalsql = "";
             
             MatZcMEntity1 matEntity = new MatZcMEntity1();
             String insertInOutListSql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.insertInOutHistoryB");//转移入库出库历史表
             PreparedStatement insertInOutpsta = null;
             for(int i = 0;i<parm.size();i++)
             {
				ArrayList<String> matList = parm.get(i);
				sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.SelectMat");//查询是否存在库存
				psta = connection.prepareStatement(sql);
				psta.setString(1, matList.get(0));
				psta.setString(2, matList.get(1));
				psta.setString(3, matList.get(2));
				psta.setString(4, departId);
				rs = psta.executeQuery();
				if(rs.next())
				{
					count = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(count>0)
				{
					sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.SelectInitalBelog");// 查询基本信息
					psta = connection.prepareStatement(sql);
					psta.setString(1, matList.get(0));
					psta.setString(2, matList.get(1));
					psta.setString(3, matList.get(2));
					psta.setString(4, departId);
					rs = psta.executeQuery();
					matEntity = EntityTool.getEntityByRs(rs, MatZcMEntity1.class);
					rs.close();
					psta.close();
					matEntity.setJudgeStoveNo(matList.get(0));
					matEntity.setBatchNo(matList.get(1));
					matEntity.setBatchGroudNo(matList.get(2));
					matEntity.setActCount(matList.get(3));
					matEntity.setActWeight(matList.get(4));
					matEntity.setBalYearMonth(matList.get(5));
					matEntity.setCreateName(matList.get(6));
					matEntity.setInstockTime(systime);
					insertInitalsql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.insertInitalBelog");
					insertInitalsql = EntityTool.fillSqlByEntity(insertInitalsql,
							matEntity);

					insertInitalpsta = connection.prepareStatement(insertInitalsql);
					insertInitalpsta.executeUpdate();
				}
//				insertInOutListSql = EntityTool.fillSqlByEntity(
//						insertInOutListSql, matEntity);
//				insertInOutpsta = connection
//						.prepareStatement(insertInOutListSql);
//				insertInOutpsta.executeUpdate();
             }
             
             connection.commit();
             cro.setV_errMsg("期初数据结转成功!");
         }
         catch (Exception e) {
             System.out.println(e.getMessage() + ss);
             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 list
     * @return
     */
    public CoreReturnObject getIntalJZ(String bathYear,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getIntalJZ");
    	sql += CoreConverter.toSqlConditionString("T.STORAGE_NO", arr);
    	
    	return this.getData(sql, new Object[]{bathYear});
    }
    
    /***
     * 查询结转数据是否存在(所属权)
     * @param bathYear
     * @param departId
     * @return
     */
    public CoreReturnObject getIntalJZB(String bathYear,String departId)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZg.getIntalJZ");
    	sql += " AND T.BELONG_CODE LIKE '"+departId+"%'";
    	return this.getData(sql, new Object[]{bathYear});
    }
}
