update:米东三方接口数据

This commit is contained in:
Yuan
2025-07-31 15:27:47 +08:00
parent 6b49fbfaca
commit d773108ac0
12 changed files with 1653 additions and 99 deletions

View File

@@ -44,6 +44,7 @@ public class ThirdBillLogController {
private ThirdFtpConfigService thirdFtpConfigService;
@Autowired
private ThirdSftpConfigService thirdSftpConfigService;
@@ -131,16 +132,25 @@ public class ThirdBillLogController {
String thirdConfigId = StringDUtil.changeNullToEmpty(bankbillGetinfo.get("THIRDCONFIGID"));
//查询配置
HashMap<Object, Object> searchThirdMap = new HashMap<>();
searchThirdMap.put("FUBS", "1");
HashMap<Object,Object> searchConfigMap=new HashMap<>();
searchConfigMap.put("thirdConfigId",thirdConfigId);
List<HashMap<Object, Object>> wlConfigList = thirdSftpConfigService.findWLIF(searchThirdMap);
HashMap<Object, Object> resultMap = new HashMap<>();
if (thirdConfigId.equals("999")){
//这里暂时将建行龙支付的情况写死在这里,后续优化
searchConfigMap.put("FUBS","1");
List<HashMap<Object, Object>> wlifList = thirdFtpConfigService.findWLIF(searchConfigMap);
resultMap = BankGetData.oneExecute(quartz_id, quartz_name, trade_date,wlifList.get(0));
}else {
List<HashMap<Object,Object>> thirdFtpConfigList =thirdFtpConfigService.findThirdFtpConfigList(searchConfigMap);
resultMap = BankGetData.oneExecute(quartz_id, quartz_name, trade_date,thirdFtpConfigList.get(0));
}
//
// HashMap<Object, Object> searchConfigMap = new HashMap<>();
// searchConfigMap.put("thirdConfigId", thirdConfigId);
// List<HashMap<Object, Object>> thirdFtpConfigList = thirdFtpConfigService.findThirdFtpConfigList(searchConfigMap);
HashMap<Object, Object> resultMap = BankGetData.oneExecute(quartz_id, quartz_name, trade_date, wlConfigList.get(0));
errCode = StringDUtil.changeNullToEmpty(resultMap.get("errCode"));
errMsg = StringDUtil.changeNullToEmpty(resultMap.get("errMsg"));

View File

@@ -1,7 +1,8 @@
package com.saye.hospitalgd.scheduler.job;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.scheduler.jobMethod.BankGetDataMethod;
import com.saye.hospitalgd.scheduler.jobMethod.*;
import com.saye.hospitalgd.service.ThirdFtpConfigService;
import com.saye.hospitalgd.service.ThirdSftpConfigService;
import org.quartz.Job;
import org.quartz.JobDataMap;
@@ -21,7 +22,7 @@ import java.util.List;
public class BankGetData implements Job {
@Autowired
private ThirdSftpConfigService thirdSftpConfigService;
private ThirdFtpConfigService thirdFtpConfigService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
@@ -34,41 +35,63 @@ public class BankGetData implements Job {
try {
HashMap<Object, Object> searchMap = new HashMap<>();
searchMap.put("FUBS", "1");
// List<HashMap<Object, Object>> thirdFtpConfigList = thirdSftpConfigService.findThirdSftpConfigList(searchMap);
List<HashMap<Object, Object>> wlConfigList = thirdSftpConfigService.findWLIF(searchMap);
searchMap.put("is_active", "1");
HashMap<Object, Object> wlifSearchMap = new HashMap<>();
wlifSearchMap.put("FUBS", "1");
List<HashMap<Object, Object>> thirdFtpConfigList = thirdFtpConfigService.findThirdFtpConfigList(searchMap);
List<HashMap<Object, Object>> wlConfigList = thirdFtpConfigService.findWLIF(searchMap);
for (int i = 0; i < thirdFtpConfigList.size(); i++) {
HashMap<Object, Object> hashMap = thirdFtpConfigList.get(i);
String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));
//商户对账单
if ("BankGetDataBySH".equals(execute_class)){
Thread thread = new Thread(() -> new BankGetDataBySH().getDate(id,name,trade_date,hashMap));
thread.start();
}
//商户POS对账单
if ("BankGetDataBySHPOS".equals(execute_class)){
Thread thread = new Thread(() -> new BankGetDataBySHPOS().getDate(id,name,trade_date,hashMap));
thread.start();
}
// 银行商户银行卡对账单
if ("BankGetDataBySHYLK".equals(execute_class)) {
Thread thread = new Thread(() -> new BankGetDataBySHYLK().getDate(id, name, trade_date, hashMap));
thread.start();
}
// 商户银联外卡对账单
if ("BankGetDataBySHYLWK".equals(execute_class)) {
Thread thread = new Thread(() -> new BankGetDataBySHYLWK().getDate(id, name, trade_date, hashMap));
thread.start();
}
// 微信账单
if ("BankGetDataByWXAPI".equals(execute_class)) {
Thread thread = new Thread(() -> new BankGetDataByWXAPI().getDate(id, name, trade_date, hashMap));
thread.start();
}
}
for (int i = 0; i < wlConfigList.size(); i++) {
HashMap<Object, Object> hashMap = wlConfigList.get(i);
String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));
//商户pos通过对账
if ("BankGetDataMethod".equals(execute_class)) {
Thread thread = new Thread(() -> new BankGetDataMethod().getDate(id, name, trade_date, hashMap));
//建行龙支付
if ("BankGetDataMethodByJHLZF".equals(execute_class)) {
Thread thread = new Thread(() -> new BankGetDataMethodByJHLZF().getDate(id, name, trade_date, hashMap));
thread.start();
}
// //商户对账单
// if ("BankGetDataBySH".equals(execute_class)){
// Thread thread = new Thread(() -> new BankGetDataBySH().getDate(id,name,trade_date,hashMap));
// thread.start();
// }
//
// //商户银行卡对账单
// if ("BankGetDataBySHYLK".equals(execute_class)){
// Thread thread = new Thread(() -> new BankGetDataBySHYLK().getDate(id,name,trade_date,hashMap));
// thread.start();
// }
//
// //商户营销联盟对账单
// if ("BankGetDataBySHYXLM".equals(execute_class)){
// Thread thread = new Thread(() -> new BankGetDataBySHYXLM().getDate(id,name,trade_date,hashMap));
// thread.start();
// }
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -84,20 +107,35 @@ public class BankGetData implements Job {
String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));
//商户pos通过对账
if ("BankGetDataMethod".equals(execute_class)) {
return new BankGetDataMethod().getDate(id, name, trade_date, hashMap);
//建行龙支付
if ("BankGetDataMethodByJHLZF".equals(execute_class)) {
return new BankGetDataMethodByJHLZF().getDate(id, name, trade_date, hashMap);
}
// //商户对账单
// if ("BankGetDataBySH".equals(execute_class)){
// return new BankGetDataBySH().getDate(id,name,trade_date,hashMap);
// }
//
// //商户银联卡对账单
// if ("BankGetDataBySHYLK".equals(execute_class)){
// return new BankGetDataBySHYLK().getDate(id,name,trade_date,hashMap);
// }
//商户对账单
if ("BankGetDataBySH".equals(execute_class)){
return new BankGetDataBySH().getDate(id,name,trade_date,hashMap);
}
//商户POS对账单
if ("BankGetDataBySHPOS".equals(execute_class)){
return new BankGetDataBySHPOS().getDate(id,name,trade_date,hashMap);
}
// 银行商户银联卡对账单
if ("BankGetDataBySHYLK".equals(execute_class)) {
return new BankGetDataBySHYLK().getDate(id, name, trade_date, hashMap);
}
// 商户银联外卡对账单
if ("BankGetDataBySHYLWK".equals(execute_class)) {
return new BankGetDataBySHYLWK().getDate(id, name, trade_date, hashMap);
}
// 微信账单
if ("BankGetDataByWXAPI".equals(execute_class)) {
return new BankGetDataByWXAPI().getDate(id, name, trade_date, hashMap);
}
//
// //商户营销联盟对账单
// if ("BankGetDataBySHYXLM".equals(execute_class)){

View File

@@ -0,0 +1,181 @@
package com.saye.hospitalgd.scheduler.jobMethod;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.service.BankbillGetinfoService;
import com.saye.hospitalgd.service.BankbillHistoryService;
import com.saye.hospitalgd.service.datamanager.DataManagerService;
import com.saye.hospitalgd.service.impl.BankbillGetinfoServiceImpl;
import com.saye.hospitalgd.service.impl.BankbillHistoryServiceImpl;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.service.system.impl.ServiceParamsServiceImpl;
import com.saye.hospitalgd.util.HttpDutil;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author thuang
* @version 1.0
* @description: 获取商户POS通对账单 调用dmz服务器部署的接口
* @date 2021/9/13 14:46
*/
public class BankGetDataBySHPOS {
public HashMap<Object, Object> getDate(String id, String name, String trade_date, HashMap<Object, Object> map) {
HashMap<Object, Object> resultMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
BankbillHistoryService bankbillHistoryService = GetBeanUtil.getBean(BankbillHistoryServiceImpl.class);
BankbillGetinfoService bankbillGetinfoService = GetBeanUtil.getBean(BankbillGetinfoServiceImpl.class);
ServiceParamsService serviceParamsService = GetBeanUtil.getBean(ServiceParamsServiceImpl.class);
DataManagerService dataManagerService = GetBeanUtil.getBean(DataManagerService.class);
boolean isOk = false;
// 重新执行最多10次 还没获取到肯定有问题 不执行了
// 如果有传入时间 那就只执行一次 获取不到就还是获取不到
int num = "".equals(trade_date) ? 0 : 9;
while (!isOk && num < 10) {
num++;
String bill_table_name = StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId = StringDUtil.changeNullToEmpty(map.get("ID"));
String ftp_file_name = StringDUtil.changeNullToEmpty(map.get("FTP_FILE_NAME"));
String mch_id = StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
// 替换其中的标记
Matcher mat = Pattern.compile("\\{([^}]*)\\}").matcher(ftp_file_name);
while (mat.find()) {
String group = mat.group();
group = group.substring(1, group.length() - 1);
// 替换商户id
if ("MCH_ID".equals(group)) {
ftp_file_name = ftp_file_name.replaceFirst("\\{([^}]*)\\}", mch_id);
}
// 替换日期 主要试t+1 还是t+2这种
if (group.contains("DATE")) {
String dateStr = "";
// 如果有传入时间 那就使用传入时间
if ("".equals(trade_date)) {
group = group.replaceFirst("DATE", "");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
// 执行修改日期
if (!"".equals(group)) {
int t = Integer.parseInt(group);
calendar.add(Calendar.DATE, t);
}
Date searchDate = calendar.getTime();
trade_date = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, searchDate);
dateStr = DateDUtil.DateToStr(DateDUtil.yyyyMMdd, searchDate);
} else {
Date date = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, trade_date);
dateStr = DateDUtil.DateToStr(DateDUtil.yyyyMMdd, date);
}
ftp_file_name = ftp_file_name.replaceFirst("\\{([^}]*)\\}", dateStr);
}
}
map.put("FTP_FILE_NAME", ftp_file_name);
map.put("trade_date", trade_date);
try {
List<HashMap<Object, Object>> serviceParams = serviceParamsService.findParamValByParamCode("hgd_dmz");
String dmz_url = StringDUtil.changeNullToEmpty(serviceParams.get(0).get("PARAM_VAL"));
// HashMap<Object, Object> bankDataBySHPOS = dataManagerService.getBankDataBySHPOS(map);
String result = HttpDutil.post(dmz_url + "/getBankDataBySHPOS", map);
// 解析返回的json字符串
JSONObject jsonObject = JSONObject.parseObject(result);
errCode = StringDUtil.changeNullToEmpty(jsonObject.get("errCode"));
errMsg = StringDUtil.changeNullToEmpty(jsonObject.get("errMsg"));
// 如果没问题 解析返回的集合
if ("0".equals(errCode)) {
String list = StringDUtil.changeNullToEmpty(jsonObject.get("list"));
List<BankbillHistory> bankbillHistories = JSONArray.parseArray(list, BankbillHistory.class);
// 如果有数据
if (bankbillHistories != null && bankbillHistories.size() > 0) {
// 先存一份原始的 这份只是留底查询
bankbillHistoryService.insertBankBillOriginal(bankbillHistories, trade_date, bill_table_name);
// 再存一份修改其中某些字段的
bankbillHistoryService.insertAllBankHistory(bankbillHistories, trade_date, bill_table_name);
}
isOk = true;
} else {
isOk = false;
}
} catch (Exception e) {
e.printStackTrace();
errCode = "999";
errMsg = "处理数据错误,原因:" + e.getMessage();
isOk = false;
}
// 记录数据获取记录
HashMap<Object, Object> addMap = new HashMap<>();
addMap.put("trade_date", trade_date);
addMap.put("quartz_id", id);
addMap.put("quartz_name", name);
addMap.put("bill_table_name", bill_table_name);
addMap.put("thirdConfigId", thirdConfigId);
if (isOk) {
addMap.put("is_ok", "1");
} else {
addMap.put("is_ok", "0");
}
addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
bankbillGetinfoService.insertBankbillGetinfo(addMap);
// 判断是否成功 如果失败 执行休眠一小时
if (!isOk && num < 10) {
try {
// 记录日志
LogUtil.error(this.getClass(), errMsg);
System.out.println("获取对账记录没有成功1小时后重新执行");
Thread.sleep(1000 * 60 * 60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
resultMap.put("errCode", errCode);
resultMap.put("errMsg", errMsg);
return resultMap;
}
}

View File

@@ -0,0 +1,174 @@
package com.saye.hospitalgd.scheduler.jobMethod;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.service.BankbillGetinfoService;
import com.saye.hospitalgd.service.BankbillHistoryService;
import com.saye.hospitalgd.service.impl.BankbillGetinfoServiceImpl;
import com.saye.hospitalgd.service.impl.BankbillHistoryServiceImpl;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.service.system.impl.ServiceParamsServiceImpl;
import com.saye.hospitalgd.util.HttpDutil;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author yuan
* @version 1.0
* @description: 获取商户银联外卡对账单 调用dmz服务器部署的接口
* @date 2025/7/18 14:46
*/
public class BankGetDataBySHYLWK {
public HashMap<Object,Object> getDate(String id,String name,String trade_date,HashMap<Object,Object> map){
HashMap<Object,Object> resultMap=new HashMap<>();
String errCode="0";
String errMsg="";
BankbillHistoryService bankbillHistoryService = GetBeanUtil.getBean(BankbillHistoryServiceImpl.class);
BankbillGetinfoService bankbillGetinfoService = GetBeanUtil.getBean(BankbillGetinfoServiceImpl.class);
ServiceParamsService serviceParamsService= GetBeanUtil.getBean(ServiceParamsServiceImpl.class);
boolean isOk=false;
//重新执行最多10次 还没获取到肯定有问题 不执行了
//如果有传入时间 那就只执行一次 获取不到就还是获取不到
int num ="".equals(trade_date)?0:9;
while (!isOk && num<10){
num++;
String bill_table_name=StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId=StringDUtil.changeNullToEmpty(map.get("ID"));
String ftp_file_name=StringDUtil.changeNullToEmpty(map.get("FTP_FILE_NAME"));
String mch_id=StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
//替换其中的标记
Matcher mat = Pattern.compile("\\{([^}]*)\\}").matcher(ftp_file_name);
while(mat.find()){
String group = mat.group();
group=group.substring(1,group.length()-1);
//替换商户id
if ("MCH_ID".equals(group)){
ftp_file_name = ftp_file_name.replaceFirst("\\{([^}]*)\\}", mch_id);
}
//替换日期 主要试t+1 还是t+2这种
if (group.contains("DATE")){
String dateStr="";
//如果有传入时间 那就使用传入时间
if ("".equals(trade_date)){
group=group.replaceFirst("DATE","");
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
//执行修改日期
if (!"".equals(group)){
int t = Integer.parseInt(group);
calendar.add(Calendar.DATE,t);
}
Date searchDate = calendar.getTime();
trade_date= DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd,searchDate);
dateStr=DateDUtil.DateToStr(DateDUtil.yyyyMMdd,searchDate);
}else {
Date date = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, trade_date);
dateStr=DateDUtil.DateToStr(DateDUtil.yyyyMMdd,date);
}
ftp_file_name = ftp_file_name.replaceFirst("\\{([^}]*)\\}", dateStr);
}
}
map.put("FTP_FILE_NAME",ftp_file_name);
map.put("trade_date",trade_date);
try {
List<HashMap<Object, Object>> serviceParams = serviceParamsService.findParamValByParamCode("hgd_dmz");
String dmz_url = StringDUtil.changeNullToEmpty(serviceParams.get(0).get("PARAM_VAL"));
String result = HttpDutil.post(dmz_url+"/getBankDataBySHYLWK", map);
//解析返回的json字符串
JSONObject jsonObject = JSONObject.parseObject(result);
errCode = StringDUtil.changeNullToEmpty(jsonObject.get("errCode"));
errMsg = StringDUtil.changeNullToEmpty(jsonObject.get("errMsg"));
//如果没问题 解析返回的集合
if ("0".equals(errCode)){
String list = StringDUtil.changeNullToEmpty(jsonObject.get("list"));
List<BankbillHistory> bankbillHistories = JSONArray.parseArray(list, BankbillHistory.class);
//如果有数据
if (bankbillHistories!=null && bankbillHistories.size()>0){
//先存一份原始的 这份只是留底查询
bankbillHistoryService.insertBankBillOriginal(bankbillHistories,trade_date,bill_table_name);
//再存一份修改替换其中一些字段的
bankbillHistoryService.insertAllBankHistory(bankbillHistories,trade_date,bill_table_name);
}
isOk=true;
}else {
isOk=false;
}
}catch (Exception e){
e.printStackTrace();
errCode="999";
errMsg="处理数据错误,原因:"+e.getMessage();
isOk=false;
}
//记录数据获取记录
HashMap<Object,Object> addMap=new HashMap<>();
addMap.put("trade_date",trade_date);
addMap.put("quartz_id",id);
addMap.put("quartz_name",name);
addMap.put("bill_table_name",bill_table_name);
addMap.put("thirdConfigId",thirdConfigId);
if (isOk){
addMap.put("is_ok","1");
}else {
addMap.put("is_ok","0");
}
addMap.put("modify_time",DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
bankbillGetinfoService.insertBankbillGetinfo(addMap);
//判断是否成功 如果失败 执行休眠一小时
if (!isOk && num<10) {
try {
//记录日志
LogUtil.error(this.getClass(), errMsg);
System.out.println("获取对账记录没有成功1小时后重新执行");
Thread.sleep(1000 * 60 * 60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
resultMap.put("errCode",errCode);
resultMap.put("errMsg",errMsg);
return resultMap;
}
}

View File

@@ -0,0 +1,137 @@
package com.saye.hospitalgd.scheduler.jobMethod;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.service.BankbillGetinfoService;
import com.saye.hospitalgd.service.BankbillHistoryService;
import com.saye.hospitalgd.service.impl.BankbillGetinfoServiceImpl;
import com.saye.hospitalgd.service.impl.BankbillHistoryServiceImpl;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.service.system.impl.ServiceParamsServiceImpl;
import com.saye.hospitalgd.util.HttpDutil;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
/**
* @author Mr.zs
* @date 2024/6/25
*/
@Slf4j
public class BankGetDataByWXAPI {
public HashMap<Object, Object> getDate(String id, String name, String trade_date, HashMap<Object, Object> map) {
HashMap<Object, Object> resultMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
BankbillHistoryService bankbillHistoryService = GetBeanUtil.getBean(BankbillHistoryServiceImpl.class);
BankbillGetinfoService bankbillGetinfoService = GetBeanUtil.getBean(BankbillGetinfoServiceImpl.class);
ServiceParamsService serviceParamsService = GetBeanUtil.getBean(ServiceParamsServiceImpl.class);
boolean isOk = false;
// 重新执行最多10次 还没获取到肯定有问题 不执行了
// 如果有传入时间 那就只执行一次 获取不到就还是获取不到
int num = "".equals(trade_date) ? 0 : 9;
while (!isOk && num < 10) {
num++;
String bill_table_name = StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId = StringDUtil.changeNullToEmpty(map.get("ID"));
String dateStr = "";
// 如果有传入时间 那就使用传入时间
if (StrUtil.isNotBlank(trade_date)) {
DateTime parse = DateUtil.parse(trade_date);
dateStr = DateUtil.format(parse, "yyyyMMdd");
} else {
DateTime yesterday = DateUtil.yesterday();
dateStr = DateUtil.format(yesterday, "yyyyMMdd");
trade_date = DateUtil.format(yesterday, "yyyy-MM-dd");
}
map.put("trade_date", dateStr);
try {
List<HashMap<Object, Object>> serviceParams = serviceParamsService.findParamValByParamCode("hgd_dmz");
String dmz_url = StringDUtil.changeNullToEmpty(serviceParams.get(0).get("PARAM_VAL"));
String result = HttpDutil.post(dmz_url + "/getBankDataByWXAPI", map);
// 解析返回的json字符串
JSONObject jsonObject = JSONObject.parseObject(result);
errCode = StringDUtil.changeNullToEmpty(jsonObject.get("errCode"));
errMsg = StringDUtil.changeNullToEmpty(jsonObject.get("errMsg"));
// 如果没问题 解析返回的集合
if ("0".equals(errCode)) {
String list = StringDUtil.changeNullToEmpty(jsonObject.get("list"));
List<BankbillHistory> bankbillHistories = JSONArray.parseArray(list, BankbillHistory.class);
// 如果有数据
if (bankbillHistories != null && bankbillHistories.size() > 0) {
// 先存一份原始的 这份只是留底查询
bankbillHistoryService.insertBankBillOriginal(bankbillHistories, trade_date, bill_table_name);
// 再存一份修改替换其中一些字段的
bankbillHistoryService.insertAllBankHistory(bankbillHistories, trade_date, bill_table_name);
}
isOk = true;
} else {
isOk = false;
}
} catch (Exception e) {
e.printStackTrace();
errCode = "999";
errMsg = "处理数据错误,原因:" + e.getMessage();
isOk = false;
}
// 记录数据获取记录
HashMap<Object, Object> addMap = new HashMap<>();
addMap.put("trade_date", trade_date);
addMap.put("quartz_id", id);
addMap.put("quartz_name", name);
addMap.put("bill_table_name", bill_table_name);
addMap.put("thirdConfigId", thirdConfigId);
if (isOk) {
addMap.put("is_ok", "1");
} else {
addMap.put("is_ok", "0");
}
addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
bankbillGetinfoService.insertBankbillGetinfo(addMap);
// 判断是否成功 如果失败 执行休眠一小时
if (!isOk && num < 10) {
try {
// 记录日志
LogUtil.error(this.getClass(), errMsg);
System.out.println("获取对账记录没有成功1小时后重新执行");
Thread.sleep(1000 * 60 * 60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
resultMap.put("errCode", errCode);
resultMap.put("errMsg", errMsg);
return resultMap;
}
}

View File

@@ -6,11 +6,12 @@ import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.service.BankbillGetinfoService;
import com.saye.hospitalgd.service.BankbillsHistoryService;
import com.saye.hospitalgd.service.BankbillHistoryService;
import com.saye.hospitalgd.service.ThirdSftpConfigService;
import com.saye.hospitalgd.service.impl.BankbillGetinfoServiceImpl;
import com.saye.hospitalgd.service.impl.BankbillsHistoryServiceImpl;
import com.saye.hospitalgd.service.impl.BankbillHistoryServiceImpl;
import com.saye.hospitalgd.service.impl.ThirdSftpConfigServiceImpl;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.service.system.impl.ServiceParamsServiceImpl;
@@ -42,16 +43,15 @@ import java.util.zip.ZipInputStream;
* @date 2021/9/13 14:46
*/
@Slf4j
public class BankGetDataMethod {
public class BankGetDataMethodByJHLZF {
public HashMap<Object, Object> getDate(String id, String name, String trade_date, HashMap<Object, Object> map) {
HashMap<Object, Object> resultMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
BankbillsHistoryService bankbillsHistoryService = GetBeanUtil.getBean(BankbillsHistoryServiceImpl.class);
BankbillHistoryService bankbillHistoryService = GetBeanUtil.getBean(BankbillHistoryServiceImpl.class);
// BankbillGetinfoService bankbillGetinfoService = GetBeanUtil.getBean(BankbillGetinfoServiceImpl.class);
BankbillGetinfoService bankbillGetinfoService = GetBeanUtil.getBean(BankbillGetinfoServiceImpl.class);
ServiceParamsService serviceParamsService = GetBeanUtil.getBean(ServiceParamsServiceImpl.class);
@@ -233,7 +233,7 @@ public class BankGetDataMethod {
txtList.add(line);
}
log.info("继续执行程序3");
List<HashMap<Object, Object>> dataList = new ArrayList<>();
List<BankbillHistory> bankbillHistoryList = new ArrayList<>();
for (int i = 2; i < txtList.size(); i++) {
String d = txtList.get(i);
@@ -243,34 +243,51 @@ public class BankGetDataMethod {
continue;
}
HashMap<Object, Object> temMap = new HashMap<>();
BankbillHistory bankbillHistory = new BankbillHistory();
String[] split = s1[0].split(" ");
temMap.put("i_jyrq", split[0]);
temMap.put("i_jysj", split[1]);
temMap.put("i_jzrq", s1[1]);
temMap.put("i_yhlsh", s1[2]);
temMap.put("i_shlsh", s1[3]);
temMap.put("i_ddh", s1[4]);
temMap.put("i_ddzt", s1[5]);
temMap.put("i_fkfzh", s1[6]);
temMap.put("i_fkfhm", s1[7]);
temMap.put("i_ddje", s1[8]);
temMap.put("i_jyje", s1[9]);
temMap.put("i_sxf", s1[10]);
temMap.put("i_jsje", s1[11]);
temMap.put("i_gtdm", s1[12]);
temMap.put("i_fxk", s1[13]);
temMap.put("i_zfkz", s1[14]);
temMap.put("i_jylx", s1[15]);
temMap.put("i_qs", s1[16]);
// 设置BankbillHistory的字段
bankbillHistory.setCJyrq(split[0]); // 交易日期
bankbillHistory.setCJysj(split[1]); // 交易时间
bankbillHistory.setCQsrq(s1[1]); // 清算日期
bankbillHistory.setCLsh(s1[2]); // 流水号
bankbillHistory.setCShddh(s1[3]); // 商户订单号
bankbillHistory.setCYsddh(s1[4]); // 银商订单号
bankbillHistory.setCJyje(s1[9]); // 交易金额
bankbillHistory.setCSxf(s1[10]); // 手续费
bankbillHistory.setCJylx(s1[15]); // 交易类型
// 设置默认值
bankbillHistory.setCQsje(s1[9]); // 清算金额 = 交易金额
bankbillHistory.setCSjzfje(s1[9]); // 实际支付金额 = 交易金额
bankbillHistory.setCCkh(s1[2]); // 参考号 = 流水号
bankbillHistory.setCCard(""); // 卡号
bankbillHistory.setCKlx(""); // 卡类型
bankbillHistory.setCFkh(s1[13]); // 发卡行 = 付款行
bankbillHistory.setCZffs("建行龙支付"); // 支付方式
bankbillHistory.setCBzzd(""); // 备注字段
bankbillHistory.setCQbyhje("0"); // 钱包优惠金额
bankbillHistory.setCShyhje("0"); // 商户优惠金额
bankbillHistory.setCYjylsh(""); // 原交易流水号
bankbillHistory.setCFqqs(""); // 分期期数
bankbillHistory.setCFqsxf("0"); // 分期手续费
bankbillHistory.setCFqfwf(""); // 分期服务方
bankbillHistory.setCFqfxf(""); // 分期付息方
bankbillHistory.setCQtyhje("0"); // 其他优惠金额
bankbillHistory.setCThddh(""); // 退货订单号
bankbillHistory.setCFkfy(""); // 付款附言
bankbillHistory.setCFdjc(""); // 分店简称
bankbillHistory.setCZddh(""); // 子订单号
bankbillHistory.setBillTableName("建行龙支付对账单"); // 对账表名
dataList.add(temMap);
bankbillHistoryList.add(bankbillHistory);
}
if (!CollectionUtils.isEmpty(dataList)) {
bankbillsHistoryService.saveOriginalData(dataList, trade_date);//存一份原始数据
bankbillsHistoryService.saveHistoryData(dataList, trade_date);//存一份修改的用于对账
if (!CollectionUtils.isEmpty(bankbillHistoryList)) {
// 先存一份原始的 这份只是留底查询
bankbillHistoryService.insertBankBillOriginal(bankbillHistoryList, trade_date, "建行龙支付对账单");
// 再存一份修改的用于对账
bankbillHistoryService.insertAllBankHistory(bankbillHistoryList, trade_date, "建行龙支付对账单");
isOk = true;
} else {
errCode = "999";
@@ -295,7 +312,7 @@ public class BankGetDataMethod {
addMap.put("quartz_id", id);
addMap.put("quartz_name", name);
addMap.put("bill_table_name", fileName);
addMap.put("thirdConfigId", "3");
addMap.put("thirdConfigId", "999");
if (isOk) {
addMap.put("is_ok", "1");

View File

@@ -0,0 +1,17 @@
package com.saye.hospitalgd.service.datamanager;
import java.util.HashMap;
/**
* @author Mr.zs
* @date 2024/12/26
*/
public interface DataManagerService {
HashMap<Object, Object> getBankDataBySH(HashMap<Object, Object> map);
HashMap<Object, Object> getBankDataBySHPOS(HashMap<Object, Object> map);
HashMap<Object, Object> getBankDataBySHYLK(HashMap<Object, Object> map);
HashMap<Object, Object> getBankDataByWXAPI(HashMap<Object, Object> map);
}

View File

@@ -0,0 +1,896 @@
package com.saye.hospitalgd.service.datamanager.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.service.datamanager.DataManagerService;
import com.saye.hospitalgd.util.DownloadFtpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* @author Mr.zs
* @date 2024/12/26
*/
@Service
@Slf4j
public class DataManagerServiceImpl implements DataManagerService {
private static String grant_type = "client_credential";
private static String appid = "wx83bc9715be856b14";
private static String secret = "8b2d3e8cb0e590c9884d2c278519a200";
/**
* 获取银行端商户对账数据
*
* @param map
* @return
*/
public HashMap<Object, Object> getBankDataBySH(@RequestBody HashMap<Object, Object> map) {
HashMap<Object, Object> responseMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
// 先下载文件
String host = StringDUtil.changeNullToEmpty(map.get("FTP_HOST"));
String portStr = StringDUtil.changeNullToEmpty(map.get("FTP_PORT"));
int port = Integer.parseInt("".equals(portStr) ? "21" : portStr);
String username = StringDUtil.changeNullToEmpty(map.get("FTP_USER"));
String password = StringDUtil.changeNullToEmpty(map.get("FTP_PASSWORD"));
String localPath = StringDUtil.changeNullToEmpty(map.get("LOCAL_PATH"));
String mch_id = StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
String ftp_path = StringDUtil.changeNullToEmpty(map.get("FTP_PATH"));
String ftp_file_name = StringDUtil.changeNullToEmpty(map.get("FTP_FILE_NAME"));
String bill_table_name = StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId = StringDUtil.changeNullToEmpty(map.get("ID"));
String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));
HSSFWorkbook sheets = null;
try {
// 下载文件
boolean b = DownloadFtpUtil.downloadFtpFile(host, username, password, port, ftp_path, localPath, ftp_file_name);
// 判断是否下载到文件
if (!b) {
throw new RuntimeException("没有下载到文件" + ftp_file_name);
}
// 判断本地是否有文件
File file = new File(localPath + "/" + ftp_file_name);
if (file.exists()) {
// 存在 开始解析 存入数据库
FileInputStream fileInputStream = new FileInputStream(localPath + "/" + ftp_file_name);
sheets = new HSSFWorkbook(fileInputStream);
HSSFSheet sheet = sheets.getSheetAt(0);
// 获取sheet中第一行行号
int firstRowNum = sheet.getFirstRowNum();
// 获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
List<BankbillHistory> list = new ArrayList<>();
for (int i = firstRowNum + 3; i <= lastRowNum - 2; i++) {// 因为表格中第一行为说明,第二行为列标题
HSSFRow row = sheet.getRow(i);
BankbillHistory bankbillHistory = new BankbillHistory();
// 清算日期
HSSFCell qsrq = row.getCell(0);
qsrq.setCellType(Cell.CELL_TYPE_STRING);
String qsrqStr = qsrq.getStringCellValue().trim();
// 拿第一个字段判断这条记录是否为空 如果为空直接跳出 一般这字段为空就是数据结束了或根本没有
// 为合计也跳出
if ("".equals(qsrqStr) || "合计".equals(qsrqStr)) {
break;
}
bankbillHistory.setCQsrq(qsrqStr);
// 交易日期
HSSFCell jyrq = row.getCell(1);
String jyrqStr = "";
jyrq.setCellType(Cell.CELL_TYPE_NUMERIC);
if (HSSFDateUtil.isCellDateFormatted(jyrq)) {
Date dateCellValue = jyrq.getDateCellValue();
jyrqStr = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, dateCellValue);
}
bankbillHistory.setCJyrq(jyrqStr);
if (i == 0) {
trade_date = jyrqStr;
}
// 交易时间
HSSFCell jysj = row.getCell(2);
jysj.setCellType(Cell.CELL_TYPE_STRING);
String jysjStr = jysj.getStringCellValue().trim();
bankbillHistory.setCJysj(jysjStr);
// 卡号
HSSFCell card = row.getCell(3);
card.setCellType(Cell.CELL_TYPE_STRING);
String khStr = card.getStringCellValue().trim();
bankbillHistory.setCCard(khStr);
// 交易类型
HSSFCell jylx = row.getCell(4);
jylx.setCellType(Cell.CELL_TYPE_STRING);
String jylxStr = jylx.getStringCellValue().trim();
bankbillHistory.setCJylx(jylxStr);
// 交易金额
HSSFCell jyje = row.getCell(5);
jyje.setCellType(Cell.CELL_TYPE_STRING);
String jyjeStr = jyje.getStringCellValue().trim();
bankbillHistory.setCJyje(jyjeStr);
// 终端号
HSSFCell zdh = row.getCell(6);
zdh.setCellType(Cell.CELL_TYPE_STRING);
String zdhStr = zdh.getStringCellValue().trim();
bankbillHistory.setCZdh(zdhStr);
// 清算金额
HSSFCell qsje = row.getCell(7);
qsje.setCellType(Cell.CELL_TYPE_STRING);
String qsjeStr = qsje.getStringCellValue().trim();
bankbillHistory.setCQsje(qsjeStr);
// 手续费
HSSFCell sxf = row.getCell(8);
sxf.setCellType(Cell.CELL_TYPE_STRING);
String sxfStr = sxf.getStringCellValue().trim();
bankbillHistory.setCSxf(sxfStr);
// 参考号
HSSFCell ckh = row.getCell(9);
ckh.setCellType(Cell.CELL_TYPE_STRING);
String ckhStr = ckh.getStringCellValue().trim();
bankbillHistory.setCCkh(ckhStr);
// 流水号
HSSFCell lsh = row.getCell(10);
lsh.setCellType(Cell.CELL_TYPE_STRING);
String lshStr = lsh.getStringCellValue().trim();
bankbillHistory.setCLsh(lshStr);
// 卡类型
HSSFCell klx = row.getCell(11);
klx.setCellType(Cell.CELL_TYPE_STRING);
String klxStr = klx.getStringCellValue().trim();
bankbillHistory.setCKlx(klxStr);
// 商户订单号
HSSFCell shddh = row.getCell(12);
shddh.setCellType(Cell.CELL_TYPE_STRING);
String shddhStr = shddh.getStringCellValue().trim();
bankbillHistory.setCShddh(shddhStr);
// 支付方式
HSSFCell zffs = row.getCell(13);
zffs.setCellType(Cell.CELL_TYPE_STRING);
String zffsStr = zffs.getStringCellValue().trim();
bankbillHistory.setCZffs(zffsStr);
// 银商订单号
HSSFCell ysddh = row.getCell(14);
ysddh.setCellType(Cell.CELL_TYPE_STRING);
String ysddhStr = ysddh.getStringCellValue().trim();
bankbillHistory.setCYsddh(ysddhStr);
// 退货订单号
HSSFCell thddh = row.getCell(15);
thddh.setCellType(Cell.CELL_TYPE_STRING);
String thddhStr = thddh.getStringCellValue().trim();
bankbillHistory.setCThddh(thddhStr);
// 实际支付金额
HSSFCell sjzfje = row.getCell(16);
sjzfje.setCellType(Cell.CELL_TYPE_STRING);
String sjzfjeStr = sjzfje.getStringCellValue().trim();
bankbillHistory.setCSjzfje(sjzfjeStr);
// 备注字段
HSSFCell bzzd = row.getCell(17);
bzzd.setCellType(Cell.CELL_TYPE_STRING);
String bzzdStr = bzzd.getStringCellValue().trim();
bankbillHistory.setCBzzd(bzzdStr);
// 付款附言
HSSFCell fkfy = row.getCell(18);
fkfy.setCellType(Cell.CELL_TYPE_STRING);
String fkfyStr = fkfy.getStringCellValue().trim();
bankbillHistory.setCFkfy(fkfyStr);
// 钱包优惠金额
HSSFCell qbyhje = row.getCell(19);
qbyhje.setCellType(Cell.CELL_TYPE_STRING);
String qbyhjeStr = qbyhje.getStringCellValue().trim();
bankbillHistory.setCQbyhje(qbyhjeStr);
// 商户优惠金额
HSSFCell shyhje = row.getCell(20);
shyhje.setCellType(Cell.CELL_TYPE_STRING);
String shyhjeStr = shyhje.getStringCellValue().trim();
bankbillHistory.setCShyhje(shyhjeStr);
// 发卡行
HSSFCell fkh = row.getCell(21);
fkh.setCellType(Cell.CELL_TYPE_STRING);
String fkhStr = fkh.getStringCellValue().trim();
bankbillHistory.setCFkh(fkhStr);
// 分店简称
HSSFCell fdjc = row.getCell(22);
fdjc.setCellType(Cell.CELL_TYPE_STRING);
String fdjcStr = fdjc.getStringCellValue().trim();
bankbillHistory.setCFdjc(fdjcStr);
// 其他优惠金额
HSSFCell qtyhje = row.getCell(23);
qtyhje.setCellType(Cell.CELL_TYPE_STRING);
String qtyhjeStr = qtyhje.getStringCellValue().trim();
bankbillHistory.setCQtyhje(qtyhjeStr);
// 分期期数
HSSFCell fqqs = row.getCell(24);
fqqs.setCellType(Cell.CELL_TYPE_STRING);
String fqqsStr = fqqs.getStringCellValue().trim();
bankbillHistory.setCFqqs(fqqsStr);
// 分期手续费
HSSFCell fqsxf = row.getCell(25);
fqsxf.setCellType(Cell.CELL_TYPE_STRING);
String fqsxfStr = fqsxf.getStringCellValue().trim();
bankbillHistory.setCFqsxf(fqsxfStr);
// 分期服务方
HSSFCell fqfwf = row.getCell(26);
fqfwf.setCellType(Cell.CELL_TYPE_STRING);
String fqfwfStr = fqfwf.getStringCellValue().trim();
bankbillHistory.setCFqfwf(fqfwfStr);
// 分期付息方
HSSFCell fqfxf = row.getCell(27);
fqfxf.setCellType(Cell.CELL_TYPE_STRING);
String fqfxfStr = fqfxf.getStringCellValue().trim();
bankbillHistory.setCFqfxf(fqfxfStr);
// 子订单号
HSSFCell zddh = row.getCell(28);
zddh.setCellType(Cell.CELL_TYPE_STRING);
String zddhStr = zddh.getStringCellValue().trim();
bankbillHistory.setCZddh(zddhStr);
// 表名
bankbillHistory.setBillTableName(bill_table_name);
list.add(bankbillHistory);
}
responseMap.put("list", list);
} else {
System.out.println("执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name);
errCode = "999";
errMsg = "执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("执行失败,原因:" + e.getMessage());
errCode = "999";
errMsg = "执行失败,原因:" + e.getMessage();
} finally {
try {
if (sheets != null) {
sheets.close();
}
} catch (Exception ie) {
ie.printStackTrace();
}
}
responseMap.put("errCode", errCode);
responseMap.put("errMsg", errMsg);
return responseMap;
}
/**
* 获取银行端商户POS对账数据
*
* @param map
* @return
*/
public HashMap<Object, Object> getBankDataBySHPOS(@RequestBody HashMap<Object, Object> map) {
HashMap<Object, Object> responseMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
// 先下载文件
String host = StringDUtil.changeNullToEmpty(map.get("FTP_HOST"));
String portStr = StringDUtil.changeNullToEmpty(map.get("FTP_PORT"));
int port = Integer.parseInt("".equals(portStr) ? "21" : portStr);
String username = StringDUtil.changeNullToEmpty(map.get("FTP_USER"));
String password = StringDUtil.changeNullToEmpty(map.get("FTP_PASSWORD"));
String localPath = StringDUtil.changeNullToEmpty(map.get("LOCAL_PATH"));
String mch_id = StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
String ftp_path = StringDUtil.changeNullToEmpty(map.get("FTP_PATH"));
String ftp_file_name = StringDUtil.changeNullToEmpty(map.get("FTP_FILE_NAME"));
String bill_table_name = StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId = StringDUtil.changeNullToEmpty(map.get("ID"));
String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));
HSSFWorkbook sheets = null;
try {
// 下载文件
boolean b = DownloadFtpUtil.downloadFtpFile(host, username, password, port, ftp_path, localPath, ftp_file_name);
// 判断是否下载到文件
if (!b) {
throw new RuntimeException("没有下载到文件" + ftp_file_name);
}
// 判断本地是否有文件
File file = new File(localPath + "/" + ftp_file_name);
if (file.exists()) {
// 存在 开始解析 存入数据库
FileInputStream fileInputStream = new FileInputStream(localPath + "/" + ftp_file_name);
sheets = new HSSFWorkbook(fileInputStream);
HSSFSheet sheet = sheets.getSheetAt(0);
// 获取sheet中第一行行号
int firstRowNum = sheet.getFirstRowNum();
// 获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
List<BankbillHistory> list = new ArrayList<>();
for (int i = firstRowNum + 3; i <= lastRowNum - 4; i++) {
log.info("正在解析第" + i + "行数据");// 因为表格中第一行为说明,第二行为列标题
HSSFRow row = sheet.getRow(i);
BankbillHistory bankbillHistory = new BankbillHistory();
// 清算日期
HSSFCell qsrq = row.getCell(0);
qsrq.setCellType(Cell.CELL_TYPE_STRING);
String qsrqStr = qsrq.getStringCellValue().trim();
// 拿第一个字段判断这条记录是否为空 如果为空直接跳出 一般这字段为空就是数据结束了或根本没有
if ("".equals(qsrqStr) || "汇总信息".equals(qsrqStr)) {
break;
}
bankbillHistory.setCQsrq(qsrqStr);
// 交易日期
HSSFCell jyrq = row.getCell(1);
String jyrqStr = "";
jyrq.setCellType(Cell.CELL_TYPE_NUMERIC);
if (HSSFDateUtil.isCellDateFormatted(jyrq)) {
Date dateCellValue = jyrq.getDateCellValue();
jyrqStr = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, dateCellValue);
}
bankbillHistory.setCJyrq(jyrqStr);
if (i == 0) {
trade_date = jyrqStr;
}
// 交易时间
HSSFCell jysj = row.getCell(2);
jysj.setCellType(Cell.CELL_TYPE_STRING);
String jysjStr = jysj.getStringCellValue().trim();
bankbillHistory.setCJysj(jysjStr);
// 终端号
HSSFCell zdh = row.getCell(3);
zdh.setCellType(Cell.CELL_TYPE_STRING);
String zdhStr = zdh.getStringCellValue().trim();
bankbillHistory.setCZdh(zdhStr);
// 卡号
HSSFCell card = row.getCell(4);
card.setCellType(Cell.CELL_TYPE_STRING);
String khStr = card.getStringCellValue().trim();
bankbillHistory.setCCard(khStr);
// 交易类型
HSSFCell jylx = row.getCell(5);
jylx.setCellType(Cell.CELL_TYPE_STRING);
String jylxStr = jylx.getStringCellValue().trim();
bankbillHistory.setCJylx(jylxStr);
// 交易金额
HSSFCell jyje = row.getCell(6);
jyje.setCellType(Cell.CELL_TYPE_STRING);
String jyjeStr = jyje.getStringCellValue().trim();
log.info("jyjeStr:" + jyjeStr);
bankbillHistory.setCJyje(jyjeStr);
// 清算金额
HSSFCell qsje = row.getCell(7);
qsje.setCellType(Cell.CELL_TYPE_STRING);
String qsjeStr = qsje.getStringCellValue().trim();
bankbillHistory.setCQsje(qsjeStr);
// 手续费
HSSFCell sxf = row.getCell(8);
sxf.setCellType(Cell.CELL_TYPE_STRING);
String sxfStr = sxf.getStringCellValue().trim();
bankbillHistory.setCSxf(sxfStr);
// 参考号
HSSFCell ckh = row.getCell(9);
ckh.setCellType(Cell.CELL_TYPE_STRING);
String ckhStr = ckh.getStringCellValue().trim();
bankbillHistory.setCCkh(ckhStr);
// 流水号
HSSFCell lsh = row.getCell(10);
lsh.setCellType(Cell.CELL_TYPE_STRING);
String lshStr = lsh.getStringCellValue().trim();
bankbillHistory.setCLsh(lshStr);
// 卡类型
HSSFCell klx = row.getCell(11);
klx.setCellType(Cell.CELL_TYPE_STRING);
String klxStr = klx.getStringCellValue().trim();
bankbillHistory.setCKlx(klxStr);
// 发卡行
HSSFCell fkh = row.getCell(12);
fkh.setCellType(Cell.CELL_TYPE_STRING);
String fkhStr = fkh.getStringCellValue().trim();
bankbillHistory.setCFkh(fkhStr);
// 支付方式
HSSFCell zffs = row.getCell(13);
zffs.setCellType(Cell.CELL_TYPE_STRING);
String zffsStr = zffs.getStringCellValue().trim();
bankbillHistory.setCZffs(zffsStr);
// 银商订单号
HSSFCell ysddh = row.getCell(15);
ysddh.setCellType(Cell.CELL_TYPE_STRING);
String ysddhStr = ysddh.getStringCellValue().trim();
log.info("ysddhStr:" + ysddhStr);
bankbillHistory.setCYsddh(ysddhStr);
// 商户订单号
HSSFCell shddh = row.getCell(16);
shddh.setCellType(Cell.CELL_TYPE_STRING);
String shddhStr = shddh.getStringCellValue().trim();
bankbillHistory.setCShddh(shddhStr);
// 备注字段
HSSFCell bzzd = row.getCell(18);
bzzd.setCellType(Cell.CELL_TYPE_STRING);
String bzzdStr = bzzd.getStringCellValue().trim();
bankbillHistory.setCBzzd(bzzdStr);
// 钱包优惠金额
HSSFCell fdmcjj = row.getCell(19);
fdmcjj.setCellType(Cell.CELL_TYPE_STRING);
String fdmcjjStr = fdmcjj.getStringCellValue().trim();
bankbillHistory.setCFdjc(fdmcjjStr);
// 表名
bankbillHistory.setBillTableName(bill_table_name);
list.add(bankbillHistory);
}
responseMap.put("list", list);
} else {
System.out.println("执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name);
errCode = "999";
errMsg = "执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("执行失败,原因:" + e.getMessage());
errCode = "999";
errMsg = "执行失败,原因:" + e.getMessage();
} finally {
try {
if (sheets != null) {
sheets.close();
}
} catch (Exception ie) {
ie.printStackTrace();
}
}
responseMap.put("errCode", errCode);
responseMap.put("errMsg", errMsg);
return responseMap;
}
/**
* 获取银行端商户银联卡对账数据
*
* @param map
* @return
*/
public HashMap<Object, Object> getBankDataBySHYLK(@RequestBody HashMap<Object, Object> map) {
HashMap<Object, Object> responseMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
// 先下载文件
String host = StringDUtil.changeNullToEmpty(map.get("FTP_HOST"));
String portStr = StringDUtil.changeNullToEmpty(map.get("FTP_PORT"));
int port = Integer.parseInt("".equals(portStr) ? "21" : portStr);
String username = StringDUtil.changeNullToEmpty(map.get("FTP_USER"));
String password = StringDUtil.changeNullToEmpty(map.get("FTP_PASSWORD"));
String localPath = StringDUtil.changeNullToEmpty(map.get("LOCAL_PATH"));
String mch_id = StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
String ftp_path = StringDUtil.changeNullToEmpty(map.get("FTP_PATH"));
String ftp_file_name = StringDUtil.changeNullToEmpty(map.get("FTP_FILE_NAME"));
String bill_table_name = StringDUtil.changeNullToEmpty(map.get("BILL_TABLE_NAME"));
String thirdConfigId = StringDUtil.changeNullToEmpty(map.get("ID"));
String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));
HSSFWorkbook sheets = null;
try {
// 下载文件
boolean b = DownloadFtpUtil.downloadFtpFile(host, username, password, port, ftp_path, localPath, ftp_file_name);
// 判断是否下载到文件
if (!b) {
throw new RuntimeException("没有下载到文件" + ftp_file_name);
}
// 判断本地是否有文件
File file = new File(localPath + "/" + ftp_file_name);
if (file.exists()) {
// 存在 开始解析 存入数据库
FileInputStream fileInputStream = new FileInputStream(localPath + "/" + ftp_file_name);
sheets = new HSSFWorkbook(fileInputStream);
HSSFSheet sheet = sheets.getSheetAt(0);
// 获取sheet中第一行行号
int firstRowNum = sheet.getFirstRowNum();
// 获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
List<BankbillHistory> list = new ArrayList<>();
for (int i = firstRowNum + 3; i <= lastRowNum - 2; i++) {// 因为表格中第一行为说明,第二行为列标题
HSSFRow row = sheet.getRow(i);
BankbillHistory bankbillHistory = new BankbillHistory();
// 清算日期
HSSFCell qsrq = row.getCell(0);
qsrq.setCellType(Cell.CELL_TYPE_STRING);
String qsrqStr = qsrq.getStringCellValue().trim();
// 拿第一个字段判断这条记录是否为空 如果为空直接跳出 一般这字段为空就是数据结束了或根本没有
if ("".equals(qsrqStr) || "合计".equals(qsrqStr)) {
break;
}
bankbillHistory.setCQsrq(qsrqStr);
// 交易日期
HSSFCell jyrq = row.getCell(1);
String jyrqStr = "";
jyrq.setCellType(Cell.CELL_TYPE_NUMERIC);
if (HSSFDateUtil.isCellDateFormatted(jyrq)) {
Date dateCellValue = jyrq.getDateCellValue();
jyrqStr = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, dateCellValue);
}
bankbillHistory.setCJyrq(jyrqStr);
if (i == 0) {
trade_date = jyrqStr;
}
// 交易时间
HSSFCell jysj = row.getCell(2);
jysj.setCellType(Cell.CELL_TYPE_STRING);
String jysjStr = jysj.getStringCellValue().trim();
bankbillHistory.setCJysj(jysjStr);
// 终端号
HSSFCell zdh = row.getCell(3);
zdh.setCellType(Cell.CELL_TYPE_STRING);
String zdhStr = zdh.getStringCellValue().trim();
bankbillHistory.setCZdh(zdhStr);
// 卡号
HSSFCell card = row.getCell(4);
card.setCellType(Cell.CELL_TYPE_STRING);
String khStr = card.getStringCellValue().trim();
bankbillHistory.setCCard(khStr);
// 交易类型
HSSFCell jylx = row.getCell(5);
jylx.setCellType(Cell.CELL_TYPE_STRING);
String jylxStr = jylx.getStringCellValue().trim();
bankbillHistory.setCJylx(jylxStr);
// 交易金额
HSSFCell jyje = row.getCell(6);
jyje.setCellType(Cell.CELL_TYPE_STRING);
String jyjeStr = jyje.getStringCellValue().trim();
bankbillHistory.setCJyje(jyjeStr);
// 清算金额
HSSFCell qsje = row.getCell(7);
qsje.setCellType(Cell.CELL_TYPE_STRING);
String qsjeStr = qsje.getStringCellValue().trim();
bankbillHistory.setCQsje(qsjeStr);
// 手续费
HSSFCell sxf = row.getCell(8);
sxf.setCellType(Cell.CELL_TYPE_STRING);
String sxfStr = sxf.getStringCellValue().trim();
bankbillHistory.setCSxf(sxfStr);
// 流水号
HSSFCell lsh = row.getCell(9);
lsh.setCellType(Cell.CELL_TYPE_STRING);
String lshStr = lsh.getStringCellValue().trim();
bankbillHistory.setCLsh(lshStr);
// 卡类型
HSSFCell klx = row.getCell(10);
klx.setCellType(Cell.CELL_TYPE_STRING);
String klxStr = klx.getStringCellValue().trim();
bankbillHistory.setCKlx(klxStr);
// 发卡行
HSSFCell fkh = row.getCell(11);
fkh.setCellType(Cell.CELL_TYPE_STRING);
String fkhStr = fkh.getStringCellValue().trim();
bankbillHistory.setCFkh(fkhStr);
// 系统参考号 (对应银商订单号)
HSSFCell xtckh = row.getCell(12);
xtckh.setCellType(Cell.CELL_TYPE_STRING);
String xtckhStr = xtckh.getStringCellValue().trim();
// 支付方式
bankbillHistory.setCZffs("银行卡支付");
// 银商订单号
bankbillHistory.setCYsddh(xtckhStr);
// 表名
bankbillHistory.setBillTableName(bill_table_name);
list.add(bankbillHistory);
}
responseMap.put("list", list);
} else {
System.out.println("执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name);
errCode = "999";
errMsg = "执行失败,原因:路径" + ftp_path + "下无下载文件" + ftp_file_name;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("执行失败,原因:" + e.getMessage());
errCode = "999";
errMsg = "执行失败,原因:" + e.getMessage();
} finally {
try {
if (sheets != null) {
sheets.close();
}
} catch (Exception ie) {
ie.printStackTrace();
}
}
responseMap.put("errCode", errCode);
responseMap.put("errMsg", errMsg);
return responseMap;
}
/**
* 获取微信支付对账数据
*
* @param map
* @return
*/
public HashMap<Object, Object> getBankDataByWXAPI(@RequestBody HashMap<Object, Object> map) {
List<BankbillHistory> list = new ArrayList<>();
log.info("开始获取微信账单");
HashMap<Object, Object> responseMap = new HashMap<>();
String errCode = "0";
String errMsg = "";
String localPath = StringDUtil.changeNullToEmpty(map.get("LOCAL_PATH"));
String mch_id = StringDUtil.changeNullToEmpty(map.get("MCH_ID"));
String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));
HashMap<String, Object> reqMap = new HashMap<>();
reqMap.put("grant_type", grant_type);
reqMap.put("appid", appid);
reqMap.put("secret", secret);
String randomStr = RandomUtil.randomString(32);
String signString = "appid=" + appid + "&bill_date=" + trade_date + "&bill_type=ALL&mch_id=" + mch_id + "&nonce_str=" + randomStr + "&key=9b996ad13a7d11ee8c2b000c29686702";
String sign = SecureUtil.md5(signString).toUpperCase();
String reqXml = "<xml>\n" +
" <appid>" + appid + "</appid>\n" +
" <bill_date>" + trade_date + "</bill_date>\n" +
" <bill_type>ALL</bill_type>\n" +
" <mch_id>" + mch_id + "</mch_id>\n" +
" <nonce_str>" + randomStr + "</nonce_str>\n" +
" <sign>" + sign + "</sign>\n" +
"</xml>";
// 获取access_token
String body1 = HttpUtil.createPost("https://api.mch.weixin.qq.com/pay/downloadbill").header("Content-Type", "text/xml").body(reqXml).execute().body();
String[] split = body1.split("\n");
for (int i = 1; i < split.length - 2; i++) {
String[] split1 = split[i].split(",");
BankbillHistory bankbillHistory = new BankbillHistory();
int flag = 0;
for (int j = 0; j < split1.length; j++) {
if (j == 0) {
String s = split1[j].replaceAll("`", "");
String[] split2 = s.split(" ");
bankbillHistory.setCQsrq(split2[0]);
bankbillHistory.setCJyrq(split2[0]);
bankbillHistory.setCJysj(split2[1]);
}
if (j == 1) {
String s = split1[j].replaceAll("`", "");
bankbillHistory.setCZdh(s);
}
if (j == 2) {
String s = split1[j].replaceAll("`", "");
bankbillHistory.setCCkh(s);
}
if (j == 5) {
String s = split1[j].replaceAll("`", "");
bankbillHistory.setCYsddh(s);
}
if (j == 6) {
String s = split1[j].replaceAll("`", "");
bankbillHistory.setCShddh(s);
}
if (j == 7) {
String s = split1[j].replaceAll("`", "");
bankbillHistory.setCCard(s);
}
if (j == 9) {
String s = split1[j].replaceAll("`", "");
if (s.equals("REFUND")) {
flag = -1;
}
}
if (j == 21) {
String s = split1[j].replaceAll("`", "");
String s1 = s.replaceAll(" ", ",").replaceAll("\\\\", "");
JSONObject entries = JSONUtil.parseObj(s1);
Object zfje = entries.get("zfje");
if (flag < 0) {
bankbillHistory.setCJyje("-" + Convert.toDouble(zfje));
} else {
bankbillHistory.setCJyje(Convert.toDouble(zfje).toString());
}
}
bankbillHistory.setBillTableName("微信支付账单");
}
list.add(bankbillHistory);
}
// 获取access_token
String body = HttpUtil.createPost("https://api.weixin.qq.com/cgi-bin/stable_token").body(JSONUtil.toJsonStr(reqMap)).execute().body();
String access_token = JSONUtil.parseObj(body).getStr("access_token");
// 获取医保账单下载地址
// https://api.weixin.qq.com/payinsurance/billdownload?access_token=ACCESS_TOKEN
String signYBString = "appid=" + appid + "&bill_date=" + trade_date + "&bill_type=ALL&mch_id=" + mch_id + "&nonce_str=" + randomStr + "&key=f6d394fbc881fee73172232a08dc9c03";
String signYB = SecureUtil.md5(signYBString).toUpperCase();
String reqYbXml = "<xml>\n" +
" <appid>" + appid + "</appid>\n" +
" <bill_date>" + trade_date + "</bill_date>\n" +
" <bill_type>ALL</bill_type>\n" +
" <mch_id>" + mch_id + "</mch_id>\n" +
" <nonce_str>" + randomStr + "</nonce_str>\n" +
" <sign>" + signYB + "</sign>\n" +
"</xml>";
String resBody = HttpUtil.createPost("https://api.weixin.qq.com/payinsurance/billdownload?access_token=" + access_token).header("Content-Type", "text/xml").body(reqYbXml).execute().body();
Document document = XmlUtil.parseXml(resBody);
Element elementG = XmlUtil.getRootElement(document);
Element returnCode = XmlUtil.getElement(elementG, "return_code");
if (returnCode.getTextContent().equals("SUCCESS")) {// 响应成功
// 下载文件
Element downloadUrl = XmlUtil.getElement(elementG, "download_url");
String dwUrl = downloadUrl.getTextContent();
log.info("开始下载文件");
HttpUtil.downloadFileFromUrl(dwUrl, localPath + File.separator + trade_date + ".csv");
HttpUtil.downloadFileFromUrl(dwUrl, localPath + File.separator + trade_date + ".txt");
// HttpUtil.downloadFileFromUrl(dwUrl, localPath);
log.info("下载文件成功");
CsvReader reader = CsvUtil.getReader();
try {
CsvData read = reader.read(FileUtil.file(localPath + File.separator + trade_date + ".csv"));
List<CsvRow> rows = read.getRows();
for (int i = 1; i < rows.size(); i++) {
CsvRow row = rows.get(i);
BankbillHistory bankbillHistory = new BankbillHistory();
String s = row.get(0).replaceAll("`", "");
String[] s1 = s.split(" ");
bankbillHistory.setCJyrq(s1[0]);
bankbillHistory.setCQsrq(s1[0]);
bankbillHistory.setCJysj(s1[1]);
bankbillHistory.setCZdh(row.get(1).replaceAll("`", ""));
bankbillHistory.setCCkh(row.get(2).replaceAll("`", ""));
bankbillHistory.setCLsh(row.get(5).replaceAll("`", ""));
bankbillHistory.setCShddh(row.get(6).replaceAll("`", ""));
bankbillHistory.setCCard(row.get(7).replaceAll("`", ""));
bankbillHistory.setCYsddh(row.get(25).replaceAll("`", ""));
if (row.get(46).replaceAll("`", "").equals("0.00")) {
continue;
}
if (row.get(45).replaceAll("`", "").equals("REFUND")) {
bankbillHistory.setCJyje("-" + row.get(46).replaceAll("`", ""));
} else {
bankbillHistory.setCJyje(row.get(46).replaceAll("`", ""));
}
bankbillHistory.setBillTableName("微信支付账单");
list.add(bankbillHistory);
}
responseMap.put("list", list);
} catch (IORuntimeException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} else {
// 返回响应结果
Element returnMsg = XmlUtil.getElement(elementG, "return_msg");
errCode = "999";
errMsg = returnMsg.getTextContent();
}
responseMap.put("errCode", errCode);
responseMap.put("errMsg", errMsg);
return responseMap;
}
}

View File

@@ -105,13 +105,21 @@ public class BankbillHistoryServiceImpl implements BankbillHistoryService {
String cZffs = bankbillHistory.getCZffs();
String zffs = payMethodMap.get(cZffs);
//修改支付方式变为字典表中对应的值
if ("商户银联卡对账单".equals(billTableName)){
if ("招商商户银联卡对账单".equals(billTableName)){
zffs = payMethodMap.get("银行卡支付");
bankbillHistory.setCZffs(zffs);
}else if ("商户对账单".equals(billTableName)){
}else if ("浦发商户银联卡对账单".equals(billTableName)) {
zffs = payMethodMap.get("银行卡支付");
bankbillHistory.setCZffs(zffs);
}
else if ("招商商户对账单".equals(billTableName)){
zffs = payMethodMap.get("掌医支付");
bankbillHistory.setCZffs(zffs);
}else{
}else if ("浦发商户对账单".equals(billTableName)){
zffs = payMethodMap.get("掌医支付");
bankbillHistory.setCZffs(zffs);
}
else{
if (zffs==null){
zffs = payMethodMap.get("其他支付");
bankbillHistory.setCZffs(zffs);

View File

@@ -35,22 +35,52 @@ public class DownloadFtpUtil {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
// 设置连接超时时间
ftpClient.setConnectTimeout(30000); // 30秒连接超时
ftpClient.setDataTimeout(60000); // 60秒数据传输超时
log.info("正在连接FTP服务器: " + ftpHost + ":" + ftpPort);
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
// 检查连接是否成功
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("未连接到FTP用户名或密码错误。");
log.error("FTP连接失败服务器响应码: " + ftpClient.getReplyCode());
ftpClient.disconnect();
} else {
log.info("FTP连接成功。");
return null;
}
log.info("正在登录FTP服务器用户名: " + ftpUserName);
boolean loginSuccess = ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!loginSuccess) {
log.error("FTP登录失败请检查用户名和密码");
ftpClient.disconnect();
return null;
}
log.info("FTP连接和登录成功");
return ftpClient;
} catch (SocketException e) {
log.error("FTP连接Socket异常: " + e.getMessage());
e.printStackTrace();
log.info("FTP的IP地址可能错误请正确配置。");
} catch (IOException e) {
log.error("FTP连接IO异常: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.error("FTP连接未知异常: " + e.getMessage());
e.printStackTrace();
log.info("FTP的端口错误,请正确配置。");
}
return ftpClient;
// 如果连接失败,确保断开连接
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
log.error("断开FTP连接失败: " + e.getMessage());
}
}
return null;
}
@@ -72,10 +102,11 @@ public class DownloadFtpUtil {
* @param fileName 文件名称
*/
public static boolean downloadFtpFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
FTPClient ftpClient = null;
OutputStream os = null;
try {
//先判断下载的路径是否存在 不存在创建
File file = new File(localPath);
@@ -84,28 +115,73 @@ public class DownloadFtpUtil {
}
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
// 检查FTP客户端是否为空
if (ftpClient == null) {
log.error("FTP客户端创建失败请检查FTP连接参数");
return false;
}
// 检查连接是否成功
if (!ftpClient.isConnected()) {
log.error("FTP连接失败无法连接到服务器");
return false;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
boolean b = ftpClient.retrieveFile(fileName, os);
// 设置文件类型前检查连接状态
if (ftpClient.isConnected()) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
os.close();
ftpClient.logout();
// 切换到指定目录
if (ftpPath != null && !ftpPath.trim().isEmpty()) {
ftpClient.changeWorkingDirectory(ftpPath);
}
File localFile = new File(localPath + File.separatorChar + fileName);
os = new FileOutputStream(localFile);
boolean b = ftpClient.retrieveFile(fileName, os);
if (b) {
log.info("文件下载成功: " + fileName);
} else {
log.error("文件下载失败: " + fileName);
}
return b;
} else {
log.error("FTP连接已断开");
return false;
}
return b;
} catch (FileNotFoundException e) {
log.error("没有找到" + ftpPath + "文件");
log.error("没有找到文件: " + ftpPath + "/" + fileName);
e.printStackTrace();
} catch (SocketException e) {
log.error("连接FTP失败.");
log.error("连接FTP失败: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("文件读取错误: " + e.getMessage());
e.printStackTrace();
log.error("文件读取错误。");
} finally {
// 确保资源正确关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
log.error("关闭输出流失败: " + e.getMessage());
}
}
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
log.error("关闭FTP连接失败: " + e.getMessage());
}
}
}
return false;
}