package kr.co.kihyun.beans.entity;

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

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import kr.co.kihyun.beans.entity.ecross.NeisParty;
import kr.co.kihyun.beans.entity.util.MPersistenceManager;

import kr.co.kihyun.beans.entity.util.QueryImpl;
import kr.co.kihyun.moumi.MoumiConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@PersistenceCapable(identityType = IdentityType.NONDURABLE, detachable = "true")
public class MUser extends AbstractOrganization<String> implements HandySync {
	/**
	 *
	 */
	private static final long serialVersionUID = -413639019780269518L;
	private static final Logger LOG = LoggerFactory.getLogger(MUser.class);

	@Persistent
	protected String id;

	private transient boolean isDecrypted;

	@Persistent
	private String password;

	@SuppressWarnings("unused")
	@Persistent
	private String name;

	private transient String decName;

	@Persistent
	private String slNo1;

	@Persistent
	private String slNo2;

	@Persistent
	private String email;

	@Persistent
	private String phone;

	@Persistent
	private String cellPhone;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private SysAuth sysAuth = SysAuth.TOTPER;

	@Persistent
	private String isDeleted = "N";

	@Persistent(nullValue = NullValue.EXCEPTION)
	private String mDept;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private Long priority = 0L;

	@Persistent
	private String dutyName;

	@Persistent
	private String gwId;

	@Persistent
	private Date delDate;

	@Persistent
	private String empCode;

	@Persistent
	private Role roles;

        @Persistent
	private String noteReqCount;

        @Persistent(nullValue = NullValue.DEFAULT)
	private String modYn = "N";        //column name:mod_yn, 기본값 : N.수정무

        @Persistent(nullValue = NullValue.DEFAULT)
	private String dualPostYn = "N";        //column name:dual_post_yn, 기본값 : N.겸임아님

        @Persistent
	private String orignDeptId;        //column name:orign_dept_id(원소속 부서 ID)

	public MUser(String id, String password, MDept dept) {
//		this.setId(id);
//		this.setPassword(password);
//		this.setUpperDept(dept);
	}

	public MUser() {
	}

    public String getModYn() {
        return modYn;
    }

    public void setModYn(String modYn) {
        this.modYn = modYn;
    }

    public String getDualPostYn() {
        return dualPostYn;
    }

    public void setDualPostYn(String dualPostYn) {
        this.dualPostYn = dualPostYn;
    }

    public String getOrignDeptId() {
        return orignDeptId;
    }

    public void setOrignDeptId(String orignDeptId) {
        this.orignDeptId = orignDeptId;
    }

	@Override
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getId() {
		return id;
	}

	public String getPassword() {
		return this.password;
	}

//	public void setPassword(String password) {
//		this.password = MoumiConfig.getUsePasswdEncrypt() ? new StandardStringDigester().digest(password) : password;
//	}


	public String getName(PersistenceManager pm) {
//		if (!this.isDecrypted) {
//			Query q = pm.newQuery("javax.jdo.query.SQL",
//					"SELECT sp_cm_dec_fu(user_nm_encpt) FROM TN_SYMUSMUM001 WHERE usrid = ?");
//			q.setUnique(true);
//			this.setDecName((String) q.execute(this.getId()));
//			this.isDecrypted = true;
//		}
		return this.name;
	}

	@Override
	public String getName() {
		if (this.isDecrypted)
			return this.getDecName();
		throw new RuntimeException("Unable to read name. You must decrypt this field first. Invoking getName(PersistenceManager pm) will make this field decrypted.");
	}

	public String getEncName() {
		return this.name;
	}

//	public void setName(String name) {
//		this.name = name;
//	}

	@Deprecated
	public String getSlNo1() {
		return this.slNo1;
	}

//	@Deprecated
//	public void setSlNo1(String slNo1) {
//		this.slNo1 = slNo1;
//	}

	@Deprecated
	public String getSlNo2() {
		return this.slNo2;
	}

//	@Deprecated
//	public void setSlNo2(String slNo2) {
//		this.slNo2 = slNo2;
//	}

	public String getEmail() {
		return this.email;
	}

//	public void setEmail(String email) {
//		this.email = email;
//	}

	public String getPhone() {
		return this.phone;
	}

//	public void setPhone(String phone) {
//		this.phone = phone;
//	}

	public String getCellPhone() {
		return this.cellPhone;
	}

//	public void setCellPhone(String cellPhone) {
//		this.cellPhone = cellPhone;
//	}

	public SysAuth getSysAuth() {
		return this.sysAuth;
	}

//	public void setSysAuth(SysAuth sysAuth) {
//		this.sysAuth = sysAuth;
//	}

	public String isDeleted() {
		return this.isDeleted;
	}

//	public void setDeleted(boolean isDeleted) {
//		this.isDeleted = isDeleted;
//	}

	@Override
	public MDept getUpperDept(PersistenceManager pm) {
		try {
			pm = new MPersistenceManager(pm);
			return pm.getObjectById(MDept.class, this.mDept);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

	@Override
	public MDept getRootDept(PersistenceManager pm) {
		return this.getUpperDept(pm).getRootDept(pm);
	}

	@Override
	public NeisParty getNeisParty(PersistenceManager pm) {
		return this.getUpperDept(pm).getNeisParty(pm);
	}

	public void setUpperDept(MDept dept) {
		if (dept == null)
			this.mDept = null;
		else
			this.mDept = dept.getId();
	}

	@Override
	public Long getPriority() {
		return this.priority;
	}

//	public void setPriority(int priority) {
//		this.priority = priority;
//	}

	public String getDutyName() {
		return this.dutyName;
	}

//	public void setDutyName(String dutyName) {
//		this.dutyName = dutyName;
//	}

	public String getGwId() {
		return this.gwId;
	}

//	public void setGwId(String gwId) {
//		this.gwId = gwId;
//	}

	public String getEmpCode() {
		return this.empCode;
	}

//	public void setEmpCode(String empCode) {
//		this.empCode = empCode;
//	}

	public Date getDelDate() {
		return this.delDate;
	}

//	public void setDelDate(Date delDate) {
//		this.delDate = delDate;
//	}

//	public void setRoles(Role roles) {
//		this.roles = roles;
//	}

	public Role getRoles() {
		return roles;
	}

	public static QueryImpl getUnwrittenReportsQuery(PersistenceManager pm) {
		return getUnwrittenReportsQuery(pm, null);
	}

	public static QueryImpl getUnwrittenReportsQuery(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return getUnwrittenReportsQuery(pm, filterMap);
	}

	public List<TotReport> getUnwrittenReports2(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getUnwrittenReports2(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public static QueryImpl getUnwrittenReportsQuery2(PersistenceManager pm, Map<String, Object> filterMap)
	{
		QueryImpl q = new QueryImpl(pm, TotReport.class);

    StringBuilder filter = new StringBuilder(110).append("isDeleted == false && type == TotReportType.REPORT")
		.append(" && (").append(" process == TotReportProcess.ACCEPT || process == TotReportProcess.REASSIGN")
		.append(" || process == TotReportProcess.ASSIGN || process == TotReportProcess.RETURN)")
		.append(" && (mDept == dept||mDept==userOrgan||mDept==upperDept)").append(" && totDoc.isDeleted == false")
		.append(" && (mUser == user || (mUser == null && isMyReport))");


/*
      StringBuilder filter = new StringBuilder(110).append("isDeleted == false && type == TotReportType.REPORT")
		.append(" && (").append(" process == TotReportProcess.ACCEPT || process == TotReportProcess.RETURN)")
		.append(" && (mDept == dept||mDept==userOrgan)").append(" && totDoc.isDeleted == false")
		.append(" && (mUser == user || (mUser == null && isMyReport))");
*/



		q.setFilter(filter.toString());
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.setSearchFilter(filterMap);
        q.declareParameters("String dept, String userOrgan,String upperDept,java.util.Date sysdate, String user, boolean isMyReport");
		return q;
	}

        public static List<String> getSearchUserName(PersistenceManager pm, Object searchVal)
		{
			try
			{
                            //System.out.println("SELECT id FROM moumi_muser WHERE name like '%"+searchVal+"%'");
				Query q=pm.newQuery("javax.jdo.query.SQL","SELECT id FROM moumi_muser WHERE sp_cm_dec_fu(name) like '%"+searchVal+"%'");
                //v2. 6.SQL 삽입 : DO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
     		return (List <String>) q.execute();
                //================
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
				throw new UnsupportedOperationException("Error! Error!");

			}
		}

	@SuppressWarnings("unchecked")
	public List<TotReport> getUnwrittenReports2(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = getUnwrittenReportsQuery2(pm, filterMap);


List tmpList=null;
String findWord=null;
for (Iterator<String> keyIterator = filterMap.keySet().iterator(); keyIterator.hasNext();)
{
    String filterKey = keyIterator.next();
    Object filterValue = filterMap.get(filterKey);
    if(filterKey == null || filterKey.length() <= 0) continue;
    if(filterKey.equals("totDoc.mUser.name")) //tmpList=getSearchUserName(pm,filterValue);
      findWord=(String)filterValue;
}


		q.setOrdering("totDoc.endDate ASC");

                List<TotReport> totReports=null;
		totReports = (List<TotReport>) q.executeWithArray(this.mDept, this.getUpperDept(pm).getOrgan(),this.getUpperDept(pm).getUpper(),new Date(), this.getId(),MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true);



/*
		List<TotReport> result = new ArrayList<TotReport>();
		for (TotReport totReport : totReports)
                {
			if (totReport.getTotDoc().getProcess(pm) == TotDocProcess.PRSS)
                        {
                            if(tmpList!=null)
                            {
                                for(int ii=0;ii<tmpList.size();ii++)
                                {
                                    if(((String)(tmpList.get(ii))).equals(totReport.getTotDoc().getUser())) result.add(totReport);
                                    System.out.println(tmpList.get(ii)+"^^"+totReport.getTotDoc().getUser());
                               }
                            }
                            else result.add(totReport);
			}
		}
 */

                List<TotReport> result = new ArrayList<TotReport>();
		for (TotReport totReport : totReports)
                {
			if (totReport.getTotDoc().getProcess(pm) == TotDocProcess.PRSS)
                        {
                            if(findWord!=null)
                            {
                                String tmp=totReport.getTotDoc().getOwnerName(pm);
                                if(tmp==null) continue;
                                if(tmp.indexOf(findWord)>=0) result.add(totReport);


                            }
                            else result.add(totReport);
			}
		}


		return result;
	}

	public List<TotReport> getUnwrittenReports(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getUnwrittenReports(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public static QueryImpl getUnwrittenReportsQuery(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotReport.class);
		StringBuilder filter = new StringBuilder(110).append("isDeleted == false && type == TotReportType.REPORT")
				.append(" && (").append(" process == TotReportProcess.ACCEPT || process == TotReportProcess.REASSIGN")
				.append(" || process == TotReportProcess.ASSIGN || process == TotReportProcess.RETURN)")
				.append(" && mDept == dept").append(" && totDoc.isDeleted == false")
				.append(" && (mUser == user || (mUser == null && isMyReport))");
		q.setFilter(filter.toString());
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.setSearchFilter(filterMap);
		q.declareParameters("String dept, java.util.Date sysdate, String user, boolean isMyReport");

        //LOG.debug("filter: {}", filter.toString());
		return q;
        //this.mDept,new Date(),this.getId(), MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true)
	}

	@SuppressWarnings("unchecked")
	public List<TotReport> getUnwrittenReports(PersistenceManager pm, Map<String, Object> filterMap) {
	QueryImpl q = getUnwrittenReportsQuery(pm, filterMap);
		q.setOrdering("totDoc.endDate ASC");
    List<TotReport> totReports = (List<TotReport>) q.executeWithArray(this.mDept, new Date(), this.getId(),
		MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true);

		List<TotReport> result = new ArrayList<TotReport>();
		for(TotReport totReport : totReports) {
			if(totReport.getTotDoc().getProcess(pm) == TotDocProcess.PRSS) {
				result.add(totReport);
				//LOG.debug("{} : {}", totReport, totReport.getProcess());
			}
		}

		return result;
	}

	public List<TotReport> getUnwrittenReports(PersistenceManager pm) {
		return this.getUnwrittenReports(pm, null);
	}

	public Long getUnwrittenReportsCount(PersistenceManager pm) {
		return this.getUnwrittenReportsCount(pm, null);
	}

	public Long getUnwrittenReportsCount(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getUnwrittenReportsCount(pm, filterMap);
	}

	public Long getUnwrittenReportsCount(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = getUnwrittenReportsQuery(pm, filterMap);
		q.setResult("totDoc");
		List<TotDoc> totDocs = (List<TotDoc>) q.executeWithArray(this.mDept,new Date(),this.getId(), MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true);
//System.out.println("count===========================================================================================>"+totDocs.size());
		int count = 0;
		for (TotDoc totDoc : totDocs) {
			if (totDoc.getProcess(pm) == TotDocProcess.PRSS) {
				count++;
			}
		}
		return new Long(count);
	}

	public Long getUnwrittenReportsCount2(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = getUnwrittenReportsQuery2(pm, filterMap);
		q.setResult("totDoc");

                int flag=0;
                List tmpList=null;
                for (Iterator<String> keyIterator = filterMap.keySet().iterator(); keyIterator.hasNext();)
                {
                    String filterKey = keyIterator.next();
                    Object filterValue = filterMap.get(filterKey);
                    if(filterKey == null || filterKey.length() <= 0) continue;
                    if(!filterKey.equals("totDoc.mUser.name")) continue;
                    flag=1;
                    tmpList=getSearchUserName(pm,filterValue);
                }

                List<TotDoc> totDocs=null;
		if(flag==0) totDocs = (List<TotDoc>) q.executeWithArray(this.mDept, this.getUpperDept(pm).getOrgan(),new Date(), this.getId(),
				MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true);
                else if(flag==1) totDocs = (List<TotDoc>) q.executeWithArray(this.mDept, this.getUpperDept(pm).getOrgan(),new Date(), this.getId(),
				MoumiConfig.getUseAssign() ? this.roles == Role.DOC_MANAGER : true,tmpList);
		int count = 0;
		for (TotDoc totDoc : totDocs) {
			if (totDoc.getProcess(pm) == TotDocProcess.PRSS) {
				count++;
			}
		}
		return new Long(count);
	}

	public List<TotReport> getSavedReports(PersistenceManager pm) {
		return this.getSavedReports(pm, null);
	}

	public List<TotReport> getSavedReports(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getSavedReports(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotReport> getSavedReports(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotReport.class);

		StringBuilder filter = new StringBuilder(110).append("isDeleted == false && type == TotReportType.REPORT")
				.append(" && (process == TotReportProcess.TEMP || process == TotReportProcess.APPROVE)")
				.append(" && mUser == user");
		q.setFilter(filter.toString());
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.declareParameters("String user");
		q = new QueryImpl(pm, TotReport.class, (List<TotReport>) q.execute(this.getId()));

		filter = new StringBuilder(300).append("totDoc.isDeleted == false");
		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.declareParameters("java.util.Date sysdate");
		q.setOrdering("totDoc.id DESC");

                List<TotReport> result = (List<TotReport>) q.execute(new Date());
		for (TotReport totReport : result) {
			//LOG.debug("{} : {}", totReport, totReport.getProcess());
		}

		return result;
	}

	public List<TotReport> getRecogReports(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getRecogReports(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotReport> getRecogReports(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotReport.class);
		StringBuilder filter = new StringBuilder(110).append(
				"isDeleted == false && process == TotReportProcess.APPROVE").append(
				" && recog.ordinal() == Recog.PRSS.ordinal() && currentRepoadm.recogUser == mUser");
		q.setFilter(filter.toString());
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.declareParameters("String mUser");
		q = new QueryImpl(pm, TotReport.class, (List<TotReport>) q.execute(this.getId()));

		filter = new StringBuilder(300).append("totDoc.isDeleted == false");
		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.declareParameters("int endProcessOrdinal, java.util.Date sysdate");
		q.setOrdering("totDoc.id DESC");

		return (List<TotReport>) q.execute(TotDocProcess.END.ordinal(), new Date());
	}

	public List<TotReport> getCompReports(PersistenceManager pm) {
		return this.getCompReports(pm, null, null);
	}

	public List<TotReport> getCompReports(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getCompReports(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotReport> getCompReports(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotReport.class);
		StringBuilder filter = new StringBuilder(300).append("isDeleted == false && type == TYPE_REPORT")
				.append(" && (process == PROCESS_COMP || process == PROCESS_END").append(" || process == PROCESS_NOT)")
				.append(" && totDoc.isDeleted == false && mUser == user");

List tmpList=null;
String findWord=null;
for (Iterator<String> keyIterator = filterMap.keySet().iterator(); keyIterator.hasNext();)
{
    String filterKey = keyIterator.next();
    Object filterValue = filterMap.get(filterKey);
    if(filterKey == null || filterKey.length() <= 0) continue;
    if(filterKey.equals("totDoc.mUser.name")) findWord=(String)filterValue;
}





		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.declareParameters("String user");
		q.setOrdering("totDoc.id DESC");

		//return (List<TotReport>) q.execute(this.getId());
                List<TotReport> totReports = new ArrayList<TotReport>();
                totReports=(List<TotReport>) q.execute(this.getId());
                List<TotReport> result = new ArrayList<TotReport>();

                for (TotReport totReport : totReports)
                {

                            if(findWord!=null)
                            {
                                String tmp=totReport.getTotDoc().getOwnerName(pm);
                                if(tmp==null) continue;
                                if(tmp.indexOf(findWord)>=0) result.add(totReport);


                            }
                            else result.add(totReport);

		}


		return result;
	}

	public List<TotDoc> getEndedDocs(PersistenceManager pm) {
		return this.getEndedDocs(pm, null);
	}

	public List<TotDoc> getEndedDocs(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getEndedDocs(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotDoc> getEndedDocs(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = getAccessableDocsQuery(pm, filterMap);
		q.setOrdering("id DESC");

		List<TotDoc> totDocs = (List<TotDoc>) q.execute(TotDocType.TOT_DOC, this.mDept, this.getId());
		List<TotDoc> resultDocs = new ArrayList<TotDoc>();
		for (TotDoc totDoc : totDocs) {
			if (totDoc.getProcess(pm) == TotDocProcess.END)
				resultDocs.add(totDoc);
		}

		return resultDocs;
	}

	public List<TotDoc> getRegDocs(PersistenceManager pm) {
		return this.getRegDocs(pm, null);
	}

	public List<TotDoc> getRegDocs(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getRegDocs(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotDoc> getRegDocs(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = this.getAccessableDocsQuery(pm, filterMap);
		q.setOrdering("id DESC");

		List<TotDoc> totDocs = (List<TotDoc>) q.execute(TotDocType.TOT_DOC, this.mDept, this.getId());
		List<TotDoc> resultDocs = new ArrayList<TotDoc>();
		for (TotDoc totDoc : totDocs) {
			if (totDoc.getProcess(pm) == TotDocProcess.REG)
				resultDocs.add(totDoc);
		}

		return resultDocs;
	}

	public List<TotDoc> getOnGoingDocs(PersistenceManager pm) {
		return this.getOnGoingDocs(pm, null);
	}

	public List<TotDoc> getOnGoingDocs(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getOnGoingDocs(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotDoc> getOnGoingDocs(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = this.getAccessableDocsQuery(pm, filterMap);
		q.setOrdering("id DESC");




		List<TotDoc> totDocs = (List<TotDoc>) q.execute(TotDocType.TOT_DOC, this.mDept, this.getId());
		List<TotDoc> resultDocs = new ArrayList<TotDoc>();
		for (TotDoc totDoc : totDocs)
                {
			if (totDoc.getProcess(pm) == TotDocProcess.PRSS)
                        {
                            resultDocs.add(totDoc);
                        }
		}
		return resultDocs;





	}

	public Long getOnGoingDocsCount(PersistenceManager pm) {
		return getOnGoingDocsCount(pm, null);
	}

	public Long getOnGoingDocsCount(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return getOnGoingDocsCount(pm, filterMap);
	}

	public Long getOnGoingDocsCount(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = this.getAccessableDocsQuery(pm, filterMap);

		@SuppressWarnings("unchecked")
		List<TotDoc> totDocs = (List<TotDoc>) q.execute(TotDocType.TOT_DOC, this.mDept, this.getId());
		Long count = (long) totDocs.size();
		for (TotDoc totDoc : totDocs) {
			if (totDoc.getProcess(pm) != TotDocProcess.PRSS)
				count--;
		}

		return count;
	}

	private QueryImpl getAccessableDocsQuery(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotDoc.class);

		StringBuilder filter = new StringBuilder(110).append("isDeleted == false && type == doc_type");
		if (!this.getSysAuth().equals(SysAuth.SYSADM) && !this.getSysAuth().equals(SysAuth.SUB_SYSADM)) { //7, 9의 권한이 아닐 경우
                    		filter.append(" && (").append(" mUser == user")
					.append(" || (mDept == dept && accessAuth == AccessAuth.DEPT)")
					.append(" || accessAuth == AccessAuth.ALL").append(" )");
                }
                if(this.getSysAuth().equals(SysAuth.SUB_SYSADM)){               //7권한을 같은 하위기관 집계진행
                           filter.append(" && (").append(" mUser == user")
					.append(" || (mDept == dept && accessAuth == AccessAuth.PART)")
					.append(" || accessAuth == AccessAuth.ALL").append(" )");

                }
		//LOG.debug("filter: {}", filter.toString());
		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.declareParameters("TotDocType doc_type, String dept, String user");
		return q;
	}

	public List<TotDoc> getForms(PersistenceManager pm) {
		return this.getForms(pm, null);
	}


	public List<TotDoc> getForms(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getForms(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<TotDoc> getForms(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, TotDoc.class);

		StringBuilder filter = new StringBuilder(110)
				.append("isDeleted == false && type == doc_type").append(" && (")
				.append(" mUser == user").append(" || (mDept == dept && accessAuth == AccessAuth.DEPT)")
				.append(" || (accessAuth == AccessAuth.PART)").append(" )");
		q.setFilter(filter.toString());
		q.setSearchFilter(filterMap);
		q.declareImports("import kr.co.kihyun.beans.entity.*");
		q.declareParameters("TotDocType doc_type, String dept, String user");
		q.setOrdering("id DESC");

		return (List<TotDoc>) q.execute(TotDocType.FORM_DOC, this.mDept, this.getId());
	}

	public List<UserPart> getGroupList(PersistenceManager pm, String filterKey, Object filterValue) {
		Map<String, Object> filterMap = new HashMap<String, Object>(1);
		filterMap.put(filterKey, filterValue);
		return this.getGroupList(pm, filterMap);
	}

	@SuppressWarnings("unchecked")
	public List<UserPart> getGroupList(PersistenceManager pm, Map<String, Object> filterMap) {
		QueryImpl q = new QueryImpl(pm, UserPart.class, "mUser == user");
		q.declareParameters("String user");
		q.setSearchFilter(filterMap);
		q.setOrdering("id DESC");
		return (List<UserPart>) q.execute(this.getId());
	}

	@Override
	public String getHandyId() {
		return id;
	}

	private void setDecName(String decName) {
		this.decName = decName;
	}

	private String getDecName() {
		return decName;
	}

  public String getDept()
  {
            return mDept;
  }

  public String getNoteReqCount()
  {
       return noteReqCount;
  }

}
