
package nccloud.impl.hrkq.hrkq;

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

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.hr.utils.InSQLCreator;
import nc.hr.utils.ResHelper;
import nc.itf.org.IOrgConst;
import nc.itf.uap.IUAPQueryBS;
import nc.md.persist.framework.MDPersistenceService;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.hrkq.monthottime.AggMonthOtTimeVO;
import nc.vo.hrkq.monthottime.MonthOtTimeBVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nccloud.impl.pub.ace.AceAggbusiMonthOtTimeVOPubServiceImpl;
import nccloud.itf.hrkq.hrkq.IMonthottimevoMaintain ;
import nccloud.pub.hrkq.util.StringListProcessor;
import nccloud.pubitf.baseapp.apprbac.IAppAndOrgPermQueryPubService;

public class MonthottimevoMaintainImpl extends AceAggbusiMonthOtTimeVOPubServiceImpl implements IMonthottimevoMaintain  {

        @Override
        public void delete(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                super.pubdeleteBills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] insert(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubinsertBills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] update(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubupdateBills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] query(IQueryScheme queryScheme)
                        throws BusinessException {
                return super.pubquerybills(queryScheme);
        }

        @Override
        public AggMonthOtTimeVO[] save(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubsendapprovebills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] unsave(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubunsendapprovebills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] approve(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubapprovebills(clientFullVOs, originBills);
        }

        @Override
        public AggMonthOtTimeVO[] unapprove(AggMonthOtTimeVO[] clientFullVOs,
                        AggMonthOtTimeVO[] originBills) throws BusinessException {
                return super.pubunapprovebills(clientFullVOs, originBills);
        }

		@Override
		public List<Map<String, UFDate>> queryHoliday(String pk_psndoc, String pk_org) throws BusinessException {
			return super.queryHoliday(pk_psndoc, pk_org);
		}

		@Override
		public List<String> queryDefdoc() throws BusinessException {
			return super.queryDefdoc();
		}
		
		@Override
		public AggMonthOtTimeVO queryByPk(String pk) throws BusinessException {
			AggMonthOtTimeVO aggvo = MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByPK(AggMonthOtTimeVO.class, pk, false);
			if(aggvo != null) {
				Collection<MonthOtTimeBVO> bbills = MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByCond(MonthOtTimeBVO.class, "pk_monthottime" + "='" + pk + "' and dr = 0", true,
		                false);
				aggvo.setChildrenVO(bbills.toArray(new MonthOtTimeBVO[0]));
			}
			return aggvo;
		}
        
		@Override
		public List<DefdocVO> queryDefdoc2() throws BusinessException {
			return super.queryDefdoc2();
		}

		@Override
		public Map<String, UFDouble> queryMonth(String pk_psndoc) throws BusinessException {
			return super.queryMonth(pk_psndoc);
		}

		@Override
		public List<MonthOtTimeBVO> queryBVOByPkPsndoc(String pk_psndoc) throws BusinessException {
			return super.queryBVOByPkPsndoc(pk_psndoc);
		}
		
		/**
	     * 获取当前登录人有权限的部门
	     *
	     * @param userId  用户
	     * @param PkGroup 集团
	     * @param appCode appCode
	     * @return 返回sql
	     * @throws BusinessException
	     */
	    private String getPowerDeptCondition(String userId, String PkGroup, String appCode) throws BusinessException {
	        IAppAndOrgPermQueryPubService service = NCLocator.getInstance().lookup(IAppAndOrgPermQueryPubService.class);

	        String[] deptIds = service.queryUserPermOrgPksByAppAndWithOrgTypesFilter(userId, appCode, new String[]{IOrgConst.DEPTORGTYPE}, PkGroup);
	        if (deptIds == null || deptIds.length == 0) {
	            return null;
	        }
	        InSQLCreator isc = new InSQLCreator();
	        String strInSql = null;
	        try {
	            strInSql = isc.getInSQL(deptIds);
	        } catch (BusinessException e) {
	            Logger.error(e.getMessage(), e);
	            throw new RuntimeException(ResHelper.getString("6020hrkq", "6020hrkq00014")
	                    /*@res "查询有权限部门异常！" */);
	        }
	        if (strInSql.startsWith("(")) {
	            return "(select org_dept.pk_dept from org_dept inner join org_admin_enable on org_dept.pk_org=org_admin_enable.pk_adminorg where org_dept.hrcanceled<>'Y' and org_dept.pk_dept in " + strInSql + ")";
	        } else {
	            return "(select org_dept.pk_dept from org_dept inner join org_admin_enable on org_dept.pk_org=org_admin_enable.pk_adminorg where org_dept.hrcanceled<>'Y' and org_dept.pk_dept in (" + strInSql + "))";
	        }
	    }
	    
	    /**
	     * 获取有权限部门map
	     *
	     * @param userId  userId
	     * @param PkGroup pkGroup
	     * @param appCode appCode
	     * @return map
	     * @throws BusinessException BusinessException
	     */
	    @Override
	    public List<String> getPowerDeptMap(String userId, String PkGroup, String appCode) throws BusinessException {
//	        IAppAndOrgPermQueryPubService service = NCLocator.getInstance().lookup(IAppAndOrgPermQueryPubService.class);
//	        List<String> result = new ArrayList();
//	        String[] deptIds = service.queryUserPermOrgPksByAppAndWithOrgTypesFilter(userId, appCode, new String[]{IOrgConst.DEPTORGTYPE}, PkGroup);
//	        if (deptIds == null || deptIds.length == 0) {
//	            return null;
//	        }
//	        InSQLCreator isc = new InSQLCreator();
//	        String strInSql = null;
//	        try {
//	            strInSql = isc.getInSQL(deptIds);
//	        } catch (BusinessException e) {
//	            Logger.error(e.getMessage(), e);
//	            throw new RuntimeException(ResHelper.getString("6020hrkq", "6020hrkq00014")
//	                    /*@res "查询有权限部门异常！" */);
//	        }
//	        String sql;
//	        if (strInSql.startsWith("(")) {
//	            sql = "(select org_dept.pk_dept from org_dept inner join org_admin_enable on org_dept.pk_org=org_admin_enable.pk_adminorg where org_dept.hrcanceled<>'Y' and org_dept.pk_dept in " + strInSql + ")";
//	        } else {
//	            sql = "(select org_dept.pk_dept from org_dept inner join org_admin_enable on org_dept.pk_org=org_admin_enable.pk_adminorg where org_dept.hrcanceled<>'Y' and org_dept.pk_dept in (" + strInSql + "))";
//	        }
//	        List<String> powerDeptIds = (List<String>) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql,
//	                new StringListProcessor());
//	        Map<String, String> deptMap = new HashMap<>();
//	        for (String powerDeptId : powerDeptIds) {
//	            if (!deptMap.containsKey(powerDeptId)) {
//	                deptMap.put(powerDeptId, "1");
//	                result.add(powerDeptId);
//	            }
//	        }
//	        if (deptMap.isEmpty()) {
//	            return null;
//	        }
	    	

			String sql = "select  c.pk_org as pk_org\n" +
						"          from sm_user a\n" + 
						"         inner join sm_user_role b\n" + 
						"            on a.cuserid = b.cuserid\n" + 
						"         inner join sm_subject_org c\n" + 
						"            on b.pk_role = c.subjectid\n" + 
						"         inner join org_dept d\n" + 
						"            on c.pk_org = d.pk_dept\n" + 
						"         inner join sm_role e\n" + 
						"            on b.pk_role = e.pk_role\n" + 
						"         where   a.cuserid = '"+userId+"'";
			List<String> powerDeptIds = (List<String>) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql,
	                new StringListProcessor());
	    	
	        return powerDeptIds;
	    }
}
