diff --git a/code/zcdev/zcgj-zcdev-zcdev-fs/src/main/java/zcgj/zcdev/zcdev/fs/plugin/form/TaskListBuildVoucherPlugin.java b/code/zcdev/zcgj-zcdev-zcdev-fs/src/main/java/zcgj/zcdev/zcdev/fs/plugin/form/TaskListBuildVoucherPlugin.java index e0de2e3..5c24f7f 100644 --- a/code/zcdev/zcgj-zcdev-zcdev-fs/src/main/java/zcgj/zcdev/zcdev/fs/plugin/form/TaskListBuildVoucherPlugin.java +++ b/code/zcdev/zcgj-zcdev-zcdev-fs/src/main/java/zcgj/zcdev/zcdev/fs/plugin/form/TaskListBuildVoucherPlugin.java @@ -1,8 +1,5 @@ package zcgj.zcdev.zcdev.fs.plugin.form; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.alibaba.fastjson.TypeReference; import kd.bos.dataentity.OperateOption; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.resource.ResManager; @@ -11,7 +8,6 @@ import kd.bos.entity.datamodel.ListSelectedRow; import kd.bos.entity.datamodel.ListSelectedRowCollection; import kd.bos.entity.operate.result.IOperateInfo; import kd.bos.entity.operate.result.OperationResult; -import kd.bos.form.IFormView; import kd.bos.form.ShowType; import kd.bos.form.control.events.ItemClickEvent; import kd.bos.list.BillList; @@ -19,10 +15,6 @@ import kd.bos.list.ListShowParameter; import kd.bos.list.plugin.AbstractListPlugin; import kd.bos.logging.Log; import kd.bos.logging.LogFactory; -import kd.bos.openapi.common.result.OpenApiResult; -import kd.bos.openapi.common.util.OpenApiSdkUtil; -import kd.bos.orm.query.QCP; -import kd.bos.orm.query.QFilter; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.OperationServiceHelper; import kd.sdk.plugin.Plugin; @@ -41,174 +33,142 @@ public class TaskListBuildVoucherPlugin extends AbstractListPlugin implements Pl public void itemClick(ItemClickEvent evt) { super.itemClick(evt); String itemKey = evt.getItemKey(); - if("zcgj_buildvoucher".equals(itemKey)) { - BillList billList = this.getView().getControl(AbstractListPlugin.BILLLISTID); - //获取到选中行的数据 - ListSelectedRowCollection selectedRows = billList.getSelectedRows(); - if(selectedRows.isEmpty()){ - this.getView().showTipNotification(String.format("请选择需要生成凭证的数据。")); - return; - } - List voucherIds = new ArrayList<>(); - Map par= new HashMap<>(); - //{"ignorebillstatus":"true"} - par.put("ignorebillstatus","true");//忽略单据状态 - //par.put("$key$","ignorebillstatus");//冗余参数 - //par.put("$value$","true");//冗余参数 - - int errorCount =0; - int allCount =0; - int successCount =0; - StringBuilder sb = new StringBuilder(); - - Map> map = new HashMap<>(); - //Map idNumberMap = new HashMap<>(); - for (ListSelectedRow selectedRow : selectedRows) { - Long pk = (Long) selectedRow.getPrimaryKeyValue(); - DynamicObject taskobj = BusinessDataServiceHelper.loadSingle(pk, "task_task"); - DynamicObject billtype = taskobj.getDynamicObject("billtype"); - String entityName = billtype.getDynamicObject("bindbill").getString("number"); - String billid = taskobj.getString("billid"); - //messageBill.put(billid,taskobj.getString("billnumber")+":"+taskobj.getString("subject")); - String billnumber = taskobj.getString("billnumber"); - log.info("TaskListBuildVoucherPlugin:billid:"+billid+" billnumber:"+billnumber); - //idNumberMap.put(billid,billnumber); - if(map.containsKey(entityName)){ - map.get(entityName).add(billid); - }else{ - List idList = new ArrayList<>(); - idList.add(billid); - map.put(entityName,idList); - } - } - OperateOption option = OperateOption.create(); - option.setVariableValue("ignoreValidation", "true"); - option.setVariableValue("ignorebillstatus", "true"); - Map voucherOperate = null; - List successBillIds = new ArrayList<>(); - - OperationResult operationResult = null; - for (String entityName : map.keySet()) { - List billIdList = map.get(entityName); - voucherOperate = EntityMetadataCache.getDataEntityOperate(entityName, "generatevoucher"); - operationResult = OperationServiceHelper.executeOperate((String)voucherOperate.get("key"), entityName, billIdList.toArray(), option); - - if (!operationResult.isSuccess()) { - List allErrorInfo = operationResult.getAllErrorOrValidateInfo(); - allErrorInfo.forEach((error) -> sb.append(error.getMessage())); - errorCount++; - } else { - //VoucherUtil.viewVoucher(billid, this.getView(), true); - List successPkIds = operationResult.getSuccessPkIds(); - successBillIds.addAll(successPkIds); - Map billNos = operationResult.getBillNos(); - } - } - - if(errorCount == 0){//没有失败的,使用成功弹框提醒 - //this.getView().showSuccessNotification(String.format("凭证生成完成,一共%d条,成功:%d条,失败:%d条。",allCount,successCount,errorCount)); - if(successBillIds.size() ==1 ){ - VoucherUtil.viewVoucher(String.valueOf(successBillIds.get(0)), this.getView(), true); - }else{ - List vids = new ArrayList<>(); - Set vidsSet = new HashSet<>();//去重 - for (Object successBillId : successBillIds) { - List vouncheridByBillid = VoucherUtil.getVouncheridByBillid(Long.valueOf(String.valueOf(successBillId))); - // voucherIdList.addAll(vouncheridByBillid); - vids.addAll(vouncheridByBillid); - vidsSet.addAll(vouncheridByBillid); - } - if(vidsSet.size() ==1){ - VoucherUtil.viewVoucher(String.valueOf(successBillIds.get(0)), this.getView(), true); - }else{ - ListShowParameter listShowParameter = new ListShowParameter(); - listShowParameter.setFormId("bos_list"); - listShowParameter.setBillFormId("gl_voucher"); - listShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage); - // 设置联查PK - for (Long voucherId : vids) { - listShowParameter.addLinkQueryPkId(voucherId); - } - this.getView().showForm(listShowParameter); - } - - } - }else{//有失败的,使用警告弹框提醒 - //this.getView().showTipNotification(String.format("凭证生成完成,一共%d条,成功:%d条,失败:%d条。",allCount,successCount,errorCount)); - this.getView().showTipNotification(sb.toString()); - } - this.getView().invokeOperation("refresh");//调用表单的刷新操作 - } - else if("zcgj_deletevoucher".equals(itemKey)){ - BillList billList = this.getView().getControl(AbstractListPlugin.BILLLISTID); - //获取到选中行的数据 - ListSelectedRowCollection selectedRows = billList.getSelectedRows(); - if(selectedRows.isEmpty()){ - this.getView().showTipNotification(String.format("请选择需要删除凭证的数据。")); - return; - } - List voucherIds = new ArrayList<>(); - int errorCount =0; - int allCount =0; - int successCount =0; - StringBuilder sb = new StringBuilder(); - Map> map = new HashMap<>(); - for (ListSelectedRow selectedRow : selectedRows) { - Long pk = (Long) selectedRow.getPrimaryKeyValue(); - DynamicObject taskobj = BusinessDataServiceHelper.loadSingle(pk, "task_task"); - DynamicObject billtype = taskobj.getDynamicObject("billtype"); - DynamicObject bindbill = billtype.getDynamicObject("bindbill"); //源单 - String billnumber = taskobj.getString("billnumber"); - String entityName = bindbill.getString("number"); - //String vouchernum = bindbill.getString("vouchernum"); - String billid = taskobj.getString("billid"); - //log.info("TaskListBuildVoucherPlugin-delete:vouchernum:"+vouchernum); - log.info("TaskListBuildVoucherPlugin-delete:billid:"+billid); - if(map.containsKey(entityName)){ - map.get(entityName).add(billid); - }else{ - List idList = new ArrayList<>(); - idList.add(billid); - map.put(entityName,idList); - } - } - OperateOption option = OperateOption.create(); - OperationResult operationResult = null; - Map voucherOperate = null; - for (String entityName : map.keySet()) { - List billIds = map.get(entityName); - for (String billId : billIds) { - List vouncherids = VoucherUtil.getVouncheridByBillid(Long.parseLong(billId)); - if (vouncherids.isEmpty()) { - this.getView().showTipNotification(ResManager.loadKDString("单据不存在关联凭证。", "VoucherUtil_4", "ssc-task-formplugin", new Object[0])); - }else{ - voucherOperate = EntityMetadataCache.getDataEntityOperate(entityName, "deletevoucher"); - operationResult = OperationServiceHelper.executeOperate((String)voucherOperate.get("key"), entityName, billIds.toArray(), option); - if (!operationResult.isSuccess()) { - List allErrorInfo = operationResult.getAllErrorOrValidateInfo(); - allErrorInfo.forEach((error) -> sb.append(error.getMessage())); - // this.getView().showTipNotification(sb.toString()); - log.info("删除凭证失败,单据id:" + billId + ",失败原因:" + sb); - errorCount++; - } - } - - } - - } - if(errorCount == 0){//没有失败的,使用成功弹框提醒 - // this.getView().showSuccessNotification(String.format("删除凭证完成,一共%d条,成功:%d条,失败:%d条。",allCount,successCount,errorCount)); - String text = "成功删除凭证。"; - this.getView().showSuccessNotification(String.format(ResManager.loadKDString(" %s", "TaskVoucherApproveFormPlugin_3", "ssc-task-formplugin", new Object[0]), text)); - }else{//有失败的,使用警告弹框提醒 - //this.getView().showTipNotification(String.format("删除凭证完成,一共%d条,成功:%d条,失败:%d条。",allCount,successCount,errorCount)); - this.getView().showTipNotification(sb.toString()); - } - this.getView().invokeOperation("refresh");//调用表单的刷新操作 + if ("zcgj_buildvoucher".equals(itemKey)) { + handleBuildVoucher(); + } else if ("zcgj_deletevoucher".equals(itemKey)) { + handleDeleteVoucher(); } } + private void handleBuildVoucher() { + BillList billList = this.getView().getControl(AbstractListPlugin.BILLLISTID); + ListSelectedRowCollection selectedRows = billList.getSelectedRows(); + + if (selectedRows.isEmpty()) { + this.getView().showTipNotification("请选择需要生成凭证的数据。"); + return; + } + + Map> entityBillMap = new HashMap<>(); + for (ListSelectedRow row : selectedRows) { + DynamicObject task = BusinessDataServiceHelper.loadSingle((Long) row.getPrimaryKeyValue(), "task_task"); + String entityName = task.getDynamicObject("billtype").getDynamicObject("bindbill").getString("number"); + String billId = task.getString("billid"); + + log.info("生成凭证: billid=" + billId + ", billnumber=" + task.getString("billnumber")); + entityBillMap.computeIfAbsent(entityName, k -> new ArrayList<>()).add(billId); + } + + OperateOption option = OperateOption.create(); + option.setVariableValue("ignoreValidation", "true"); + option.setVariableValue("ignorebillstatus", "true"); + + StringBuilder errorMessages = new StringBuilder(); + List successBillIds = new ArrayList<>(); + + for (Map.Entry> entry : entityBillMap.entrySet()) { + Map operateInfo = EntityMetadataCache.getDataEntityOperate(entry.getKey(), "generatevoucher"); + OperationResult result = OperationServiceHelper.executeOperate( + (String) operateInfo.get("key"), + entry.getKey(), + entry.getValue().toArray(), + option + ); + + if (result.isSuccess()) { + successBillIds.addAll(result.getSuccessPkIds()); + } else { + result.getAllErrorOrValidateInfo().forEach(err -> errorMessages.append(err.getMessage())); + } + } + + showVoucherResult(successBillIds, errorMessages.toString()); + this.getView().invokeOperation("refresh"); + } + + private void showVoucherResult(List successBillIds, String errorMessage) { + if (errorMessage.isEmpty()) { + Set voucherIdSet = new HashSet<>(); + List allVoucherIds = new ArrayList<>(); + + for (Object billIdObj : successBillIds) { + List voucherIds = VoucherUtil.getVouncheridByBillid(Long.parseLong(billIdObj.toString())); + allVoucherIds.addAll(voucherIds); + voucherIdSet.addAll(voucherIds); + } + + if (voucherIdSet.size() == 1) { + VoucherUtil.viewVoucher(successBillIds.get(0).toString(), this.getView(), true); + } else { + ListShowParameter param = new ListShowParameter(); + param.setFormId("bos_list"); + param.setBillFormId("gl_voucher"); + param.getOpenStyle().setShowType(ShowType.MainNewTabPage); + allVoucherIds.forEach(param::addLinkQueryPkId); + this.getView().showForm(param); + } + } else { + this.getView().showTipNotification(errorMessage); + } + } + + private void handleDeleteVoucher() { + BillList billList = this.getView().getControl(AbstractListPlugin.BILLLISTID); + ListSelectedRowCollection selectedRows = billList.getSelectedRows(); + + if (selectedRows.isEmpty()) { + this.getView().showTipNotification("请选择需要删除凭证的数据。"); + return; + } + + Map> entityBillMap = new HashMap<>(); + for (ListSelectedRow row : selectedRows) { + DynamicObject task = BusinessDataServiceHelper.loadSingle((Long) row.getPrimaryKeyValue(), "task_task"); + String entityName = task.getDynamicObject("billtype").getDynamicObject("bindbill").getString("number"); + String billId = task.getString("billid"); + + log.info("删除凭证: billid=" + billId); + entityBillMap.computeIfAbsent(entityName, k -> new ArrayList<>()).add(billId); + } + + OperateOption option = OperateOption.create(); + StringBuilder errorMessages = new StringBuilder(); + + for (Map.Entry> entry : entityBillMap.entrySet()) { + String entityName = entry.getKey(); + for (String billId : entry.getValue()) { + List voucherIds = VoucherUtil.getVouncheridByBillid(Long.parseLong(billId)); + if (voucherIds.isEmpty()) { + this.getView().showTipNotification("单据不存在关联凭证,或凭证已删除。"); + continue; + } + + Map operateInfo = EntityMetadataCache.getDataEntityOperate(entityName, "deletevoucher"); + OperationResult result = OperationServiceHelper.executeOperate( + (String) operateInfo.get("key"), + entityName, + entry.getValue().toArray(), + option + ); + + if (!result.isSuccess()) { + result.getAllErrorOrValidateInfo().forEach(err -> errorMessages.append(err.getMessage())); + log.info("删除凭证失败,单据id:" + billId + ",失败原因:" + errorMessages); + } + } + } + + if (errorMessages.length() == 0) { + this.getView().showSuccessNotification("成功删除凭证。"); + } else { + this.getView().showTipNotification(errorMessages.toString()); + } + + this.getView().invokeOperation("refresh"); + } + + }