package nccloud.impl.bd.wadataexplain;

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

import nc.bs.bd.baseservice.md.BatchBaseService;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.hrwa.wadataexplain.Wadataexplain;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.commons.collections.CollectionUtils;
import nccloud.itf.bd.wadataexplain.IWadataexplainQryService;
import nccloud.itf.bd.wadataexplain.IWadataexplainService;


public class WadataexplainServiceImpl extends BatchBaseService<Wadataexplain>
		implements IWadataexplainService, IWadataexplainQryService {

	private IMDPersistenceQueryService queryService = null;

	public WadataexplainServiceImpl() {
		super("cfcfc751-a268-4c9e-b7a0-f38533e78990");
	}

	@Override
	public BatchOperateVO batchSaveCurrtype(BatchOperateVO vo) throws BusinessException {
		return batchSave(vo);
	}

	@Override
	public Wadataexplain findByPK(String key) throws BusinessException {
		Wadataexplain wadataexplain = null;
		try {
			Object vo = new BaseDAO().retrieveByPK(Wadataexplain.class, key);
			wadataexplain = (vo == null ? null : (Wadataexplain) vo);
		} catch (Exception e) {
			Logger.error(e.getMessage(), e);
			throw new BusinessException(e);
		}

		return wadataexplain;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Wadataexplain[] queryAll() throws BusinessException {
		Collection<Wadataexplain> col = getQueryService().queryBillOfVOByCondWithOrder(Wadataexplain.class, "1=1", true,
				false, new String[] {});
		return col.toArray(new Wadataexplain[0]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Wadataexplain[] query(String where) throws BusinessException {
		Collection<Wadataexplain> col = getQueryService().queryBillOfVOByCondWithOrder(Wadataexplain.class, where, true,
				false, new String[] {});

		return col.toArray(new Wadataexplain[0]);
	}

	private IMDPersistenceQueryService getQueryService() {
		if (queryService == null)
			queryService = NCLocator.getInstance().lookup(IMDPersistenceQueryService.class);
		return queryService;

	}

	@Override
	public String[] queryPKs(String where) throws BusinessException {
		String[] pks = null;
		StringBuilder querySQL = new StringBuilder("select ");
		querySQL.append(" pk_wa_data_explain ");
		querySQL.append(" from ");
		querySQL.append(" wa_data_explain ");
		querySQL.append(" where DR='0' ");//这里根据dr进行查询
		querySQL.append("and " + where);
		querySQL.append(" order by ");
		querySQL.append(" ts ");

		List<String> pkList = (ArrayList<String>) new BaseDAO().executeQuery(querySQL.toString(),
				new ColumnListProcessor());
		if (pkList != null && pkList.size() > 0) {
			pks = pkList.toArray(new String[pkList.size()]);
		}

		return pks;
	}

	@Override
	public List<Wadataexplain> batchCommit(StringBuffer pk_wa_class_buf, StringBuffer pk_buf, HashMap<String, String> psndocMap,StringBuffer cyearperiodbuf)
			throws BusinessException {
		// TODO Auto-generated method stub
		//当前会计期间已经 全部审批，不允许提交 
		StringBuffer dataBufall = new StringBuffer();
		dataBufall.append(" select pk_wa_data from wa_data");
		dataBufall.append(" where checkflag = 'N'");
		dataBufall.append(" and pk_wa_class in ( ");
		dataBufall.append(pk_wa_class_buf.substring(0, pk_wa_class_buf.length()-1));
		dataBufall.append(" )");
		dataBufall.append(" and cyearperiod in ( ");
		dataBufall.append(cyearperiodbuf.substring(0, cyearperiodbuf.length()-1));
		dataBufall.append(" )");
		dataBufall.append(" and nvl(dr,0)=0");
		
		List<Map<String, Object>> alldatalist = (List<Map<String, Object>>)getNcDAO().executeQuery(dataBufall.toString(), new MapListProcessor());
		if(CollectionUtils.isEmpty(alldatalist))
			ExceptionUtils.wrappBusinessException("薪资发放节点数据已全部审核，不允许提交");
		
		//当前会计期间未全部审批，只要对应的数据审批，就可以提交 
		StringBuffer dataBuf = new StringBuffer();
		dataBuf.append(" select pk_wa_data from wa_data");
		dataBuf.append(" where checkflag = 'Y'");
		dataBuf.append(" and pk_wa_class in ( ");
		dataBuf.append(pk_wa_class_buf.substring(0, pk_wa_class_buf.length()-1));
		dataBuf.append(" )");
		dataBuf.append(" and cyearperiod in ( ");
		dataBuf.append(cyearperiodbuf.substring(0, cyearperiodbuf.length()-1));
		dataBuf.append(" )");
		dataBuf.append(" and nvl(dr,0)=0");
		
		List<Map<String, Object>> dataList = (List<Map<String, Object>>)getNcDAO().executeQuery(dataBuf.toString(), new MapListProcessor());
		if(dataList!=null && dataList.size()>0){
			StringBuffer retbuf = new StringBuffer();
			retbuf.append("薪资发放节点数据已审核，不允许提交");
//			for(int i=0;i<dataList.size();i++){
//				retbuf.append(psndocMap.get(dataList.get(i).get("pk_wa_data")));
//				retbuf.append(" ");
//			}
			ExceptionUtils.wrappBusinessException( retbuf.toString());
		}
		
		StringBuffer updatesql = new StringBuffer();
		updatesql.append(" update wa_data_explain");
		updatesql.append(" set iscommit = 'Y'");
		updatesql.append(" where pk_wa_data_explain in ( ");
		updatesql.append(pk_buf.substring(0, pk_buf.length()-1));
		updatesql.append(" )");
		
		int updateret = getNcDAO().executeUpdate(updatesql.toString());
		String sql = "select * from wa_data_explain where pk_wa_data_explain in (" +pk_buf.substring(0, pk_buf.length()-1)+")";
		 List<Wadataexplain> result = (List<Wadataexplain>)getNcDAO().executeQuery(sql,  new BeanListProcessor(Wadataexplain.class));
		
		return result;
	}

	@Override
	public List<Wadataexplain> batchUncommit(StringBuffer pk_wa_class_buf, StringBuffer pk_buf, HashMap<String, String> psndocMap,StringBuffer cyearperiodbuf)
			throws BusinessException {
		// TODO Auto-generated method stub
		//当前会计期间已经 全部审批，不允许提交 
		StringBuffer dataBufall = new StringBuffer();
		dataBufall.append(" select pk_wa_data from wa_data");
		dataBufall.append(" where checkflag = 'N'");
		dataBufall.append(" and pk_wa_class in ( ");
		dataBufall.append(pk_wa_class_buf.substring(0, pk_wa_class_buf.length()-1));
		dataBufall.append(" )");
		dataBufall.append(" and cyearperiod in ( ");
		dataBufall.append(cyearperiodbuf.substring(0, cyearperiodbuf.length()-1));
		dataBufall.append(" )");
		dataBufall.append(" and nvl(dr,0)=0");
		
		List<Map<String, Object>> alldatalist = (List<Map<String, Object>>)getNcDAO().executeQuery(dataBufall.toString(), new MapListProcessor());
		if(CollectionUtils.isEmpty(alldatalist))
			ExceptionUtils.wrappBusinessException("薪资发放节点数据已全部审核，不允许收回");
		
		StringBuffer dataBuf = new StringBuffer();
		dataBuf.append(" select pk_wa_data from wa_data");
		dataBuf.append(" where checkflag = 'Y'");
		dataBuf.append(" and pk_wa_class in ( ");
		dataBuf.append(pk_wa_class_buf.substring(0, pk_wa_class_buf.length()-1));
		dataBuf.append(" )");
		dataBuf.append(" and cyearperiod in ( ");
		dataBuf.append(cyearperiodbuf.substring(0, cyearperiodbuf.length()-1));
		dataBuf.append(" )");
		dataBuf.append(" and nvl(dr,0)=0");
		
		
		List<Map<String, Object>> dataList = (List<Map<String, Object>>)getNcDAO().executeQuery(dataBuf.toString(), new MapListProcessor());
		if(dataList!=null && dataList.size()>0){
			StringBuffer retbuf = new StringBuffer();
			retbuf.append("薪资发放节点数据已审核，不允许收回 ");
//			for(int i=0;i<dataList.size();i++){
//				retbuf.append(psndocMap.get(dataList.get(i).get("pk_wa_data")));
//				retbuf.append(" ");
//			}
			ExceptionUtils.wrappBusinessException( retbuf.toString());
		}
		
		StringBuffer updatesql = new StringBuffer();
		updatesql.append(" update wa_data_explain");
		updatesql.append(" set iscommit = 'N' ,isquote = 'N' " );
		updatesql.append(" where pk_wa_data_explain in ( ");
		updatesql.append(pk_buf.substring(0, pk_buf.length()-1));
		updatesql.append(" )");
		
		int updateret = getNcDAO().executeUpdate(updatesql.toString());
		String sql = "select * from wa_data_explain where pk_wa_data_explain in (" +pk_buf.substring(0, pk_buf.length()-1)+")";
		 List<Wadataexplain> result = (List<Wadataexplain>)getNcDAO().executeQuery(sql,  new BeanListProcessor(Wadataexplain.class));
		
		return result;
	}
	
	
	
	private BaseDAO baseDAO;//NC系统数据源
	private BaseDAO getNcDAO() {
		if(this.baseDAO==null){
			this.baseDAO = new BaseDAO();
		}
		return this.baseDAO;
	}
}
