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.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.Date; import java.util.Map; /** * 后台任务插件 */ public class DobeDWprojectTask extends AbstractTask implements Plugin { private static final String entityName = "repmd_projectbill";//表名 t_repmd_projectbill private static Log log = LogFactory.getLog(DobeDWprojectTask.class); private static final String dw_menthod = "mdm_projectinfo"; @Override public void execute(RequestContext requestContext, Map 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 forgid = null;//所属组织-行政组织 Date project_getdate = null;//项目获取时间 String fdwid = null;//数仓项目id DynamicObject orginfo = null; DynamicObject projectinfo = null; 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"); fprojectstageid = json_body.getString("project_stage"); fdwid = json_body.getString("project_id"); if(DobeDWUtils.isEmpty(fbillno) || DobeDWUtils.isEmpty(fbillname) || DobeDWUtils.isEmpty(forgid) || 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"))){ //如果项目已审核,则不进行修改 continue; } //已存在,做更新 名称 阶段 版本号等信息;组织、编号、是否叶子节点不能更新;需要前台操作 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("billstatus", "A");//单据状态 A保存 B已提交 C已审核 projectinfo.set("showflag", true);//是否列表显示 projectinfo.set("enable", 1);//是否启用 projectinfo.set("islatestversion", true);//是否最新版 projectinfo.set("mainprojectid", projectinfo.getLong("id"));//主项目ID // projectinfo.set("billno", fbillno); // projectinfo.set("org", ); // projectinfo.set("isleaf", fisleaf); SaveServiceHelper.update(projectinfo); }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 = QueryServiceHelper.queryOne("bos_org","id,number,name",new QFilter[]{new QFilter("number","=",forgid)}); if(orginfo != null){ projectinfo.set("org", orginfo.getLong("id"));//项目所属组织 projectinfo.set("purchaseorg", orginfo.getLong("id"));//项目采购组织同所属组织 }else{ log.info(String.format("数仓传入的项目所属组织在金蝶中找不到:%s", forgid)); DobeDWUtils.saveLog(fbillno,"数仓项目同步",json_body.toString(),"数仓传入的项目所属组织在金蝶中找不到:"+forgid,false,"定时任务"); } projectinfo.set("isleaf", true); projectinfo.set("enable", 1);//是否启用 projectinfo.set("billstatus", "A");//单据状态 A保存 B已提交 C已审核 projectinfo.set("creator", 43007523L);//创建人默认指定为金小蝶 //保存数据:直接保存入库,不走操作校验 SaveServiceHelper.save(new DynamicObject[]{projectinfo}); } } } }