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 com.engine.kq.wfset.attendance.framework.BaseConnection;
import com.engine.kq.wfset.attendance.framework.BaseDao;
import com.engine.kq.wfset.attendance.domain.HrmAttProcRelation;
import weaver.conn.RecordSet;

/**
 * HrmAttProcRelationDao
 * @author wcd
 * @version 1.0 2015-04-24
 */
public class HrmAttProcRelationDao extends BaseConnection implements BaseDao<HrmAttProcRelation> {

	public Comparable insert(HrmAttProcRelation bean) {
    RecordSet rs = new RecordSet();
		if(bean == null) return -1;
		StringBuffer sql = new StringBuffer()
		.append(" insert into kq_ATT_PROC_RELATION (mfid,field001,field002,field003,field004 )")
		.append(" values("+bean.getMfid()+","+bean.getField001()+","+bean.getField002()+","+bean.getField003()+",")
		.append(" '"+bean.getField004()+"' )");
		rs.executeSql(sql.toString());
		return 0;
	}

	public void insert(List<HrmAttProcRelation> list) {
    RecordSet rs = new RecordSet();
		if(list == null) return;
		StringBuffer sql = new StringBuffer(), tempSql = new StringBuffer()
		.append(" insert into kq_ATT_PROC_RELATION (mfid,field001,field002,field003,field004 )");
		int count = 0;
		for(HrmAttProcRelation bean : list) {
			sql.append(count == 0 ? tempSql.toString() : " UNION ").append(bean.getSql()).append(isOracle() ? " from dual" : "");
			if(++count > 100 || count >= list.size()) {
				rs.executeSql(sql.toString());
				sql.setLength(count = 0);
			}
		}
	}

	public void update(HrmAttProcRelation bean) {
    RecordSet rs = new RecordSet();
		if(bean == null) return;
		StringBuffer sql = new StringBuffer()
		.append(" update kq_ATT_PROC_RELATION set")
		.append(" mfid = "+bean.getMfid()+",field001 = "+bean.getField001()+",field002 = "+bean.getField002()+",")
		.append(" field003 = "+bean.getField003()+",field004 = '"+bean.getField004()+"'")
		.append(" where id = "+bean.getId()+"");
		rs.executeSql(sql.toString());
	}

	public List<HrmAttProcRelation> find(Map<String, Comparable> map) {
    RecordSet rs = new RecordSet();
		List<HrmAttProcRelation> list = new ArrayList<HrmAttProcRelation>();
		StringBuffer sql = new StringBuffer()
		.append(" select t.id,t.mfid,t.field001,t.field002,t.field003,t.field004,f.field001 as field005,f.field002 as field006")
		.append(" from kq_ATT_PROC_RELATION t left join kq_ATT_PROC_FIELDS f on t.field002 = f.id")
		.append(" where  1 = 1");
		if(map != null) {
			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("mfid")){
				sql.append(" and t.mfid = ").append(StringUtil.vString(map.get("mfid")));
			}
			if(map.containsKey("begin_mfid")){
				sql.append(" and t.mfid >= ").append(StringUtil.vString(map.get("begin_mfid")));
			}
			if(map.containsKey("end_mfid")){
				sql.append(" and t.mfid < ").append(StringUtil.vString(map.get("end_mfid")));
			}
			if(map.containsKey("sql_mfid")){
				sql.append(" " + StringUtil.vString(map.get("sql_mfid")));
			}
			if(map.containsKey("field001")){
				sql.append(" and t.field001 = ").append(StringUtil.vString(map.get("field001")));
			}
			if(map.containsKey("begin_field001")){
				sql.append(" and t.field001 >= ").append(StringUtil.vString(map.get("begin_field001")));
			}
			if(map.containsKey("end_field001")){
				sql.append(" and t.field001 < ").append(StringUtil.vString(map.get("end_field001")));
			}
			if(map.containsKey("sql_field001")){
				sql.append(" " + StringUtil.vString(map.get("sql_field001")));
			}
			if(map.containsKey("field002")){
				sql.append(" and t.field002 = ").append(StringUtil.vString(map.get("field002")));
			}
			if(map.containsKey("begin_field002")){
				sql.append(" and t.field002 >= ").append(StringUtil.vString(map.get("begin_field002")));
			}
			if(map.containsKey("end_field002")){
				sql.append(" and t.field002 < ").append(StringUtil.vString(map.get("end_field002")));
			}
			if(map.containsKey("sql_field002")){
				sql.append(" " + StringUtil.vString(map.get("sql_field002")));
			}
			if(map.containsKey("field003")){
				sql.append(" and t.field003 = ").append(StringUtil.vString(map.get("field003")));
			}
			if(map.containsKey("begin_field003")){
				sql.append(" and t.field003 >= ").append(StringUtil.vString(map.get("begin_field003")));
			}
			if(map.containsKey("end_field003")){
				sql.append(" and t.field003 < ").append(StringUtil.vString(map.get("end_field003")));
			}
			if(map.containsKey("sql_field003")){
				sql.append(" " + StringUtil.vString(map.get("sql_field003")));
			}
			if(map.containsKey("field004")){
				sql.append(" and t.field004 = '").append(StringUtil.vString(map.get("field004"))).append("'");
			}
			if(map.containsKey("like_field004")){
				sql.append(" and t.field004 like '%").append(StringUtil.vString(map.get("like_field004"))).append("%'");
			}
			if(map.containsKey("sql_field004")){
				sql.append(" " + StringUtil.vString(map.get("sql_field004")));
			}
			if(map.containsKey("field005")){
				sql.append(" and f.field001 = ").append(StringUtil.vString(map.get("field005")));
			}
			if(map.containsKey("field006")){
				sql.append(" and f.field002 = '").append(StringUtil.vString(map.get("field006"))).append("'");
			}
			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.id ").append(StringUtil.vString(map.get("sqlsortway")).length() > 0 ? StringUtil.vString(map.get("sqlsortway")) : "desc");
			}
		}
		rs.executeSql(sql.toString());
		HrmAttProcRelation bean = null;
		while(rs.next()){
			bean = new HrmAttProcRelation();
			bean.setId(StringUtil.parseToLong(rs.getString("id")));
			bean.setMfid(StringUtil.parseToLong(rs.getString("mfid")));
			bean.setField001(StringUtil.parseToInt(rs.getString("field001")));
			bean.setField002(StringUtil.parseToInt(rs.getString("field002")));
			bean.setField003(StringUtil.parseToInt(rs.getString("field003")));
			bean.setField004(StringUtil.vString(rs.getString("field004")));
			bean.setField005(StringUtil.parseToInt(rs.getString("field005")));
			bean.setField006(StringUtil.vString(rs.getString("field006")));
			list.add(bean);
		}
		return list;
	}

	public HrmAttProcRelation get(Comparable id) {
		HrmAttProcRelation bean = null;
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		map.put("id", id);
		List<HrmAttProcRelation> list = find(map);
		if(list != null && list.size() > 0){
			bean = list.get(0);
		}
		return bean;
	}

	public void delete(Comparable id) {
    RecordSet rs = new RecordSet();
		rs.executeSql("delete from kq_ATT_PROC_RELATION where id in ( "+id+" )");
	}

	public void delete(Map<String, Comparable> map) {
    RecordSet rs = new RecordSet();
		if(map == null || map.isEmpty()) return;
		StringBuffer sql = new StringBuffer("delete from kq_ATT_PROC_RELATION where 1=1 ");
		if(map.containsKey("field001")) sql.append(" and field001 = ").append(StringUtil.vString(map.get("field001")));
		rs.executeSql(sql.toString());
	}

}
