package com.integrated.zyyt.service; import cn.hutool.core.convert.Convert; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.integrated.zyyt.ZyytConstant; import com.integrated.zyyt.enetity.StationInfo; import com.integrated.zyyt.enetity.YyztTDjtjb; import com.integrated.zyyt.enetity.YyztTShkdrb; import com.integrated.zyyt.enetity.ZyytDataResult; import com.integrated.zyyt.enetity.business.Zyyt; import com.integrated.zyyt.enetity.business.ZyytStationInfo; import com.integrated.zyyt.enetity.business.ZyytTDjtjb; import com.integrated.zyyt.enetity.business.ZyytTShkdrb; import com.integrated.zyyt.util.ZyytUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import javax.annotation.Resource; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Map; @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class ZyytService { // @Resource // private NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Resource private JdbcTemplate jdbcTemplate; // private Integer getBatchIsFinish(String batchNo) { // String querySql = "select IS_FINISH from ZYYT_RECORD where BATCH_NO = '" + batchNo+"'"; // return jdbcTemplate.queryForObject(querySql, Integer.class); // } //站点信息表 public void stationInfoTasks() { log.info("开始进行 机构同步"); LocalDate yestDay = LocalDate.now().minus(1, ChronoUnit.DAYS); String batchNo = ZyytUtil.getBatchNo(yestDay); if (!canGetAgain("STATIONINFO", yestDay)) { log.info("批次{}正在运行", batchNo); return; } JSONObject queryParams = new JSONObject(); Zyyt stationInfoService = new ZyytStationInfo( ZyytConstant.URL_AUTHON, ZyytConstant.URL_STATIONINFO_QUERY, false, ZyytConstant.CLIENT_STATIONINFO, ZyytConstant.SECRET_STATIONINFO, ZyytConstant.X_EOS_SOURCESYSKEY_STATIONINFO, queryParams ); String exeSql = "merge into STATIONINFO A USING" + " (select ? V_JGBH from dual ) C " + "ON(A.V_JGBH=C.V_JGBH) " + "when matched then " + "update SET A.V_SFMC=?,A.V_SFDM=?,A.V_DSDM=?,A.V_DSMC=?,A.V_QXDM=?,A.V_XSMC=?,A.V_TDJGBH=?,A.V_TDJGMC=?,A.V_JGMC=?,A.V_LXRXM=?,A.V_LXDH=?,A.V_SJHM=?,A.V_LXDZ=?,A.ISCOUNTRY=?,A.C_WDLX =?,A.C_YZBM=?,A.V_YYWDBH=?,A.V_YYWDMC=?,A.V_GPSJD=?,A.V_GPSWD=?,A.YLGZDID =?,A.YLGZDMC=?,A.V_STATUS=?,A.D_SQRQ=?,A.D_SPRQ=?" + "when not matched then " + "insert(A.V_SFMC,A.V_SFDM,A.V_DSDM,A.V_DSMC,A.V_QXDM,A.V_XSMC,A.V_TDJGBH,A.V_TDJGMC,A.V_JGBH,A.V_JGMC,A.V_LXRXM,A.V_LXDH,A.V_SJHM,A.V_LXDZ,A.ISCOUNTRY,A.C_WDLX ,A.C_YZBM,A.V_YYWDBH,A.V_YYWDMC,A.V_GPSJD,A.V_GPSWD,A.YLGZDID ,A.YLGZDMC,A.V_STATUS,A.D_SQRQ,A.D_SPRQ) values " + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; Long recordId = stationInfoService.insertRecord("STATIONINFO", batchNo); Long totalSize = 0L; int pageNum = 0; int pageSize = 500; //设置回滚点 Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); try { // 查询总数 ZyytDataResult zyytDataResult = stationInfoService.getData(pageNum, 1); Long totalElements = zyytDataResult.getTotalElements(); Long totalPages = 0L; totalPages = totalElements / pageSize; if (totalElements % pageSize != 0) { totalPages++; } log.info("共{}页,{}条数据", totalPages, totalElements); for (pageNum = 0; pageNum <= totalPages - 1; pageNum++) { zyytDataResult = stationInfoService.getData(pageNum, pageSize); List stationInfoList = zyytDataResult.getContent(); log.info("第{}次获取,本次获取到{}条数据", pageNum + 1, zyytDataResult.getContentSize()); totalSize += zyytDataResult.getContentSize(); // 将数据分批次插入对应的数据表 int batchSize = 500; int length = stationInfoList.size(); // 计算可以分成多少组 int num = length / batchSize; for (int i = 0; i <= num; i++) { // 开始位置 int fromIndex = i * batchSize; // 结束位置 int toIndex = (i + 1) * batchSize < length ? (i + 1) * batchSize : length; if (fromIndex == batchSize) { continue; } System.out.println(fromIndex + " === " + toIndex); List stationInfoListCopy = stationInfoList.subList(fromIndex, toIndex); try { jdbcTemplate.batchUpdate(exeSql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { StationInfo stationInfo = stationInfoListCopy.get(i); log.info("stationInfo==> {}", JSON.toJSONString(stationInfo)); ps.setObject(1, stationInfo.getVJgbh()); ps.setObject(2, stationInfo.getVSfmc()); ps.setObject(3, stationInfo.getVSfdm()); ps.setObject(4, stationInfo.getVDsdm()); ps.setObject(5, stationInfo.getVDsmc()); ps.setObject(6, stationInfo.getVQxdm()); ps.setObject(7, stationInfo.getVXsmc()); ps.setObject(8, stationInfo.getVTdjgbh()); ps.setObject(9, stationInfo.getVTdjgmc()); ps.setObject(10, stationInfo.getVJgmc()); ps.setObject(11, stationInfo.getVLxrxm()); ps.setObject(12, stationInfo.getVLxdh()); ps.setObject(13, stationInfo.getVSjhm()); ps.setObject(14, stationInfo.getVLxdz()); ps.setObject(15, stationInfo.getIscountry()); ps.setObject(16, stationInfo.getCWdlx()); ps.setObject(17, stationInfo.getCYzbm()); ps.setObject(18, stationInfo.getVYywdbh()); ps.setObject(19, stationInfo.getVYywdmc()); ps.setObject(20, stationInfo.getVGpsjd()); ps.setObject(21, stationInfo.getVGpswd()); ps.setObject(22, stationInfo.getYlgzdid()); ps.setObject(23, stationInfo.getYlgzdmc()); ps.setObject(24, stationInfo.getVStatus()); ps.setDate(25, new Date(stationInfo.getDSqrq().getTime())); // ps.setObject(25, stationInfo.getDSqrq()); // ps.setObject(26, stationInfo.getDSprq()); ps.setDate(26, new Date(stationInfo.getDSprq().getTime())); ps.setObject(27, stationInfo.getVSfmc()); ps.setObject(28, stationInfo.getVSfdm()); ps.setObject(29, stationInfo.getVDsdm()); ps.setObject(30, stationInfo.getVDsmc()); ps.setObject(31, stationInfo.getVQxdm()); ps.setObject(32, stationInfo.getVXsmc()); ps.setObject(33, stationInfo.getVTdjgbh()); ps.setObject(34, stationInfo.getVTdjgmc()); ps.setObject(35, stationInfo.getVJgbh()); ps.setObject(36, stationInfo.getVJgmc()); ps.setObject(37, stationInfo.getVLxrxm()); ps.setObject(38, stationInfo.getVLxdh()); ps.setObject(39, stationInfo.getVSjhm()); ps.setObject(40, stationInfo.getVLxdz()); ps.setObject(41, stationInfo.getIscountry()); ps.setObject(42, stationInfo.getCWdlx()); ps.setObject(43, stationInfo.getCYzbm()); ps.setObject(44, stationInfo.getVYywdbh()); ps.setObject(45, stationInfo.getVYywdmc()); ps.setObject(46, stationInfo.getVGpsjd()); ps.setObject(47, stationInfo.getVGpswd()); ps.setObject(48, stationInfo.getYlgzdid()); ps.setObject(49, stationInfo.getYlgzdmc()); ps.setObject(50, stationInfo.getVStatus()); // ps.setObject(51, stationInfo.getDSqrq()); ps.setDate(51, new Date(stationInfo.getDSqrq().getTime())); ps.setObject(52, stationInfo.getDSprq()); ps.setDate(52, new Date(stationInfo.getDSprq().getTime())); } @Override public int getBatchSize() { return stationInfoListCopy.size(); } }); } catch (Exception e) { log.error(e.getMessage()); //手工回滚异常 TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } } } } catch (Exception e) { log.error(e.getMessage()); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } stationInfoService.updateRecord(recordId, 0, totalSize, null); log.info("···········中邮易通 站点信息 任务执行完毕···········"); } //社会快递日报表 /** * @param date */ public void shkdrbTasks(LocalDate date) { log.info("开始进行 社会快递日报表"); String batchNo = ZyytUtil.getBatchNo(date); // Integer batchIsFinish = getBatchIsFinish(batchNo); // if (batchIsFinish != null && batchIsFinish == 0) { // log.info("批次{}正在运行", batchNo); // return; // } if (!canGetAgain("YYZT_T_SHKDRB", date)) { log.info("YYZT_T_SHKDRB 的 批次{}正在运行", batchNo); return; } // 删除已有批次数据 String delSql = "delete from YYZT_T_SHKDRB where BATCH_NO='" + batchNo + "'"; jdbcTemplate.update(delSql); // 设置请求参数 JSONObject queryParams = new JSONObject(); JSONObject criteria = new JSONObject(); criteria.put("match", "and"); JSONArray matchChildren = new JSONArray(); JSONObject children1 = new JSONObject(); children1.put("name", "dRbrq"); JSONArray child1C = new JSONArray(); child1C.add(date + " 00:00:00"); children1.put("value", child1C); children1.put("match", "eq"); matchChildren.add(children1); criteria.put("children", matchChildren); queryParams.put("criteria", criteria); Zyyt stationInfoService = new ZyytTShkdrb( ZyytConstant.URL_AUTHON, ZyytConstant.URL_SHKDRB_QUERY, false, ZyytConstant.CLIENT_SHKDRB, ZyytConstant.SECRET_SHKDRB, ZyytConstant.X_EOS_SOURCESYSKEY_SHKDRB, queryParams ); Long recordId = stationInfoService.insertRecord("YYZT_T_SHKDRB", batchNo); Long totalSize = 0L; //设置回滚点 Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); try { int pageNum = 0; int pageSize = 500; String batchInsertSql = "INSERT INTO YYZT_T_SHKDRB (\"V_SFDM\",\"V_SFMC\",\"V_DSDM\",\"V_DSMC\",\"V_QXDM\",\"V_XSMC\",\"V_TDJGBH\",\"V_TDJGMC\",\"V_JGBH\",\"V_JGMC\",\"D_RBRQ\",\"N_ZGYZLJS\",\"N_ZGYZQJS\",\"N_SHKDLJS\",\"N_SHKDQJS\",\"N_ZTLJS\",\"N_ZTQJS\",\"N_YTLJS\",\"N_YTQJS\",\"N_STLJS\",\"N_STQJS\",\"N_BSLJS\",\"N_BSQJS\",\"N_YDLJS\",\"N_YDQJS\",\"N_YFLJS\",\"N_YFQJS\",\"N_JDLJS\",\"N_JDQJS\",\"N_JTLJS\",\"N_JTQJS\",\"N_TTLJS\",\"N_TTQJS\",\"N_DBLJS\",\"N_DBQJS\",\"N_DNLJS\",\"N_DNQJS\",\"N_FWLJS\",\"N_FWQJS\",\"N_YSLJS\",\"N_YSQJS\",\"N_QTLJS\",\"N_QTQJS\",\"RESERVED1\",\"RESERVED2\",\"RESERVED3\",\"RESERVED4\",\"BATCH_NO\") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; // 查询总数 ZyytDataResult zyytDataResult = stationInfoService.getData(pageNum, 1); Long totalElements = zyytDataResult.getTotalElements(); Long totalPages = 0L; totalPages = totalElements / pageSize; if (totalElements % pageSize != 0) { totalPages++; } log.info("共{}页,{}条数据", totalPages, totalElements); for (pageNum = 0; pageNum <= totalPages - 1; pageNum++) { zyytDataResult = stationInfoService.getData(pageNum, pageSize); List stationInfoList = zyytDataResult.getContent(); totalSize += zyytDataResult.getContentSize(); log.info("第{}次获取,本次获取到{}条数据", pageNum + 1, zyytDataResult.getContentSize()); // 将数据分批次插入对应的数据表 int batchSize = 500; int length = stationInfoList.size(); // 计算可以分成多少组 int num = length / batchSize; for (int i = 0; i <= num; i++) { // 开始位置 int fromIndex = i * batchSize; // 结束位置 int toIndex = (i + 1) * batchSize < length ? (i + 1) * batchSize : length; if (fromIndex == batchSize) { continue; } List yyztTShkdrbs = stationInfoList.subList(fromIndex, toIndex); try { List insertShkdrbList = new ArrayList<>(); yyztTShkdrbs.forEach(item -> { insertShkdrbList.add(new Object[]{ item.getVSfdm(), item.getVSfmc(), item.getVDsdm(), item.getVDsmc(), item.getVQxdm(), item.getVXsmc(), item.getVTdjgbh(), item.getVTdjgmc(), item.getVJgbh(), item.getVJgmc(), item.getDRbrq(), item.getNZgyzljs(), item.getNZgyzqjs(), item.getNShkdljs(), item.getNShkdqjs(), item.getNZtljs(), item.getNZtqjs(), item.getNYtljs(), item.getNYtqjs(), item.getNStljs(), item.getNStqjs(), item.getNBsljs(), item.getNBsqjs(), item.getNYdljs(), item.getNYdqjs(), item.getNYfljs(), item.getNYfqjs(), item.getNJdljs(), item.getNJdqjs(), item.getNJtljs(), item.getNJtqjs(), item.getNTtljs(), item.getNTtqjs(), item.getNDbljs(), item.getNDbqjs(), item.getNDnljs(), item.getNDnqjs(), item.getNFwljs(), item.getNFwqjs(), item.getNYsljs(), item.getNYsqjs(), item.getNQtljs(), item.getNQtqjs(), item.getReserved1(), item.getReserved2(), item.getReserved3(), item.getReserved4(), item.getBatchNo() }); }); jdbcTemplate.batchUpdate(batchInsertSql, insertShkdrbList); } catch (Exception e) { log.error(e.getMessage()); //手工回滚异常 TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } } } } catch ( Exception e) { log.error(e.getMessage()); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } stationInfoService.updateRecord(recordId, 0, totalSize, null); log.info("···········中邮易通 社会快递日报表 任务执行完毕···········"); } // 代寄统计表 public void djtjbTasks(LocalDate date) { log.info("开始进行 代寄统计表"); String batchNo = ZyytUtil.getBatchNo(date); // Integer batchIsFinish = getBatchIsFinish(batchNo); // if (batchIsFinish != null && batchIsFinish == 0) { // log.info("批次{}正在运行", batchNo); // return; // } if (!canGetAgain("YYZT_T_DJTJB", date)) { log.info("YYZT_T_DJTJB 的 批次{}正在运行", batchNo); return; } // 删除已有批次数据 String delSql = "delete from YYZT_T_DJTJB where BATCH_NO='" + batchNo + "'"; jdbcTemplate.update(delSql); // 设置请求参数 JSONObject queryParams = new JSONObject(); JSONObject criteria = new JSONObject(); criteria.put("match", "and"); JSONArray matchChildren = new JSONArray(); JSONObject children1 = new JSONObject(); children1.put("name", "dRbrq"); JSONArray child1C = new JSONArray(); child1C.add(date + " 00:00:00"); children1.put("value", child1C); children1.put("match", "eq"); matchChildren.add(children1); criteria.put("children", matchChildren); queryParams.put("criteria", criteria); Zyyt stationInfoService = new ZyytTDjtjb( ZyytConstant.URL_AUTHON, ZyytConstant.URL_DJTJB_QUERY, false, ZyytConstant.CLIENT_DJTJB, ZyytConstant.SECRET_DJTJB, ZyytConstant.X_EOS_SOURCESYSKEY_DJTJB, queryParams ); Long recordId = stationInfoService.insertRecord("YYZT_T_DJTJB", batchNo); Long totalSize = 0L; //设置回滚点 Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); try { int pageNum = 0; int pageSize = 500; String batchInsertSql = "INSERT INTO YYZT_T_DJTJB (\"V_SFDM\", \"V_SFMC\", \"V_DSDM\", \"V_DSMC\", \"V_QXDM\", \"V_XSMC\", \"V_TDJGBH\", \"V_JGBH\", \"V_JGMC\", \"D_RBRQ\", \"V_YLGZDID\", \"V_YLGZDMC\", \"N_SJYWL\", \"N_TKYWL\", \"N_BKYEL\", \"N_SJZJE\", \"N_TKSR\", \"N_BKSR\", \"BATCH_NO\") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // 查询总数 ZyytDataResult zyytDataResult = stationInfoService.getData(pageNum, 1); Long totalElements = zyytDataResult.getTotalElements(); Long totalPages = 0L; totalPages = totalElements / pageSize; if (totalElements % pageSize != 0) { totalPages++; } log.info("共{}页,{}条数据", totalPages, totalElements); for (pageNum = 0; pageNum <= totalPages - 1; pageNum++) { zyytDataResult = stationInfoService.getData(pageNum, pageSize); List djtjbList = zyytDataResult.getContent(); totalSize += zyytDataResult.getContentSize(); log.info("第{}次获取,本次获取到{}条数据", pageNum + 1, zyytDataResult.getContentSize()); // 将数据分批次插入对应的数据表 int batchSize = 500; int length = djtjbList.size(); // 计算可以分成多少组 int num = length / batchSize; for (int i = 0; i <= num; i++) { // 开始位置 int fromIndex = i * batchSize; // 结束位置 int toIndex = (i + 1) * batchSize < length ? (i + 1) * batchSize : length; if (fromIndex == batchSize) { continue; } System.out.println(fromIndex + " === " + toIndex); List yyztTDjtjbs = djtjbList.subList(fromIndex, toIndex); try { List insertShkdrbList = new ArrayList<>(); yyztTDjtjbs.forEach(item -> { insertShkdrbList.add(new Object[]{ item.getVSfdm(), item.getVSfmc(), item.getVDsdm(), item.getVDsmc(), item.getVQxdm(), item.getVXsmc(), item.getVTdjgbh(), item.getVJgbh(), item.getVJgmc(), item.getDRbrq(), item.getVYlgzdid(), item.getVYlgzdmc(), item.getNSjywl(), item.getNTkywl(), item.getNBkyel(), item.getNSjzje(), item.getNTksr(), item.getNBksr(), item.getBatchNo() }); }); jdbcTemplate.batchUpdate(batchInsertSql, insertShkdrbList); } catch (Exception e) { log.error(e.getMessage()); //手工回滚异常 TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } } } } catch (Exception e) { log.error(e.getMessage()); stationInfoService.updateRecord(recordId, 1, 0L, e.getMessage()); return; } stationInfoService.updateRecord(recordId, 0, totalSize, null); log.info("···········中邮易通 代寄统计表 任务执行完毕···········"); } /** * 判断指定日期是否需要重新执行 * * @param tableName * @param date * @return true 需要执行,false 不需要 */ private boolean canGetAgain(String tableName, LocalDate date) { String sql = "SELECT*FROM (SELECT*FROM ZYYT_RECORD WHERE BATCH_NO='" + ZyytConstant.batchNoPrefix + date + "' AND TABLE_NAME='" + tableName + "' ORDER BY START_TIME DESC) tmp WHERE ROWNUM<=1"; Map map = null; try { map = jdbcTemplate.queryForMap(sql); } catch (Exception e) { log.info("sql=={}没有查询到数据", sql); } if (map != null) { String isFinish = Convert.toStr(map.get("IS_FINISH")); if (!"1".equals(isFinish)) { return false; } //下面 isFinish都是1 String isError = Convert.toStr(map.get("IS_ERROR")); if ("1".equals(isError)) { // 执行有误 return true; } // 成功执行没有错 String countNum = Convert.toStr(map.get("COUNT_NUM")); if ("0".equals(countNum)) { //没有拉取到数据 log.info("stationInfoTask"); return true; } } else { // 该批次未执行 log.info("stationInfoTask"); return true; } return false; } }