dobe_comic8/main/java/shkd/repc/task/DobeDWaccountTask.java

200 lines
11 KiB
Java
Raw Normal View History

2024-11-04 15:32:41 +00:00
package shkd.repc.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import shkd.utils.DobeDWUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 后台任务插件
*/
public class DobeDWaccountTask extends AbstractTask implements Plugin {
private static Log log = LogFactory.getLog(DobeDWaccountTask.class);
private static final String entityName = "recos_stdcostaccount";//标准成本科目实体 供应链库 表名 t_recos_scostaccount
private static final String acctViewEntity = "bd_accountview";//会计科目实体 财务库 表名 T_BD_Account
// private static final String accEntity = "costaccountentry";//科目分录实体 供应链库 表名 t_recos_costaccount
// private static final String projectEntity = "repmd_projectbill";//项目实体 表名 t_repmd_projectbill
private static final String dw_menthod = "mdm_costsubject";
@Override
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
//调用数仓查询接口
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(DobeDWUtils.dwUrl+dw_menthod)
.post(DobeDWUtils.createRequestBody("account",1))
.header("Content-Type", "application/json")
.header("Authorization", DobeDWUtils.appCode)
.build();
String resultData = null;
try {
Response response = client.newCall(request).execute();
resultData = response.body().string();
log.info("成本科目接口返回结果:\n{}", resultData);
} catch (IOException e) {
log.info(String.format("成本科目接口异常:%s", e.getMessage()));
throw new RuntimeException(e);
}
JSONObject json_body = JSON.parseObject(resultData);
//接口返回的数据进行了分页
// int totalNum = json_body.getIntValue("totalNum");//分页-SQL查询总数据量
//解析接口返回值,与系统数据比较
// String projectnumber = json_body.getString("projectnumber");
// DynamicObject projectinfo = QueryServiceHelper.queryOne(projectEntity,"id,billno",new QFilter[]{new QFilter("billno","=",projectnumber)});
// if(projectinfo == null){
// log.info(String.format("成本科目接口异常未根据项目编号找到对应项目:%s", projectnumber));
// return;
// }
//根据id查找标准成本科目的表头信息
long bzkmid = 2012525353133704192l;//测试id
// long bzkmid = 2037039962179791872l;//正式id
DynamicObject billinfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("id","=",bzkmid)});
if(billinfo == null){
//根据标准科目id未找到对应记录
DobeDWUtils.saveLog(bzkmid+"","数仓成本科目同步","","根据标准科目ID未找到对应记录"+bzkmid,false,"定时任务");
return;
}
String acctid = null;
String number = null;
String name = null;
// String longnumber = null;
// String longname = null;
// String ciaccountflag = null;
// String apportionway = null;
// String taxrate = null;
// String isleaf = null;
String kjkm_num = null;
String kjkm_name = null;
String parentid = null;
DynamicObject acctinfo = null;
// DynamicObject mapAcctinfo = null;
DynamicObject acctViewinfo = null;
Map<String,DynamicObject> parentAccts = new HashMap<>();//现有成本科目集合
// Map<String,DynamicObject> mappingAccts = new HashMap<>();//现有成本科目映射集合
Map<String,DynamicObject> acctViewMap = new HashMap<>();//会计科目缓存
JSONArray detailsJson = json_body.getJSONArray("data");
DynamicObjectCollection doDetails = billinfo.getDynamicObjectCollection("costaccountentry");
for (int i = 0; i < doDetails.size(); i++) {
acctinfo = doDetails.get(i);
//以数仓科目id作为key科目对象为value
parentAccts.put(acctinfo.getString("caentry_srcid"),acctinfo);
}
// DynamicObjectCollection mapDetails = billinfo.getDynamicObjectCollection("accountmapentry");
// for (int i = 0; i < mapDetails.size(); i++) {
// mapAcctinfo = mapDetails.get(i);
// //以成本科目长编码作为key映射对象为value
// mappingAccts.put(mapAcctinfo.getString("amentry_longnumber"),mapAcctinfo);
// }
//循环入参,判断哪些科目该修改,哪些该新增
for (int i = 0; i < detailsJson.size(); i++) {
json_body = detailsJson.getJSONObject(i);
acctid = json_body.getString("costsubject_id");//科目id
number = json_body.getString("costsubject_code");//科目编号
name = json_body.getString("costsubject_name");//科目名称
kjkm_num = json_body.getString("accsubject_code");//会计科目编码
kjkm_name = json_body.getString("accsubject_name");//会计科目名称
// longnumber = json_body.getString("longnumber");//科目长编号
// longname = json_body.getString("longname");//科目长名称
// ciaccountflag = json_body.getString("ciaccountflag");//科目类别 非建安科目and建安科目
// apportionway = json_body.getString("apportionway");//分摊方式
// taxrate = json_body.getString("taxrate");//税率
// isleaf = json_body.getString("isleaf");//是否叶子节点
// level = json_body.getString("level");//科目级次
parentid = json_body.getString("costsubject_pid");//科目父级id
if(DobeDWUtils.isEmpty(number) || DobeDWUtils.isEmpty(name) || DobeDWUtils.isEmpty(acctid)){
//如果组织ID和组织编码 名称是空的,则跳过此记录
log.info(String.format("成本科目入参为空异常:%s", json_body.toJSONString()));
continue;
}
// else if(Integer.parseInt(level) >= 2 && DobeDWUtils.isEmpty(parentid)){
// log.info(String.format("成本科目级次大于等于2但是无对应的父级科目ID%s", json_body.toJSONString()));
// continue;
// }
//先从现有科目集合中获取,未获取到则新增
acctinfo = parentAccts.get(acctid);
if(acctinfo == null){
acctinfo = doDetails.addNew();
acctinfo.set("caentry_srcid", acctid);//源ID 用于存储数仓的科目id
// acctinfo.set("caentry_project", projectinfo.getLong("id"));//项目
acctinfo.set("caentry_enable", true);//默认 启用
acctinfo.set("caentry_isstdaccount", true);//是否标准科目
acctinfo.set("caentry_standlibflag", true);//是否标准科目库
acctinfo.set("caentry_isleaf", "1");//是否叶子节点新增默认为true
acctinfo.set("caentry_longnumber", number);//科目长编号
acctinfo.set("caentry_fullname", name);//科目长名称
//将此次新增的科目放到现有科目集合中,方便后续科目能获取到父级科目
parentAccts.put(acctid,acctinfo);
}
//已存在,做更新 编号、名称、长编号、名称、科目类别等
acctinfo.set("caentry_number", number);
acctinfo.set("caentry_name", name);
acctinfo.set("caentry_ciaccountflag", "1");//科目类别 建安 1 非建安 0 默认都是建安类
// acctinfo.set("caentry_apportionway", null);//分摊方式 基础资料
// acctinfo.set("caentry_taxrate", taxrate);//税率
//创建组织 caentry_createorg
//组织 caentry_org
//业务组织 caentry_useorg
//处理父级科目
if(parentAccts.get(parentid) != null){
acctinfo.set("pid", parentAccts.get(parentid).getLong("id"));//设置父级科目
//如果数仓不指定长编码和长名称,则需要在此处手动处理
acctinfo.set("caentry_longnumber", parentAccts.get(parentid).getString("caentry_longnumber")+"."+number);
acctinfo.set("caentry_fullname", parentAccts.get(parentid).getString("caentry_fullname")+"_"+name);
//科目级次=父级科目级次+1
acctinfo.set("caentry_level", parentAccts.get(parentid).getInt("caentry_level")+1);
//父级科目的是否叶子节点为否
parentAccts.get(parentid).set("caentry_isleaf", "0");//是否叶子节点
}else{
//没有父节点的是根节点级次是1
acctinfo.set("caentry_level", 1);
}
//处理成本科目与会计科目的映射关系--该关系并不是有个分表来存储,如下代码注释
// mapAcctinfo = mappingAccts.get(acctinfo.getString("caentry_longnumber"));
// if(mapAcctinfo == null){
// //根据科目长编码未找到对应映射关系,则新增一个映射关系
// mapAcctinfo = mapDetails.addNew();
// mapAcctinfo.set("amentry_longnumber",acctinfo.getString("caentry_longnumber"));//科目长编码
// mapAcctinfo.set("amentry_name",name);//科目名称
// mapAcctinfo.set("amentry_isleaf",acctinfo.getString("caentry_isleaf"));//是否叶子
// mapAcctinfo.set("amentry_ciaccountflag","1");//科目类别 建安 1 非建安 0 默认都是建安类
// mapAcctinfo.set("amentry_enable",true);//是否启用 默认启用
// mapAcctinfo.set("amentry_level",acctinfo.getInt("caentry_level"));//级次
// }
//设置对应会计科目字段id
if(!DobeDWUtils.isEmpty(kjkm_num)){
acctViewinfo = acctViewMap.get(kjkm_num);
if(acctViewinfo == null){
acctViewinfo = QueryServiceHelper.queryOne(acctViewEntity,"id,number,longnumber",new QFilter[]{new QFilter("longnumber","=",kjkm_num)});
}
if(acctViewinfo == null){
DobeDWUtils.saveLog(acctinfo.getString("caentry_longnumber"),"数仓项目同步",json_body.toString(),"会计科目在金蝶中找不到:"+kjkm_num,false,"定时任务");
}else{
acctViewMap.put(kjkm_num,acctViewinfo);
// mapAcctinfo.set("amentry_account",acctViewinfo.getLong("id"));
acctinfo.set("caentry_account",acctViewinfo.getLong("id"));
}
}
}
SaveServiceHelper.save(new DynamicObject[]{billinfo});
}
}