From 29cc466912bf1d8db516695423656c0e578c67a9 Mon Sep 17 00:00:00 2001 From: yuxueliang0813 <407010292@qq.com> Date: Fri, 9 May 2025 13:00:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/operate/SupplierInitOperation.java | 232 ++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 main/java/shjh/jhzj7/fi/fi/plugin/operate/SupplierInitOperation.java diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/operate/SupplierInitOperation.java b/main/java/shjh/jhzj7/fi/fi/plugin/operate/SupplierInitOperation.java new file mode 100644 index 0000000..9142c07 --- /dev/null +++ b/main/java/shjh/jhzj7/fi/fi/plugin/operate/SupplierInitOperation.java @@ -0,0 +1,232 @@ +package shjh.jhzj7.fi.fi.plugin.operate; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONException; +import com.alibaba.fastjson.JSONObject; +import com.sap.db.jdbc.packet.ErrorLevel; +import kd.bos.context.RequestContext; +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.dataentity.entity.DynamicObjectCollection; +import kd.bos.entity.operate.result.OperateErrorInfo; +import kd.bos.entity.plugin.AbstractOperationServicePlugIn; +import kd.bos.entity.plugin.args.AfterOperationArgs; +import kd.bos.id.ID; +import kd.bos.logging.Log; +import kd.bos.logging.LogFactory; +import kd.bos.orm.query.QFilter; +import kd.bos.servicehelper.BusinessDataServiceHelper; +import kd.bos.servicehelper.operation.SaveServiceHelper; +import kd.sdk.plugin.Plugin; +import shjh.jhzj7.fi.fi.utils.EsbUtils; +import shjh.jhzj7.fi.fi.utils.JhzjUtils; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +//供应商初始化操作 +public class SupplierInitOperation extends AbstractOperationServicePlugIn implements Plugin { + private static final String entityName = "bd_supplier";//系统库 表名 T_BD_Supplier + private static final String supTypeName = "bd_suppliergroup";//系统库 表名 t_bd_suppliergroup + private static final String bebankName = "bd_bebank";//系统库 行名行号 表名 t_bd_bebank + private static final String gdName = "bd_suppliergroupdetail";//系统库 供应商分组 表名 t_bd_suppliergroupdetail + private static final Log log = LogFactory.getLog(SupplierInitOperation.class); + + @Override + public void afterExecuteOperationTransaction(AfterOperationArgs e) { + super.afterExecuteOperationTransaction(e); + String eok = e.getOperationKey(); + if("supplierinit".equals(eok)){ + DynamicObject[] dos = e.getDataEntities(); + DynamicObject pzinfo = BusinessDataServiceHelper.loadSingle(dos[0].getPkValue(), dos[0].getDataEntityType().getName()); + JSONObject json_obj; + try{ + //解析入参,如果格式不正确,日志记录,并反馈esb + json_obj = JSONObject.parseObject(pzinfo.getString("shjh_inputs_tag")); + }catch (JSONException e1) { + String jsonResult = String.format("供应商接口入参异常:%s", e1.getMessage()); + log.error(jsonResult); + addErrorInfo(pzinfo,"接口入参异常"+jsonResult); + return; + } + + JSONArray itemsJson = new JSONArray();//返回值明细集合 + JSONObject itemInfo;//返回值明细对象 + JSONArray detailsJson = json_obj.getJSONArray("items"); + if(detailsJson == null){ + addErrorInfo(pzinfo,"未识别到items参数"); + return ; + } + JSONArray banksJson = json_obj.getJSONArray("banks");//供应商银行 + JSONArray companysJson = json_obj.getJSONArray("companys");//供应商所属公司 + String supnumber; + String supname; + String taxno; + String type; + String bankcode; + DynamicObject currentSup; + DynamicObject suptype; + JSONObject json_body; + DynamicObjectCollection doc_bank; + DynamicObject bankinfo; + DynamicObject bebankinfo; + JSONObject json_bank; + Long currentSupId; + DynamicObject suppliergroupdetail; + Map cusids = new HashMap<>();//供应商编号和ID对应关系 + for (int i = 0; i < detailsJson.size(); i++) { + json_body = detailsJson.getJSONObject(i); + supnumber = json_body.getString("code"); + supname = json_body.getString("name"); + taxno = json_body.getString("taxno"); + type = json_body.getString("type"); + + if(EsbUtils.isEmpty(supnumber) || EsbUtils.isEmpty(supname) || EsbUtils.isEmpty(taxno) || EsbUtils.isEmpty(type)){ + log.error(String.format("供应商接口入参为空异常:%s", json_body.toJSONString())); + itemInfo = new JSONObject(); + itemInfo.put("code",supnumber); + itemInfo.put("error","入参值为空"); + itemsJson.add(itemInfo); + continue; + } + suptype = BusinessDataServiceHelper.loadSingleFromCache(supTypeName,"id,number",new QFilter[]{new QFilter("number","=",type)}); + if(suptype == null){ + log.error(String.format("供应商分类未匹配:%s", json_body.toJSONString())); + itemInfo = new JSONObject(); + itemInfo.put("code",supnumber); + itemInfo.put("error","供应商分类未匹配"); + itemsJson.add(itemInfo); + continue; + } + currentSup = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",supnumber)}); + if(currentSup == null){ + //根据编号找不到供应商,则新增 + currentSup = BusinessDataServiceHelper.newDynamicObject(entityName); + currentSup.set("number",supnumber); + currentSup.set("status","C");//数据状态 + currentSup.set("creator", RequestContext.get().getCurrUserId());//创建人 + currentSup.set("approverid", RequestContext.get().getCurrUserId());//审核人 + currentSup.set("approvedate", new Date());//审核时间 + currentSup.set("enable",1);//默认可用 + currentSup.set("type","1");//伙伴类型 默认法人企业 + currentSup.set("bizfunction",",1,2,3,4,");//业务职能 默认全选 + currentSup.set("createorg", JhzjUtils.GROUPID);//创建组织 默认为集团 + currentSup.set("org", JhzjUtils.GROUPID);//管理组织 默认为集团 + currentSup.set("source","SELF");//来源 默认自建 + currentSup.set("ctrlstrategy","1");//控制策略 默认逐级分配 + currentSup.set("group",suptype.getLong("id"));//供应商分组 + currentSup.set("bitindex",6);//位图 + currentSup.set("supplier_status",EsbUtils.SUPPLIERSTATUS);//供应商状态 默认合格 + //设置id和masterid 不然不能进行分配动作 + currentSupId = ID.genLongId(); + currentSup.set("id", currentSupId); + currentSup.set("masterid",currentSupId); + //处理分类 + suppliergroupdetail = BusinessDataServiceHelper.newDynamicObject(gdName); + suppliergroupdetail.set("createorg",JhzjUtils.GROUPID);//创建组织 + suppliergroupdetail.set("standard",EsbUtils.SUPPLIERSTAND);//供应商分类标准 + suppliergroupdetail.set("group",suptype.getLong("id"));//具体分类 + suppliergroupdetail.set("supplier",currentSupId);//供应商 + SaveServiceHelper.save(new DynamicObject[]{suppliergroupdetail}); + } + currentSup.set("name",supname);//供应商名称 + currentSup.set("tx_register_no",taxno);//税务登记号 + currentSup.set("societycreditcode",taxno);//统一社会信用代码 + currentSup.set("linkman",json_body.getString("contacts"));//联系人 + currentSup.set("bizpartner_phone",json_body.getString("telnum"));//联系电话 + //处理银行信息-分表 + if(banksJson != null){ + doc_bank = currentSup.getDynamicObjectCollection("entry_bank"); + for (int j = 0; j < banksJson.size(); j++) { + json_bank = banksJson.getJSONObject(j); + if(!supnumber.equals(json_bank.getString("code"))){ + continue; + } + bankcode = json_bank.getString("bankcode"); + if(EsbUtils.isEmpty(bankcode)){ + //联行号为空,不处理 + continue; + } + bebankinfo = BusinessDataServiceHelper.loadSingleFromCache(bebankName,"id,number",new QFilter[]{new QFilter("number","=",bankcode)}); + if(bebankinfo == null){ + //联行号在金蝶中无对应记录,不处理 + continue; + } + //供应商银行账户对象从现有集合中查找,查找不到,则新增 + bankinfo = getBankInfo(doc_bank,json_bank.getString("banknumber")); + if(bankinfo == null && "E".equals(json_bank.getString("bankstatus"))){ + //如果本次入参银行账户未在系统生成,且sap是删除的,我方不接收 + continue; + } + if(bankinfo == null){ + bankinfo = doc_bank.addNew(); + } + bankinfo.set("bankaccount",json_bank.getString("banknumber"));//银行账号 + bankinfo.set("accountname",json_bank.getString("acctname"));//账户名称 + bankinfo.set("bank",bebankinfo.getLong("id"));//开户银行-行名行号 根据入参查找 + bankinfo.set("currency",JhzjUtils.CNY);//币别 默认人民币 + bankinfo.set("shjh_banktype",json_bank.getString("banktype"));//合作银行类型 + bankinfo.set("shjh_bankstatus",json_bank.getString("bankstatus"));//账户状态 + } + if(!doc_bank.isEmpty()){ + //首行账户作为默认 + doc_bank.get(0).set("isdefault_bank",true); + } + } + //处理 sap公司信息 结算方式 统驭科目 + handleSAPCompany(companysJson,currentSup.getDynamicObjectCollection("shjh_entry_sap"),supnumber); + //保存供应商 + SaveServiceHelper.save(new DynamicObject[]{currentSup}); + cusids.put(supnumber,currentSup.getLong("id")); + } + + //处理供应商分配 + EsbUtils.handleAssign(companysJson, cusids, entityName); + //处理供应商取消分配 + EsbUtils.handleUnAssign(companysJson, cusids, entityName); + + if(itemsJson.isEmpty()){ + this.operationResult.addSuccessPkId(pzinfo.getPkValue()); + }else{ + addErrorInfo(pzinfo,"接口处理异常"+itemsJson.toJSONString()); + } + } + } + + private void addErrorInfo(DynamicObject bill, String errorMsg) { + OperateErrorInfo operateErrorInfo = new OperateErrorInfo(); + operateErrorInfo.setMessage(errorMsg); + operateErrorInfo.setErrorLevel(ErrorLevel.Error.name()); + operateErrorInfo.setPkValue(bill.getPkValue()); + this.operationResult.addErrorInfo(operateErrorInfo); + } + + private void handleSAPCompany(JSONArray companysJson,DynamicObjectCollection doc_sap,String supnumber){ + if(companysJson == null){ + return; + } + doc_sap.clear(); + JSONObject json_body; + DynamicObject fiveinfo; + for (int i = 0; i < companysJson.size(); i++) { + json_body = companysJson.getJSONObject(i); + if(supnumber.equals(json_body.getString("code"))){ + fiveinfo = doc_sap.addNew(); + fiveinfo.set("shjh_settlementmethod",json_body.getString("settlementmethod"));//结算方式编号 + fiveinfo.set("shjh_accountnumber",json_body.getString("accountnumber"));//统驭科目编号 + fiveinfo.set("shjh_companynum",json_body.getString("companynum"));//公司编号 + fiveinfo.set("shjh_status",EsbUtils.getStatusForBoolean(json_body.getString("companystatus")));//使用状态 + } + } + } + + private DynamicObject getBankInfo(DynamicObjectCollection doc_bank, String banknumber){ + //从当前供应商的银行账户集合中获取对应的账户的对象 + for (DynamicObject bankinfo : doc_bank) { + if(banknumber.equals(bankinfo.getString("bankaccount"))){ + return bankinfo; + } + } + return null; + } +}