package com.gx.example.itemapply.business.ebo;

import com.gx.example.itemapply.business.ebi.ApplyEbi;
import com.gx.example.itemapply.business.valueobject.ApplyChildModel;
import com.gx.example.itemapply.business.valueobject.ApplyMainModel;
import com.gx.webpage.framework.objectidservice.dao.factory.ObjectIdFactory;
import com.gx.webpage.framework.util.db.*;
import java.util.*;

import javax.sql.DataSource;
import java.sql.*;


/**
 * 项目立项请求的业务实现
 * @author Administrator
 *
 */
public class ApplyEbo implements ApplyEbi{
	/**
	 * 新增项目立项请求记录
	 * @param amm 项目申请主值对象，里面带子表值对象集合
	 * @return 新增成功，返回true
	 * @throws Exception
	 */
	public boolean addApply(ApplyMainModel amm)throws Exception{
		//只是演示，所以这里直接写sql进行处理了，没有再去分出数据处理层来，用了一些WebPage3中的工具类
		//实际上这里是逻辑层的具体实现，跟WebPage3已经没有关系了，可以用任意可用的技术来实现
		//如:Ejb,Spring+Hibernate等等
		
		//注意：没有做安全、事务等，仅作简单的演示
		
		//1：先添加主表
		String uuid = ObjectIdFactory.getObjectIdDAO().getNextObjectId();
		String sql = "insert into TBL_SHENQING values('"+uuid+"','"+amm.getShenQingRen()
		+"','"+amm.getTitle()+"','"+amm.getDescription()+"',"+amm.getShiJian_Hidden()+")";
		DbHelper.getInstance().executeSql(sql);
		//2：然后添加子表
		Iterator it = amm.getColChild().iterator();
		while(it.hasNext()){
			ApplyChildModel acm = (ApplyChildModel)it.next();
			String childUuid = ObjectIdFactory.getObjectIdDAO().getNextObjectId();
			sql = "insert into TBL_SHENQING_C values('"+childUuid+"','"+uuid
			+"','"+acm.getFiles_Hidden()+"','"+acm.getDescription_c()+"')";
			DbHelper.getInstance().executeSql(sql);
		}
		
		return true;
	}	
	/**
	 * 根据主表的uuid来获取相应的主表和子表的数据
	 * @param uuid 主表的uuid 
	 * @return 相应的主表和子表的数据，封装成ApplyMainModel
	 * @throws Exception
	 */
	public ApplyMainModel getAMMByUuid(String uuid)throws Exception{
		ApplyMainModel amm = new ApplyMainModel();
		//简化演示，直接写JDBC 来处理		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			DataSource ds = EjbConnDB.getInstance().getDS("");
			conn = ds.getConnection();
			//先取主表的值
			String sql = "select * from TBL_SHENQING where uuid='"+uuid+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()){
				//开始封装ApplyMainModel
				amm.setMainUuid(uuid);
				amm.setDescription(rs.getString("DESCRIPTION"));
				amm.setShenQingRen(rs.getString("SHENQINGREN"));
				amm.setShiJian_Hidden(rs.getLong("SHIJIAN"));
				amm.setTitle(rs.getString("TITLE"));
			}
			//关闭rs 和 pstmt
			rs.close();
			pstmt.close();
			
			//开始取子表的值
			sql = "select * from TBL_SHENQING_C where TBL_SHENQING_FK='"+uuid+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			Collection tempCol = new ArrayList();
			while(rs.next()){
				//开始封装ApplyChildModel
				ApplyChildModel acm = new ApplyChildModel();
				acm.setChildUuid(rs.getString("uuid"));
				acm.setDescription_c(rs.getString("DESCRIPTION"));
				acm.setFiles_Hidden(rs.getString("FILES"));
				
				tempCol.add(acm);
			}
			amm.setColChild(tempCol);
			//关闭rs 和 pstmt
			rs.close();
			pstmt.close();
		}finally{
			conn.close();
		}
		return amm;
	}
}
