package weaver.interfaces.htsc.gwky;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.util.HT_Util;

import java.util.List;
import java.util.Map;

/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class GongGaoSearchTemplate 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 {
		BaseBean bb = new BaseBean();
		User user = (User)param.get("user");
		String sqlcon = getUserWork(user.getUID());
		//bb.writeLog("sqlcon:"+sqlcon);
		return sqlcon;
	}

	public String getUserWork(int userid) {
		BaseBean bb = new BaseBean();
		try {
			ResourceComInfo resComInfo = new ResourceComInfo();
			String sql =" with a as(\n" +
						" select t1.bt,t1.syqz,t1.syjg,t1.wh,t1.szbm,t1.ngr,t1.modedatacreatedate as fbrq,t1.id,t1.formmodeid,m.formid,\n" +
						"        t1.sysyr,t1.syry,t1.sybm\n" +
						" from uf_gwky t1\n" +
						" left join modeinfo m on m.id = t1.formmodeid\n" +
						" where t1.fzzdlx = 0 " +
					    " ) ";

			RecordSet rs = new RecordSet();
			HT_Util ht_util = new HT_Util();
			rs.executeQuery(" select id from hrmresource where workcode in (select workcode from hrmresource where id=? ) and status = 1 ", userid);
			String userids = "";
			String hrmwhere = "";
			String deptwhere = "";
			String jgwhere = "";
			String qzwhere = "";
			String sql_px = "";
			while (rs.next()) {
				String id = Util.null2String(rs.getString("id"));
				String departmentid = resComInfo.getDepartmentID(id);
				userids += userids == "" ? id : "," + id;
				hrmwhere += hrmwhere == "" ? " instr(','||a1.syry||',','," + id + ",')>0" : " or instr(','||a1.syry||',','," + id + ",')>0";
				deptwhere += deptwhere == "" ? " instr(','||a1.sybm||',','," + departmentid + ",')>0 " : " or instr(','||a1.sybm||',','," + departmentid + ",')>0 ";
				String szjgid = ht_util.getszjg(id);
				if (szjgid != "") {
					jgwhere += jgwhere == "" ? " instr(','||a1.syjg||',','," + szjgid + ",')>0 " : " or instr(','||a1.syjg||',','," + szjgid + ",')>0 ";
				}
			}

			if (userids.length() > 0)
			{
				List<String> listvalue = ht_util.getszqz(userids);
				if (listvalue != null && listvalue.size() > 0)
				{
					for (int i = 0; i < listvalue.size(); i++)
					{
						String value = listvalue.get(i);
						if (value != "")
						{
							qzwhere += qzwhere == "" ? " instr(','||a1.syqz||',','," + value + ",')>0 " : " or instr(','||a1.syqz||',','," + value + ",')>0 ";
						}
					}
				}
			}

			String userSqls=" select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
							" from a a1 " +
							" where a1.sysyr = 1 " ;
			if(hrmwhere!=""){
				userSqls+=	" union all " +
							" select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
								" from a a1 " +
							" where " + hrmwhere ;
			}
			if(deptwhere!="")
			{
				userSqls+=	" union all " +
							" select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
							" from a a1 " +
							" where " + deptwhere;
			}
			if (jgwhere != "") {
				userSqls += " union all " +
							" select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
							" from a a1 " +
							" where " + jgwhere;
			}
			if (qzwhere != "") {
				userSqls += " union all " +
							" select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
							" from a a1 " +
							" where " + qzwhere;
			}
			sql_px = " select p.* from(" + userSqls + ") p where not exists(select 1 from uf_gwky_dt5 l where instr('," + userids + ",',','||l.ry||',')>0 and l.mainid = p.id) ";
			String countsql = sql + " select w.id as total from (" + sql_px + ") w ";
			//bb.writeLog("countsql:"+countsql);
			return " t1.id in ("+countsql+")";
		}catch (Exception exo){
			return "t1.id in (-1)";
		}
	}
}
