科目初始化优化

This commit is contained in:
yuxueliang0813 2025-04-03 14:59:21 +08:00
parent bd10bc0f4c
commit 670c39f54f
2 changed files with 234 additions and 176 deletions

View File

@ -47,95 +47,37 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
super.beforeImportEntry(e); super.beforeImportEntry(e);
//处理导入的excel数据 注意 excel中的列名必须与分录的列标识一致否则无法识别到excel中的数据 //处理导入的excel数据 注意 excel中的列名必须与分录的列标识一致否则无法识别到excel中的数据
HashMap itemEntry = (HashMap) e.getSource();//excel表格全量数据 HashMap itemEntry = (HashMap) e.getSource();//excel表格全量数据
ArrayList list = (ArrayList) itemEntry.get("shjh_details");//excel表格的某个页签 ArrayList list = (ArrayList) itemEntry.get("shjh_details");//excel表格的某个页签-科目基本信息
Iterator iterator = list.iterator();//遍历页签 Iterator iterator = list.iterator();//遍历页签
ImportEntryData importData; ImportEntryData importData;
JSONObject rowdata; JSONObject rowdata;
while (iterator.hasNext()) {
importData = (ImportEntryData) iterator.next();
rowdata = importData.getData();//具体的某行数据key值为列名
}
}
/**
* 按钮监听注册
*/
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
//确认按钮
// Button selectedButton = this.getView().getControl(OK_BUTTON_KEY);
// selectedButton.addClickListener(this);
}
/**
* 按钮点击实现方法
*/
@Override
public void click(EventObject evt) {
try {
Control source = (Control) evt.getSource();
String key = source.getKey();
if (StringUtils.equals(OK_BUTTON_KEY, key)) {
// 点击确认按钮
String params = (String) this.getModel().getValue("shjh_largetextfield");
if(EsbUtils.isEmpty(params)){
this.getView().showMessage("请输入科目JSON格式参数");
return ;
}
params = params.replace("\n","").replace("\t","");
JSONObject json_obj = JSONObject.parseObject(params);
JSONArray detailsJson = json_obj.getJSONArray("items");
if(detailsJson == null){
this.getView().showMessage("未识别到items参数");
return ;
}
JSONArray itemsJson = new JSONArray();//返回值明细集合
JSONObject itemInfo;//返回值明细对象
String number;//科目编号 String number;//科目编号
String name;//科目名称 String name;//科目名称
String acctType;//科目类型 资产负债类 损益类 String acctType;//科目类型 资产负债类 损益类
DynamicObject acctInfo; DynamicObject acctInfo;
DynamicObject acctTypeInfo; DynamicObject acctTypeInfo;
JSONObject json_body;
Map<String, Long> acctids = new HashMap<>();//科目编号和ID对应关系 Map<String, Long> acctids = new HashMap<>();//科目编号和ID对应关系
Map<String, DynamicObject> accountMaps = new HashMap<>();//将科目编号与对象关联 Map<String, DynamicObject> accountMaps = new HashMap<>();//将科目编号与对象关联
//先根据基础信息在集团层面生成科目不考虑核算维度使用逐级分配 //先根据基础信息在集团层面生成科目不考虑核算维度使用逐级分配
//再根据公司信息进行科目分配 //再根据公司信息进行科目分配
//最后处理每个公司下科目的核算维度和禁用状态 //最后处理每个公司下科目的核算维度和禁用状态
for (int i = 0; i < detailsJson.size(); i++) { StringBuilder resultstr = new StringBuilder();
json_body = detailsJson.getJSONObject(i); while (iterator.hasNext()) {
number = json_body.getString("code"); importData = (ImportEntryData) iterator.next();
name = json_body.getString("name"); rowdata = importData.getData();//具体的某行数据key值为列名
acctType = json_body.getString("type"); if("0".equals(rowdata.getString("shjh_status"))){
if(EsbUtils.isEmpty(number) || EsbUtils.isEmpty(name) || EsbUtils.isEmpty(acctType)){ //科目状态为0不导入
log.error(String.format("会计科目接口入参为空异常:%s", json_body.toJSONString()));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","入参值为空");
itemsJson.add(itemInfo);
continue; continue;
} }
//根据科目编号和集团ID查找对应科目是否已存在 number = rowdata.getString("shjh_code");//科目编号
acctInfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",number), name = rowdata.getString("shjh_name");//科目名称
new QFilter("createorg.id","=", JhzjUtils.GROUPID)}); acctType = rowdata.getString("shjh_type");//科目类型
if(acctInfo != null){ acctTypeInfo = BusinessDataServiceHelper.loadSingleFromCache(acctTypeName,new QFilter[]{
//处理科目更新逻辑此时能修改哪些字段名称 是否可用 new QFilter("number","=",acctType)});
if("0".equals(json_body.getString("status")) || !name.equals(acctInfo.getString("name"))){
acctInfo.set("enable", 0);//科目禁用处理
acctInfo.set("name", name);
acctInfo.set("fullname", name);
SaveServiceHelper.save(new DynamicObject[]{acctInfo});
}
}else{
acctTypeInfo = BusinessDataServiceHelper.loadSingleFromCache(acctTypeName,new QFilter[]{new QFilter("number","=",acctType)});
if(acctTypeInfo == null){ if(acctTypeInfo == null){
log.error(String.format("科目类型在金蝶中找不到:%s", acctType)); log.error(String.format("科目类型在金蝶中找不到:%s", acctType));
itemInfo = new JSONObject(); resultstr.append(number);
itemInfo.put("code",number); resultstr.append("科目类型在金蝶中找不到");
itemInfo.put("error","科目类型在金蝶中找不到");
itemsJson.add(itemInfo);
continue; continue;
} }
//不存在做新增 根据实体名称创建动态对象 //不存在做新增 根据实体名称创建动态对象
@ -183,38 +125,46 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
SaveServiceHelper.save(new DynamicObject[]{acctInfo}); SaveServiceHelper.save(new DynamicObject[]{acctInfo});
//处理科目使用范围 //处理科目使用范围
DB.update(DBRoute.of("fi"), insertSql, new Object[]{kmId,JhzjUtils.GROUPID}); DB.update(DBRoute.of("fi"), insertSql, new Object[]{kmId,JhzjUtils.GROUPID});
}
acctids.put(number,acctInfo.getLong("id")); acctids.put(number,acctInfo.getLong("id"));
accountMaps.put(number,acctInfo); accountMaps.put(number,acctInfo);
} }
list = (ArrayList) itemEntry.get("shjh_companys");//excel表格的某个页签-科目公司信息
JSONArray companysJson = json_obj.getJSONArray("companys"); iterator = list.iterator();//遍历页签
JSONArray companysJson = new JSONArray();
while (iterator.hasNext()) {
importData = (ImportEntryData) iterator.next();
rowdata = importData.getData();//具体的某行数据key值为列名
companysJson.add(rowdata);
}
//处理科目在每个公司生成 //处理科目在每个公司生成
Map<String, DynamicObject> companyAcctMaps = handleAccountCompany(accountMaps,companysJson); Map<String, DynamicObject> companyAcctMaps = handleAccountCompany(accountMaps,companysJson);
//处理科目分配和反分配 //处理科目分配
EsbUtils.handleAssign(companysJson, acctids, entityName); EsbUtils.handleAssignInit(companysJson, acctids, entityName);
EsbUtils.handleUnAssign(companysJson, acctids, entityName);
//处理每个公司下科目的核算维度和禁用状态 //处理每个公司下科目的核算维度和禁用状态
JSONArray asstacttypesJson = json_obj.getJSONArray("asstacttypes"); list = (ArrayList) itemEntry.get("shjh_hswds");//excel表格的某个页签-科目核算维度信息
if(asstacttypesJson != null){ iterator = list.iterator();//遍历页签
JSONArray asstacttypesJson = new JSONArray();
while (iterator.hasNext()) {
importData = (ImportEntryData) iterator.next();
rowdata = importData.getData();//具体的某行数据key值为列名
asstacttypesJson.add(rowdata);
}
if(!asstacttypesJson.isEmpty()){
String hsxm;//核算维度名称 基础资料 String hsxm;//核算维度名称 基础资料
DynamicObject hsxmInfo;//核算维度对象 DynamicObject hsxmInfo;//核算维度对象
DynamicObjectCollection dochswd; DynamicObjectCollection dochswd;
String companynum; String companynum;
DynamicObject checkitementryInfo = null; DynamicObject checkitementryInfo = null;
JSONObject json_body;
for (int i = 0; i < asstacttypesJson.size(); i++) { for (int i = 0; i < asstacttypesJson.size(); i++) {
json_body = asstacttypesJson.getJSONObject(i); json_body = asstacttypesJson.getJSONObject(i);
hsxm = json_body.getString("asstactname"); hsxm = json_body.getString("shjh_asstactname");
number = json_body.getString("code"); number = json_body.getString("shjh_hcode");
if(EsbUtils.isEmpty(hsxm)){ if(EsbUtils.isEmpty(hsxm)){
log.error(String.format("核算维度为空:%s", number)); log.error(String.format("核算维度为空:%s", number));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","核算维度为空"+number);
itemsJson.add(itemInfo);
continue; continue;
} }
companynum = json_body.getString("companynum"); companynum = json_body.getString("shjh_companynums");
//根据科目核算维度中的科目编号和公司编号获取对应科目对象不存在的不处理 //根据科目核算维度中的科目编号和公司编号获取对应科目对象不存在的不处理
acctInfo = companyAcctMaps.get(number+companynum); acctInfo = companyAcctMaps.get(number+companynum);
if(acctInfo == null){ if(acctInfo == null){
@ -223,10 +173,8 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
hsxmInfo = getHsxmInfo(hsxm); hsxmInfo = getHsxmInfo(hsxm);
if(hsxmInfo == null){ if(hsxmInfo == null){
log.error(String.format("核算维度在金蝶中找不到:%s", hsxm)); log.error(String.format("核算维度在金蝶中找不到:%s", hsxm));
itemInfo = new JSONObject(); resultstr.append(number);
itemInfo.put("code",number); resultstr.append("核算维度在金蝶中找不到");
itemInfo.put("error","核算维度在金蝶中找不到"+hsxm);
itemsJson.add(itemInfo);
continue; continue;
} }
//判断当前科目是否存在此核算维度不存在则新增存在则判断是否必录 //判断当前科目是否存在此核算维度不存在则新增存在则判断是否必录
@ -243,7 +191,7 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
checkitementryInfo.set("asstactitem",hsxmInfo); checkitementryInfo.set("asstactitem",hsxmInfo);
checkitementryInfo.set("isdetail", true);//明细 checkitementryInfo.set("isdetail", true);//明细
} }
if("1".equals(json_body.getString("isneed"))){ if("1".equals(json_body.getString("shjh_isneed"))){
checkitementryInfo.set("isrequire", true);//必录 checkitementryInfo.set("isrequire", true);//必录
}else{ }else{
checkitementryInfo.set("isrequire", false);//必录 checkitementryInfo.set("isrequire", false);//必录
@ -254,12 +202,61 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
} }
} }
if(!itemsJson.isEmpty()){ if(resultstr.length() > 0){
this.getView().showMessage("科目数据处理异常"+itemsJson.toJSONString());; this.getView().showMessage("科目数据处理异常"+resultstr);;
return; return;
} }
this.getView().showMessage("处理成功"); this.getView().showMessage("处理成功");
} }
/**
* 按钮监听注册
*/
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
//确认按钮
// Button selectedButton = this.getView().getControl(OK_BUTTON_KEY);
// selectedButton.addClickListener(this);
}
/**
* 按钮点击实现方法
*/
@Override
public void click(EventObject evt) {
try {
Control source = (Control) evt.getSource();
String key = source.getKey();
if (StringUtils.equals(OK_BUTTON_KEY, key)) {
// 点击确认按钮
String params = (String) this.getModel().getValue("shjh_largetextfield");
if(EsbUtils.isEmpty(params)){
this.getView().showMessage("请输入科目JSON格式参数");
return ;
}
params = params.replace("\n","").replace("\t","");
JSONObject json_obj = JSONObject.parseObject(params);
JSONArray detailsJson = json_obj.getJSONArray("items");
if(detailsJson == null){
this.getView().showMessage("未识别到items参数");
return ;
}
JSONArray itemsJson = new JSONArray();//返回值明细集合
JSONObject itemInfo;//返回值明细对象
JSONObject json_body;
// for (int i = 0; i < detailsJson.size(); i++) {
// json_body = detailsJson.getJSONObject(i);
// if(EsbUtils.isEmpty(number) || EsbUtils.isEmpty(name) || EsbUtils.isEmpty(acctType)){
// log.error(String.format("会计科目接口入参为空异常:%s", json_body.toJSONString()));
// itemInfo = new JSONObject();
// itemInfo.put("code",number);
// itemInfo.put("error","入参值为空");
// itemsJson.add(itemInfo);
// continue;
// }
// }
}
} catch (Exception e) { } catch (Exception e) {
log.error("按钮处理异常:"+e.getMessage()); log.error("按钮处理异常:"+e.getMessage());
this.getView().showMessage("按钮处理异常"+e.getMessage()); this.getView().showMessage("按钮处理异常"+e.getMessage());
@ -269,9 +266,6 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
private Map<String, DynamicObject> handleAccountCompany(Map<String, DynamicObject> accountMaps,JSONArray companysJson){ private Map<String, DynamicObject> handleAccountCompany(Map<String, DynamicObject> accountMaps,JSONArray companysJson){
Map<String, DynamicObject> baseMaps = new HashMap<>(); Map<String, DynamicObject> baseMaps = new HashMap<>();
if(companysJson == null){
return baseMaps;
}
JSONObject json_body; JSONObject json_body;
DynamicObject oldAcctInfo; DynamicObject oldAcctInfo;
DynamicObject newAcctInfo; DynamicObject newAcctInfo;
@ -280,8 +274,8 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
String acctnum; String acctnum;
for (int i = 0; i < companysJson.size(); i++) { for (int i = 0; i < companysJson.size(); i++) {
json_body = companysJson.getJSONObject(i); json_body = companysJson.getJSONObject(i);
acctnum = json_body.getString("code");//科目编号 acctnum = json_body.getString("shjh_ccode");//科目编号
compnum = json_body.getString("companynum");//公司编号 compnum = json_body.getString("shjh_companynum");//公司编号
if(EsbUtils.isEmpty(acctnum) || EsbUtils.isEmpty(compnum)){ if(EsbUtils.isEmpty(acctnum) || EsbUtils.isEmpty(compnum)){
continue; continue;
} }
@ -300,7 +294,7 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
newAcctInfo = copydo(oldAcctInfo,hsorgInfo,json_body); newAcctInfo = copydo(oldAcctInfo,hsorgInfo,json_body);
}else{ }else{
//修改币别相关属性 //修改币别相关属性
newAcctInfo.set("acctcurrency", json_body.getString("acctcurrency")); newAcctInfo.set("acctcurrency", json_body.getString("shjh_acctcurrency"));
//如果是指定核算币别 //如果是指定核算币别
// if("descurrency".equals(json_body.getString("acctcurrency"))){ // if("descurrency".equals(json_body.getString("acctcurrency"))){
// //处理具体币别分录数据根据逗号进行分割 // //处理具体币别分录数据根据逗号进行分割
@ -333,12 +327,13 @@ public class InitAccountFormPlugin extends AbstractFormPlugin {
//科目类型 //科目类型
newAcctInfo.set("accounttype", olddo.getDynamicObject("accounttype")); newAcctInfo.set("accounttype", olddo.getDynamicObject("accounttype"));
//币别核算--外币核算类型 //币别核算--外币核算类型
newAcctInfo.set("acctcurrency", json_body.getString("acctcurrency"));//不核算外币nocurrency 指定核算币别descurrency 核算所有币别allcurrency newAcctInfo.set("acctcurrency", json_body.getString("shjh_acctcurrency"));//不核算外币nocurrency 指定核算币别descurrency 核算所有币别allcurrency
//如果是指定核算币别 //如果是指定核算币别
if("descurrency".equals(json_body.getString("acctcurrency")) && !EsbUtils.isEmpty(json_body.getString("descurrencynum"))){ if("descurrency".equals(json_body.getString("shjh_acctcurrency")) &&
!EsbUtils.isEmpty(json_body.getString("shjh_descurrencynum"))){
//处理具体币别分录数据根据逗号进行分割 //处理具体币别分录数据根据逗号进行分割
DynamicObjectCollection docbb = newAcctInfo.getDynamicObjectCollection("currencyentry"); DynamicObjectCollection docbb = newAcctInfo.getDynamicObjectCollection("currencyentry");
String[] dess = json_body.getString("descurrencynum").split(","); String[] dess = json_body.getString("shjh_descurrencynum").split(",");
String bbiso; String bbiso;
DynamicObject bbInfo; DynamicObject bbInfo;
for (int j = 0; j < dess.length; j++) { for (int j = 0; j < dess.length; j++) {

View File

@ -38,6 +38,69 @@ public class EsbUtils {
return null; return null;
} }
//处理科目初始化时的分配
public static void handleAssignInit(JSONArray companysJson, Map<String, Long> cusids, String entityName) {
if(companysJson == null || cusids.isEmpty()){
return;
}
JSONObject json_body;
String compnum;
String cusnumber = "";//当前编号
String firstnumber = "firstnumber";//初始化编号
Long curid;//当前数据id
DynamicObject compinfo;
List<Long> dataList = new ArrayList<>();
List<Long> orgList = new ArrayList<>();
for (int i = 0; i < companysJson.size(); i++) {
json_body = companysJson.getJSONObject(i);
cusnumber = json_body.getString("shjh_ccode");//客户-供应商编号
compnum = json_body.getString("shjh_companynum");//公司编号
if(EsbUtils.isEmpty(cusnumber) || EsbUtils.isEmpty(compnum) || "0".equals(json_body.getString("shjh_companystatus"))){
//公司编号为空 或者 公司层面使用状态-禁用的 不处理
continue;
}
//还原公司id
compinfo = BusinessDataServiceHelper.loadSingleFromCache(orgName,"id,number",new QFilter[]{new QFilter("number","=",compnum)});
if(compinfo == null){
continue;
}
if("firstnumber".equals(firstnumber)){
//初始化改变初始化值
firstnumber = cusnumber;
orgList.add(compinfo.getLong("id"));
}else if(firstnumber.equals(cusnumber)){
//还是同一个客户
orgList.add(compinfo.getLong("id"));
}else{
//另一个客户先处理上一个客户的分配
if(!orgList.isEmpty()){
curid = cusids.get(cusnumber);
if(curid == null){
//当前编号对应的id不存在即上一个数据没有保存成功 不处理分配上一个组织数据清空增加当前组织
orgList.clear();
orgList.add(compinfo.getLong("id"));
continue;
}
dataList.clear();
dataList.add(curid);
BaseDataServiceHelper.batchAssignWithDetail(entityName,JhzjUtils.GROUPID,dataList,orgList);
//处理完成后清空组织列表
orgList.clear();
orgList.add(compinfo.getLong("id"));
}
firstnumber = cusnumber;
orgList.add(compinfo.getLong("id"));
}
}
curid = cusids.get(cusnumber);
//考虑最后一个客户的分配情况
if(!orgList.isEmpty() && curid != null){
dataList.clear();
dataList.add(curid);
BaseDataServiceHelper.batchAssignWithDetail(entityName,JhzjUtils.GROUPID,dataList,orgList);
}
}
//处理分配 //处理分配
public static void handleAssign(JSONArray companysJson, Map<String, Long> cusids, String entityName) { public static void handleAssign(JSONArray companysJson, Map<String, Long> cusids, String entityName) {
if(companysJson == null || cusids.isEmpty()){ if(companysJson == null || cusids.isEmpty()){