278 lines
8.1 KiB
Java
278 lines
8.1 KiB
Java
package tqq9.lc123.cloud.app.api.utils;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import kd.bos.dataentity.entity.DynamicObject;
|
||
import kd.bos.dataentity.utils.StringUtils;
|
||
import kd.bos.entity.operate.result.IOperateInfo;
|
||
import kd.bos.openapi.common.custom.annotation.ApiModel;
|
||
import kd.bos.openapi.common.custom.annotation.ApiParam;
|
||
|
||
import java.io.Serializable;
|
||
import java.math.BigDecimal;
|
||
import java.util.*;
|
||
|
||
@ApiModel
|
||
public class ApiResultExt implements Serializable {
|
||
@ApiParam("操作失败数量")
|
||
private int failCount;
|
||
@ApiParam("操作成功数量")
|
||
private int successCount;
|
||
@ApiParam("返回结果")
|
||
private List<ResultBean> result;
|
||
|
||
public int getFailCount() {
|
||
return failCount;
|
||
}
|
||
|
||
public void setFailCount(int failCount) {
|
||
this.failCount = failCount;
|
||
}
|
||
|
||
public int getSuccessCount() {
|
||
return successCount;
|
||
}
|
||
|
||
public void setSuccessCount(int successCount) {
|
||
this.successCount = successCount;
|
||
}
|
||
|
||
public List<ResultBean> getResult() {
|
||
return result;
|
||
}
|
||
|
||
public void setResult(List<ResultBean> result) {
|
||
this.result = result;
|
||
}
|
||
|
||
@ApiModel
|
||
public static class ResultBean implements Serializable {
|
||
|
||
|
||
@ApiParam("数据索引")
|
||
private int billIndex;
|
||
@ApiParam("接口操作是否成功")
|
||
private boolean billStatus;
|
||
@ApiParam("错误信息")
|
||
private Error errors;
|
||
@ApiParam("金蝶单据id")
|
||
private String id;
|
||
@ApiParam("候选键信息")
|
||
private JSONObject keys;
|
||
@ApiParam("金蝶单据编号")
|
||
private String number;
|
||
@ApiParam("接口操作类型")
|
||
private String type;
|
||
|
||
public int getBillIndex() {
|
||
return billIndex;
|
||
}
|
||
|
||
public void setBillIndex(int billIndex) {
|
||
this.billIndex = billIndex;
|
||
}
|
||
|
||
public boolean isBillStatus() {
|
||
return billStatus;
|
||
}
|
||
|
||
public void setBillStatus(boolean billStatus) {
|
||
this.billStatus = billStatus;
|
||
}
|
||
|
||
public Error getErrors() {
|
||
return errors;
|
||
}
|
||
|
||
public void setErrors(Error errors) {
|
||
this.errors = errors;
|
||
}
|
||
|
||
public String getId() {
|
||
return id;
|
||
}
|
||
|
||
public void setId(String id) {
|
||
this.id = id;
|
||
}
|
||
|
||
public JSONObject getKeys() {
|
||
return keys;
|
||
}
|
||
|
||
public void setKeys(JSONObject keys) {
|
||
this.keys = keys;
|
||
}
|
||
|
||
public String getNumber() {
|
||
return number;
|
||
}
|
||
|
||
public void setNumber(String number) {
|
||
this.number = number;
|
||
}
|
||
|
||
public String getType() {
|
||
return type;
|
||
}
|
||
|
||
public void setType(String type) {
|
||
this.type = type;
|
||
}
|
||
}
|
||
|
||
public static class Error implements Serializable {
|
||
private String entityKey;
|
||
private String entryRowIndex;
|
||
private JSONObject keys;
|
||
private ArrayList<String> rowMsg;
|
||
private String subEntryRowIndex;
|
||
|
||
// Getter 和 Setter
|
||
public String getEntityKey() {
|
||
return entityKey;
|
||
}
|
||
|
||
public void setEntityKey(String entityKey) {
|
||
this.entityKey = entityKey;
|
||
}
|
||
|
||
public String getEntryRowIndex() {
|
||
return entryRowIndex;
|
||
}
|
||
|
||
public void setEntryRowIndex(String entryRowIndex) {
|
||
this.entryRowIndex = entryRowIndex;
|
||
}
|
||
|
||
public JSONObject getKeys() {
|
||
return keys;
|
||
}
|
||
|
||
public void setKeys(JSONObject keys) {
|
||
this.keys = keys;
|
||
}
|
||
|
||
public ArrayList<String> getRowMsg() {
|
||
return rowMsg;
|
||
}
|
||
|
||
public void setRowMsg(ArrayList<String> rowMsg) {
|
||
this.rowMsg = rowMsg;
|
||
}
|
||
|
||
public String getSubEntryRowIndex() {
|
||
return subEntryRowIndex;
|
||
}
|
||
|
||
public void setSubEntryRowIndex(String subEntryRowIndex) {
|
||
this.subEntryRowIndex = subEntryRowIndex;
|
||
}
|
||
}
|
||
|
||
public HashMap<String,Object> addErrorToResultBeanByNumber(List<ResultBean> resultBeans, IOperateInfo iOperateInfo, String entityKey) {
|
||
Set<String> idSet = new HashSet<>();
|
||
for (ResultBean resultBean : resultBeans) {
|
||
Error error = resultBean.getErrors();
|
||
String message = iOperateInfo.getMessage();
|
||
String id = iOperateInfo.getPkValue().toString();
|
||
if (StringUtils.equals(resultBean.getId(),id)) {
|
||
resultBean.setBillStatus(false);
|
||
if (error == null) {
|
||
error = new Error();
|
||
error.setRowMsg(new ArrayList<String>());
|
||
}
|
||
error.setKeys(resultBean.getKeys());
|
||
error.setEntityKey(entityKey);
|
||
ArrayList<String> rowMsg = error.getRowMsg();
|
||
rowMsg.add(message);
|
||
error.setRowMsg(rowMsg);
|
||
resultBean.setErrors(error);
|
||
break;
|
||
}
|
||
}
|
||
HashMap<String, Object> returnMap = new HashMap<>();
|
||
returnMap.put("ResultBeanList",resultBeans);
|
||
return returnMap;
|
||
}
|
||
|
||
public HashMap<String,Object> addOneErrorToResultBeanByNumber(List<ResultBean> resultBeans, String errormsg, String entityKey) {
|
||
Set<String> idSet = new HashSet<>();
|
||
for (ResultBean resultBean : resultBeans) {
|
||
Error error = resultBean.getErrors();
|
||
resultBean.setBillStatus(false);
|
||
if (error == null) {
|
||
error = new Error();
|
||
error.setRowMsg(new ArrayList<String>());
|
||
}
|
||
error.setKeys(resultBean.getKeys());
|
||
error.setEntityKey(entityKey);
|
||
ArrayList<String> rowMsg = error.getRowMsg();
|
||
rowMsg.add(errormsg);
|
||
error.setRowMsg(rowMsg);
|
||
resultBean.setErrors(error);
|
||
break;
|
||
}
|
||
HashMap<String, Object> returnMap = new HashMap<>();
|
||
returnMap.put("ResultBeanList",resultBeans);
|
||
return returnMap;
|
||
}
|
||
|
||
public static DynamicObject[] removeBillByBillno(DynamicObject[] bills, Set<String> idSet) {
|
||
DynamicObject[] tempBills = new DynamicObject[bills.length];
|
||
int index = 0;
|
||
|
||
// 遍历原数组,复制不需要删除的元素到 tempBills
|
||
for (DynamicObject bill : bills) {
|
||
if (!idSet.contains(bill.getString("id"))) {
|
||
tempBills[index++] = bill;
|
||
}
|
||
}
|
||
// 使用 Arrays.copyOf 修剪数组到实际大小
|
||
return Arrays.copyOf(tempBills, index);
|
||
}
|
||
|
||
/**
|
||
* 校验必填项
|
||
* @param value
|
||
* @param type
|
||
* @return
|
||
*/
|
||
public static boolean validateRequired(Object value, String type) {
|
||
if (value == null) {
|
||
return true; // 空值直接返回 true
|
||
}
|
||
|
||
switch (type) {
|
||
case "String":
|
||
return ((String) value).isEmpty(); // 字符串为空返回 true
|
||
case "BigDecimal":
|
||
try {
|
||
new BigDecimal(value.toString()); // 如果能转为BigDecimal,则返回 false
|
||
return false;
|
||
} catch (NumberFormatException e) {
|
||
return true; // 无法转为BigDecimal返回 true
|
||
}
|
||
default:
|
||
return true; // 如果没有匹配的类型,返回 true
|
||
}
|
||
}
|
||
public static ApiResultExt errorRetrun(String entityType, String errormsg, Error error, ApiResultExt resultExt) {
|
||
List<ResultBean> result = resultExt.getResult();
|
||
ResultBean resultBean = result.get(0);
|
||
JSONObject key = resultBean.getKeys();
|
||
List<String> rowMsg = error.getRowMsg();
|
||
rowMsg.add(errormsg);
|
||
error.setEntityKey(entityType);
|
||
error.setKeys(key);
|
||
resultBean.setId("");
|
||
resultBean.setBillStatus(false);
|
||
resultBean.setErrors(error);
|
||
resultExt.setFailCount(1);
|
||
resultExt.setSuccessCount(0);
|
||
resultExt.setResult(result);
|
||
return resultExt;
|
||
}
|
||
|
||
|
||
}
|