package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

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;

public class FrmInListAdjustYear extends DBCommon{

	/***
	 * 查询入库实绩
	 * @param list
	 * @param departId
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parm,String departId)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmInListAdjustYear.doQueryInList");
		if(!parm.get(0).equals(""))
		{
			sql += " AND T.INSTOCK_TIME>=TO_DATE('" + parm.get(0)
			+ "','YYYY-MM-DD HH24:MI:SS') AND T.INSTOCK_TIME<= TO_DATE('"
			+ parm.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND t.JUDGE_STOVE_NO = '"+parm.get(2)+"'";
		}
		if(!parm.get(3).equals(""))
		{
			sql += " AND T.PRO_ORDER_NO LIKE '"+parm.get(3)+"%'";
		}
		if(!parm.get(4).equals(""))
		{
			sql += " AND T.LOT_NO = '"+parm.get(4)+"'";
		}
		sql += " AND T.BELONG_CODE LIKE '"+departId+"%'";
		sql += " group by t.INSTOCK_TYPE_CODE,t.INSTOCK_TYPE_NAME, t.LOT_NO,t.PRO_ORDER_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.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,t.INSTOCK_DOC,";
		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.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,t.MODEL_CODE,t.PRODUCT_FLAG,t.ZB_RCDID,t.PLINE_CODE,t.PLINE_NAME,t.mat_status,t.STORAGE_NO";
		
		return this.getData(sql);
	}
	
	/***
	 * 调整结转年月
	 * @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("FrmInListAdjustYea.adjustBalYearMoth");
            
            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;
            }
            ydmEntity.setInstockTime(list.get(2));
            ydmEntity.setBalYearMonth(list.get(1));
            updateInListBalYearMothSql = EntityTool.fillSqlByEntity(updateInListBalYearMothSql, ydmEntity);
            PreparedStatement updateInListBalYearMothPsta = connection.prepareStatement(updateInListBalYearMothSql);
            updateInListBalYearMothPsta.setString(1, depart);
            updateInListBalYearMothPsta.setString(2, depart);
            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;
	}
}
