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.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.Map; /** * 后台任务插件 */ public class DobeDWorgRelationTask extends AbstractTask implements Plugin { private static Log log = LogFactory.getLog(DobeDWorgRelationTask.class); private static final String entityName = "qeug_recon_orgrelation";//供应链库 表名 tk_qeug_recon_orgrelation private static final String orgEntityName = "bos_org";//系统库 表名 t_ORG_ORG private static final String dw_menthod = "mdm_orgcorp"; @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("",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查询总数据量 handleOrgRelation(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("",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); handleOrgRelation(json_body); } } } private void handleOrgRelation(JSONObject json_body) { //解析接口返回值,与系统数据比较 JSONArray detailsJson = json_body.getJSONArray("data"); DynamicObject orginfo = null; String orgNumber = null; String orgName = null; String companyNumber = null; String companyName = null; String deptNumber = null; String deptName = null; String paybank = null; String paynumber = null; String taxno = null; DynamicObject yworginfo = null; for (int i = 0; i < detailsJson.size(); i++) { json_body = detailsJson.getJSONObject(i); orgNumber = json_body.getString("oaorg_code");//oa组织code orgName = json_body.getString("oaorg_name");//oa组织名称 companyNumber = json_body.getString("corp_code");//财务公司编号 companyName = json_body.getString("corp_name");//财务公司名称 deptNumber = json_body.getString("corpdep_code");//财务公司部门编号 deptName = json_body.getString("corpdep_name");//财务公司部门名称 paybank = json_body.getString("corp_bank");//财务公司银行 paynumber = json_body.getString("corp_bankaccount");//财务公司银行账户 taxno = json_body.getString("corp_tax");//财务公司税号 if(DobeDWUtils.isEmpty(orgNumber) || DobeDWUtils.isEmpty(companyNumber) || DobeDWUtils.isEmpty(deptNumber) || DobeDWUtils.isEmpty(paynumber)){ log.info(String.format("组织对应关系接口入参为空异常:%s", json_body.toJSONString())); continue; } //根据组织编号查找系统现有数据是否存在 "id,number,name,qeug_companynumber,qeug_companyname,qeug_deptnumber,qeug_deptname", orginfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",orgNumber)}); if(orginfo != null && isNeedUpdate(orginfo,companyNumber,deptNumber,paynumber,taxno)){ //QueryServiceHelper.queryOne查出来的对象不是DynamicObject 而是平铺对象plainobject 此对象不能在后续代码中进行修改和更新; orginfo.set("name", orgName); orginfo.set("qeug_companynumber", companyNumber); orginfo.set("qeug_companyname", companyName); orginfo.set("qeug_deptnumber", deptNumber); orginfo.set("qeug_deptname", deptName); orginfo.set("qeug_paybank", paybank); orginfo.set("qeug_paynumber", paynumber); orginfo.set("qeug_taxno", taxno); SaveServiceHelper.update(orginfo); }else{ //不存在,做新增 根据实体名称创建动态对象 orginfo = BusinessDataServiceHelper.newDynamicObject(entityName); orginfo.set("number", orgNumber); orginfo.set("name", orgName); orginfo.set("qeug_companynumber", companyNumber); orginfo.set("qeug_companyname", companyName); orginfo.set("qeug_deptnumber", deptNumber); orginfo.set("qeug_deptname", deptName); orginfo.set("qeug_paybank", paybank); orginfo.set("qeug_paynumber", paynumber); orginfo.set("qeug_taxno", taxno); orginfo.set("enable", 1); orginfo.set("creator", 43007523L);//创建人默认指定为金小蝶 //保存数据:直接保存入库,不走操作校验 SaveServiceHelper.save(new DynamicObject[]{orginfo}); } //根据行政组织编号找到星瀚上的业务单元对象 yworginfo = BusinessDataServiceHelper.loadSingle(orgEntityName,new QFilter[]{new QFilter("number","=",orgNumber)}); if(yworginfo != null){ yworginfo.set("ffirmname", companyName);//业务单元-公司名称 yworginfo.set("depositbank", paybank);//业务单元-开户行 yworginfo.set("bankaccount", paynumber);//业务单元-银行账户 yworginfo.set("uniformsocialcreditcode", taxno);//业务单元-统一社会信用代码 SaveServiceHelper.update(yworginfo); } } } private boolean isNeedUpdate(DynamicObject orginfo,String companyNumber,String deptNumber,String paynumber,String taxno){ if(!companyNumber.equals(orginfo.getString("qeug_companynumber"))){ return true; } if(!deptNumber.equals(orginfo.getString("qeug_deptnumber"))){ return true; } if(!paynumber.equals(orginfo.getString("qeug_paynumber"))){ return true; } if(DobeDWUtils.isEmpty(taxno) || !taxno.equals(orginfo.getString("qeug_taxno"))){ return true; } return false; } }