package com.steering.pss.ydm.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.ibm.websphere.wim.model.Entity;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.ydm.entity.YdmBsPurviewDEntity;
import com.steering.pss.ydm.entity.YdmBsPurviewMEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
public class FrmBelong extends DBCommon{

	/***
	 * 获取所有角色
	 * @return
	 */
	public CoreReturnObject doQueryRole()
	{
		return this.getDataBySqlID("FrmBelong.doQueryRole");
	}
	/***
	 * 查询角色是否启用所属权
	 * @param roleId
	 * @return
	 */
	public CoreReturnObject doQueryRoleData(String roleId)
	{
		return this.getDataBySqlID("FrmBelong.doQueryRoleData", new Object[]{roleId});
	}
	/***
	 * 新增所属权启用信息
	 * @param parm
	 * @return
	 */
	public CoreReturnObject addYdmBsPu(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String addBsPuSql = "";
		String sql = "";
		PreparedStatement psta = null;
		PreparedStatement addBsPuPsta = null;
		ResultSet rs = null;
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for(String str : parm)
			{
				addBsPuSql = CoreXmlSqlParsersFactory.getSql("FrmBelong.addYdmBsPu");
				YdmBsPurviewMEntity ydmBsPu = EntityTool.getEntityByJSONStr(str, YdmBsPurviewMEntity.class);
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmBelong.selectYdmBsPu");
				psta = connection.prepareStatement(sql);
				psta.setString(1, ydmBsPu.getRoleid());
				rs = psta.executeQuery();
				if(rs.next())
				{
					count = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(count>0)
				{
					cro.setV_errMsg("角色名已有数据维护,不允许新增!");
					return cro;
				}
				addBsPuSql = EntityTool.fillSqlByEntity(addBsPuSql, ydmBsPu);
				addBsPuPsta = connection.prepareStatement(addBsPuSql);
				addBsPuPsta.executeUpdate();
			}
			addBsPuPsta.close();
			connection.commit();
			cro.setV_errMsg("新增成功!");
		} 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;
	}
	
	/***
	 * 修改所属权启用信息
	 * @param parm
	 * @return
	 */
	public CoreReturnObject updateYdmBsPu(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String addBsPuSql = "";
		String sql = "";
		PreparedStatement psta = null;
		PreparedStatement addBsPuPsta = null;
		ResultSet rs = null;
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for(String str : parm)
			{
				addBsPuSql = CoreXmlSqlParsersFactory.getSql("FrmBelong.updateYdmBsPu");
				YdmBsPurviewMEntity ydmBsPu = EntityTool.getEntityByJSONStr(str, YdmBsPurviewMEntity.class);

				addBsPuSql = EntityTool.fillSqlByEntity(addBsPuSql, ydmBsPu);
				addBsPuPsta = connection.prepareStatement(addBsPuSql);
				addBsPuPsta.executeUpdate();
			}
			addBsPuPsta.close();
			connection.commit();
			cro.setV_errMsg("修改成功!");
		} 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;
	}
	/***
	 * 获取所有科室、部门
	 * @return
	 */
	public CoreReturnObject doQueryDepart()
	{
		return this.getDataBySqlID("FrmBelong.doQueryDepart");
	}
	/***
	 * 获取所有所属权
	 * @return
	 */
	public CoreReturnObject doQueryBelong()
	{
		return this.getDataBySqlID("FrmBelong.doQueryBelong");
	}
	/***
	 * 所属权查询
	 * @param departId
	 * @return
	 */
	public CoreReturnObject doQueryDepartIdData(String departId)
	{
		return this.getDataBySqlID("FrmBelong.doQueryDepartIdData", new Object[]{departId});
	}
	
	/***
	 * 新增科室、部门对应所属权
	 * @param parm
	 * @return
	 */
	public CoreReturnObject addYdmBsPuD(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String addBsPuSql = "";
		String sql = "";
		PreparedStatement psta = null;
		PreparedStatement addBsPuPsta = null;
		ResultSet rs = null;
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for(String str : parm)
			{
				addBsPuSql = CoreXmlSqlParsersFactory.getSql("FrmBelong.addYdmBsPuD");
				YdmBsPurviewDEntity ydmBsPu = EntityTool.getEntityByJSONStr(str, YdmBsPurviewDEntity.class);
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmBelong.selectYdmBsPuD");
				psta = connection.prepareStatement(sql);
				psta.setString(1, ydmBsPu.getDepartId());
				psta.setString(2, ydmBsPu.getBelongId());
				rs = psta.executeQuery();
				if(rs.next())
				{
					count = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(count>0)
				{
					cro.setV_errMsg("科室、部门对应的所属权已维护,不允许新增!");
					return cro;
				}
				addBsPuSql = EntityTool.fillSqlByEntity(addBsPuSql, ydmBsPu);
				addBsPuPsta = connection.prepareStatement(addBsPuSql);
				addBsPuPsta.executeUpdate();
			}
			addBsPuPsta.close();
			connection.commit();
			cro.setV_errMsg("新增成功!");
		} 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;
	}
	
	/***
	 * 删除
	 * @param parm
	 * @return
	 */
	public CoreReturnObject deleteYdmBsPuD(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String addBsPuSql = "";
		String sql = "";
		PreparedStatement psta = null;
		PreparedStatement addBsPuPsta = null;
		ResultSet rs = null;
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for(String str : parm)
			{
				addBsPuSql = CoreXmlSqlParsersFactory.getSql("FrmBelong.deleteYdmBsPu");
				YdmBsPurviewDEntity ydmBsPu = EntityTool.getEntityByJSONStr(str, YdmBsPurviewDEntity.class);

				addBsPuSql = EntityTool.fillSqlByEntity(addBsPuSql, ydmBsPu);
				addBsPuPsta = connection.prepareStatement(addBsPuSql);
				addBsPuPsta.executeUpdate();
			}
			addBsPuPsta.close();
			connection.commit();
			cro.setV_errMsg("删除成功!");
		} 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;
	}
}
