共享合并生成凭证问题

This commit is contained in:
zhangzhiguo 2025-07-14 16:15:30 +08:00
parent 0caafcd63d
commit 74447e38ec
1 changed files with 132 additions and 172 deletions

View File

@ -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,173 +33,141 @@ public class TaskListBuildVoucherPlugin extends AbstractListPlugin implements Pl
public void itemClick(ItemClickEvent evt) {
super.itemClick(evt);
String itemKey = evt.getItemKey();
if("zcgj_buildvoucher".equals(itemKey)) {
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(String.format("请选择需要生成凭证的数据。"));
if (selectedRows.isEmpty()) {
this.getView().showTipNotification("请选择需要生成凭证的数据。");
return;
}
List<Long> voucherIds = new ArrayList<>();
Map<String,String> 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<String, List<String>> 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");
Map<String,List<String>> map = new HashMap<>();
//Map<String,String> 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<String> idList = new ArrayList<>();
idList.add(billid);
map.put(entityName,idList);
}
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");
Map<String, Object> voucherOperate = null;
StringBuilder errorMessages = new StringBuilder();
List<Object> successBillIds = new ArrayList<>();
OperationResult operationResult = null;
for (String entityName : map.keySet()) {
List<String> billIdList = map.get(entityName);
voucherOperate = EntityMetadataCache.getDataEntityOperate(entityName, "generatevoucher");
operationResult = OperationServiceHelper.executeOperate((String)voucherOperate.get("key"), entityName, billIdList.toArray(), option);
for (Map.Entry<String, List<String>> entry : entityBillMap.entrySet()) {
Map<String, Object> operateInfo = EntityMetadataCache.getDataEntityOperate(entry.getKey(), "generatevoucher");
OperationResult result = OperationServiceHelper.executeOperate(
(String) operateInfo.get("key"),
entry.getKey(),
entry.getValue().toArray(),
option
);
if (!operationResult.isSuccess()) {
List<IOperateInfo> allErrorInfo = operationResult.getAllErrorOrValidateInfo();
allErrorInfo.forEach((error) -> sb.append(error.getMessage()));
errorCount++;
if (result.isSuccess()) {
successBillIds.addAll(result.getSuccessPkIds());
} else {
//VoucherUtil.viewVoucher(billid, this.getView(), true);
List<Object> successPkIds = operationResult.getSuccessPkIds();
successBillIds.addAll(successPkIds);
Map<Object, String> billNos = operationResult.getBillNos();
result.getAllErrorOrValidateInfo().forEach(err -> errorMessages.append(err.getMessage()));
}
}
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<Long> vids = new ArrayList<>();
Set<Long> vidsSet = new HashSet<>();//去重
for (Object successBillId : successBillIds) {
List<Long> 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);
showVoucherResult(successBillIds, errorMessages.toString());
this.getView().invokeOperation("refresh");
}
private void showVoucherResult(List<Object> successBillIds, String errorMessage) {
if (errorMessage.isEmpty()) {
Set<Long> voucherIdSet = new HashSet<>();
List<Long> allVoucherIds = new ArrayList<>();
for (Object billIdObj : successBillIds) {
List<Long> voucherIds = VoucherUtil.getVouncheridByBillid(Long.parseLong(billIdObj.toString()));
allVoucherIds.addAll(voucherIds);
voucherIdSet.addAll(voucherIds);
}
}else{//有失败的使用警告弹框提醒
//this.getView().showTipNotification(String.format("凭证生成完成,一共%d条成功%d条失败%d条。",allCount,successCount,errorCount));
this.getView().showTipNotification(sb.toString());
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);
}
this.getView().invokeOperation("refresh");//调用表单的刷新操作
} else {
this.getView().showTipNotification(errorMessage);
}
else if("zcgj_deletevoucher".equals(itemKey)){
}
private void handleDeleteVoucher() {
BillList billList = this.getView().getControl(AbstractListPlugin.BILLLISTID);
//获取到选中行的数据
ListSelectedRowCollection selectedRows = billList.getSelectedRows();
if(selectedRows.isEmpty()){
this.getView().showTipNotification(String.format("请选择需要删除凭证的数据。"));
if (selectedRows.isEmpty()) {
this.getView().showTipNotification("请选择需要删除凭证的数据。");
return;
}
List<Long> voucherIds = new ArrayList<>();
int errorCount =0;
int allCount =0;
int successCount =0;
StringBuilder sb = new StringBuilder();
Map<String,List<String>> 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<String> idList = new ArrayList<>();
idList.add(billid);
map.put(entityName,idList);
}
Map<String, List<String>> 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();
OperationResult operationResult = null;
Map<String, Object> voucherOperate = null;
for (String entityName : map.keySet()) {
List<String> billIds = map.get(entityName);
for (String billId : billIds) {
List<Long> 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<IOperateInfo> allErrorInfo = operationResult.getAllErrorOrValidateInfo();
allErrorInfo.forEach((error) -> sb.append(error.getMessage()));
// this.getView().showTipNotification(sb.toString());
log.info("删除凭证失败单据id" + billId + ",失败原因:" + sb);
errorCount++;
StringBuilder errorMessages = new StringBuilder();
for (Map.Entry<String, List<String>> entry : entityBillMap.entrySet()) {
String entityName = entry.getKey();
for (String billId : entry.getValue()) {
List<Long> voucherIds = VoucherUtil.getVouncheridByBillid(Long.parseLong(billId));
if (voucherIds.isEmpty()) {
this.getView().showTipNotification("单据不存在关联凭证,或凭证已删除。");
continue;
}
Map<String, Object> 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");
}
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");//调用表单的刷新操作
}
}
}