package com.kingdee.shr.custom.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.org.AdminOrgUnitCollection;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class GetAdminInfoService implements IHRMsfService {
	private static final Logger logger = LoggerFactory.getLogger(GetAdminInfoService.class);
	
	 /**
     * 组织下属组织
     * @param ctx
     * @param param
     * @return
     * @throws BOSException
     * @throws SQLException
     */
	public Object process(Context ctx, Map<String, Object> param) throws EASBizException, BOSException {
		String orgId = StringUtils.cnulls(param.get("orgId"));
		String checkType = StringUtils.cnulls(param.get("checkType"));
		String isOfficer = StringUtils.cnulls(param.get("isOfficer"));
		if(StringUtils.equals("1", orgId)){
			orgId = GetOrgUtils.getRootOrg(ctx);
		}
		if(StringUtils.isEmpty(orgId) ){
			logger.error("查询到orgId为空");
			return new ArrayList();
		}
		Map <String,String> map = new HashMap();
		map.put("orgId", orgId);
		map.put("isOfficer", isOfficer);
        return getSubOrgAndPerson(ctx,checkType,map);
    }

	 
	  
	/**
	 * 获取直接下级组织,并且获取组织下的成员的数量
	 * checkType 为1,则只查询直接下级的成员数量,
	 * 否则,所有下级的成员数量
	 * @param ctx
	 * @param orgId
	 * @return
	 * @throws BOSException
	 * @throws EASBizException
	 */
	private List getSubOrgAndPerson(Context ctx, String checkType,Map <String,String> paramMap ) throws BOSException, EASBizException {
		String orgId =  paramMap.get("orgId");
		String isOfficer =  paramMap.get("isOfficer");
		List<Map> maplist = new ArrayList();
		IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
        EntityViewInfo view = new EntityViewInfo();
        SelectorItemCollection sic =new SelectorItemCollection();
        sic.add("id");
        sic.add("longNumber");
        sic.add("name");
        FilterInfo filter = new FilterInfo();
        filter.getFilterItems().add(new FilterItemInfo("parent.id", orgId ,CompareType.EQUALS));
        //是否封存,  
        filter.getFilterItems().add(new FilterItemInfo("isSealUp", false ,CompareType.EQUALS));
        SorterItemCollection sc = new SorterItemCollection();
        sc.add(new SorterItemInfo("number"));
        view.setFilter(filter);
        view.setSelector(sic);
        view.setSorter(sc);
        AdminOrgUnitCollection col = iAdminOrgUnit.getAdminOrgUnitCollection(view);
         
        HashMap<String, Integer> admPersonMap = getDirectSubCol(ctx,isOfficer);
        for(int i =0; i < col.size(); i++) {
        	AdminOrgUnitInfo adminInfo = col.get(i);
        	Map map = new HashMap();
        	map.put("id", adminInfo.getId().toString());
        	map.put("name",  adminInfo.getName().toString());
        	String adminId = adminInfo.getId().toString();
        	
        	if(admPersonMap.containsKey(adminId)) {
        		map.put("count",admPersonMap.get(adminId));
        	}else {
        		map.put("count",0);
        	}
        	//传参
        	paramMap.put("orgId", adminId);
        	int totalCount = GetOrgUtils.getPersonCount (ctx, "0" ,paramMap);
			if(totalCount > 0  ){
				map.put("totalCount",totalCount);
				maplist.add(map);
			}
        }
        return maplist;
		
	}

	//获取所有的组织与组织的直接人数
	public  HashMap<String, Integer>  getDirectSubCol(Context ctx,String isOfficer) throws BOSException {
		HashMap<String, Integer> admPersonMap = new HashMap ();
		try {
			//权限
			String perStr = GetOrgUtils.addPermissionFilterSql(ctx);
			String filterStr = " and admOrg.fisSealUp <> 1 " +perStr;
			if(StringUtils.equals(isOfficer, "1")) {
				filterStr += " and pers.CFIsCadre  = 1 ";
			}
	        String pinSql = GetOrgUtils.pinPersonSql( "adminPerson",filterStr);
			IRowSet rs = DbUtil.executeQuery(ctx, pinSql);
			while(rs.next()) {
				int pcount = rs.getInt("pcount");
				String depId = StringUtils.cnulls(rs.getString("depId"));
				admPersonMap.put(depId, pcount);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (EASBizException e) {
			throw new RuntimeException(e);
		}
		return admPersonMap;
	}
}