263 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Java
		
	
	
	
			
		
		
	
	
			263 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Java
		
	
	
	
| package tqq9.lc123.cloud.app.plugin.utils;
 | ||
| 
 | ||
| import com.alibaba.fastjson.JSON;
 | ||
| import com.alibaba.fastjson.JSONObject;
 | ||
| import kd.bos.data.BusinessDataReader;
 | ||
| import kd.bos.dataentity.entity.DynamicObject;
 | ||
| import kd.bos.dataentity.entity.LocaleString;
 | ||
| import kd.bos.dataentity.metadata.IDataEntityType;
 | ||
| import kd.bos.dataentity.resource.ResManager;
 | ||
| import kd.bos.dataentity.utils.StringUtils;
 | ||
| import kd.bos.entity.EntityMetadataCache;
 | ||
| import kd.bos.entity.LinkSetElement;
 | ||
| import kd.bos.entity.LinkSetItemElement;
 | ||
| import kd.bos.entity.MainEntityType;
 | ||
| import kd.bos.entity.botp.LinkRecordType;
 | ||
| import kd.bos.entity.botp.runtime.ConvertOperationResult;
 | ||
| import kd.bos.entity.botp.runtime.PushArgs;
 | ||
| import kd.bos.entity.botp.runtime.SourceBillReport;
 | ||
| import kd.bos.entity.datamodel.IRefrencedataProvider;
 | ||
| import kd.bos.entity.datamodel.ListSelectedRow;
 | ||
| import kd.bos.exception.KDBizException;
 | ||
| import kd.bos.isv.ISVService;
 | ||
| import kd.bos.metadata.botp.ConvertRuleMetadata;
 | ||
| import kd.bos.metadata.botp.ConvertRuleTemplate;
 | ||
| import kd.bos.servicehelper.botp.ConvertMetaServiceHelper;
 | ||
| import kd.bos.servicehelper.botp.ConvertServiceHelper;
 | ||
| 
 | ||
| import java.util.*;
 | ||
| 
 | ||
| /**
 | ||
|  * BOTP代码生成工具类
 | ||
|  */
 | ||
| public class BotpParamUtils {
 | ||
| 
 | ||
|     public static PushArgs getPushArgs(String srcEntityNumber, String targetEntityNumber, Map<String, Object> param, List<Long> pkids)  throws KDBizException {
 | ||
|         return getPushArgs(srcEntityNumber, targetEntityNumber, param, pkids, "");
 | ||
|     }
 | ||
| 
 | ||
|     public static PushArgs getPushArgs(String srcEntityNumber, String targetEntityNumber, Map<String, Object> param, List<Long> pkids, String rule) {
 | ||
|         PushArgs pushArgs = new PushArgs();
 | ||
|         pushArgs.setSourceEntityNumber(srcEntityNumber);		// 必选,源单标识
 | ||
|         pushArgs.setTargetEntityNumber(targetEntityNumber);		// 必选,目标单标识
 | ||
|         pushArgs.setHasRight(true);		// 可选,传入true,不检查目标单新增权
 | ||
|         pushArgs.setAppId("");				// 可选,传入目标单验权使用的应用编码
 | ||
|         pushArgs.setDefOrgId(0L);			// 可选,传入目标单主组织默认值
 | ||
|         pushArgs.setRuleId(rule);				// 可选,传入本次下推使用的转换规则id;传入空值,由系统自动寻找合适的转换规则
 | ||
|         pushArgs.setBuildConvReport(true);	// 是否输出详细错误报告
 | ||
| 
 | ||
|         //传递参数
 | ||
|         if(param != null) {
 | ||
|             for(Map.Entry<String, Object> obj : param.entrySet()) {
 | ||
|                 pushArgs.addCustomParam(obj.getKey(), JSON.toJSONString(obj.getValue()));
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         //原单单据id
 | ||
|         List<ListSelectedRow> selectRows = new ArrayList<>();
 | ||
|         for(Long pkid : pkids) {
 | ||
|             ListSelectedRow row = new ListSelectedRow(pkid);
 | ||
|             selectRows.add(row);
 | ||
|         }
 | ||
|         pushArgs.setSelectedRows(selectRows);
 | ||
|         return pushArgs;
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 按分录下推
 | ||
|      * @param srcEntityNumber 源单标识
 | ||
|      * @param targetEntityNumber 目标标识
 | ||
|      * @param srcEntryEntity 源单分录标识
 | ||
|      * @param param 参数
 | ||
|      * @param entitypkMap 源单分录id
 | ||
|      * @param rule 转换规则id
 | ||
|      * @return
 | ||
|      * @throws KDBizException
 | ||
|      */
 | ||
|     public static PushArgs getPushArgs(String srcEntityNumber, String targetEntityNumber, String srcEntryEntity,
 | ||
|                                        Map<String, Set<JSONObject>> param, HashMap<Long, Set<Long>> entitypkMap, String rule)  throws KDBizException{
 | ||
|         PushArgs pushArgs = new PushArgs();
 | ||
|         pushArgs.setSourceEntityNumber(srcEntityNumber);		// 必选,源单标识
 | ||
|         pushArgs.setTargetEntityNumber(targetEntityNumber);		// 必选,目标单标识
 | ||
|         pushArgs.setHasRight(true);		// 可选,传入true,不检查目标单新增权
 | ||
|         pushArgs.setAppId("");				// 可选,传入目标单验权使用的应用编码
 | ||
|         pushArgs.setDefOrgId(0L);			// 可选,传入目标单主组织默认值
 | ||
|         pushArgs.setRuleId(rule);	// 可选,传入本次下推使用的转换规则id;传入空值,由系统自动寻找合适的转换规则
 | ||
|         pushArgs.setBuildConvReport(true);	// 是否输出详细错误报告
 | ||
| 
 | ||
|         //传递参数
 | ||
|         if(param != null) {
 | ||
|             for(Map.Entry<String, Set<JSONObject>> obj : param.entrySet()) {
 | ||
|                 pushArgs.getCustomParams().put(obj.getKey(),JSON.toJSONString(obj.getValue()));
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         //原单单据id
 | ||
|         ArrayList<ListSelectedRow> auditResult = new ArrayList<ListSelectedRow>();
 | ||
|         for (Map.Entry<Long,  Set<Long>> entry : entitypkMap.entrySet()) {
 | ||
|             Set<Long> value = entry.getValue();
 | ||
|             for (Long v : value) {
 | ||
|                 ListSelectedRow row = new ListSelectedRow();
 | ||
|                 row.setPrimaryKeyValue(entry.getKey());
 | ||
|                 row.setEntryEntityKey(srcEntryEntity);
 | ||
|                 row.setEntryPrimaryKeyValue(v);
 | ||
|                 auditResult.add(row);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         pushArgs.setSelectedRows(auditResult);
 | ||
|         return pushArgs;
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     /**
 | ||
|      * 通过BOTP生成目标对象并返回
 | ||
|      * @param srcEntityNumber
 | ||
|      * @param targetEntityNumber
 | ||
|      * @param pkids
 | ||
|      * @return
 | ||
|      */
 | ||
|     public static List<DynamicObject> getDestObject(String srcEntityNumber, String targetEntityNumber, List<Long> pkids) throws KDBizException{
 | ||
|         PushArgs pushArgs = getPushArgs(srcEntityNumber,targetEntityNumber,null, pkids);
 | ||
|         return getDestObject(pushArgs);
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 根据下推参数返回,目标单对象(new)
 | ||
|      * @param pushArgs
 | ||
|      * @return
 | ||
|      */
 | ||
|     public static List<DynamicObject> getDestObject(PushArgs pushArgs){
 | ||
|         // 调用下推引擎,下推目标单
 | ||
|         ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
 | ||
|         // 判断下推是否成功,如果失败,提炼失败消息输出
 | ||
|         if (!pushResult.isSuccess()) {
 | ||
|             // 错误摘要
 | ||
|             String errMessage = pushResult.getMessage();
 | ||
|             throw new KDBizException(ResManager.loadKDString("下推失败:", "SchemeEngineService_1", "node-debug-mservice_test") + errMessage);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取生成的目标单数据包
 | ||
|         MainEntityType targetMainType = EntityMetadataCache.getDataEntityType(pushArgs.getTargetEntityNumber());
 | ||
|         List<DynamicObject> targetBillObjs = pushResult.loadTargetDataObjects(new IRefrencedataProvider() {
 | ||
|             @Override
 | ||
|             public void fillReferenceData(Object[] objs, IDataEntityType dType) {
 | ||
|                 BusinessDataReader.loadRefence(objs, dType);
 | ||
|             }
 | ||
|         }, targetMainType);
 | ||
|         return targetBillObjs;
 | ||
|     }
 | ||
|     /**
 | ||
|      * 获取下推之后的返回结果
 | ||
|      * @param pushArgs
 | ||
|      * @return
 | ||
|      */
 | ||
|     public static ConvertOperationResult getPushResult(PushArgs pushArgs)  throws KDBizException{
 | ||
|         // 调用下推引擎,下推目标单
 | ||
|         ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
 | ||
| 
 | ||
|         // 判断下推是否成功,如果失败,提炼失败消息输出
 | ||
|         if (!pushResult.isSuccess()) {
 | ||
|             String errMessage = pushResult.getMessage();	// 错误摘要
 | ||
|             for(SourceBillReport billReport : pushResult.getBillReports()) {
 | ||
|                 // 演示如何提取各单错误报告
 | ||
|                 if (!billReport.isSuccess()) {
 | ||
|                     String billMessage = billReport.getFailMessage();
 | ||
|                 }
 | ||
|             }
 | ||
|             throw new KDBizException("下推失败:" + errMessage);
 | ||
|         }
 | ||
| 
 | ||
|         return pushResult;
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 后台下推完成,不考虑展示目标单据界面
 | ||
|      * @param srcEntityNumber:源单标识
 | ||
|      * @param targetEntityNumber:目标单表
 | ||
|      * @param param:参数
 | ||
|      * @param pkids:下推单据id
 | ||
|      * @return
 | ||
|      */
 | ||
|     public static Boolean getBotpSuccess(String srcEntityNumber, String targetEntityNumber, Map<String, Object> param, List<Long> pkids)  throws KDBizException{
 | ||
|         PushArgs pushArgs = getPushArgs(srcEntityNumber, targetEntityNumber, param, pkids);
 | ||
| 
 | ||
|         // 调用下推引擎,下推目标单
 | ||
|         ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
 | ||
| 
 | ||
|         // 判断下推是否成功,如果失败,提炼失败消息输出
 | ||
|         if (!pushResult.isSuccess()) {
 | ||
|             String errMessage = pushResult.getMessage();	// 错误摘要
 | ||
|             for(SourceBillReport billReport : pushResult.getBillReports()) {
 | ||
|                 // 演示如何提取各单错误报告
 | ||
|                 if (!billReport.isSuccess()) {
 | ||
|                     String billMessage = billReport.getFailMessage();
 | ||
|                 }
 | ||
|             }
 | ||
|             throw new KDBizException("下推失败:" + errMessage);
 | ||
|         }
 | ||
|         return true;
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 获取指定:装换规则id
 | ||
|      * @param srcEntityNumber:源单
 | ||
|      * @param targetEntityNumber:目标单
 | ||
|      * @param ruleName:规则名称
 | ||
|      * @return
 | ||
|      */
 | ||
|     public static String getBotpRuleId(String srcEntityNumber,String targetEntityNumber,String ruleName) {
 | ||
|         String ruleId = "";
 | ||
|         List<ConvertRuleMetadata> ruleMetadatas = loadMetas(srcEntityNumber, targetEntityNumber, true);
 | ||
|         Iterator<ConvertRuleMetadata> rootRuleNode = ruleMetadatas.iterator();
 | ||
|         while(rootRuleNode.hasNext()) {
 | ||
|             ConvertRuleMetadata rootNode = rootRuleNode.next();
 | ||
|             LocaleString name = rootNode.getRuleElement().getName(); //规则名称
 | ||
|             if(StringUtils.equals(name.toString(), ruleName)) {
 | ||
|                 ruleId = rootNode.getRuleElement().getId();
 | ||
|             }
 | ||
|         }
 | ||
|         return ruleId;
 | ||
|     }
 | ||
| 
 | ||
|     private static ConvertRuleMetadata createBlankRule(String sourceBill, String targetBill) {
 | ||
|         ConvertRuleTemplate template = new ConvertRuleTemplate();
 | ||
|         ConvertRuleMetadata newRuleMeta = template.CreateBlankRule();
 | ||
|         newRuleMeta.getRuleElement().setSourceEntityNumber(sourceBill);
 | ||
|         newRuleMeta.getRuleElement().setTargetEntityNumber(targetBill);
 | ||
|         newRuleMeta.getRuleElement().setVisibled(true);
 | ||
|         LinkSetElement linkSet;
 | ||
|         if(StringUtils.isNotBlank(targetBill)) {
 | ||
|             linkSet = ConvertMetaServiceHelper.loadLinkSet(targetBill);
 | ||
|             if(linkSet != null && linkSet.getItems().size() > 0) {
 | ||
|                 newRuleMeta.getRuleElement().getLinkEntityPolicy().setTargetEntryKey(((LinkSetItemElement)linkSet.getItems().get(0)).getParentEntityKey());
 | ||
|                 newRuleMeta.getRuleElement().getLinkEntityPolicy().setLinkRecordType(LinkRecordType.Link);
 | ||
|             }
 | ||
|         }
 | ||
|         if(StringUtils.isNotBlank(sourceBill)) {
 | ||
|             linkSet = ConvertMetaServiceHelper.loadLinkSet(sourceBill);
 | ||
|             if(linkSet != null && linkSet.getItems().size() > 0) {
 | ||
|                 newRuleMeta.getRuleElement().getLinkEntityPolicy().setSourceEntryKey(((LinkSetItemElement)linkSet.getItems().get(0)).getParentEntityKey());
 | ||
|             }
 | ||
|         }
 | ||
|         return newRuleMeta;
 | ||
|     }
 | ||
| 
 | ||
|     private static List<ConvertRuleMetadata> loadMetas(String sourceBill, String targetBill, boolean autoCreateNew) {
 | ||
|         List<ConvertRuleMetadata> ruleMetas = ConvertMetaServiceHelper.loadMetas(sourceBill, targetBill);
 | ||
|         if(ruleMetas.isEmpty() && autoCreateNew) {
 | ||
|             ConvertRuleMetadata newRuleMeta = createBlankRule(sourceBill, targetBill);
 | ||
|             newRuleMeta.getRuleElement().setDefRule(true);
 | ||
|             String currentIsv = ISVService.getISVInfo().getId();
 | ||
|             newRuleMeta.setIsv(currentIsv);
 | ||
|             if(StringUtils.isNotBlank(sourceBill) && StringUtils.isNotBlank(targetBill)) {
 | ||
|                 ConvertMetaServiceHelper.save(newRuleMeta);
 | ||
|             }
 | ||
|             ruleMetas.add(newRuleMeta);
 | ||
|         }
 | ||
|         return ruleMetas;
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
| }
 |