package core.stlmes.server.sale.dbhelp;

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

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CorePageObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA06.CoreSqlType.CoreOracleType;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

public class CoreUserInfo extends CoreIComponent {
	
	//查询Dept属性
	public CoreReturnObject doGetDeptInfo()
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("query_userinfo_0001");
		cro = this.getDao("KgDao").ExcuteQuery(strSql);
		return cro;
	}
	
	//根据条件或者无条件查询
	public CoreReturnObject doGetUserInfo(String userName,String deptNo)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("query_userinfo_0002");
		String strWhere = "";
		ArrayList al = new ArrayList();
		Object[] parame = null;
		if(!userName.equals(""))
		{
			strWhere = "AND USERNAME like '%'||?||'%'";
			al.add(userName);
		}
		if(!deptNo.equals(""))
		{
			strWhere = "AND DEPTID = ?";
			al.add(deptNo);
		}
		strSql += strWhere;
		if(al.size() > 0)
		{
			parame = al.toArray();
		}		
		cro = this.getDao("KgDao").ExcuteQuery(strSql,parame);
		return cro;
	}
	
	//新增信息
	public CoreReturnObject doAdd(String userId,String userName,String sex,String deptId,String joinDate)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("query_userinfo_0003");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{userId,userName,sex,deptId,joinDate});
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//删除
	public CoreReturnObject doDelete(String userId)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("query_userinfo_0004");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{userId});
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//修改
	public CoreReturnObject Query(String sqlStr)
	{
		CoreReturnObject cro = new CoreReturnObject();
		
		try {
			cro = this.getDao("KgDao").ExcuteQuery(sqlStr);
		} catch (Exception e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	public CoreReturnObject doEdit(ArrayList parms)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("query_userinfo_0005");
		try {
			this.getDao("KgDao").ExcuteNonQuery(strSql,parms.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//发布通知
	public CoreReturnObject doGetUserInfo(String deptId)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0001");
		cro = this.getDao("KgDao").ExcuteQuery(strSql,new Object[]{deptId});
		return cro;
	}
	
	//查询通知
	public CoreReturnObject doQuerySendInfo(String noticeId,String deptId,String published,String publiSher,String userId,String sendInfo,String receive,String send)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0003");
		String strWhere = "";
		ArrayList al = new ArrayList();
		Object[] parame = null;
		if(!noticeId.equals(""))
		{
			strWhere += " AND NOTICEID like '%'||?||'%'";
			al.add(noticeId);
		}
		if(!deptId.equals(""))
		{
			strWhere += " AND DEPTID = ?";
			al.add(deptId);
		}
		if(!published.equals(""))
		{
			strWhere += " AND SEND = ?";
			al.add(published);
		}
		if(!publiSher.equals(""))
		{
			strWhere += " AND PUBLISHER = ?";
			al.add(publiSher);
		}
		if(!userId.equals(""))
		{
			strWhere += " AND USERID = ?";
			al.add(userId);
		}
		if(!receive.equals(""))
		{
			strWhere += " AND RECEIVE = ?";
			al.add(receive);
		}
		strSql += strWhere;
		if(al.size() > 0)
		{
			parame = al.toArray();
		}		
		cro = this.getDao("KgDao").ExcuteQuery(strSql,parame);
		return cro;
	}
	
	//新增通知信息
	public CoreReturnObject doAddSendMessageInfo(String noticeId,String publiSher,String deptId,String userId,String sendInfo)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0002");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{noticeId,publiSher,deptId,userId,sendInfo});
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//删除通知
	public CoreReturnObject doDeletSendInfo(String noticeId)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0004");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{noticeId});
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//修改通知
	public CoreReturnObject doEditSendInfo(ArrayList params)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0005");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//修改发布状态
	public CoreReturnObject doEditNoticeState(String nowDate,String noticeId)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0006");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{nowDate,noticeId});
			System.out.println(strSql);
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//修改查看状态
	public CoreReturnObject doEditNSeeState(String noticeId)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("send_message_0007");
		try {
			cro = this.getDao("KgDao").ExcuteNonQuery(strSql,new Object[]{noticeId});
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	//查询
	public CoreReturnObject doQuery(ArrayList parm)
	{
		CoreReturnObject cro=new CoreReturnObject();
		String strSql=CoreXmlSqlParsersFactory.getSql("");
		String strWhere="";	
		ArrayList p=new ArrayList();
		Object[] parms=null;
		if(parm.size()==2)
		{
			if(!parm.get(0).toString().equals(""))
			{
				strWhere+=" And USERNAME=?";
				p.add(parm.get(0).toString());
				
			}
			if(!parm.get(1).toString().equals(""))
			{
				strWhere+=" And DEPARTID=?";
				p.add(parm.get(1).toString());
				
			}	
			strSql+=strWhere;
			if(p.size()>0)
			{
				parms=p.toArray();
			}
		}
		
		cro=this.getDao("KgDao").ExcuteSortResultByQuery(strSql, parms);
		
		
		return cro;		
	}
	//分页
	public CoreReturnObject doQueryForPage(CorePageObject cpo)
	{
		CoreReturnObject cro=new CoreReturnObject();
		//cpo.getSqlparamter()
    	cpo.setSql(CoreXmlSqlParsersFactory.getSql("query_userinfo_0001"));
    	cro=this.getDao(CoreConstantData.sysbeanstr).ExcutePageQuery(cpo);
    	return cro;
		
	}
	
	public CoreReturnObject doAdd(ArrayList parms)
	{
		CoreReturnObject cro=new CoreReturnObject();
		String strSql=CoreXmlSqlParsersFactory.getSql("query_userinfo_0002");
		
		try {
			cro=this.getDao("KgDao").ExcuteNonQuery(strSql, parms.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			cro=cro.SetErrInfo(e.getMessage());			
		}
		
		return cro;
	}
	public CoreReturnObject doModify(ArrayList parms)
	{
		CoreReturnObject cro=new CoreReturnObject();
		String strSql=CoreXmlSqlParsersFactory.getSql("query_userinfo_0003");
		
		try {
			cro=this.getDao("KgDao").ExcuteNonQuery(strSql, parms.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			cro=cro.SetErrInfo(e.getMessage());			
		}
		
		return cro;
	}
	//大数据类型的处Blob Clob
	public CoreReturnObject doOperationBlob()
	{
		CoreReturnObject cro=new CoreReturnObject();
		try {
			this.getDao("KgDao").ExceuteQueryForBlobList("sql", new Object[]{});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return cro;
		
	}
	//存储过程
	public CoreReturnObject doProcedure()
	{
		CoreReturnObject cro=new CoreReturnObject();
	    //test(v in str_ary_type, v0 in varchar2,v1 in varchar2,v2 out varchar2,v3 out varchar2)
		//this.getDao("Kgdao").ExcuteProcedure("存储过程的名字", "ArrayList集合，用来存放每一个输入参数的数据类型", "ArrayList集合,存放输入值", "ArrayList集合，用来存放每一个输出参数的数据类型", "ArrayList集合,存放输出值");
		ArrayList arrList=new ArrayList();
		arrList.add("1");
		arrList.add("2");
		
		String strProcedureName="";
		ArrayList inParmType=new ArrayList();
		inParmType.add(CoreOracleType.ARRAY_TYPE.ordinal());
		inParmType.add(CoreOracleType.STRING_TYPE.ordinal());
		inParmType.add(CoreOracleType.STRING_TYPE.ordinal());
		
		ArrayList inParm=new ArrayList();
		try {
			inParm.add(this.getDao("KgDao").getArray("str_ary_type", arrList));
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		inParm.add("t");
		inParm.add("t1");
		
		ArrayList outParmType=new ArrayList();
		outParmType.add(CoreOracleType.STRING_TYPE.ordinal());
		
		ArrayList outParm=new ArrayList();
		
		ArrayList customType=new ArrayList();
		customType.add("str_ary_type");
		try {
			cro=this.getDao("KgDao").ExcuteProcedure(strProcedureName,inParmType,inParm,outParmType,outParm,customType);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//cro.getResult() ArrayList
		return cro;
		
	}
	
	//事务的处理
	public CoreReturnObject doTran()
	{
		CoreReturnObject cro=new CoreReturnObject();
		//1同一个数据库的事务处理
		//this.getDao("KgDao").ExcuteTransction("SQL的ArrayList", "SQL参数的ArrayList")
		ArrayList sqlList=new ArrayList();
		sqlList.add("sql1 where t=?,t1=?");
		sqlList.add("sql2 where z=?");
		
		ArrayList sqlParms=new ArrayList();
		sqlParms.add(new Object[]{"t1","t2"});
		sqlParms.add(new Object[]{"z"});
		
		//2不同数据库的事务处理
		Connection conn=this.getDao("KgDao").getConnection();
		Connection conn1=this.getDao("SysDao").getConnection();
		try {
			this.getDao("KgDao").ExceuteQueryTranscation("sql", new Object[]{}, conn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		try {
			this.getDao("KgDao").ExceuteQueryTranscation("sql", new Object[]{}, conn1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			conn.rollback();
//			conn1.rollback();
			
		}
		
		return cro;
	}
	
}
