package weaver.interfaces.htsc.cowork.search;

import java.util.*;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.hrm.resource.ResourceComInfo;

/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class CustomSearchTemplate extends AbstractCustomSqlConditionJavaCode {

	/**
	 * 生成SQL查询限制条件
	 * @param param
	 *  param包含(但不限于)以下数据
	 *  user 当前用户
	 * 
	 * @return
	 *  返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
	 *  其中t1为表单主表表名的别名
	 */
	public String generateSqlCondition(Map<String, Object> param) throws Exception {
		User user = (User)param.get("user");
		int userid = user.getUID();
		RecordSet rs = new RecordSet();
		BaseBean bb = new BaseBean();
		ResourceComInfo resComInfo;
		String sqlCondition = "";
		try {
			String userids = "";
			String xzwcywhere = "";

			if("1".equals(userid)){
				String id = "1";
				userids = id;
				xzwcywhere += xzwcywhere =="" ? " (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0 )" : " or (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0)";
			}else{
				resComInfo = new ResourceComInfo();
				String workcode = resComInfo.getWorkcode(userid+"");
				rs.executeQuery(" select id from hrmresource where workcode=? and status = 1 ",workcode);
				while (rs.next())
				{
					String id = Util.null2String(rs.getString("id"));
					xzwcywhere += xzwcywhere =="" ? " (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0 )" : " or (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0)";
					userids += userids == "" ? id : ","+id ;
				}
			}

			if(!"".equals(xzwcywhere))
			{
				xzwcywhere = " and ("+ xzwcywhere +")";
			}

			String xzsql =	"   select t.id\n" +
					"   from uf_xzsy t " +
					"   where cjzt= 1 \n" + xzwcywhere +
					"   union all \n" +
					"   select t.id \n" +
					"   from uf_xzsy t \n" +
					"   inner join uf_xzsy_dt1 l on l.mainid = t.id \n" +
					"   where cjzt= 1 and l.bgxr in("+userids+") " ;

			sqlCondition = " t1.id in("+xzsql+")";

			//bb.writeLog("sqlCondition:"+sqlCondition);
		}catch (Exception e){
			bb.writeLog("e:"+e);
		}
		return sqlCondition;
	}

}
