package com.steering.ydm.bc.report;

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.MatBcMEntity;

public class FrmBcStoreSummary extends DBCommon{

	/***
	 * 查询(仓库权限)
	 * @param list
	 * @param arr
	 * @return
	 */
	public CoreReturnObject queryData(ArrayList<String> list,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStoreSummary.queryData").replace("#columns#", list.get(0));
		String str = CoreConverter.toSqlConditionString("T2.STORAGE_NO", arr);
		sql = sql.replace("#conditions#", list.get(1));
		sql = sql.replace("#conditions1#", str + list.get(2));
		return this.getData(sql);	
	}
	
	/***
	 * 查询(所属权)
	 * @param list
	 * @param arr
	 * @return
	 */
	public CoreReturnObject queryDataBelong(ArrayList<String> list,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStoreSummary.queryDataBelong").replace("#columns#", list.get(0));
		sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE", arr);
		sql = sql.replace("#conditions#", list.get(1));
		sql = sql + list.get(2);
		return this.getData(sql);	
	}
	
	/***
	 * 查询(所有)
	 * @param list
	 * @param arr
	 * @return
	 */
	public CoreReturnObject queryDataAll(ArrayList<String> list)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStoreSummary.queryDataAll").replace("#columns#", list.get(0));
		sql = sql.replace("#conditions#", list.get(1));
		sql = sql.replace("#conditions1#", list.get(2));
		return this.getData(sql);	
	}
	
	/**
	 * 查询
	 * @param list
	 * @param arr
	 * @return
	 */
	public CoreReturnObject queryDataMemo(ArrayList<String> list,String[]arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStoreSummary.queryDataMemo");
		String sql1 = CoreConverter.toSqlConditionString("T2.STORAGE_NO", arr);
		if (!"".equals(list.get(0))) {
			sql1 += " AND T1.STOVE_NO = '" + list.get(0) + "'";
		}
		if (!"".equals(list.get(1))) {
			sql1 += " AND T1.JUDGE_STOVE_NO = '" + list.get(1) + "'";
		}
		if (!"".equals(list.get(2))) {
			sql1 += " AND T1.FLAG_STOVE_NO_ZG = '" + list.get(2) + "'";
		}
		if (!"".equals(list.get(3))) {
			sql1 += " AND T1.PRO_ORDER_NO LIKE '%" + list.get(3) + "%'";
		}
		sql = sql.replaceAll("#something#", sql1);
		return this.getData(sql);	
	}
	
	/**
	 * 修改备注
	 * @param parm
	 * @return
	 */
	public CoreReturnObject updateMemo(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateTargetSql = CoreXmlSqlParsersFactory
					.getSql("FrmStoreSummary.updateMemo");// 修改去向
			PreparedStatement updateTargetPsta = connection
					.prepareStatement(updateTargetSql);
			for (String str : parm) {
				MatBcMEntity matBcTity = EntityTool.getEntityByJSONStr(str,
						MatBcMEntity.class);
				updateTargetPsta.setString(1, matBcTity.getJudgeStoveNo());
				updateTargetPsta.setString(2, matBcTity.getBatchNo());
				updateTargetPsta.setString(3, matBcTity.getBatchGroudNo());
				updateTargetPsta.setString(4, matBcTity.getActCount());
				updateTargetPsta.setString(5, matBcTity.getMemo());
				updateTargetPsta.setString(6, matBcTity.getLocationNo());
				updateTargetPsta.setString(7, matBcTity.getMemo2());
				updateTargetPsta.setString(8, matBcTity.getProOrderNo());
				updateTargetPsta.setString(9, matBcTity.getMatStatus());
				updateTargetPsta.setString(10, matBcTity.getPlineName());
				updateTargetPsta.setString(11, matBcTity.getStaus());
				updateTargetPsta.addBatch();
			}
			updateTargetPsta.executeBatch();
			cro.setV_errMsg("修改成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
