package com.engine.kq.wfset.attendance.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import com.engine.kq.wfset.attendance.framework.BaseDao;
import com.engine.kq.wfset.attendance.domain.WorkflowBillfield;

/**
 * WorkflowBillfieldDao
 * @author wcd
 * @version 1.0 2015-04-24
 */
public class WorkflowBillfieldDao implements BaseDao<WorkflowBillfield> {

	public WorkflowBillfieldDao() {

	}

	public Comparable insert(WorkflowBillfield bean) {
		return 0;
	}

	public void update(WorkflowBillfield bean) {}

	/**
	 * map：
	 * 如没传入：languageid，则会每有一种语种则查询出一条表单字段记录
	 * 如没传入：viewtype，则默认只查询viewtype=0的主表表单字段记录
	 */
	public List<WorkflowBillfield> find(Map<String, Comparable> map) {
		List<WorkflowBillfield> list = new ArrayList<WorkflowBillfield>();
		StringBuffer sql = new StringBuffer()
		.append(" select t.id,t.billid,t.fieldname,t.fieldlabel,t.fielddbtype,t.fieldhtmltype,t.type,")
		.append(" t.viewtype,t.detailtable,t.fromUser,t.textheight,t.dsporder,t.childfieldid,")
		.append(" t.imgheight,t.imgwidth,t.places,t.qfws,t.textheight_2,t.selectitem,")
		.append(" t.linkfield,b.labelname,b.languageid")
		.append(" from workflow_billfield t left join HtmlLabelInfo b on t.fieldlabel = b.indexid")
		.append(" where 1=1 ");
		if(map == null) {
			map = new HashMap<String, Comparable>();
		}
		if(!map.containsKey("viewtype")){
			map.put("viewtype", "0");
		}
		
		if(map.containsKey("id")){
			sql.append(" and t.id = ").append(StringUtil.vString(map.get("id")));
		}
		if(map.containsKey("begin_id")){
			sql.append(" and t.id >= ").append(StringUtil.vString(map.get("begin_id")));
		}
		if(map.containsKey("end_id")){
			sql.append(" and t.id < ").append(StringUtil.vString(map.get("end_id")));
		}
		if(map.containsKey("sql_id")){
			sql.append(" " + StringUtil.vString(map.get("sql_id")));
		}
		if(map.containsKey("billid")){
			sql.append(" and t.billid = ").append(StringUtil.vString(map.get("billid")));
		}
		if(map.containsKey("begin_billid")){
			sql.append(" and t.billid >= ").append(StringUtil.vString(map.get("begin_billid")));
		}
		if(map.containsKey("end_billid")){
			sql.append(" and t.billid < ").append(StringUtil.vString(map.get("end_billid")));
		}
		if(map.containsKey("sql_billid")){
			sql.append(" " + StringUtil.vString(map.get("sql_billid")));
		}
		if(map.containsKey("fieldlabel")){
			sql.append(" and t.fieldlabel = ").append(StringUtil.vString(map.get("fieldlabel")));
		}
		if(map.containsKey("begin_fieldlabel")){
			sql.append(" and t.fieldlabel >= ").append(StringUtil.vString(map.get("begin_fieldlabel")));
		}
		if(map.containsKey("end_fieldlabel")){
			sql.append(" and t.fieldlabel < ").append(StringUtil.vString(map.get("end_fieldlabel")));
		}
		if(map.containsKey("sql_fieldlabel")){
			sql.append(" " + StringUtil.vString(map.get("sql_fieldlabel")));
		}
		if(map.containsKey("languageid")){
			sql.append(" and b.languageid = ").append(StringUtil.vString(map.get("languageid")));
		}
		if(map.containsKey("sql_languageid")){
			sql.append(" " + StringUtil.vString(map.get("sql_languageid")));
		}
		if(map.containsKey("type")){
			sql.append(" and t.type = ").append(StringUtil.vString(map.get("type")));
		}
		if(map.containsKey("begin_type")){
			sql.append(" and t.type >= ").append(StringUtil.vString(map.get("begin_type")));
		}
		if(map.containsKey("end_type")){
			sql.append(" and t.type < ").append(StringUtil.vString(map.get("end_type")));
		}
		if(map.containsKey("sql_type")){
			sql.append(" " + StringUtil.vString(map.get("sql_type")));
		}
		if(map.containsKey("viewtype")){
			sql.append(" and t.viewtype = ").append(StringUtil.vString(map.get("viewtype")));
		}
		if(map.containsKey("begin_viewtype")){
			sql.append(" and t.viewtype >= ").append(StringUtil.vString(map.get("begin_viewtype")));
		}
		if(map.containsKey("end_viewtype")){
			sql.append(" and t.viewtype < ").append(StringUtil.vString(map.get("end_viewtype")));
		}
		if(map.containsKey("sql_viewtype")){
			sql.append(" " + StringUtil.vString(map.get("sql_viewtype")));
		}
		if(map.containsKey("textheight")){
			sql.append(" and t.textheight = ").append(StringUtil.vString(map.get("textheight")));
		}
		if(map.containsKey("begin_textheight")){
			sql.append(" and t.textheight >= ").append(StringUtil.vString(map.get("begin_textheight")));
		}
		if(map.containsKey("end_textheight")){
			sql.append(" and t.textheight < ").append(StringUtil.vString(map.get("end_textheight")));
		}
		if(map.containsKey("sql_textheight")){
			sql.append(" " + StringUtil.vString(map.get("sql_textheight")));
		}
		if(map.containsKey("dsporder")){
			sql.append(" and t.dsporder = ").append(StringUtil.vString(map.get("dsporder")));
		}
		if(map.containsKey("begin_dsporder")){
			sql.append(" and t.dsporder >= ").append(StringUtil.vString(map.get("begin_dsporder")));
		}
		if(map.containsKey("end_dsporder")){
			sql.append(" and t.dsporder < ").append(StringUtil.vString(map.get("end_dsporder")));
		}
		if(map.containsKey("sql_dsporder")){
			sql.append(" " + StringUtil.vString(map.get("sql_dsporder")));
		}
		if(map.containsKey("childfieldid")){
			sql.append(" and t.childfieldid = ").append(StringUtil.vString(map.get("childfieldid")));
		}
		if(map.containsKey("begin_childfieldid")){
			sql.append(" and t.childfieldid >= ").append(StringUtil.vString(map.get("begin_childfieldid")));
		}
		if(map.containsKey("end_childfieldid")){
			sql.append(" and t.childfieldid < ").append(StringUtil.vString(map.get("end_childfieldid")));
		}
		if(map.containsKey("sql_childfieldid")){
			sql.append(" " + StringUtil.vString(map.get("sql_childfieldid")));
		}
		if(map.containsKey("imgheight")){
			sql.append(" and t.imgheight = ").append(StringUtil.vString(map.get("imgheight")));
		}
		if(map.containsKey("begin_imgheight")){
			sql.append(" and t.imgheight >= ").append(StringUtil.vString(map.get("begin_imgheight")));
		}
		if(map.containsKey("end_imgheight")){
			sql.append(" and t.imgheight < ").append(StringUtil.vString(map.get("end_imgheight")));
		}
		if(map.containsKey("sql_imgheight")){
			sql.append(" " + StringUtil.vString(map.get("sql_imgheight")));
		}
		if(map.containsKey("imgwidth")){
			sql.append(" and t.imgwidth = ").append(StringUtil.vString(map.get("imgwidth")));
		}
		if(map.containsKey("begin_imgwidth")){
			sql.append(" and t.imgwidth >= ").append(StringUtil.vString(map.get("begin_imgwidth")));
		}
		if(map.containsKey("end_imgwidth")){
			sql.append(" and t.imgwidth < ").append(StringUtil.vString(map.get("end_imgwidth")));
		}
		if(map.containsKey("sql_imgwidth")){
			sql.append(" " + StringUtil.vString(map.get("sql_imgwidth")));
		}
		if(map.containsKey("places")){
			sql.append(" and t.places = ").append(StringUtil.vString(map.get("places")));
		}
		if(map.containsKey("begin_places")){
			sql.append(" and t.places >= ").append(StringUtil.vString(map.get("begin_places")));
		}
		if(map.containsKey("end_places")){
			sql.append(" and t.places < ").append(StringUtil.vString(map.get("end_places")));
		}
		if(map.containsKey("sql_places")){
			sql.append(" " + StringUtil.vString(map.get("sql_places")));
		}
		if(map.containsKey("selectitem")){
			sql.append(" and t.selectitem = ").append(StringUtil.vString(map.get("selectitem")));
		}
		if(map.containsKey("begin_selectitem")){
			sql.append(" and t.selectitem >= ").append(StringUtil.vString(map.get("begin_selectitem")));
		}
		if(map.containsKey("end_selectitem")){
			sql.append(" and t.selectitem < ").append(StringUtil.vString(map.get("end_selectitem")));
		}
		if(map.containsKey("sql_selectitem")){
			sql.append(" " + StringUtil.vString(map.get("sql_selectitem")));
		}
		if(map.containsKey("linkfield")){
			sql.append(" and t.linkfield = ").append(StringUtil.vString(map.get("linkfield")));
		}
		if(map.containsKey("begin_linkfield")){
			sql.append(" and t.linkfield >= ").append(StringUtil.vString(map.get("begin_linkfield")));
		}
		if(map.containsKey("end_linkfield")){
			sql.append(" and t.linkfield < ").append(StringUtil.vString(map.get("end_linkfield")));
		}
		if(map.containsKey("sql_linkfield")){
			sql.append(" " + StringUtil.vString(map.get("sql_linkfield")));
		}
		if(map.containsKey("fieldname")){
			sql.append(" and t.fieldname = '").append(StringUtil.vString(map.get("fieldname"))).append("'");
		}
		if(map.containsKey("like_fieldname")){
			sql.append(" and t.fieldname like '%").append(StringUtil.vString(map.get("like_fieldname"))).append("%'");
		}
		if(map.containsKey("sql_fieldname")){
			sql.append(" " + StringUtil.vString(map.get("sql_fieldname")));
		}
		if(map.containsKey("fielddbtype")){
			sql.append(" and t.fielddbtype = '").append(StringUtil.vString(map.get("fielddbtype"))).append("'");
		}
		if(map.containsKey("like_fielddbtype")){
			sql.append(" and t.fielddbtype like '%").append(StringUtil.vString(map.get("like_fielddbtype"))).append("%'");
		}
		if(map.containsKey("sql_fielddbtype")){
			sql.append(" " + StringUtil.vString(map.get("sql_fielddbtype")));
		}
		if(map.containsKey("fieldhtmltype")){
			sql.append(" and t.fieldhtmltype = '").append(StringUtil.vString(map.get("fieldhtmltype"))).append("'");
		}
		if(map.containsKey("like_fieldhtmltype")){
			sql.append(" and t.fieldhtmltype like '%").append(StringUtil.vString(map.get("like_fieldhtmltype"))).append("%'");
		}
		if(map.containsKey("sql_fieldhtmltype")){
			sql.append(" " + StringUtil.vString(map.get("sql_fieldhtmltype")));
		}
		if(map.containsKey("detailtable")){
			sql.append(" and t.detailtable = '").append(StringUtil.vString(map.get("detailtable"))).append("'");
		}
		if(map.containsKey("like_detailtable")){
			sql.append(" and t.detailtable like '%").append(StringUtil.vString(map.get("like_detailtable"))).append("%'");
		}
		if(map.containsKey("sql_detailtable")){
			sql.append(" " + StringUtil.vString(map.get("sql_detailtable")));
		}
		if(map.containsKey("fromUser")){
			sql.append(" and t.fromUser = '").append(StringUtil.vString(map.get("fromUser"))).append("'");
		}
		if(map.containsKey("like_fromUser")){
			sql.append(" and t.fromUser like '%").append(StringUtil.vString(map.get("like_fromUser"))).append("%'");
		}
		if(map.containsKey("sql_fromUser")){
			sql.append(" " + StringUtil.vString(map.get("sql_fromUser")));
		}
		if(map.containsKey("qfws")){
			sql.append(" and t.qfws = '").append(StringUtil.vString(map.get("qfws"))).append("'");
		}
		if(map.containsKey("like_qfws")){
			sql.append(" and t.qfws like '%").append(StringUtil.vString(map.get("like_qfws"))).append("%'");
		}
		if(map.containsKey("sql_qfws")){
			sql.append(" " + StringUtil.vString(map.get("sql_qfws")));
		}
		if(map.containsKey("textheight2")){
			sql.append(" and t.textheight_2 = '").append(StringUtil.vString(map.get("textheight2"))).append("'");
		}
		if(map.containsKey("like_textheight2")){
			sql.append(" and t.textheight_2 like '%").append(StringUtil.vString(map.get("like_textheight2"))).append("%'");
		}
		if(map.containsKey("sql_textheight2")){
			sql.append(" " + StringUtil.vString(map.get("sql_textheight2")));
		}
		if(map.containsKey("mfsql")){
			sql.append(" " + StringUtil.vString(map.get("mfsql")));
		}
		if(map.containsKey("sqlorderby")){
			sql.append(" order by " + StringUtil.vString(map.get("sqlorderby")));
		}else{
			sql.append(" order by t.dsporder, t.id ").append(StringUtil.vString(map.get("sqlsortway")).length() > 0 ? StringUtil.vString(map.get("sqlsortway")) : "desc");
		}
    RecordSet rs = new RecordSet();
		rs.executeSql(sql.toString());
		WorkflowBillfield bean = null;
		while(rs.next()){
			bean = new WorkflowBillfield();
			bean.setId(StringUtil.parseToInt(rs.getString("id")));
			bean.setBillid(StringUtil.parseToInt(rs.getString("billid")));
			bean.setFieldname(StringUtil.vString(rs.getString("fieldname")));
			bean.setFieldlabel(StringUtil.parseToInt(rs.getString("fieldlabel")));
			bean.setLabelName(StringUtil.vString(rs.getString("labelname")));
			bean.setLanguageid(StringUtil.parseToInt(rs.getString("languageid")));
			bean.setFielddbtype(StringUtil.vString(rs.getString("fielddbtype")));
			bean.setFieldhtmltype(StringUtil.vString(rs.getString("fieldhtmltype")));
			bean.setType(StringUtil.parseToInt(rs.getString("type")));
			bean.setViewtype(StringUtil.parseToInt(rs.getString("viewtype")));
			bean.setDetailtable(StringUtil.vString(rs.getString("detailtable")));
			bean.setFromuser(StringUtil.vString(rs.getString("fromUser")));
			bean.setTextheight(StringUtil.parseToInt(rs.getString("textheight")));
			bean.setDsporder(StringUtil.parseToDouble(rs.getString("dsporder")));
			bean.setChildfieldid(StringUtil.parseToInt(rs.getString("childfieldid")));
			bean.setImgheight(StringUtil.parseToInt(rs.getString("imgheight")));
			bean.setImgwidth(StringUtil.parseToInt(rs.getString("imgwidth")));
			bean.setPlaces(StringUtil.parseToInt(rs.getString("places")));
			bean.setQfws(StringUtil.vString(rs.getString("qfws")));
			bean.setTextheight2(StringUtil.vString(rs.getString("textheight_2")));
			bean.setSelectitem(StringUtil.parseToInt(rs.getString("selectitem")));
			bean.setLinkfield(StringUtil.parseToInt(rs.getString("linkfield")));
			list.add(bean);
		}
		return list;
	}

	public WorkflowBillfield get(Comparable id) {
		return null;
	}

	public void delete(Comparable id) {}

}
