package com.kingdee.eas.custom.facade.IntelligentRules;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.basedata.hraux.StateEnum;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.compensation.ExcuteSatate;
import com.kingdee.shr.compensation.app.intelligent.CmpIntelRuleFactory;
import com.kingdee.shr.compensation.app.intelligent.CmpIntelRuleInfo;
import com.kingdee.shr.compensation.app.intelligent.ICmpIntelRule;
import com.kingdee.shr.compensation.exception.CmpMatchException;
import com.kingdee.shr.compensation.intelligent.execution.CmpIntelExecutor;
import com.kingdee.shr.compensation.intelligent.util.CmpProgressHelper;

public class IntelligentRulesFacadeControllerBean extends AbstractIntelligentRulesFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.facade.IntelligentRules.IntelligentRulesFacadeControllerBean");
    
    @Override
    protected void _rules(Context ctx, String ruleNumber) throws BOSException, EASBizException {
    	String number = "GTIIT001";
    	if(StringUtils.isNotBlank(ruleNumber)) {
    		number = ruleNumber;
    	}
    	logger.error("智能匹配规则编码："+number);
    	String sql = "select * from T_HR_SCmpIntelRule where fnumber = '"+number+"'";
    	logger.error("查询智能匹配规则SQL："+sql);
    	IRowSet rowSet = DbUtil.executeQuery(ctx, sql);
    	String ruleBillId = "";
    	try {
			while(rowSet.next()) {
				ruleBillId = rowSet.getString("fid");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	logger.error("智能匹配规则ID："+ruleBillId);
    	if(StringUtils.isBlank(ruleBillId)) {
    		return;
    	}
    	CoreBaseCollection colls = new CoreBaseCollection();
		try {
			//调用标准执行操作
			ICmpIntelRule cmpIntelRule = CmpIntelRuleFactory.getLocalInstance(ctx);
			CmpIntelRuleInfo ruleInfo = cmpIntelRule.getCmpIntelRuleInfo((IObjectPK)new ObjectUuidPK(ruleBillId));
			checkRuleStatus(ctx, ruleInfo);
			boolean debug = false;
			CmpProgressHelper progressHelper = CmpProgressHelper.getInstance();
			CmpIntelExecutor executor = new CmpIntelExecutor();
			executor.setCtx(ctx);
			executor.setProgressHelper(progressHelper);
			executor.setDebug(debug);
			List<String> ruleList = new ArrayList<>();
			ruleList.add(ruleBillId);
			Map<String, Object> param = new HashMap<>();
			param.put("ruleIds", ruleList);
			executor.execute(param);
			ruleInfo.setExcuteState(ExcuteSatate.finish);
			colls.add((CoreBaseInfo)ruleInfo);
			cmpIntelRule.save(colls);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    }
    
    private void checkRuleStatus(Context ctx, CmpIntelRuleInfo ruleInfo) throws  BOSException, EASBizException {
		StateEnum state = ruleInfo.getState();
		ExcuteSatate excuteSatate = ruleInfo.getExcuteState();
		ICmpIntelRule cmpIntelRule = CmpIntelRuleFactory.getLocalInstance(ctx);
		if (StateEnum.DISABLE.equals(state)) {
			throw new BOSException(new CmpMatchException(CmpMatchException.RULENORUN));
		}
		if (ExcuteSatate.excuting.equals(excuteSatate)) {
			throw new BOSException(new CmpMatchException(CmpMatchException.EXCUTING, new Object[] { ruleInfo.getName() }));
		}
		ruleInfo.setExcuteState(ExcuteSatate.excuting);
		cmpIntelRule.save((CoreBaseInfo)ruleInfo);
	}
}