291 lines
16 KiB
Java
291 lines
16 KiB
Java
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.OperateOption;
|
||
import kd.bos.dataentity.entity.DynamicObject;
|
||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||
import kd.bos.entity.operate.result.OperateErrorInfo;
|
||
import kd.bos.entity.operate.result.OperationResult;
|
||
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.OperationServiceHelper;
|
||
import kd.sdk.plugin.Plugin;
|
||
import shjh.jhzj7.fi.fi.utils.EsbUtils;
|
||
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
public class CustomerInitOperation extends AbstractOperationServicePlugIn implements Plugin {
|
||
|
||
private static final String entityName = "bd_customer";//系统库 表名 T_BD_Customer
|
||
|
||
private static final String cusTypeName = "bd_customergroup";//系统库 表名 t_bd_customergroup
|
||
private static final String gdName = "bd_customergroupdetail";//系统库 客户分组 表名 t_bd_customergroupdetail
|
||
private static final String bebankName = "bd_bebank";//系统库 行名行号 表名 t_bd_bebank
|
||
private static final Log log = LogFactory.getLog(CustomerInitOperation.class);
|
||
|
||
|
||
@Override
|
||
public void afterExecuteOperationTransaction(AfterOperationArgs e) {
|
||
super.afterExecuteOperationTransaction(e);
|
||
String eok = e.getOperationKey();
|
||
if("customerinit".equals(eok)){
|
||
DynamicObject[] dos = e.getDataEntities();
|
||
JSONObject json_obj;
|
||
JSONObject itemInfo;//返回值明细对象
|
||
String cusnumber;
|
||
String cusname;
|
||
String taxno;
|
||
String type;
|
||
DynamicObject currentCus;
|
||
DynamicObject custype;
|
||
JSONObject json_body;
|
||
Long currentSupId;
|
||
DynamicObject custgroupdetail;
|
||
OperateOption oop = OperateOption.create();
|
||
DynamicObject grouporginfo = EsbUtils.getGroupOrg();
|
||
OperationResult operResult;
|
||
DynamicObject rmbinfo = EsbUtils.getRmb();
|
||
for (DynamicObject pzinfo : dos) {
|
||
pzinfo = BusinessDataServiceHelper.loadSingle(pzinfo.getPkValue(), pzinfo.getDataEntityType().getName());
|
||
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);
|
||
continue;
|
||
}
|
||
JSONArray detailsJson = json_obj.getJSONArray("items");//客户基本信息
|
||
if(detailsJson == null){
|
||
addErrorInfo(pzinfo,"未识别到items参数");
|
||
continue;
|
||
}
|
||
JSONArray banksJson = json_obj.getJSONArray("banks");//客户银行
|
||
JSONArray companysJson = json_obj.getJSONArray("companys");//客户所属公司
|
||
JSONArray customerfivesJson = json_obj.getJSONArray("customerfives");//客户组5
|
||
Map<String, Long> cusids = new HashMap<>();//客户编号和ID对应关系
|
||
JSONArray itemsJson = new JSONArray();//返回值明细集合
|
||
|
||
for (int i = 0; i < detailsJson.size(); i++) {
|
||
json_body = detailsJson.getJSONObject(i);
|
||
cusnumber = json_body.getString("code");
|
||
cusname = json_body.getString("name");
|
||
taxno = json_body.getString("taxno");
|
||
type = json_body.getString("type");
|
||
|
||
if(EsbUtils.isEmpty(cusnumber) || EsbUtils.isEmpty(cusname) || EsbUtils.isEmpty(type)){
|
||
log.error(String.format("客户接口入参为空异常:%s", json_body.toJSONString()));
|
||
itemInfo = new JSONObject();
|
||
itemInfo.put("code",cusnumber);
|
||
itemInfo.put("error","code name type入参值为空");
|
||
itemsJson.add(itemInfo);
|
||
continue;
|
||
}
|
||
cusname = cusname.trim();//名称去除前后空格
|
||
currentCus = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",cusnumber)});
|
||
if(currentCus == null){
|
||
//判断客户分类
|
||
custype = BusinessDataServiceHelper.loadSingleFromCache(cusTypeName,"id,number",new QFilter[]{new QFilter("number","=",type)});
|
||
if(custype == null){
|
||
log.error(String.format("客户分类未匹配:%s", json_body.toJSONString()));
|
||
itemInfo = new JSONObject();
|
||
itemInfo.put("code",cusnumber);
|
||
itemInfo.put("error","客户分类未匹配");
|
||
itemsJson.add(itemInfo);
|
||
continue;
|
||
}
|
||
//根据编号找不到客户,则新增
|
||
currentCus = BusinessDataServiceHelper.newDynamicObject(entityName);
|
||
currentCus.set("number",cusnumber);
|
||
currentCus.set("status","C");//数据状态
|
||
currentCus.set("creator", RequestContext.get().getCurrUserId());//创建人
|
||
currentCus.set("approverid", RequestContext.get().getCurrUserId());//审核人
|
||
currentCus.set("approvedate", new Date());//审核时间
|
||
currentCus.set("enable",1);//默认可用
|
||
currentCus.set("type","1");//伙伴类型 默认法人企业
|
||
currentCus.set("bizfunction",",1,2,3,4,");//业务职能 默认全选
|
||
currentCus.set("createorg", grouporginfo);//创建组织 默认为集团
|
||
currentCus.set("org", grouporginfo);//管理组织 默认为集团
|
||
currentCus.set("ctrlstrategy","1");//控制策略 默认逐级分配1 自由分配2
|
||
currentCus.set("group",custype.getLong("id"));//客户分组
|
||
// currentCus.set("bitindex",1);//位图
|
||
// currentCus.set("customerstatus",EsbUtils.CUSTOMERSTATUS);//客户状态
|
||
currentCus.set("name",cusname);//客户名称
|
||
currentCus.set("tx_register_no",taxno);//税务登记号
|
||
currentCus.set("societycreditcode",taxno);//统一社会信用代码
|
||
currentCus.set("linkman",json_body.getString("contacts"));//联系人
|
||
currentCus.set("bizpartner_phone",json_body.getString("telnum"));//联系电话
|
||
//设置id和masterid 不然不能进行分配动作
|
||
currentSupId = ID.genLongId();
|
||
currentCus.set("id", currentSupId);
|
||
currentCus.set("masterid",currentSupId);
|
||
//处理银行-新增-员工类客户不需要处理
|
||
if(!"M400".equals(type)){
|
||
handleBank(banksJson,currentCus.getDynamicObjectCollection("entry_bank"),cusnumber,cusname,rmbinfo);
|
||
}else{
|
||
//如果是个人,则伙伴类型字段为个人
|
||
currentCus.set("type","4");//伙伴类型
|
||
currentCus.set("idno",cusnumber);//个人时必输身份证号
|
||
}
|
||
//处理 客户组5
|
||
handleFive(customerfivesJson,currentCus.getDynamicObjectCollection("shjh_entry_five"),cusnumber);
|
||
|
||
// SaveServiceHelper.save(new DynamicObject[]{currentCus});
|
||
//保存客户-必须调用保存操作,否则不符合受控基础资料开发规范
|
||
// SaveServiceHelper.save(new DynamicObject[]{currentCus});
|
||
operResult = OperationServiceHelper.executeOperate("save", entityName, new DynamicObject[]{currentCus}, oop);
|
||
if(!operResult.isSuccess()){
|
||
log.error(String.format("客户保存失败:%s", json_body.toJSONString()));
|
||
itemInfo = new JSONObject();
|
||
itemInfo.put("code",cusnumber);
|
||
itemInfo.put("error","客户保存失败"+operResult.getMessage());
|
||
itemsJson.add(itemInfo);
|
||
continue;
|
||
}
|
||
//处理分类-客户只有一个分类
|
||
custgroupdetail = BusinessDataServiceHelper.loadSingle(gdName,new QFilter[]{
|
||
new QFilter("customer.id","=",currentSupId)});
|
||
if(custgroupdetail == null){
|
||
custgroupdetail = BusinessDataServiceHelper.newDynamicObject(gdName);
|
||
custgroupdetail.set("createorg", grouporginfo);//创建组织
|
||
custgroupdetail.set("standard",EsbUtils.CUSTOMERSTAND);//客户分类标准
|
||
custgroupdetail.set("customer",currentCus);//客户
|
||
}
|
||
custgroupdetail.set("group",custype);//具体分类
|
||
operResult = OperationServiceHelper.executeOperate("save", gdName, new DynamicObject[]{custgroupdetail}, oop);
|
||
if(!operResult.isSuccess()){
|
||
log.error(String.format("客户分类保存失败:%s", json_body.toJSONString()));
|
||
itemInfo = new JSONObject();
|
||
itemInfo.put("code",cusnumber);
|
||
itemInfo.put("error","客户分类保存失败"+operResult.getMessage());
|
||
itemsJson.add(itemInfo);
|
||
}
|
||
}else{
|
||
//修改客户 名称 税务登记号 联系人 联系电话 银行信息 客户组5
|
||
currentSupId = currentCus.getLong("id");
|
||
currentCus.set("name",cusname);//客户名称
|
||
currentCus.set("tx_register_no",taxno);//税务登记号
|
||
currentCus.set("societycreditcode",taxno);//统一社会信用代码
|
||
currentCus.set("linkman",json_body.getString("contacts"));//联系人
|
||
currentCus.set("bizpartner_phone",json_body.getString("telnum"));//联系电话
|
||
//处理 客户组5
|
||
handleFive(customerfivesJson,currentCus.getDynamicObjectCollection("shjh_entry_five"),cusnumber);
|
||
//处理银行-新增-员工类客户不需要处理
|
||
if(!"M400".equals(type)){
|
||
handleBank(banksJson,currentCus.getDynamicObjectCollection("entry_bank"),cusnumber,cusname,rmbinfo);
|
||
}
|
||
operResult = OperationServiceHelper.executeOperate("save", entityName, new DynamicObject[]{currentCus}, oop);
|
||
if(!operResult.isSuccess()){
|
||
log.error(String.format("客户保存失败:%s", json_body.toJSONString()));
|
||
itemInfo = new JSONObject();
|
||
itemInfo.put("code",cusnumber);
|
||
itemInfo.put("error","客户保存失败"+operResult.getMessage());
|
||
itemsJson.add(itemInfo);
|
||
}
|
||
}
|
||
cusids.put(cusnumber,currentSupId);
|
||
}
|
||
//处理客户分配
|
||
EsbUtils.handleAssignNew(companysJson, cusids, entityName);
|
||
//处理客户取消分配--初始化时不需要用
|
||
// EsbUtils.handleUnAssign(companysJson, cusids, entityName);
|
||
if(itemsJson.isEmpty()){
|
||
this.operationResult.addSuccessPkId(pzinfo.getPkValue());
|
||
}else{
|
||
addErrorInfo(pzinfo,"接口处理异常"+itemsJson.toJSONString());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void handleFive(JSONArray customerfivesJson, DynamicObjectCollection doc_five, String cusnumber){
|
||
if(customerfivesJson == null){
|
||
return;
|
||
}
|
||
doc_five.clear();
|
||
JSONObject json_body;
|
||
DynamicObject fiveinfo;
|
||
String channelnum;
|
||
String companynum;
|
||
String fivecode;
|
||
for (int i = 0; i < customerfivesJson.size(); i++) {
|
||
json_body = customerfivesJson.getJSONObject(i);
|
||
if(cusnumber.equals(json_body.getString("code"))){
|
||
channelnum = json_body.getString("channelnum");
|
||
companynum = json_body.getString("salecompanynum");
|
||
fivecode = json_body.getString("customerfivecode");
|
||
if(EsbUtils.isEmpty(channelnum) || EsbUtils.isEmpty(companynum) || EsbUtils.isEmpty(fivecode)){
|
||
//任意一个为空,则不处理
|
||
log.error(String.format("客户组5入参值为空:%s", json_body.toJSONString()));
|
||
continue;
|
||
}
|
||
fiveinfo = doc_five.addNew();
|
||
fiveinfo.set("shjh_channelnum",channelnum);//渠道编号
|
||
fiveinfo.set("shjh_channelname",json_body.getString("channelname"));//渠道名称
|
||
fiveinfo.set("shjh_salecompanynum",companynum);//公司编号
|
||
fiveinfo.set("shjh_entryfive_number",fivecode);//编号
|
||
fiveinfo.set("shjh_entryfive_name",json_body.getString("customerfivename"));//名称
|
||
fiveinfo.set("shjh_status",EsbUtils.getStatusForBoolean(json_body.getString("customerfivestatus")));//状态
|
||
}
|
||
}
|
||
}
|
||
|
||
private void handleBank(JSONArray banksJson,DynamicObjectCollection doc_bank,String cusnumber,String cusname,DynamicObject rmbinfo){
|
||
if(banksJson == null){
|
||
return;
|
||
}
|
||
doc_bank.clear();
|
||
JSONObject json_body;
|
||
DynamicObject bankinfo;
|
||
DynamicObject bebankinfo;
|
||
for (int i = 0; i < banksJson.size(); i++) {
|
||
json_body = banksJson.getJSONObject(i);
|
||
if(cusnumber.equals(json_body.getString("code"))){
|
||
//根据详细分机银行 查找对应的行名行号
|
||
bebankinfo = BusinessDataServiceHelper.loadSingleFromCache(bebankName,"id,name",new QFilter[]{new QFilter("name","=",json_body.getString("bankname"))});
|
||
if(bebankinfo == null){
|
||
continue;
|
||
}
|
||
if(EsbUtils.isEmpty(json_body.getString("banknumber"))){
|
||
//银行账号为空,不处理
|
||
continue;
|
||
}
|
||
bankinfo = doc_bank.addNew();
|
||
bankinfo.set("bankaccount",json_body.getString("banknumber"));//银行账号
|
||
if(EsbUtils.isEmpty(json_body.getString("acctname"))){
|
||
//如果入参中没有账户名称,则使用当前名称
|
||
bankinfo.set("accountname",cusname);//账户名称
|
||
}else{
|
||
bankinfo.set("accountname",json_body.getString("acctname"));//账户名称
|
||
}
|
||
|
||
bankinfo.set("bank",bebankinfo.getLong("id"));//开户银行-行名行号 根据入参查找
|
||
bankinfo.set("currency", rmbinfo);//币别 默认人民币
|
||
}
|
||
}
|
||
if(!doc_bank.isEmpty()){
|
||
doc_bank.get(0).set("isdefault_bank",true);
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|