Authored by LUOXC

refactor

package com.yoho.ufo.order.constant;
import com.yoho.order.model.TradeBills;
import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;
import java.util.Objects;
import java.util.Optional;
/**
* 资金状态
*/
@Getter
public enum MoneyTypeEnum {
deposit_rebate(24,2,4,"申请寄存返利","打款"),
buyer_goodsPayment(12,1,2,"买家货款","退款"),
buyer_compensatoryPayment(13,1,3,"买家补偿款","打款"),
seller_goodsPayment(22,2,2,"卖家货款","打款"),
seller_compensatoryPayment(23,2,3,"卖家补偿款","打款"),
seller_deposit(21,2,1,"卖家保证金","退款"),
buyer_deposit(11,1,1,"买家定金","退款");
deposit_rebate(true, 24, 2, 4, "申请寄存返利", "打款"),
buyer_goodsPayment(12, 1, 2, "买家货款", "退款"),
buyer_compensatoryPayment(true, 13, 1, 3, "买家补偿款", "打款"),
seller_goodsPayment(true, 22, 2, 2, "卖家货款", "打款"),
seller_compensatoryPayment(true, 23, 2, 3, "卖家补偿款", "打款"),
seller_deposit(21, 2, 1, "卖家保证金", "退款"),
buyer_deposit(11, 1, 1, "买家定金", "退款");
private Integer code;
private Integer userType;
private Integer tradeType;
private int userType;
private int tradeType;
private String desc;
private String moneyFlow;
private boolean transfer;
MoneyTypeEnum(Integer code, int userType, int tradeType, String desc, String moneyFlow) {
this.code = code;
this.userType = userType;
this.tradeType = tradeType;
this.desc = desc;
this.moneyFlow = moneyFlow;
}
MoneyTypeEnum(Integer code,Integer userType,Integer tradeType, String desc,String moneyFlow){
MoneyTypeEnum(boolean transfer, Integer code, int userType, int tradeType, String desc, String moneyFlow) {
this.transfer = transfer;
this.code = code;
this.userType=userType;
this.tradeType=tradeType;
this.userType = userType;
this.tradeType = tradeType;
this.desc = desc;
this.moneyFlow = moneyFlow;
}
public static MoneyTypeEnum getMoneyTypeByCode(Integer code){
if(code==null){
public boolean isTransfer() {
return transfer;
}
public boolean isRefund() {
return !isTransfer();
}
public static MoneyTypeEnum codeOf(Integer code) {
if (code == null) {
return null;
}
for(MoneyTypeEnum item: MoneyTypeEnum.values()){
if(code.intValue() == item.code.intValue()){
for (MoneyTypeEnum item : MoneyTypeEnum.values()) {
if (code.intValue() == item.code.intValue()) {
return item;
}
}
return null;
}
public static MoneyTypeEnum getMoneyTypeByUserTypeAndTradeType(Integer userType,Integer tradeType){
if(userType==null||tradeType==null){
return null;
public static Optional<MoneyTypeEnum> from(TradeBills tradeBills) {
if (Objects.isNull(tradeBills)) {
return Optional.empty();
}
for(MoneyTypeEnum item: MoneyTypeEnum.values()){
if(userType.intValue() == item.userType.intValue()
&& tradeType.intValue() == item.tradeType.intValue()){
return item;
int userType = ObjectUtils.defaultIfNull(tradeBills.getUserType(),-1);
int tradeType=ObjectUtils.defaultIfNull(tradeBills.getTradeType(),-1);
for (MoneyTypeEnum item : MoneyTypeEnum.values()) {
if (userType == item.userType && tradeType == item.tradeType) {
return Optional.of(item);
}
}
return null;
return Optional.empty();
}
}
... ...
... ... @@ -11,9 +11,9 @@ import com.yoho.order.dal.*;
import com.yoho.order.model.*;
import com.yoho.ufo.constants.SellerTypeEnum;
import com.yoho.ufo.order.constant.MoneyTypeEnum;
import com.yoho.ufo.order.constant.TradeStatusEnum;
import com.yoho.ufo.order.response.StoredSellerRespVo;
import com.yoho.ufo.order.service.ITradeBillsService;
import com.yoho.ufo.order.service.impl.builder.TradeBillsListBuilder;
import com.yoho.ufo.order.util.ServiceExceptions;
import com.yoho.ufo.service.IQNDownloadService;
import com.yoho.ufo.service.impl.UserHelper;
... ... @@ -21,12 +21,10 @@ import com.yoho.ufo.service.model.PageResponseBO;
import com.yoho.ufo.util.CollectionUtil;
import com.yoho.ufo.util.KeyWordHiddenUtil;
import com.yohobuy.ufo.model.enums.StoredSellerStatusEnum;
import com.yohobuy.ufo.model.order.common.AlipayBatchTransferErrorEnum;
import com.yohobuy.ufo.model.order.resp.TradeBillsResp;
import com.yohobuy.ufo.model.user.req.AuthorizeInfoReq;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.collect.Maps;
... ... @@ -192,7 +190,7 @@ public class TradeBillsServiceImpl implements ITradeBillsService {
public PageResponseBO<TradeBillsResp> queryTradeBillsList(TradeBillsReq req) {
if (req.getMoneyType() != null) {
MoneyTypeEnum moneyTypeClass = MoneyTypeEnum.getMoneyTypeByCode(req.getMoneyType());
MoneyTypeEnum moneyTypeClass = MoneyTypeEnum.codeOf(req.getMoneyType());
if (moneyTypeClass != null) {
req.setUserType(moneyTypeClass.getUserType());
req.setTradeType(moneyTypeClass.getTradeType());
... ... @@ -246,10 +244,10 @@ public class TradeBillsServiceImpl implements ITradeBillsService {
if (CollectionUtils.isEmpty(tradeBillsList)) {
return null;
}
Map<Integer, String> outTradeNoMap = getTransferOutTradeNoMap(tradeBillsList);
Map<Integer, String> transferOutTradeNoMap = getTransferOutTradeNoMap(tradeBillsList);
PageResponseBO<TradeBillsResp> result = new PageResponseBO<>();
result.setList(convertToResp(tradeBillsList, outTradeNoMap));
result.setList(new TradeBillsListBuilder(tradeBillsList).transferOutTradeNoMap(transferOutTradeNoMap).build());
result.setPage(req.getPage());
result.setSize(req.getSize());
result.setTotal(total);
... ... @@ -259,7 +257,7 @@ public class TradeBillsServiceImpl implements ITradeBillsService {
private Map<Integer, String> getTransferOutTradeNoMap(List<TradeBills> tradeBillsList) {
List<Long> orderCodes = tradeBillsList.stream()
.filter(item -> isTransferTrade(item))
.filter(item -> MoneyTypeEnum.from(item).map(MoneyTypeEnum::isTransfer).orElse(Boolean.FALSE))
.map(TradeBills::getOrderCode)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(orderCodes)) {
... ... @@ -270,14 +268,6 @@ public class TradeBillsServiceImpl implements ITradeBillsService {
.collect(Collectors.toMap(TradeBillsTransferOutTradeNo::getId, TradeBillsTransferOutTradeNo::getOutTradeNo));
}
private boolean isTransferTrade(TradeBills tradeBills) {
MoneyTypeEnum moneyTypeEnum = MoneyTypeEnum.getMoneyTypeByUserTypeAndTradeType(tradeBills.getUserType(), tradeBills.getTradeType());
return moneyTypeEnum == MoneyTypeEnum.deposit_rebate
|| moneyTypeEnum == MoneyTypeEnum.buyer_compensatoryPayment
|| moneyTypeEnum == MoneyTypeEnum.seller_goodsPayment
|| moneyTypeEnum == MoneyTypeEnum.seller_compensatoryPayment;
}
public AuthorizeResultRespVO queryAuthorizeInfoByUid(Integer uid) {
if (uid == null || uid <= 0) {
return null;
... ... @@ -315,73 +305,6 @@ public class TradeBillsServiceImpl implements ITradeBillsService {
localUidAlipayAccountCache.invalidate(uid);
}
private List<TradeBillsResp> convertToResp(List<TradeBills> tradeBillsList, Map<Integer, String> outTradeNoMap) {
return tradeBillsList.stream()
.map(item -> {
TradeBillsResp resp = new TradeBillsResp();
resp.setId(item.getId());
resp.setUid(item.getUid());
resp.setMobile(StringUtils.EMPTY);
resp.setAlipayAccount(StringUtils.EMPTY);
resp.setOrderCode(item.getOrderCode());
resp.setPaidOrderCode(buildPaidOrderCode(item, outTradeNoMap));
resp.setAmount(item.getAmount());
resp.setIncomeOutcome(item.getIncomeOutcome());
resp.setCreateTimeStr(buildPaidTime(item));
resp.setOperatorUid(item.getDealUid());
resp.setOperatorName((item.getDealUid() == null || item.getDealUid() == 0) ? "系统" : item.getDealUserName());
resp.setTradeStatus(item.getTradeStatus());
//金额类型
MoneyTypeEnum moneyTypeClass = MoneyTypeEnum.getMoneyTypeByUserTypeAndTradeType(item.getUserType(), item.getTradeType());
if (moneyTypeClass != null) {
resp.setMoneyType(moneyTypeClass.getCode());
resp.setMoneyTypeDesc(moneyTypeClass.getDesc());
resp.setMoneyFlow(moneyTypeClass.getMoneyFlow());
}
//打款失败原因
TradeStatusEnum.codeOf(item.getTradeStatus())
.filter(e -> e.isFail())
.ifPresent(e -> {
String failReason;
if (Objects.nonNull(item.getPayErrorCode())) {
String payErrorCode = item.getPayErrorCode();
AlipayBatchTransferErrorEnum error = AlipayBatchTransferErrorEnum.getByCode(payErrorCode);
failReason = Objects.nonNull(error) ? error.getDesc() : payErrorCode;
} else {
failReason = e.getDesc();
}
resp.setTradeStatusDesc(failReason);
});
return resp;
})
.collect(Collectors.toList());
}
private String buildPaidOrderCode(TradeBills item, Map<Integer, String> outTradeNoMap) {
return Optional.ofNullable(item.getPaidOrderCode())
.filter(e -> e > 0)
.map(Object::toString)
.orElseGet(() -> {
String defaultValue = "-";
if (isTransferTrade(item)) {
return outTradeNoMap.getOrDefault(item.getId(), outTradeNoMap.getOrDefault(item.getDealRelateId(), defaultValue));
} else {
return defaultValue;
}
});
}
private String buildPaidTime(TradeBills item) {
long dealTime = ObjectUtils.defaultIfNull(item.getDealTime(), 0);
if (dealTime == 0) {
dealTime = item.getCreateTime();
}
return DateUtil.long2DateStr(dealTime * 1000, "yyyy-MM-dd HH:mm:ss");
}
private JSONObject asyncCallQueryAliAccountJsonObject(Integer uid) {
AuthorizeInfoReq req = new AuthorizeInfoReq();
req.setUid(uid);
... ...
package com.yoho.ufo.order.service.impl.builder;
import com.google.common.collect.Maps;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.order.model.TradeBills;
import com.yoho.ufo.order.constant.MoneyTypeEnum;
import com.yoho.ufo.order.constant.TradeStatusEnum;
import com.yohobuy.ufo.model.order.common.AlipayBatchTransferErrorEnum;
import com.yohobuy.ufo.model.order.resp.TradeBillsResp;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public class TradeBillsListBuilder {
private final List<TradeBills> tradeBillsList;
private final Map<Integer, String> paidOrderCodeMap;
public TradeBillsListBuilder(List<TradeBills> tradeBillsList) {
this.tradeBillsList = tradeBillsList;
this.paidOrderCodeMap = Maps.newHashMap();
}
public TradeBillsListBuilder transferOutTradeNoMap(Map<Integer, String> map) {
this.paidOrderCodeMap.putAll(map);
return this;
}
public List<TradeBillsResp> build() {
return tradeBillsList.stream()
.map(item -> {
TradeBillsResp resp = new TradeBillsResp();
resp.setId(item.getId());
resp.setUid(item.getUid());
resp.setMobile(StringUtils.EMPTY);
resp.setAlipayAccount(StringUtils.EMPTY);
resp.setOrderCode(item.getOrderCode());
resp.setPaidOrderCode(buildPaidOrderCode(item));
resp.setAmount(item.getAmount());
resp.setIncomeOutcome(item.getIncomeOutcome());
resp.setCreateTimeStr(buildPaidTime(item));
resp.setOperatorUid(item.getDealUid());
resp.setOperatorName(ObjectUtils.defaultIfNull(item.getDealUserName(), "系统"));
resp.setTradeStatus(item.getTradeStatus());
MoneyTypeEnum.from(item).ifPresent(moneyTypeClass -> {
resp.setMoneyType(moneyTypeClass.getCode());
resp.setMoneyTypeDesc(moneyTypeClass.getDesc());
resp.setMoneyFlow(moneyTypeClass.getMoneyFlow());
});
resp.setTradeStatusDesc(buildTradeStatusDesc(item));
return resp;
})
.collect(Collectors.toList());
}
private String buildPaidOrderCode(TradeBills item) {
return Optional.ofNullable(item.getPaidOrderCode())
.filter(e -> e > 0)
.map(Object::toString)
.orElseGet(() -> paidOrderCodeMap.getOrDefault(item.getId(), "-"));
}
private String buildPaidTime(TradeBills item) {
long dealTime = ObjectUtils.defaultIfNull(item.getDealTime(), 0);
if (dealTime == 0) {
dealTime = item.getCreateTime();
}
return DateUtil.long2DateStr(dealTime * 1000, "yyyy-MM-dd HH:mm:ss");
}
private String buildTradeStatusDesc(TradeBills item) {
final StringBuffer desc = new StringBuffer();
TradeStatusEnum.codeOf(item.getTradeStatus())
.filter(e -> e.isFail())
.ifPresent(e -> {
//打款失败原因
String failReason;
if (Objects.nonNull(item.getPayErrorCode())) {
String payErrorCode = item.getPayErrorCode();
AlipayBatchTransferErrorEnum error = AlipayBatchTransferErrorEnum.getByCode(payErrorCode);
failReason = Objects.nonNull(error) ? error.getDesc() : payErrorCode;
} else {
failReason = e.getDesc();
}
desc.append(failReason);
});
return desc.toString();
}
}
... ...