解决方法:每增加一条,设置自动提交数据库
DBTool db=new DBTool();
Connection connection = db.getConnection();
connection.setAutoCommit(true);
代码如下:
package nc.glodob.task;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.common.RuntimeEnv;
import nc.bs.logging.Logger;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.trade.business.HYPubBO;
import nc.impl.org.DeptManageServiceBasicImpl;
import nc.itf.org.IDeptManageService;
import nc.itf.uap.pf.IPFBusiAction;
import nc.vo.ecpubapp.tools.DBTool;
import nc.vo.org.DeptSaveVO;
import nc.vo.org.DeptVO;
import nc.vo.org.OrgVO;
import nc.vo.org.orgmodel.OrgTypeVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFLiteralDate;
import nc.vo.pubapp.AppContext;
import nc.vo.sm.UserVO;
import nc.ws.bean.DeptMessageBean;
import nc.ws.bean.DeptMessageData;
import nc.ws.impl.server.NCToExoForUtils;
import nc.ws.utils.InitEvn;
import nc.ws.utils.JaxbUtil;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.ibm.db2.jcc.am.v;
/**
* 从主数据系统同步部门数据到nc
*
* @author xyl
* @date 2019-09-02 15:30:17
*/
public class QuerySynZSDeptWorkPlugin implements IBackgroundWorkPlugin {
HYPubBO bo=new HYPubBO();
private static BaseDAO dao;
public static BaseDAO getDao() {
if(dao == null)
dao = new BaseDAO();
return dao;
}
public PreAlertObject executeTask(BgWorkingContext bgwc) throws BusinessException {
PreAlertObject pr=new PreAlertObject();
StringBuffer logTxt = new StringBuffer();
String ret ="";
String DataCount="";
String deptyeshu= "";
String PageCount= "";
int i1=0,u=0,d=0;
// 同步指定日期阀值
String startdate = null;
String enddate = null;
String begin_date = null;
String end_date = null;
String data_date = null;
if (bgwc.getKeyMap() != null && bgwc.getKeyMap().size() > 0) {
startdate = (String) bgwc.getKeyMap().get("startdate");
enddate = (String) bgwc.getKeyMap().get("enddate");
}
try {
if (StringUtils.isNotEmpty(startdate) && StringUtils.isNotEmpty(enddate)) {
// 判断起始日期是否大于截止日期
if (compareDate(startdate, enddate)) {
logTxt.append("实参设置:[同步起始日期(startdate)] > [同步截止日期(enddate)],请检查!");
throw new BusinessException("实参设置:[同步起始日期(startdate)] > [同步截止日期(enddate)],请检查!");
}
begin_date = startdate + " 00:00:00";
end_date = enddate + " 23:59:59";
} else {
begin_date = new UFDate().toStdString().toString() + " 00:00:00";
end_date = new UFDate().toStdString().toString() + " 23:59:59";
}
data_date = begin_date + "~" + end_date;
// 主数据系统的实例号 001
// 调用主数据webservice的业务系统的编号 NC01
// 主数据系统字典编号 MDM_LW
String url=InitEvn.getValue("url");//测试地址
String soapaction=InitEvn.getValue("soapaction");// 域名,这是在server定义的
String operationName=InitEvn.getValue("operationName");// 调用方法名
deptyeshu=InitEvn.getValue("deptyeshu");// 调用方法名
if("".equals(deptyeshu) || StringUtils.isEmpty(deptyeshu) || null==deptyeshu){
deptyeshu="1";
}
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(soapaction, operationName)); // 设置要调用哪个方法
call.addParameter(new QName(soapaction, "psAppCode"), // 设置要传递的参数 主数据系统的实例号
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "psYwxtbh"), // 设置要传递的参数 主数据业务系统编号
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "psZdbh"), // 设置要传递的参数 主数据数据字典编号
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "psStartTime"), // 需要获取增量数据的起始时间,格式为: 2018-11-20 13:45:45.000
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "psEndTime"), // 需要获取增量数据的截止时间,格式为: 2018-11-20 13:45:45.000
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "piPageIndex"), // 设置要传递的参数 获取第几页数据 参数>0的整数
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "piPageSize"), // 设置要传递的参数 每页数据量的大小 <5000
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "piPageCount"), // 总页数
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "psErrMsg"), // 此参数返回本方法中出现的异常信息和提示信息,如参数不正确之类的信息均通过此参数返回
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// (标准的类型)
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + operationName);
Object[] params = new Object[]{"001","NC01","MBMRELATION",begin_date,end_date,deptyeshu,"5000","100",""};// 调用方法并传递参数
ret = (String) call.invoke(params);// 调用方法并传递参数
Document document = DocumentHelper.parseText(ret);
Element root = document.getRootElement();
PageCount= root.attributeValue("PageCount");//总页数
String ErrMsg= root.attributeValue("ErrMsg");//主数据返回的错误信息
DataCount= root.attributeValue("DataCount");//主数据返回的总条数
if(!"".equals(ErrMsg)){
logTxt.append("从主数据一共读取到"+PageCount+"页数据,目前正在获取第"+deptyeshu+"页数据时主数据返回的错误信息:"+ErrMsg+"\n");
}
DeptMessageData depts = JaxbUtil.convertToJavaBean(ret, DeptMessageData.class);
if(null != depts){
List<DeptMessageBean> sj = depts.getDATA();
if(null==sj || sj.size()<0){
throw new BusinessException("没有获取到部门数据!");
}
IDeptManageService ideptservice = NCLocator.getInstance().lookup(IDeptManageService.class);
DBTool db=new DBTool();
Connection connection = db.getConnection();
for (DeptMessageBean deptMessageBean : sj) {
try{
//转换vo
DeptVO deptvo = convertToDeptVO(deptMessageBean);
DeptVO[] deptvos = (DeptVO[]) getDao().retrieveByClause(DeptVO.class, " dr = 0 and def2 = '"+deptvo.getDef2()+"'").toArray(new DeptVO[0]);
if(deptvos != null && deptvos.length > 0){//更新
String tybz = deptvo.getDef19();
DeptVO ableVO =null;
if(StringUtils.isNotEmpty(tybz) && "0".equals(tybz)){//启用
deptvo.setEnablestate(2);
if(deptvo.getEnablestate() != deptvos[0].getEnablestate()){
//需要经过NC启用校验
ableVO = ideptservice.enableVO(deptvos[0]);
connection.setAutoCommit(true);
}
}else if(StringUtils.isNotEmpty(tybz) && "1".equals(tybz)){//停用
deptvo.setEnablestate(1);
if(deptvo.getEnablestate() != deptvos[0].getEnablestate()){
//需要经过NC停用校验
ableVO= ideptservice.disableVO(deptvos[0]);
connection.setAutoCommit(true);
}
}
// DeptVO vo = null;
if(ableVO==null){
deptvos[0].setCode(deptvo.getCode());
deptvos[0].setName(deptvo.getName());
deptvos[0].setShortname(deptvo.getShortname());
deptvos[0].setDepttype(deptvo.getDepttype());
deptvos[0].setPk_org(deptvo.getPk_org());
deptvos[0].setPk_group(deptvo.getPk_group());
deptvos[0].setCreatedate(deptvo.getCreatedate());
deptvos[0].setCreationtime(deptvo.getCreationtime());
deptvos[0].setCreator(deptvo.getCreator());
deptvos[0].setModifiedtime(deptvo.getModifiedtime());
deptvos[0].setModifier(deptvo.getModifier());
deptvos[0].setDef2(deptvo.getDef2());
deptvos[0].setDef19(deptvo.getDef19());
deptvos[0].setStatus(VOStatus.UPDATED);
deptvos[0].setDef18("主数据修改");
// vo = (DeptVO) NCToExoForUtils.setUpdateVO(deptvo, deptvos[0]);//遇见vo里有集合得那种就没法赋值了
ideptservice.updateVO(deptvos[0]);
connection.setAutoCommit(true);
}else{
if(1==deptvo.getEnablestate()){
ableVO.setDr(1);
}
ableVO.setCode(deptvo.getCode());
ableVO.setName(deptvo.getName());
ableVO.setShortname(deptvo.getShortname());
ableVO.setDepttype(deptvo.getDepttype());
ableVO.setPk_org(deptvo.getPk_org());
ableVO.setPk_group(deptvo.getPk_group());
ableVO.setCreatedate(deptvo.getCreatedate());
ableVO.setCreationtime(deptvo.getCreationtime());
ableVO.setCreator(deptvo.getCreator());
ableVO.setModifiedtime(deptvo.getModifiedtime());
ableVO.setModifier(deptvo.getModifier());
ableVO.setDef2(deptvo.getDef2());
ableVO.setDef19(deptvo.getDef19());
ableVO.setStatus(VOStatus.UPDATED);
ableVO.setDef18("主数据修改");
// vo = (DeptVO) NCToExoForUtils.setUpdateVO(deptvo, ableVO);
ideptservice.updateVO(ableVO);
//问题现象:一条数据出错,就全部回滚了 解决方法:设置数据库自动提交 begin
connection.setAutoCommit(true);
//end
}
u++;
}else{//新增
if("0".equals(deptvo.getDef19())){
deptvo.setDef10("N");
deptvo.setIslastversion(UFBoolean.FALSE);
deptvo.setCreatedate(new UFLiteralDate());
deptvo.setDisplayorder(999999);
deptvo.setHrcanceled(UFBoolean.FALSE);
deptvo.setIsretail(UFBoolean.FALSE);
deptvo.setDef18("主数据新增");//主数据停用标识
deptvo.setModifiedtime(null);
deptvo.setModifier(null);
deptvo.setStatus(VOStatus.NEW);
deptvo.setEnablestate(2);
ideptservice.insertVO(deptvo);
connection.setAutoCommit(true);
i1++;
}else{
d++;
throw new Exception("此消息为提示消息,可忽略(从主数据新增时初始状态为停用的NC不接)");
}
}
}catch(Exception e){
logTxt.append(e.getMessage()+"==>部门内码=【"+deptMessageBean.getNM()+"】"+"\n");
continue;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
logTxt.append(ex.getMessage()+"\n");
}
String log="";
if(!"".equals(DataCount)){
log="从主数据一共读取到"+PageCount+"页数据,目前正在获取第"+deptyeshu+"页数据,从主数据系统共获取"+DataCount+"条部门信息数据,";
}
//日志记录
outXMessageText("主数据返回信息:"+ret+"\n"+log+"成功插入"+i1+"条,更新"+u+"条,新增时初始状态为停用NC不接的有"+d+"条部门数据,以下是错误记录:"+logTxt.toString()+"\n",data_date);
pr.setReturnType(PreAlertReturnType.RETURNMESSAGE);
pr.setReturnObj(logTxt.toString());
pr.setBusiObj(logTxt.toString());
return pr;
}
/**
* 转换成部门vo
* @param datas
* @return
* @throws BusinessException
*/
public DeptVO convertToDeptVO(DeptMessageBean deptb) throws Exception {
DeptVO deptvo=new DeptVO();
//编码
if(StringUtils.isNotEmpty(deptb.getORGCODE()) && null != deptb.getORGCODE()){
deptvo.setCode(deptb.getORGCODE());
}else{
throw new Exception("部门编码不能为空!");
}
//名称
if(StringUtils.isNotEmpty(deptb.getORGNAME()) && null != deptb.getORGNAME()){
deptvo.setName(deptb.getORGNAME());
}else{
throw new Exception("部门名称不能为空!");
}
//简称
deptvo.setShortname(deptb.getABBREVITION());
//部门属性
if("普通部门".equals(deptb.getMDMBMZD_SXNM())){
deptvo.setDepttype(0);
}else if("虚拟部门".equals(deptb.getMDMBMZD_SXNM())){
deptvo.setDepttype(1);
}
//查询所属组织
String ssgs = deptb.getORGUNIT();
String pk_org = (String) bo.findColValue("org_orgs", "pk_org", "def2 = '"+ssgs+"' and def1<>'N' ");
if(StringUtils.isNotEmpty(pk_org) && null != pk_org){
deptvo.setPk_org(pk_org);
}else{
throw new Exception("查询不到对应的所属组织!");
}
//查询所属集团
String pk_group = (String) bo.findColValue("org_orgs", "pk_group", "def2 = '"+ssgs+"' and def1<>'N' ");
if(pk_group == null)
pk_group = (String) bo.findColValue("org_group", "pk_group", " dr = 0 and ts in (select max(ts) from org_group where dr = 0)");
deptvo.setPk_group(pk_group);
if(StringUtils.isNotEmpty(deptb.getCREATETIME()) && null != deptb.getCREATETIME() && !"".equals(deptb.getCREATETIME())){//时间格式没定义格式没法转换
String time=deptb.getCREATETIME();
String[] split = time.split(" ");
String ymd = split[0];//年月日
String smm = split[1];//时分秒
String[] split2 = ymd.split("/");
String month=split2[1].length()<2 ? "0" + split2[1] : split2[1] ;
String day=split2[2].length()<2 ? "0" + split2[2] : split2[2] ;
ymd=split2[0]+"-"+month+"-"+day;
String[] split3 = smm.split(":");
String hour=split3[0].length()<2 ? "0" + split3[0] : split3[0] ;
String minute=split3[1].length()<2 ? "0" + split3[1] : split3[1] ;
String mm=split3[2].length()<2 ? "0" + split3[2] : split3[2] ;
smm=hour+":"+minute+":"+mm;
String date=ymd+" "+smm;
deptvo.setCreationtime(new UFDateTime(date));
deptvo.setCreatedate(new UFLiteralDate(ymd));
}else{
deptvo.setCreationtime(new UFDateTime());
deptvo.setCreatedate(new UFLiteralDate());
}
if(StringUtils.isNotEmpty(deptb.getCREATEUSER()) && null != deptb.getCREATEUSER()){
UserVO[] uservo = (UserVO[]) bo.queryByCondition(UserVO.class, "user_name = '" + deptb.getCREATEUSER() + "' and dr='0'");
if(null !=uservo && 0<uservo.length){
deptvo.setCreator(uservo[0].getCuserid());
}
}else{
deptvo.setCreator("1001B6100000000008OR");
}
if(StringUtils.isNotEmpty(deptb.getLASTMODIFIEDTIME()) && null != deptb.getLASTMODIFIEDTIME() && !"".equals(deptb.getLASTMODIFIEDTIME())){
String time=deptb.getLASTMODIFIEDTIME();
String[] split = time.split(" ");
String ymd = split[0];//年月日
String smm = split[1];//时分秒
String[] split2 = ymd.split("/");
String month=split2[1].length()<2 ? "0" + split2[1] : split2[1] ;
String day=split2[2].length()<2 ? "0" + split2[2] : split2[2] ;
ymd=split2[0]+"-"+month+"-"+day;
String[] split3 = smm.split(":");
String hour=split3[0].length()<2 ? "0" + split3[0] : split3[0] ;
String minute=split3[1].length()<2 ? "0" + split3[1] : split3[1] ;
String mm=split3[2].length()<2 ? "0" + split3[2] : split3[2] ;
smm=hour+":"+minute+":"+mm;
String date=ymd+" "+smm;
deptvo.setModifiedtime(new UFDateTime(date));
}else{
deptvo.setModifiedtime(new UFDateTime());
}
if(StringUtils.isNotEmpty(deptb.getLASTMODIFIEDUSER()) && null != deptb.getLASTMODIFIEDUSER()){
UserVO[] uservo = (UserVO[]) bo.queryByCondition(UserVO.class, "user_name = '" + deptb.getLASTMODIFIEDUSER() + "' and dr='0'");
if(null !=uservo && 0<uservo.length){
deptvo.setModifier(uservo[0].getCuserid());
}
}else{
deptvo.setModifier("1001B6100000000008OR");
}
deptvo.setDef2(deptb.getNM());//主数据内码
deptvo.setDef19(deptb.getTYBZ());//主数据停用标识
return deptvo;
}
/**
* 日志输出
* @param xml
* @param type
*/
private void outXMessageText(String xml,String date) {
try {
String path = RuntimeEnv.getInstance().getNCHome() + File.separator + "mapplogs" + File.separator + "部门信息";
File file = new File(path);
if (!(file.exists())) {
// file.mkdir();
file.mkdirs();
}
String filename = AppContext.getInstance().getServerTime().toString() + "_" + "部门信息" + ".txt";
filename = filename.replaceAll(" ", "").replaceAll(":", "").replaceAll("-", "");
String filePath = path + File.separator + filename;
// OutputStream out = new FileOutputStream(filePath);
FileWriter fw = new FileWriter(filePath);
BufferedWriter out =new BufferedWriter(fw);
out.write(xml);
out.newLine();
out.write("时间周期:");
if(null !=date || StringUtils.isNotEmpty(date)){
out.write(date);
}
out.flush();
out.close();
} catch (Exception e) {
Logger.error(xml);
}
}
/**
* 对比同步起始时间和同步截止时间的大小
*
* @param d1
* @param d2
* @return
* @throws BusinessException
* @author xueyl
* @update 2019-07-24
*/
public boolean compareDate(String d1,String d2) throws BusinessException {
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
try {
return df.parse(d1).getTime() > df.parse(d2).getTime();
} catch (ParseException e) {
throw new BusinessException(
"实参设置:[同步起始日期(startdate)]或[同步截止日期(enddate)]的日期录入有误,日期格式如:2015/12/12,请检查!");
}
}
}