提交 7bdc607a 作者: 轩辕玉

实现汇聚平台对账数据生成功能

实现数据写到excel文件功能
实现数据封装成XML格式功能
实现汇聚平台推送功能
上级 e499e03f
...@@ -6,7 +6,6 @@ import com.reconciliation.recfj.service.RecService; ...@@ -6,7 +6,6 @@ import com.reconciliation.recfj.service.RecService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
...@@ -27,4 +26,16 @@ public class RecController { ...@@ -27,4 +26,16 @@ public class RecController {
} }
return recService.createRecDataToExcel(rec); return recService.createRecDataToExcel(rec);
} }
@RequestMapping("/test_add_xml")
@ResponseBody
public ReturnValue testAddXmlToWebService() {
return recService.testAddXmlToWebService();
}
@RequestMapping("/test_delete_xml")
@ResponseBody
public ReturnValue testDelXmlToWebService() {
return recService.testDelXmlToWebService();
}
} }
package com.reconciliation.recfj.entity; package com.reconciliation.recfj.entity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*; import lombok.*;
...@@ -102,4 +103,10 @@ public class RecExcel { ...@@ -102,4 +103,10 @@ public class RecExcel {
*/ */
@ExcelProperty("备注") @ExcelProperty("备注")
private String remark; private String remark;
/**
* 临时字段:业务更新年月/年月日
*/
@ExcelIgnore
private String tbrq;
} }
package com.reconciliation.recfj.entity;
import lombok.Data;
/**
* @author : Fengmao
* @date : 2024-11-14
**/
@Data
public class RecXml {
private String primaryKey;
private String idxNo;
private String idxType;
private String tableName;
private String dateTime;
private String dataArea;
private String otherDim;
private String dataCount;
private String execTime;
private String maxUpdateTime;
private String remark;
private String type;
}
package com.reconciliation.recfj.mapper; package com.reconciliation.recfj.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reconciliation.recfj.entity.RecExcel; import com.reconciliation.recfj.entity.RecExcel;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
...@@ -11,35 +12,27 @@ import java.util.List; ...@@ -11,35 +12,27 @@ import java.util.List;
* @author 史连宁 * @author 史连宁
*/ */
@Mapper @Mapper
public interface RecMapper { public interface RecMapper extends BaseMapper<RecExcel> {
@Select("<script>" + @Select("<script>" +
" select count(*) value, max(tbrq) latestUpdateDate from ${table} t " + " select count(*) value, max(tbrq) latestUpdateDate from ${table} t " +
"</script") "</script>")
List<RecExcel> getRecAll1(String table); List<RecExcel> getRecAll1(String table);
@Select("<script>" + @Select("<script>" +
" select count(*) value, max(t.tbrq) latestUpdateDate from ${table} t, ${table}_D d where t.recordid=d.recordid" + " select count(*) value, max(t.tbrq) latestUpdateDate from ${table} t, ${table}_D d where t.recordid=d.recordid" +
"</script") "</script>")
List<RecExcel> getRecAll2(String table); List<RecExcel> getRecAll2(String table);
@Select("<script>" + @Select("select count(*), tbrq from (select TO_CHAR(t.tbrq,'yyyy-MM') tbrq from ${table} t)a group by tbrq ")
" " +
"</script")
List<RecExcel> getRecMon1(String table); List<RecExcel> getRecMon1(String table);
@Select("<script>" + @Select("select count(*), tbrq from (select TO_CHAR(t.tbrq,'yyyy-MM') tbrq from ${table} t,${table}_D d where t.recordid=d.recordid)a group by tbrq")
" " +
"</script")
List<RecExcel> getRecMon2(String table); List<RecExcel> getRecMon2(String table);
@Select("<script>" + @Select("select count(*), tbrq from (select TO_CHAR(t.tbrq,'yyyy-MM-dd') tbrq from ${table} t where t.tbrq>= to_char(now() - interval '30' day,'yyyy-MM-dd') and t.tbrq < to_char(now(),'yyyy-MM-dd'))a group by tbrq")
" " +
"</script")
List<RecExcel> getRecDay1(String table); List<RecExcel> getRecDay1(String table);
@Select("<script>" + @Select("select count(*), tbrq from (select TO_CHAR(t.tbrq,'yyyy-MM-dd') tbrq from ${table} t,${table}_D d where t.recordid=d.recordid and t.tbrq>= to_char(now() - interval '30' day,'yyyy-MM-dd') and t.tbrq < to_char(now(),'yyyy-MM-dd'))a group by tbrq")
" " +
"</script")
List<RecExcel> getRecDay2(String table); List<RecExcel> getRecDay2(String table);
} }
package com.reconciliation.recfj.service; package com.reconciliation.recfj.service;
import com.reconciliation.recfj.entity.RecExcel; import com.reconciliation.recfj.entity.RecExcel;
import com.reconciliation.recfj.entity.RecXml;
import com.reconciliation.recfj.entity.ReturnValue; import com.reconciliation.recfj.entity.ReturnValue;
import com.reconciliation.recfj.enums.RecItem; import com.reconciliation.recfj.enums.RecItem;
import com.reconciliation.recfj.enums.RecTable; import com.reconciliation.recfj.enums.RecTable;
import com.reconciliation.recfj.mapper.RecMapper; import com.reconciliation.recfj.mapper.RecMapper;
import io.micrometer.common.util.StringUtils; import com.reconciliation.recfj.util.DateUtils;
import com.reconciliation.recfj.util.ExcelUtils;
import com.reconciliation.recfj.util.XmlBuilderUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.text.SimpleDateFormat; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/** /**
* 对账实现类 * 对账实现类
*
* @author 史连宁 * @author 史连宁
*/ */
@Service @Service
...@@ -26,32 +37,267 @@ public class RecService { ...@@ -26,32 +37,267 @@ public class RecService {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
@Resource @Resource
private RecMapper recMapper; private SqlSessionFactory sqlSessionFactory;
@Resource
private SendWebService sendWebService;
/*@Value("${excel.directory.path}")
private String excelPath;*/
public ReturnValue createRecDataToExcel(RecExcel rec) { public ReturnValue createRecDataToExcel(RecExcel rec) {
ReturnValue rtv = ReturnValue.fail(); ReturnValue rtv = ReturnValue.fail();
RecTable[] recTableList = RecTable.values();
RecItem[] recItemList = RecItem.values();
if (rec != null && !ObjectUtils.isEmpty(rec.getThirdTable())) {
recTableList = new RecTable[]{RecTable.parse(rec.getThirdTable())};
}
if (rec != null && !ObjectUtils.isEmpty(rec.getRecItem())) {
recItemList = new RecItem[]{RecItem.parse(rec.getRecItem())};
}
List<RecExcel> list = new ArrayList<>();
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
RecMapper recMapper = session.getMapper(RecMapper.class);
try { try {
RecTable[] recTableList = RecTable.values();
RecItem[] recItemList = RecItem.values();
if (rec != null && StringUtils.isNotBlank(rec.getThirdTable())) {
recTableList = new RecTable[]{RecTable.parse(rec.getThirdTable())};
}
if (rec != null && StringUtils.isNotBlank(rec.getRecItem())) {
recItemList = new RecItem[]{RecItem.parse(rec.getRecItem())};
}
List<RecExcel> list = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (RecTable recTable : recTableList) { for (RecTable recTable : recTableList) {
for (RecItem recItem : recItemList) { for (RecItem recItem : recItemList) {
Date now = new Date(); if (recItem.getFunc().equals("getRecAll")) {
//sql类型
if (recTable.getSqlType().equals("1")) {
List<RecExcel> recAll1 = recMapper.getRecAll1(recTable.getSysTable());
list.addAll(handleRecExcelData(recAll1, recItem, recTable));
}
if (recTable.getSqlType().equals("2")) {
List<RecExcel> recAll2 = recMapper.getRecAll2(recTable.getSysTable());
list.addAll(handleRecExcelData(recAll2, recItem, recTable));
}
} else if (recItem.getFunc().equals("getRecDay")) {
//sql类型
if (recTable.getSqlType().equals("1")) {
List<RecExcel> recDay1 = recMapper.getRecDay1(recTable.getSysTable());
list.addAll(handleRecExcelData(recDay1, recItem, recTable));
}
if (recTable.getSqlType().equals("2")) {
List<RecExcel> recDay2 = recMapper.getRecDay2(recTable.getSysTable());
list.addAll(handleRecExcelData(recDay2, recItem, recTable));
}
} else if (recItem.getFunc().equals("getRecMon")) {
//sql类型
if (recTable.getSqlType().equals("1")) {
List<RecExcel> recMon1 = recMapper.getRecMon1(recTable.getSysTable());
list.addAll(handleRecExcelData(recMon1, recItem, recTable));
}
if (recTable.getSqlType().equals("2")) {
List<RecExcel> recMon2 = recMapper.getRecMon2(recTable.getSysTable());
list.addAll(handleRecExcelData(recMon2, recItem, recTable));
}
}
} }
} }
rtv.setSuccess(true); session.commit();
} catch (Exception e) { } catch (Exception e) {
logger.error("汇聚平台对账数据生成失败", e); logger.error("汇聚平台对账数据生成失败", e);
rtv.setMsg(e.getMessage()); rtv.setMsg(e.getMessage());
session.rollback();
} finally {
if (session != null) {
session.close();
}
}
//将数据写到excel文件: RecData_20190701140506.xlsx
writeDataToExcel(list);
//将数据封装成XML格式
String type = "add";
List<RecXml> recXmlList = packRecXmlData(list, type);
String xmlStr = XmlBuilderUtils.buildXml(recXmlList);
// 发送到汇聚平台
boolean flag = sendWebService.pushXmlData(xmlStr);
if (flag) {
rtv.setSuccess(true);
rtv.setMsg("发送到汇聚平台成功!");
} else {
rtv.setSuccess(false);
rtv.setMsg("发送到汇聚平台失败!");
} }
return rtv; return rtv;
} }
/**
* 将数据封装成XML格式
*
* @param list
* @param type
* @return
*/
private List<RecXml> packRecXmlData(List<RecExcel> list, String type) {
List<RecXml> recXmlList = new ArrayList<>();
if (ObjectUtils.isEmpty(list)) {
return null;
}
list.forEach(recExcel -> {
RecXml recXml = new RecXml();
recXml.setType(type);
recXml.setPrimaryKey(recExcel.getId());
recXml.setIdxNo(recExcel.getRecId());
recXml.setIdxType(recExcel.getRecItem());
recXml.setTableName(recExcel.getThirdTable());
recXml.setDateTime(recExcel.getDataTimeStr());
recXml.setDataArea(recExcel.getDataArea());
recXml.setOtherDim(recExcel.getOtherDimension());
recXml.setDataCount(String.valueOf(recExcel.getValue()));
Date execDate = recExcel.getExecDate();
String execDateStr = DateUtils.format(execDate, DateUtils.P_YYYY_MM_DD_HH_MM_SS);
recXml.setExecTime(execDateStr);
recXml.setMaxUpdateTime(DateUtils.format(recExcel.getLatestUpdateDate(), DateUtils.P_YYYY_MM_DD_HH_MM_SS));
recXml.setRemark(recExcel.getRemark());
recXmlList.add(recXml);
});
return recXmlList;
}
/**
* 将数据写到excel 文件格式: RecData_20190701140506.xlsx
*
* @param list
*/
private void writeDataToExcel(List<RecExcel> list) {
//创建目录
String excelPath = System.getProperty("user.dir") + "/exportExcel";
File dir = new File(excelPath);
if (!dir.exists()) {
dir.mkdir();
}
String fileName = "RecData" + "_" + DateUtils.ConvertDateToYYYYMMddHHmmss(new Date());
//创建文件
File file = new File(dir, fileName + ".xlsx");
ExcelUtils.writeExcel(file, list);
}
/**
* 封装数据
*
* @param recData
* @param recItem
* @param recTable
*/
private List<RecExcel> handleRecExcelData(List<RecExcel> recData, RecItem recItem, RecTable recTable) {
Date now = new Date();
if (!CollectionUtils.isEmpty(recData)) {
recData = recData.stream().map(recAll -> {
RecExcel recExcel = new RecExcel();
recExcel.setId(createRandomId());
recExcel.setRecId(recItem.getId() + "_" + recTable.getHjTable());
recExcel.setRecItem(recItem.getName());
recExcel.setThirdTable(recTable.getHjTable());
//指标“IDX1数据总量”此列不用填
if (!recItem.getId().equals("IDX1")) {
recExcel.setDataTimeStr(createDataTimeStr(recAll.getTbrq()));
}
recExcel.setExecDate(now);
recExcel.setDataArea(recTable.getSysTable());
recExcel.setOtherDimension(recTable.getTableName());
return recExcel;
}).collect(Collectors.toList());
}
return recData;
}
/**
* 生成更新时间:格式为yyyyMM/yyyyMMdd
*
* @param tbrq
* @return
*/
private String createDataTimeStr(String tbrq) {
if (ObjectUtils.isEmpty(tbrq)) {
return "";
}
if (tbrq.length() == 7) {
return DateUtils.convertToYYYYMM(tbrq);
}
if (tbrq.length() == 10) {
return DateUtils.convertToYYYYMMdd(tbrq);
}
return null;
}
/**
* 生成随机ID
*
* @return
*/
private String createRandomId() {
// 获取当前的Instant时间
String time = DateUtils.ConvertDateToYYYYMMddHHmmss(new Date());
// 生成六位随机数
Random random = new Random();
int randomNumber = 100000 + random.nextInt(900000); // 生成范围在100000到999999之间的随机数
// 拼接结果
String randomId = time + String.format("%06d", randomNumber); // 确保格式为六位
return randomId;
}
public static void main(String[] args) {
String time = DateUtils.ConvertDateToYYYYMMddHHmmss(new Date());
System.out.printf(time);
}
public ReturnValue testAddXmlToWebService() {
ReturnValue rvt = new ReturnValue();
List<RecExcel> list = createTestRecExcelData();
String type = "add";
List<RecXml> recXmlList = packRecXmlData(list, type);
String xmlStr = XmlBuilderUtils.buildXml(recXmlList);
// 发送到汇聚平台
boolean flag = sendWebService.pushXmlData(xmlStr);
System.out.printf("xml文件是:" + xmlStr);
if (flag) {
rvt.setMsg("推送add测试数据成功");
rvt.setSuccess(true);
} else {
rvt.setMsg("推送add测试数据失败");
rvt.setSuccess(false);
}
return rvt;
}
/**
* 生成测试数据
*
* @return
*/
private List<RecExcel> createTestRecExcelData() {
List<RecExcel> list = new ArrayList<>();
RecExcel recExcel = new RecExcel();
recExcel.setId("20241115142624296948");
recExcel.setRecId("IDX2_table34904");
recExcel.setThirdTable("table34904");
recExcel.setExecDate(new Date());
recExcel.setRecItem("IDX2创建时间月统计值");
recExcel.setDataTimeStr("");
recExcel.setLatestUpdateDate(new Date());
list.add(recExcel);
return list;
}
public ReturnValue testDelXmlToWebService() {
ReturnValue rvt = new ReturnValue();
List<RecExcel> list = createTestRecExcelData();
String type = "delete";
List<RecXml> recXmlList = packRecXmlData(list, type);
String xmlStr = XmlBuilderUtils.buildXml(recXmlList);
// 发送到汇聚平台
boolean flag = sendWebService.pushXmlData(xmlStr);
System.out.printf("xml文件是:" + xmlStr);
if (flag) {
rvt.setMsg("推送delete测试数据成功!");
rvt.setSuccess(true);
} else {
rvt.setMsg("推送delete测试数据失败!");
rvt.setSuccess(false);
}
return rvt;
}
} }
package com.reconciliation.recfj.service;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author : Fengmao
* @date : 2024-11-14
**/
@Service
public class SendWebService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${api.wsdl}")
private String wsdl;
@Value("${api.userid}")
private String userid;
@Value("${api.password}")
private String password;
@Value("${api.catalogId}")
private String catalogId;
/**
* testwebservice接口测试方法
*
* @param userName 用户名
* @param passWord 密码
* @param catalogId 目录id
* @param xmlStr xml格式参数
* @param wsdl 接口wsdl
*/
public static void testWebService(String userName, String passWord, String catalogId, String xmlStr, String wsdl) {
try {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//wsdl 为实际接口地址
org.apache.cxf.endpoint.Client client = dcf.createClient(wsdl);
//LoginByAccount 为用户身份验证接口方法名称,userName为用户名,passWord为密码
Object[] guidObjects = client.invoke("LoginByAccount", userName, passWord);
System.out.println("获取guid:" + guidObjects[0].toString());
//pushXml 为政务数据推送接口方法名称,参数个数按照接口定义进行传参,返回一个Object数组
Object[] objects = client.invoke("pushXml", guidObjects[0].toString(), catalogId, xmlStr);
//输出调用结果,Object数组第一条数据为返回结果
System.out.println("调用结果:" + objects[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean pushXmlData(String xmlStr) {
this.logger.debug("推送的wsdl:" + wsdl);
boolean flag = false;
try {
this.logger.info("推送数据---开始");
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//wsdl 为实际接口地址
org.apache.cxf.endpoint.Client client = dcf.createClient(wsdl);
//LoginByAccount 为用户身份验证接口方法名称,userName为用户名,passWord为密码
Object[] guidObjects = client.invoke("LoginByAccount", userid, password);
logger.info("获取guid:" + guidObjects[0].toString());
//pushXml 为政务数据推送接口方法名称,参数个数按照接口定义进行传参,返回一个Object数组
Object[] objects = client.invoke("pushXml", guidObjects[0].toString(), catalogId, xmlStr);
//输出调用结果,Object数组第一条数据为返回结果
logger.info("调用结果:" + objects[0].toString());
String pushXmlResult = objects[0].toString();
this.logger.info("调用结果:" + pushXmlResult);
} catch (Exception e) {
this.logger.info("推送数据---出错", e);
return flag;
}
this.logger.info("推送数据---结束");
return true;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.reconciliation.recfj.mapper.RecMapper">
</mapper>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论