package com.steering.pss.ydm.Stuffmanage;

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.Stuffmanage.Entity.MatGpMEntity;

public class FrmStuffInventoryOut extends DBCommon{

	/***
	 * 查询库存信息
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryMat(ArrayList<String> parm,String startTim,String endTim,String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffInventoryOut.doQueryMat");
		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')";
		}
		sql += CoreConverter.toSqlConditionString("S.STORAGE_NO", arr);
		sql += " group by T.MEMO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.CAST_NO,T.CCMID,T.SAMPLE_FLAG,T.CUTTING_FLAG,T.CUTTING_NUM,T.MATERIAL_NO,T.MATERIAL_NAME,";
		sql += " T.PRODUCCODE,T.PRODUCNAME,T.JUDGE_GRADECODE,T.JUDGE_GRADENAME,T.JUDGE_STD_CODE,T.JUDGE_STD_NAME,T.PRO_ORDER_NO,T.PRO_ORDER_ID,T.PLINE_CODE,T.PLINE_NAME,";
		sql += " T.ACT_DIMATER,T.ACT_LEN,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.ACT_FIX_NUM,T.ACT_FIX_LEN,T.PRODUCT_FLAG,T.MAT_STATUS,S.STORAGE_NO,T.BELONG_CODE,T.BELONG_NAME";
		return this.getData(sql, parm.toArray());
	}
	/***
	 * 查询盘亏实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryStock(ArrayList<String> parm,String startTim,String endTim,String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffInventoryOut.doQueryStock");
		if(!startTim.equals(""))
		{
			sql += " AND STOCKTAKING_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND STOCKTAKING_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += CoreConverter.toSqlConditionString("STORAGE_NO", arr);
		return this.getData(sql, parm.toArray());
	}
	
	/***
	 * 盘亏
	 * @param parm
	 * @param strBound
	 * @param strBand
	 * @param userName
	 * @param mangeNo
	 * @param mangeMent
	 * @param departId
	 * @param departMent
	 * @return
	 */
	public CoreReturnObject doAddOut(ArrayList<String> parm,String strBound,String strBand,String userName,String mangeNo,String mangeMent,String departId,String departMent)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String time = df.format(now);
		PreparedStatement psta = null;
		String sql = "";
		ResultSet rs = null;
		String acount = "";
		String sumWgt = "";
		String sumLen = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertOutSql = CoreXmlSqlParsersFactory.getSql("FrmStuffInventoryOut.doAddOut");
			PreparedStatement insertOutPsta = connection.prepareStatement(insertOutSql);
			for(String str:parm)
			{
				MatGpMEntity matGptity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffInventoryOut.selectMatStaus");
				psta = connection.prepareStatement(sql);
				psta = connection.prepareStatement(sql);
				psta.setString(1, matGptity.getStoveNo());
				psta.setString(2, matGptity.getJudgeStoveNo());
				psta.setString(3, matGptity.getBatchNo());
				psta.setString(4, matGptity.getGroudNo());
				psta.setString(5, matGptity.getProductFlag());
				psta.setString(6, matGptity.getMatStatus());
				psta.setString(7, matGptity.getStorageNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount= rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)<Integer.parseInt(matGptity.getActCount()))
				{
					cro.setV_errMsg("盘亏支数不能大于库存支数,请刷新页面!");
					return cro;
				}
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffInventoryOut.selectMatWgt");
				psta = connection.prepareStatement(sql);
				psta = connection.prepareStatement(sql);
				psta.setString(1, matGptity.getStoveNo());
				psta.setString(2, matGptity.getJudgeStoveNo());
				psta.setString(3, matGptity.getBatchNo());
				psta.setString(4, matGptity.getGroudNo());
				psta.setString(5, matGptity.getProductFlag());
				psta.setString(6, matGptity.getMatStatus());
				psta.setString(7, matGptity.getStorageNo());
				psta.setInt(8, Math.abs(Integer.parseInt(matGptity.getOutActCount())));
				rs = psta.executeQuery();
				if(rs.next())
				{
					sumWgt= rs.getString("WEIGHT");
					sumLen = rs.getString("LEN");
				}
				rs.close();
				psta.close();
				insertOutPsta.setString(1, strBound);
				insertOutPsta.setString(2, strBound);
				insertOutPsta.setString(3, strBand);
				insertOutPsta.setString(4, strBand);
				insertOutPsta.setString(5, matGptity.getStoveNo());
				insertOutPsta.setString(6, matGptity.getJudgeStoveNo());
				insertOutPsta.setString(7, matGptity.getBatchNo());
				insertOutPsta.setString(8, matGptity.getGroudNo());
				insertOutPsta.setString(9, matGptity.getProductFlag());
				insertOutPsta.setString(10, matGptity.getMatStatus());
				insertOutPsta.setString(11, matGptity.getStorageNo());
				insertOutPsta.setInt(12, Math.abs(Integer.parseInt(matGptity.getOutActCount()))+1);
				insertOutPsta.setString(13, departId);
				insertOutPsta.setString(14, departMent);
				insertOutPsta.setString(15, mangeNo);
				insertOutPsta.setString(16, mangeMent);
				insertOutPsta.setString(17, userName);
				insertOutPsta.setString(18, strBound);
				insertOutPsta.setString(19, matGptity.getStocktakingRsnName());
				insertOutPsta.setString(20, sumLen);
				insertOutPsta.setString(21, matGptity.getOutActCount());
				insertOutPsta.setString(22, sumWgt);
				
				insertOutPsta.setString(23, strBand);
				insertOutPsta.setString(24, departId);
				insertOutPsta.setString(25, departMent);
				insertOutPsta.setString(26, mangeNo);
				insertOutPsta.setString(27, mangeMent);
				insertOutPsta.setString(28, userName);
				insertOutPsta.addBatch();
			}
			insertOutPsta.executeBatch();
            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;
	}
}
