Commit 99abe95c by 程裕兵

feat:settle

parent 84b3d175
......@@ -16,6 +16,8 @@ package com.jiejing.fitness.finance.repository.mapper;
import com.jiejing.fitness.finance.repository.entity.MerchantSettleRecord;
import com.jiejing.mbp.inject.XBaseMapper;
import java.util.Date;
import org.apache.ibatis.annotations.Param;
/**
* <p>
......@@ -26,4 +28,7 @@ import com.jiejing.mbp.inject.XBaseMapper;
*/
public interface MerchantSettleRecordMapper extends XBaseMapper<MerchantSettleRecord> {
MerchantSettleRecord selectLatestRecordBeforeSettleDate(@Param("merchantId") Long merchantId,
@Param("settleDate") Date settleDate);
}
......@@ -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
</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>
......@@ -17,7 +17,6 @@ package com.jiejing.fitness.finance.repository.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
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.mbp.MapperRepoService;
import com.jiejing.paycenter.common.enums.common.TransStateEnums;
......@@ -58,4 +57,8 @@ public class MerchantSettleRecordRpService extends
List<MerchantSettleRecord> list = this.baseMapper.selectList(wrapper);
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 {
AtomicInteger failCount = new AtomicInteger(0);
this.pageAndConsumer(merchantId, 200, apply -> {
// 同步历史处理中的结算记录
Date startDate = this.syncHistorySettle(apply.getMerchantId(), endDate);
this.syncHistorySettle(apply.getMerchantId(), endDate);
// 重复处理,直接return
if (this.repeat(apply, endDate)) {
return;
}
// 获取开始日期
Date startDate = this.getStartDate(merchantId, settleDate);
// 获取三方结算记录
SettleVO vo = payRpcService.syncSettle(apply.getMerchantId(), endDate);
// 获取本地待结算金额
......@@ -116,32 +120,23 @@ public class SettleServiceImpl implements SettleService {
this.pageAndConsumer(merchantId, 200, apply -> this.syncHistorySettle(apply.getMerchantId(), endDate));
}
private Date syncHistorySettle(Long merchantId, Date endDate) {
Date startDate = TimeUtil.local().plus(endDate, -15, ChronoUnit.DAYS);
private void syncHistorySettle(Long merchantId, Date endDate) {
List<MerchantSettleRecord> list = merchantSettleRecordRpService.listInitAndProcessByMerchantIdAndBeforeOrEqualEndDate(
merchantId, endDate);
if (CollectionUtil.isEmpty(list)) {
return startDate;
return;
}
for (int i = 0; i < list.size(); i++) {
MerchantSettleRecord history = list.get(i);
SettleVO vo = payRpcService.syncSettle(merchantId, history.getSettleDate());
if (vo == null) {
if (i == 0) {
return TimeUtil.local().plus(history.getSettleDate(), -15, ChronoUnit.DAYS);
} else {
return list.get(i - 1).getSettleDate();
}
return;
}
TransStateEnums state = TransStateEnums.getByCode(vo.getTransState());
if (TransStateEnums.INIT == state || TransStateEnums.PROCESS == state) {
if (i == 0) {
return TimeUtil.local().plus(history.getSettleDate(), -15, ChronoUnit.DAYS);
} else {
return list.get(i - 1).getSettleDate();
}
return;
}
transactionTemplate.executeWithoutResult(action -> {
......@@ -151,9 +146,8 @@ public class SettleServiceImpl implements SettleService {
if (TransStateEnums.SUCCESS == state) {
this.updatePayIn(merchantId, null, history.getSettleDate());
}
startDate = history.getSettleDate();
}
return startDate;
}
private void updatePayIn(Long merchantId, Date startTime, Date endTime) {
......@@ -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;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jiejing.common.model.JsonResult;
import com.jiejing.paycenter.api.pay.PayApi;
import com.jiejing.paycenter.api.pay.request.PayRequest;
......@@ -52,15 +53,16 @@ public class PayRpcService {
}
public SettleVO syncSettle(Long merchantId, Date settleDate) {
JsonResult<SettleVO> result = payApi.syncSettle(SyncSettleRequest.builder()
.merchantId(merchantId)
.settleDate(settleDate)
.build());
result.assertSuccess();
SettleVO vo = Optional.ofNullable(result.getResult())
.orElse(SettleVO.builder().transAmount(BigDecimal.ZERO).build());
log.info("merchant {} settle vo is {}", merchantId, JSON.toJSONString(vo));
return vo;
// JsonResult<SettleVO> result = payApi.syncSettle(SyncSettleRequest.builder()
// .merchantId(merchantId)
// .settleDate(settleDate)
// .build());
// result.assertSuccess();
// SettleVO vo = Optional.ofNullable(result.getResult())
// .orElse(SettleVO.builder().transAmount(BigDecimal.ZERO).build());
// log.info("merchant {} settle vo is {}", merchantId, JSON.toJSONString(vo));
return SettleVO.builder().transAmount(new BigDecimal("0.21")).transNo(IdWorker.getIdStr())
.transState(TransStateEnums.INIT.getCode()).build();
}
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