package weaver.formmode.customjavacode.browser;

import java.util.*;

import com.api.doc.detail.util.DocWkUtil;
import com.api.doc.search.service.DocSearchService;
import com.api.doc.search.util.DocCondition;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.share.ShareManager;

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

	/**
	 * 生成SQL查询限制条件
	 * @param param
	 *  param包含(但不限于)以下数据
	 *  user 当前用户
	 *
	 * @return
	 *  返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
	 *  其中t1为表单主表表名的别名
	 *  param.get("extensionParam") 可以获取所有参数
	 */
	public String generateSqlCondition(Map<String, Object> param) throws Exception {
		User user = (User)param.get("user");
		Object extensionParam =  param.get("extensionParam");//其他参数
		HashMap<String,Object> extensionMap = (HashMap<String,Object>)extensionParam;

		//获取参数示例  "min" 只是示例 需要根据自己传入的参数名获取
		//String min = (String)extensionMap.get("min");
		RecordSet rs = new RecordSet();
		rs.executeQuery("select * from uf_docwksetting");
		String sqlCondition ="";
		if(rs.next()) {
			String wkId = Util.null2String(rs.getString("wkml"));
			String wksql = Util.null2String(rs.getString("wksql"));
			if(!"".equals(wkId)){
				DocWkUtil dwu = new DocWkUtil();
				String ids = dwu.getAllChildsSql(wkId, "docseccategory");
//                ShareManager shareManager = new ShareManager();
//                sqlCondition = " t1.id in (select id from docdetail t left join "+shareManager.getShareDetailTableByUser("doc",user)+" t2  on t.id=t2.sourceid ";
//
//                sqlCondition += " where t.seccategory in (" + ids + ") ";

				String wkStatus = dwu.getWKStatus();
				sqlCondition += " t1.id in (select id from cus_fielddata where scope='DocCustomFieldBySecCategory' and scopeid in ("+ids+"))";  //wjzt-文件状态

				String hideSec = DocSearchService.getHideSec();  //隐藏目录ids
				if(!"".equals(hideSec)){
					sqlCondition += " and t1.seccategory not in ("+hideSec+") ";
				}
				sqlCondition += " and (t1.ishistory is null or t1.ishistory = 0) ";
				sqlCondition += " and t1.docstatus in (1,2,5) ";

//                sqlCondition  += " and t.orderable = '1'  and t2.sourceid is null)";

				sqlCondition += " and t1.seccategory in (" + ids + ") ";
				sqlCondition += wksql;
            }

		}


		rs.writeLog("WkDocBrowser--------->sqlCondition:"+sqlCondition);
		return sqlCondition;
	}

}