package com.steering.pss.qcm;

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

import net.sf.json.JSONObject;
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.comm.tools.SqlBuilder;
import com.steering.pss.qcm.model.ComMscRBlSpecEntity;
import com.steering.pss.qcm.model.GenericEntity;

public class MscRBlSpec extends DBCommon{
	
	/**
	 * 获取冶金规范备料规格
	 * @param strJSON 实体类
	 * @return obj
	 */
	public CoreReturnObject get(String strJSON){
		JSONObject obj = JSONObject.fromObject(strJSON);
		ComMscRBlSpecEntity mscRBlSpec = (ComMscRBlSpecEntity)JSONObject.toBean(obj, ComMscRBlSpecEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.getMscRBlSpec");
		sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
		
		return this.getData(sql);
	}
	
	/**
	 * 获取冶金规范备料规格主表
	 * @param strJSON 实体类
	 * @return obj
	 */
	public CoreReturnObject getBlSpecHead(String strJSON){
		JSONObject obj = JSONObject.fromObject(strJSON);
		ComMscRBlSpecEntity mscRBlSpec = (ComMscRBlSpecEntity)JSONObject.toBean(obj, ComMscRBlSpecEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.getBlSpecHead");
		sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
		
		return this.getData(sql);
	}
	
	/**
	 * 设置冶金规范备料规格
	 * @param listJSON 实体类
	 * @return obj
	 */
	public CoreReturnObject set(ArrayList<String> listJSON){
		int maxRowIndex = 0;
		ArrayList<String> sqllist = new ArrayList<String>();
		for(int i = 0; i < listJSON.size(); i++){
			String strJSON = listJSON.get(i);
			JSONObject obj = JSONObject.fromObject(strJSON);
			ComMscRBlSpecEntity mscRBlSpec = (ComMscRBlSpecEntity)JSONObject.toBean(obj, ComMscRBlSpecEntity.class);
			if(i == 0){
				Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
				String sqlGetMaxRowIndex = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.getMaxMscRBlSpecRowIndex");
				sqlGetMaxRowIndex = EntityTool.fillSqlByEntity(sqlGetMaxRowIndex, mscRBlSpec);
				ArrayList<GenericEntity> list = EntityTool.getEntitysBySql(sqlGetMaxRowIndex, con, GenericEntity.class);
				maxRowIndex = Integer.parseInt(list.get(0).getMaxValue());
			}
			String sqlMscRBlSpecCnt = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.getMscRBlSpecCnt");
			sqlMscRBlSpecCnt = EntityTool.fillSqlByEntity(sqlMscRBlSpecCnt, mscRBlSpec);
			Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			ArrayList<GenericEntity> listCnt = EntityTool.getEntitysBySql(sqlMscRBlSpecCnt, con, GenericEntity.class);
			String sql = "";
			if(listCnt.get(0).getCount().equals("0")){
				sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.insertMscRBlSpec");
				maxRowIndex++;
				mscRBlSpec.setRowIndex(String.valueOf(maxRowIndex));
				sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
			}
			else{
				sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.updateMscRBlSpec");
				sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
			}
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 更新冶金规范备料码规格状态
	 * @param listJSON 实体类
	 * @param strAction 操作类型
	 * @return obj
	 */
	public CoreReturnObject updateState(ArrayList<String> listJSON, String strAction){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(int i = 0; i < listJSON.size(); i++){
			String strJSON = listJSON.get(i);
			JSONObject obj = JSONObject.fromObject(strJSON);
			ComMscRBlSpecEntity mscRBlSpec = (ComMscRBlSpecEntity)JSONObject.toBean(obj, ComMscRBlSpecEntity.class);
			String sql = "";
			if(strAction.equals("Delete")){
				sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.delete");
				sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
			}
			else{
				sql = CoreXmlSqlParsersFactory.getSql("MscRBlSpec.resume");
				sql = EntityTool.fillSqlByEntity(sql, mscRBlSpec);
			}
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 获取分组中的备料
	 * @param groupNum
	 * @param msc
	 * @return
	 */
	public CoreReturnObject getGroupBl(String groupNum, String msc){
		SqlBuilder b = new SqlBuilder("MscRBlSpec.getGroupBl");
		b.Fill(new String[]{ groupNum, msc });
		return this.getData(b.toString());
	}
	
	/**
	 * 获取分组中的备料适用信息
	 * @param msc
	 * @param groupNum
	 * @param pscBl
	 * @return
	 */
	public CoreReturnObject getGroupBlInfo(String msc, String groupNum, String pscBl){
		SqlBuilder b = new SqlBuilder("MscRBlSpec.getGroupBlInfo");
		b.Fill(new String[]{ msc, groupNum });
		
		return this.getData(b.toString());
	}
}
