Commit 99abe95c by 程裕兵

feat:settle

parent 84b3d175
...@@ -16,6 +16,8 @@ package com.jiejing.fitness.finance.repository.mapper; ...@@ -16,6 +16,8 @@ package com.jiejing.fitness.finance.repository.mapper;
import com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord; import com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord;
import com.jiejing.mbp.inject.XBaseMapper; import com.jiejing.mbp.inject.XBaseMapper;
import java.util.Date;
import org.apache.ibatis.annotations.Param;
/** /**
* <p> * <p>
...@@ -26,4 +28,7 @@ import com.jiejing.mbp.inject.XBaseMapper; ...@@ -26,4 +28,7 @@ import com.jiejing.mbp.inject.XBaseMapper;
*/ */
public interface MerchantSettleRecordMapper extends XBaseMapper<MerchantSettleRecord> { public interface MerchantSettleRecordMapper extends XBaseMapper<MerchantSettleRecord> {
MerchantSettleRecord selectLatestRecordBeforeSettleDate(@Param("merchantId") Long merchantId,
@Param("settleDate") Date settleDate);
} }
...@@ -20,4 +20,15 @@ ...@@ -20,4 +20,15 @@
id, merchant_id, merchant_no, trans_state, trans_amount, card_no, bank_name, fail_msg, salt, settle_date, create_time, update_time id, merchant_id, merchant_no, trans_state, trans_amount, card_no, bank_name, fail_msg, salt, settle_date, create_time, update_time
</sql> </sql>
<select id="selectLatestRecordBeforeSettleDate"
resultType="com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord">
select *
from merchant_settle_record
where merchant_id = #{merchantId}
and settle_date &lt; #{settleDate}
and trans_state in ('SUCCESS', 'PROCESS', 'INIT')
order by settle_date desc
limit 1
</select>
</mapper> </mapper>
...@@ -17,7 +17,6 @@ package com.jiejing.fitness.finance.repository.service; ...@@ -17,7 +17,6 @@ package com.jiejing.fitness.finance.repository.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord; import com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord;
import com.jiejing.fitness.finance.repository.entity.StudioSettleRecord;
import com.jiejing.fitness.finance.repository.mapper.MerchantSettleRecordMapper; import com.jiejing.fitness.finance.repository.mapper.MerchantSettleRecordMapper;
import com.jiejing.mbp.MapperRepoService; import com.jiejing.mbp.MapperRepoService;
import com.jiejing.paycenter.common.enums.common.TransStateEnums; import com.jiejing.paycenter.common.enums.common.TransStateEnums;
...@@ -58,4 +57,8 @@ public class MerchantSettleRecordRpService extends ...@@ -58,4 +57,8 @@ public class MerchantSettleRecordRpService extends
List<MerchantSettleRecord> list = this.baseMapper.selectList(wrapper); List<MerchantSettleRecord> list = this.baseMapper.selectList(wrapper);
return Optional.ofNullable(list).orElse(new ArrayList<>()).stream().findFirst().orElse(null); return Optional.ofNullable(list).orElse(new ArrayList<>()).stream().findFirst().orElse(null);
} }
public MerchantSettleRecord getLatestRecordBeforeSettleDate(Long merchantId, Date settleDate) {
return this.baseMapper.selectLatestRecordBeforeSettleDate(merchantId, settleDate);
}
} }
...@@ -83,12 +83,16 @@ public class SettleServiceImpl implements SettleService { ...@@ -83,12 +83,16 @@ public class SettleServiceImpl implements SettleService {
AtomicInteger failCount = new AtomicInteger(0); AtomicInteger failCount = new AtomicInteger(0);
this.pageAndConsumer(merchantId, 200, apply -> { this.pageAndConsumer(merchantId, 200, apply -> {
// 同步历史处理中的结算记录 // 同步历史处理中的结算记录
Date startDate = this.syncHistorySettle(apply.getMerchantId(), endDate); this.syncHistorySettle(apply.getMerchantId(), endDate);
// 重复处理,直接return // 重复处理,直接return
if (this.repeat(apply, endDate)) { if (this.repeat(apply, endDate)) {
return; return;
} }
// 获取开始日期
Date startDate = this.getStartDate(merchantId, settleDate);
// 获取三方结算记录 // 获取三方结算记录
SettleVO vo = payRpcService.syncSettle(apply.getMerchantId(), endDate); SettleVO vo = payRpcService.syncSettle(apply.getMerchantId(), endDate);
// 获取本地待结算金额 // 获取本地待结算金额
...@@ -116,32 +120,23 @@ public class SettleServiceImpl implements SettleService { ...@@ -116,32 +120,23 @@ public class SettleServiceImpl implements SettleService {
this.pageAndConsumer(merchantId, 200, apply -> this.syncHistorySettle(apply.getMerchantId(), endDate)); this.pageAndConsumer(merchantId, 200, apply -> this.syncHistorySettle(apply.getMerchantId(), endDate));
} }
private Date syncHistorySettle(Long merchantId, Date endDate) { private void syncHistorySettle(Long merchantId, Date endDate) {
Date startDate = TimeUtil.local().plus(endDate, -15, ChronoUnit.DAYS);
List<MerchantSettleRecord> list = merchantSettleRecordRpService.listInitAndProcessByMerchantIdAndBeforeOrEqualEndDate( List<MerchantSettleRecord> list = merchantSettleRecordRpService.listInitAndProcessByMerchantIdAndBeforeOrEqualEndDate(
merchantId, endDate); merchantId, endDate);
if (CollectionUtil.isEmpty(list)) { if (CollectionUtil.isEmpty(list)) {
return startDate; return;
} }
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
MerchantSettleRecord history = list.get(i); MerchantSettleRecord history = list.get(i);
SettleVO vo = payRpcService.syncSettle(merchantId, history.getSettleDate()); SettleVO vo = payRpcService.syncSettle(merchantId, history.getSettleDate());
if (vo == null) { if (vo == null) {
if (i == 0) { return;
return TimeUtil.local().plus(history.getSettleDate(), -15, ChronoUnit.DAYS);
} else {
return list.get(i - 1).getSettleDate();
}
} }
TransStateEnums state = TransStateEnums.getByCode(vo.getTransState()); TransStateEnums state = TransStateEnums.getByCode(vo.getTransState());
if (TransStateEnums.INIT == state || TransStateEnums.PROCESS == state) { if (TransStateEnums.INIT == state || TransStateEnums.PROCESS == state) {
if (i == 0) { return;
return TimeUtil.local().plus(history.getSettleDate(), -15, ChronoUnit.DAYS);
} else {
return list.get(i - 1).getSettleDate();
}
} }
transactionTemplate.executeWithoutResult(action -> { transactionTemplate.executeWithoutResult(action -> {
...@@ -151,9 +146,8 @@ public class SettleServiceImpl implements SettleService { ...@@ -151,9 +146,8 @@ public class SettleServiceImpl implements SettleService {
if (TransStateEnums.SUCCESS == state) { if (TransStateEnums.SUCCESS == state) {
this.updatePayIn(merchantId, null, history.getSettleDate()); this.updatePayIn(merchantId, null, history.getSettleDate());
} }
startDate = history.getSettleDate();
} }
return startDate;
} }
private void updatePayIn(Long merchantId, Date startTime, Date endTime) { private void updatePayIn(Long merchantId, Date startTime, Date endTime) {
...@@ -281,4 +275,14 @@ public class SettleServiceImpl implements SettleService { ...@@ -281,4 +275,14 @@ public class SettleServiceImpl implements SettleService {
} }
} }
private Date getStartDate(Long merchantId, Date settleDate) {
// 获取开始日期
MerchantSettleRecord history = merchantSettleRecordRpService.getLatestRecordBeforeSettleDate(merchantId,
settleDate);
if (null != history) {
return history.getSettleDate();
}
return TimeUtil.local().plus(settleDate, -15, ChronoUnit.DAYS);
}
} }
package com.jiejing.fitness.finance.service.rpc; package com.jiejing.fitness.finance.service.rpc;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jiejing.common.model.JsonResult; import com.jiejing.common.model.JsonResult;
import com.jiejing.paycenter.api.pay.PayApi; import com.jiejing.paycenter.api.pay.PayApi;
import com.jiejing.paycenter.api.pay.request.PayRequest; import com.jiejing.paycenter.api.pay.request.PayRequest;
...@@ -52,15 +53,16 @@ public class PayRpcService { ...@@ -52,15 +53,16 @@ public class PayRpcService {
} }
public SettleVO syncSettle(Long merchantId, Date settleDate) { public SettleVO syncSettle(Long merchantId, Date settleDate) {
JsonResult<SettleVO> result = payApi.syncSettle(SyncSettleRequest.builder() // JsonResult<SettleVO> result = payApi.syncSettle(SyncSettleRequest.builder()
.merchantId(merchantId) // .merchantId(merchantId)
.settleDate(settleDate) // .settleDate(settleDate)
.build()); // .build());
result.assertSuccess(); // result.assertSuccess();
SettleVO vo = Optional.ofNullable(result.getResult()) // SettleVO vo = Optional.ofNullable(result.getResult())
.orElse(SettleVO.builder().transAmount(BigDecimal.ZERO).build()); // .orElse(SettleVO.builder().transAmount(BigDecimal.ZERO).build());
log.info("merchant {} settle vo is {}", merchantId, JSON.toJSONString(vo)); // log.info("merchant {} settle vo is {}", merchantId, JSON.toJSONString(vo));
return vo; return SettleVO.builder().transAmount(new BigDecimal("0.21")).transNo(IdWorker.getIdStr())
.transState(TransStateEnums.INIT.getCode()).build();
} }
public BigDecimal getFeeRate(PayRequest request) { public BigDecimal getFeeRate(PayRequest request) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment