295 lines
16 KiB
Java
295 lines
16 KiB
Java
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.OperateOption;
|
||
import kd.bos.dataentity.entity.DynamicObject;
|
||
import kd.bos.db.DB;
|
||
import kd.bos.db.DBRoute;
|
||
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.OperationServiceHelper;
|
||
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.math.BigDecimal;
|
||
import java.math.RoundingMode;
|
||
import java.util.Date;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 后台任务插件
|
||
*/
|
||
public class DobeDWprojectTask extends AbstractTask implements Plugin {
|
||
//项目f7的基础资料是单独的表 t_repmd_project
|
||
private static final String entityName = "repmd_projectbill";//表名 t_repmd_projectbill
|
||
private static final String orgName = "bos_org";//表名 t_org_org
|
||
private static Log log = LogFactory.getLog(DobeDWprojectTask.class);
|
||
private static final String dw_menthod = "mdm_projectinfo";
|
||
private static final Long accttableid = 1318154893474663424l;
|
||
|
||
@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("project",1))
|
||
.header("Content-Type", "application/json")
|
||
.header("Authorization", DobeDWUtils.appCode)
|
||
.build();
|
||
|
||
String resultData = null;
|
||
Response response = null;
|
||
try {
|
||
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查询总数据量
|
||
handleProject(json_body);
|
||
int queryCount = DobeDWUtils.getQueryCount(totalNum);
|
||
if(queryCount > 1){
|
||
//查询次数不止一次,需要分页查询
|
||
for (int i = 2; i <= queryCount; i++) {
|
||
request = new Request.Builder().url(DobeDWUtils.dwUrl+dw_menthod)
|
||
.post(DobeDWUtils.createRequestBody("project",i))
|
||
.header("Content-Type", "application/json")
|
||
.header("Authorization", DobeDWUtils.appCode)
|
||
.build();
|
||
try {
|
||
response = client.newCall(request).execute();
|
||
resultData = response.body().string();
|
||
} catch (IOException e) {
|
||
log.info(String.format("项目接口异常:%s", e.getMessage()));
|
||
throw new RuntimeException(e);
|
||
}
|
||
json_body = JSON.parseObject(resultData);
|
||
handleProject(json_body);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
private void handleProject(JSONObject json_body) {
|
||
//解析接口返回值,与系统数据比较
|
||
JSONArray detailsJson = json_body.getJSONArray("data");
|
||
|
||
String fbillno = null;//项目编号
|
||
String fbillname = null;//项目名称
|
||
// String fprojectstageid = null;//项目阶段 repmd_projectstages t_repmd_projectstage 用户自行在界面上选择
|
||
// String fversionnum = null;//版本号
|
||
// String fisleaf = null;//是否叶子节点
|
||
String faddress = null;//项目地址
|
||
String project_stage = null;//业务体系--二开字段,用于工作流发起判断(新拓 成熟)
|
||
String forgid = null;//所属组织-行政组织的编号
|
||
Date project_getdate = null;//项目获取时间
|
||
String fdwid = null;//数仓项目id--二开字段,唯一标识
|
||
String ywlx = null;//业务类型--二开字段,仅记录数仓数据
|
||
DynamicObject orginfo = null;
|
||
DynamicObject projectinfo = null;
|
||
String updateSql = "UPDATE t_org_org SET fk_qeug_combofield=? WHERE fnumber=?;";
|
||
String updateF7Sql = "UPDATE t_repmd_project SET fk_qeug_combofield=? WHERE fnumber=?;";
|
||
// DynamicObject projectstageinfo = null;
|
||
for (int i = 0; i < detailsJson.size(); i++) {
|
||
json_body = detailsJson.getJSONObject(i);
|
||
fbillno = json_body.getString("project_code");
|
||
fbillname = json_body.getString("project_name");
|
||
faddress = json_body.getString("addr");
|
||
// fisleaf = json_body.getString("fisleaf");
|
||
forgid = json_body.getString("org_code");
|
||
project_getdate = json_body.getDate("project_getdate");
|
||
project_stage = json_body.getString("project_stage");
|
||
ywlx = json_body.getString("project_stage_detail");
|
||
fdwid = json_body.getString("project_id");
|
||
if(DobeDWUtils.isEmpty(fbillno) || DobeDWUtils.isEmpty(fbillname) || DobeDWUtils.isEmpty(fdwid)){
|
||
log.info(String.format("项目接口入参为空异常:%s", json_body.toJSONString()));
|
||
continue;
|
||
}
|
||
//根据数仓id查找项目是否已存在
|
||
projectinfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("qeug_dwid","=",fdwid)});
|
||
//项目阶段表名:t_repmd_projectstage
|
||
// projectstageinfo = QueryServiceHelper.queryOne("repmd_projectstages","id,number",new QFilter[]{new QFilter("number","=",fprojectstageid)});
|
||
if(projectinfo != null){
|
||
if("C".equals(projectinfo.getString("billstatus"))){
|
||
//如果项目已审核,则不进行项目建立的数据修改;只更新项目F7的业务体系字段
|
||
if("新拓园区".equals(project_stage)){
|
||
DB.update(DBRoute.of("scm"), updateF7Sql, new Object[]{"XTYQ",fbillno});
|
||
}else if("成熟园区".equals(project_stage)){
|
||
DB.update(DBRoute.of("scm"), updateF7Sql, new Object[]{"CSYQ",fbillno});
|
||
}
|
||
continue;
|
||
}
|
||
//已存在,做更新 名称 阶段 版本号等信息;组织、编号、是否叶子节点不能更新;需要前台操作
|
||
projectinfo.set("billno", fbillno);
|
||
projectinfo.set("longnumber", fbillno);//长编码
|
||
projectinfo.set("billname", fbillname);
|
||
projectinfo.set("fullname", fbillname);//项目全称
|
||
// if(projectstageinfo != null){
|
||
// projectinfo.set("projectstage", projectstageinfo.getLong("id"));//项目阶段
|
||
// }
|
||
projectinfo.set("address", faddress);
|
||
projectinfo.set("acquiredate", project_getdate);
|
||
projectinfo.set("qeug_ywlx", ywlx);
|
||
// projectinfo.set("billstatus", "A");//单据状态 A保存 B已提交 C已审核
|
||
// projectinfo.set("showflag", true);//是否列表显示
|
||
// projectinfo.set("enable", 1);//是否启用
|
||
// projectinfo.set("islatestversion", true);//是否最新版
|
||
projectinfo.set("mainprojectid", projectinfo.getLong("id"));//主项目ID
|
||
if("新拓园区".equals(project_stage)){
|
||
projectinfo.set("qeug_combofield", "XTYQ");
|
||
}else if("成熟园区".equals(project_stage)){
|
||
projectinfo.set("qeug_combofield", "CSYQ");
|
||
}
|
||
//如果项目原所属组织为空,则更新,不能将原组织有值的直接更新,防止数据和权限发生变化
|
||
if(projectinfo.getDynamicObject("org") == null){
|
||
orginfo = BusinessDataServiceHelper.loadSingle(orgName,new QFilter[]{new QFilter("number","=",forgid)});
|
||
if(orginfo != null){
|
||
projectinfo.set("org", orginfo);//项目所属组织
|
||
projectinfo.set("purchaseorg", orginfo);//项目采购组织同所属组织
|
||
//核算组织默认和所属组织保持一致
|
||
projectinfo.set("fiorg", orginfo);
|
||
}else{
|
||
log.info(String.format("数仓传入的项目所属组织在金蝶中找不到:%s", forgid));
|
||
DobeDWUtils.saveLog(fbillno,"数仓项目同步",json_body.toString(),"数仓传入的项目所属组织在金蝶中找不到:"+forgid,false,"定时任务");
|
||
}
|
||
}
|
||
if(projectinfo.getDynamicObject("account") == null){
|
||
projectinfo.set("account", accttableid);//会计科目表默认新准则会计科目表
|
||
}
|
||
//TODO 数仓新增两个面积字段 20250221
|
||
// projectinfo.set("qeug_decimalfield1", json_body.getBigDecimal("addr"));//原始可出租面积
|
||
// projectinfo.set("qeug_decimalfield3", json_body.getBigDecimal("addr"));//原始建筑面积
|
||
// projectinfo.set("qeug_textfield2", calcDFL(json_body.getBigDecimal("addr"),
|
||
// json_body.getBigDecimal("addr")).toString());//原始得房率=可出租/建筑面积*100%
|
||
projectinfo.set("qeug_textfield1", json_body.getString("project_oriname"));//项目原名称
|
||
projectinfo.set("manageway", getManageway(json_body.getString("investment_model")));//投资模式
|
||
projectinfo.set("landusage", getLandusage(json_body.getString("land_usage")));//用地性质
|
||
projectinfo.set("acquiredate", json_body.getDate("project_gettime"));//项目获取时间
|
||
//原保存逻辑直接存入数据库,现在改成调用保存操作
|
||
// SaveServiceHelper.update(projectinfo);
|
||
OperationServiceHelper.executeOperate("save",entityName,new DynamicObject[]{projectinfo}, OperateOption.create());
|
||
}else{
|
||
//不存在,做新增 根据实体名称创建动态对象
|
||
projectinfo = BusinessDataServiceHelper.newDynamicObject(entityName);
|
||
projectinfo.set("billno", fbillno);
|
||
projectinfo.set("qeug_dwid", fdwid);
|
||
projectinfo.set("longnumber", fbillno);//长编码
|
||
projectinfo.set("billname", fbillname);
|
||
projectinfo.set("fullname", fbillname);//项目全称
|
||
// if(!DobeDWUtils.isEmpty(fparentid)){
|
||
// projectinfo.set("parent", null);//上级项目id
|
||
// projectinfo.set("parentname", null);//上级项目名称
|
||
// }
|
||
projectinfo.set("bizdate", new Date());//业务日期
|
||
projectinfo.set("showflag", true);//是否列表显示
|
||
// if(projectstageinfo != null){
|
||
// projectinfo.set("projectstage", projectstageinfo.getLong("id"));//项目阶段
|
||
// }
|
||
projectinfo.set("address", faddress);
|
||
projectinfo.set("acquiredate", project_getdate);
|
||
orginfo = BusinessDataServiceHelper.loadSingle(orgName,new QFilter[]{new QFilter("number","=",forgid)});
|
||
if(orginfo != null){
|
||
projectinfo.set("org", orginfo);//项目所属组织
|
||
projectinfo.set("purchaseorg", orginfo);//项目采购组织同所属组织
|
||
//核算组织默认和所属组织保持一致
|
||
projectinfo.set("fiorg", orginfo);
|
||
}else{
|
||
log.info(String.format("数仓传入的项目所属组织在金蝶中找不到:%s", forgid));
|
||
DobeDWUtils.saveLog(fbillno,"数仓项目同步",json_body.toString(),"数仓传入的项目所属组织在金蝶中找不到:"+forgid,false,"定时任务");
|
||
}
|
||
projectinfo.set("account", accttableid);//会计科目表默认新准则会计科目表
|
||
projectinfo.set("isleaf", true);
|
||
projectinfo.set("enable", 1);//是否启用
|
||
// projectinfo.set("islatestversion", true);//是否最新版-审核之后系统控制的,不需要在此处设置
|
||
projectinfo.set("billstatus", "A");//单据状态 A保存 B已提交 C已审核
|
||
projectinfo.set("versionnum", "V1.0");//版本号 默认值
|
||
projectinfo.set("creator", 43007523L);//创建人默认指定为金小蝶
|
||
if("新拓园区".equals(project_stage)){
|
||
projectinfo.set("qeug_combofield", "XTYQ");
|
||
}else if("成熟园区".equals(project_stage)){
|
||
projectinfo.set("qeug_combofield", "CSYQ");
|
||
}
|
||
projectinfo.set("qeug_ywlx", ywlx);
|
||
//TODO 数仓新增两个面积字段 20250221
|
||
// projectinfo.set("qeug_decimalfield1", json_body.getBigDecimal("addr"));//原始可出租面积
|
||
// projectinfo.set("qeug_decimalfield3", json_body.getBigDecimal("addr"));//原始建筑面积
|
||
// projectinfo.set("qeug_textfield2", calcDFL(json_body.getBigDecimal("addr"),
|
||
// json_body.getBigDecimal("addr")).toString());//原始得房率=可出租/建筑面积*100%
|
||
projectinfo.set("qeug_textfield1", json_body.getString("project_oriname"));//项目原名称
|
||
projectinfo.set("manageway", getManageway(json_body.getString("investment_model")));//投资模式
|
||
projectinfo.set("landusage", getLandusage(json_body.getString("land_usage")));//用地性质
|
||
projectinfo.set("acquiredate", json_body.getDate("project_gettime"));//项目获取时间
|
||
|
||
//保存数据:直接保存入库,不走操作校验
|
||
// SaveServiceHelper.save(new DynamicObject[]{projectinfo});
|
||
OperationServiceHelper.executeOperate("save",entityName,new DynamicObject[]{projectinfo}, OperateOption.create());
|
||
}
|
||
//获得组织对象后,将本项目的体系字段更新到组织的体系上面
|
||
if(!DobeDWUtils.isEmpty(project_stage) && !DobeDWUtils.isEmpty(forgid)){
|
||
// updateSql = "UPDATE t_org_org SET fk_qeug_combofield=? WHERE fnumber=?;";
|
||
if("新拓园区".equals(project_stage)){
|
||
DB.update(DBRoute.of("sys"), updateSql, new Object[]{"XTYQ",forgid});
|
||
}else if("成熟园区".equals(project_stage)){
|
||
DB.update(DBRoute.of("sys"), updateSql, new Object[]{"CSYQ",forgid});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private BigDecimal calcDFL(BigDecimal b1, BigDecimal b2){
|
||
//原始得房率=可出租 b1/建筑面积 b2*100%
|
||
if(b1 == null || b2 == null){
|
||
return BigDecimal.ZERO;
|
||
}else{
|
||
return b1.divide(b2,4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
|
||
}
|
||
}
|
||
|
||
private String getManageway(String manageway){
|
||
//根据数仓入参返回金蝶的投资模式
|
||
String typevalue;
|
||
switch (manageway){
|
||
case "收入要素":
|
||
typevalue = "1";
|
||
case "成本要素":
|
||
typevalue = "2";
|
||
case "管理费用":
|
||
typevalue = "3";
|
||
default:
|
||
typevalue = "0";
|
||
}
|
||
return typevalue;
|
||
}
|
||
|
||
private String getLandusage(String landusage){
|
||
//根据数仓入参返回金蝶的用地性质
|
||
String typevalue;
|
||
switch (landusage){
|
||
case "收入要素":
|
||
typevalue = "1";
|
||
case "成本要素":
|
||
typevalue = "2";
|
||
case "管理费用":
|
||
typevalue = "3";
|
||
default:
|
||
typevalue = "0";
|
||
}
|
||
return typevalue;
|
||
}
|
||
} |