package com.steering.pss.ydm.Report;

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.converter.CoreConverter;
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.MilPlanEntity;
import com.steering.pss.ydm.entity.YdmZcInitialZgEntity;

public class FrmRunTimeRptZgNew extends DBCommon{

	//期初
	public CoreReturnObject getOriginalLastB(String bathYear,String[] belogCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.getOriginalLastB");
    	sql += CoreConverter.toSqlConditionString("t.PLINE_CODE", belogCode);
    	sql += " group by t.JUDGE_STOVE_NO,t.PLINE_NAME";
    	return this.getData(sql,new Object[]{bathYear});
	}
	//入库
	public CoreReturnObject getThisInB(String bathYear,String[] belogCode)
    {
		String str = CoreConverter.toSqlConditionString("T1.PLINE_CODE", belogCode);
//		if(belogCode.equals("002001016"))
//		{
//			plineCode = " AND T1.PLINE_CODE IN('C008','C009')";
//		}else if(belogCode.equals("002001017"))
//		{
//			plineCode = " AND T1.PLINE_CODE = 'C010'";
//		}else if(belogCode.equals("002001018"))
//		{
//			plineCode = " AND T1.PLINE_CODE = 'C017'";
//		}
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.getThisInB").replace("#PLINE_CODE#", str);
    	return this.getData(sql, new Object[]{bathYear,bathYear,bathYear});
    }
	//出库
	public CoreReturnObject getThisOutB(String bathYear,String[] belogCode)
    {
		String str = CoreConverter.toSqlConditionString("T1.PLINE_CODE", belogCode);
//		if(belogCode.equals("002001016"))
//		{
//			plineCode = " AND T1.PLINE_CODE IN('C008','C009')";
//		}else if(belogCode.equals("002001017"))
//		{
//			plineCode = " AND T1.PLINE_CODE = 'C010'";
//		}else if(belogCode.equals("002001018"))
//		{
//			plineCode = " AND T1.PLINE_CODE = 'C017'";
//		}
    	//String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.getThisOutB").replace("#PLINE_CODE#", plineCode).replace("#BAL_YEAR_MONTH#", bathYear);
    	//return this.getData(sql, new Object[]{bathYear,bathYear,bathYear,bathYear});
		String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.getThisOutB").replace("#PLINE_CODE#", str);
		return this.getData(sql, new Object[]{bathYear,bathYear,bathYear,bathYear,bathYear,bathYear});
    }
	
	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 = "";
             String updateQcmStausSql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.UpdateQcmStatus");//对判定表当月判定记录封账
             PreparedStatement updateQcmStausPsta = connection.prepareStatement(updateQcmStausSql);
             MilPlanEntity milPlan = new MilPlanEntity();
             for(int i = 0;i<parm.size();i++)
             {
            	    ArrayList<String> matList = parm.get(i);
					sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.SelectInitalBelog");// 查询基本信息
					psta = connection.prepareStatement(sql);
					psta.setString(1, matList.get(0));
					rs = psta.executeQuery();
					milPlan = EntityTool.getEntityByRs(rs, MilPlanEntity.class);
					rs.close();
					psta.close();
                    YdmZcInitialZgEntity ydmZcEntity = new YdmZcInitialZgEntity();
                    
                    if(milPlan == null)
                    {
                    	sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.SelectInitalZg");// 查询期初基本信息
    					psta = connection.prepareStatement(sql);
    					psta.setString(1, matList.get(0));
    					rs = psta.executeQuery();
    					milPlan = EntityTool.getEntityByRs(rs, MilPlanEntity.class);
    					rs.close();
    					psta.close();
                    }
                    ydmZcEntity.setJudgeStoveNo(matList.get(0));
                    ydmZcEntity.setActCount(matList.get(1));
                    ydmZcEntity.setActWeight(matList.get(2));
                    ydmZcEntity.setInstockTime(systime);
                    ydmZcEntity.setBalYearMonth(matList.get(3));
                    ydmZcEntity.setCreateName(matList.get(4));
                    ydmZcEntity.setLotNo(milPlan.getLotNo().toString());
                    ydmZcEntity.setProduccode(milPlan.getVarietiesCode());
                    ydmZcEntity.setProducname(milPlan.getVarieties());
                    ydmZcEntity.setSteelcode(milPlan.getSteelcode());
                    ydmZcEntity.setSteelname(milPlan.getSteelname());
                    ydmZcEntity.setSpecCode(milPlan.getPipeSizeCode());
                    ydmZcEntity.setSpecName(milPlan.getPipeSize());
                    ydmZcEntity.setModelCode(milPlan.getBuckleTypeCode());
                    ydmZcEntity.setModelDesc(milPlan.getBuckleType());
                    ydmZcEntity.setStdCode(milPlan.getRollStandradCode());
                    ydmZcEntity.setStdName(milPlan.getRollStandrad());
                    ydmZcEntity.setStdStyle(milPlan.getStdStyle());
                    ydmZcEntity.setStdStyleDesc(milPlan.getStdStyleDesc());
                    ydmZcEntity.setProOrderNo(milPlan.getContractNo());
                    ydmZcEntity.setProOrderId(milPlan.getProPlanId());
                    ydmZcEntity.setGpDimater(milPlan.getPipeSection());
                    ydmZcEntity.setGradecode(milPlan.getGradecode());
                    ydmZcEntity.setGradename(milPlan.getGradename());
                    ydmZcEntity.setPlineCode(milPlan.getPlineCode());
                    ydmZcEntity.setTubeType(milPlan.getTubeType());
                    ydmZcEntity.setTubeSuorce(milPlan.getTubeSuorce());
                    ydmZcEntity.setPipeSection(milPlan.getPipeSection());
                    if(milPlan.getPlineCode().equals("C008"))
                    {
                    	ydmZcEntity.setPlineName("250机组");
                    	ydmZcEntity.setBelongCode("002001016003");
                    	ydmZcEntity.setBelongName("250机组");
                    }else if(milPlan.getPlineCode().equals("C009"))
                    {
                    	ydmZcEntity.setPlineName("258机组");
                    	ydmZcEntity.setBelongCode("002001016002");
                    	ydmZcEntity.setBelongName("258机组");
                    }else if(milPlan.getPlineCode().equals("C010"))
                    {
                    	ydmZcEntity.setPlineName("168机组");
                    	ydmZcEntity.setBelongCode("002001017003");
                    	ydmZcEntity.setBelongName("168机组");
                    }
                    else if(milPlan.getPlineCode().equals("C017"))
                    {
                    	ydmZcEntity.setPlineName("460机组");
                    	ydmZcEntity.setBelongCode("002001018003");
                    	ydmZcEntity.setBelongName("460机组");
                    }
                    else if(milPlan.getPlineCode().equals("C072"))
                    {
                    	ydmZcEntity.setPlineName("天淮508机组");
                    	ydmZcEntity.setBelongCode("002023018001");
                    	ydmZcEntity.setBelongName("天淮508机组");
                    }
                    
                    
					insertInitalsql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.insertInitalBelog");
					insertInitalsql = EntityTool.fillSqlByEntity(insertInitalsql,
							ydmZcEntity);
					insertInitalpsta = connection.prepareStatement(insertInitalsql);
					insertInitalpsta.executeUpdate();
					insertInitalpsta.close();
             }
             
             updateQcmStausPsta.setString(1, bathYear);
             updateQcmStausPsta.executeUpdate();
             updateQcmStausPsta.close();
             
             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 bathYear
	 * @param bathYear2
	 * @return
	 */
	public CoreReturnObject cancelInsertTal(String bathYear,String bathYear2)
    {
    	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);

         String ss = "";
         try {
             connection.setAutoCommit(false); // 取消自动提交事务
             PreparedStatement insertInitalpsta = null;
             String insertInitalsql = "";
             String updateQcmStausSql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.UpdateQcmStatusBack");//撤销对判定表当月判定记录封账
             PreparedStatement updateQcmStausPsta = connection.prepareStatement(updateQcmStausSql);
             
             String deleteQcmStausSql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptZgNew.deleteQcmStatus");//删除结转数据
             PreparedStatement deleteQcmStausPsta = connection.prepareStatement(deleteQcmStausSql);
             
             updateQcmStausPsta.setString(1, bathYear);
             updateQcmStausPsta.executeUpdate();
             updateQcmStausPsta.close();
             
             deleteQcmStausPsta.setString(1, bathYear2);
             deleteQcmStausPsta.executeUpdate();
             deleteQcmStausPsta.close();
             
             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;
    }
}
