Authored by Lixiaodi

增加人工打款

... ... @@ -23,12 +23,18 @@ public interface TradeBillsMapper {
//根据用户uid,获取用户总支出
List<Map<String, Object>> selectSpendAmountByUid(@Param("uid") Integer uid);
int updateToOkByPrimaryKey(TradeBills record);
int updateToFailByPrimaryKey(TradeBills record);
TradeBills selectByDealRelateId(Integer dealRelateId);
/**
int deleteByPrimaryKey(Integer id);
int insertSelective(TradeBills record);
int updateByPrimaryKeySelective(TradeBills record);
int updateByPrimaryKey(TradeBills record);
**/
... ...
... ... @@ -31,6 +31,13 @@
from trade_bills
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByDealRelateId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from trade_bills
where deal_relate_id = #{dealRelateId,jdbcType=INTEGER}
</select>
<select id="selectCountGoodsIncomeOrCompensateIncomeByUid" resultType="java.lang.Integer" parameterType="java.lang.Integer" >
select
... ... @@ -66,6 +73,60 @@
select trade_type as tradeType,sum(amount) as allAmount from trade_bills
where uid = #{uid} and income_outcome = 2 group by trade_type
</select>
<update id="updateToOkByPrimaryKey" parameterType="com.yohoufo.dal.order.model.TradeBills" >
update trade_bills
<set >
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
<if test="orderCode != null" >
order_code = #{orderCode,jdbcType=BIGINT},
</if>
<if test="userType != null" >
user_type = #{userType,jdbcType=INTEGER},
</if>
<if test="payType != null" >
pay_type = #{payType,jdbcType=INTEGER},
</if>
<if test="tradeType != null" >
trade_type = #{tradeType,jdbcType=INTEGER},
</if>
<if test="incomeOutcome != null" >
income_outcome = #{incomeOutcome,jdbcType=INTEGER},
</if>
<if test="amount != null" >
amount = #{amount,jdbcType=DECIMAL},
</if>
<if test="systemAmount != null" >
system_amount = #{systemAmount,jdbcType=DECIMAL},
</if>
<if test="tradeStatus != null" >
trade_status = #{tradeStatus,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="dealUid != null" >
deal_uid = #{dealUid,jdbcType=INTEGER},
</if>
<if test="dealStatus != null" >
deal_status = #{dealStatus,jdbcType=INTEGER},
</if>
<if test="dealTime != null" >
deal_time = #{dealTime,jdbcType=INTEGER},
</if>
<if test="dealRelateId != null" >
deal_relate_id = #{dealRelateId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER} and trade_status != 100
</update>
<update id="updateToFailByPrimaryKey" parameterType="com.yohoufo.dal.order.model.TradeBills" >
update trade_bills set trade_status = #{tradeStatus,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER} and trade_status = 100
</update>
<!--
... ... @@ -146,42 +207,6 @@
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.order.model.TradeBills" >
update trade_bills
<set >
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
<if test="orderCode != null" >
order_code = #{orderCode,jdbcType=BIGINT},
</if>
<if test="userType != null" >
user_type = #{userType,jdbcType=INTEGER},
</if>
<if test="payType != null" >
pay_type = #{payType,jdbcType=INTEGER},
</if>
<if test="tradeType != null" >
trade_type = #{tradeType,jdbcType=INTEGER},
</if>
<if test="incomeOutcome != null" >
income_outcome = #{incomeOutcome,jdbcType=INTEGER},
</if>
<if test="amount != null" >
amount = #{amount,jdbcType=DECIMAL},
</if>
<if test="systemAmount != null" >
system_amount = #{systemAmount,jdbcType=DECIMAL},
</if>
<if test="tradeStatus != null" >
trade_status = #{tradeStatus,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.order.model.TradeBills" >
update trade_bills
set uid = #{uid,jdbcType=INTEGER},
... ...
... ... @@ -23,7 +23,7 @@ import com.yohoufo.order.constants.RefundContant;
import com.yohoufo.order.model.NotifyResponse;
import com.yohoufo.order.model.PayRefundBo;
import com.yohoufo.order.model.PaymentData;
import com.yohoufo.order.model.request.ManualTransferMoneyRequest;
import com.yohoufo.order.model.request.ManualDealRequest;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.model.response.PaymentConfirmRsp;
... ... @@ -250,14 +250,14 @@ public class PaymentController {
}
@RequestMapping(params = "method=ufo.order.manualTransferMon")
public ApiResponse manualTransferMon(ManualTransferMoneyRequest req) {
@RequestMapping(params = "method=ufo.order.manualDealMon")
public ApiResponse manualDealMon(ManualDealRequest req) {
try {
paymentService.manualTransfer(req);
paymentService.manualDeal(req);
} catch (ServiceException e) {
return new ApiResponse.ApiResponseBuilder().code(e.getCode()).data("").message(e.getErrorMessage()).build();
}
return new ApiResponse.ApiResponseBuilder().code(200).data("").message("转账成功").build();
return new ApiResponse.ApiResponseBuilder().code(200).data("").message("处理成功").build();
}
... ...
... ... @@ -11,11 +11,13 @@ import lombok.experimental.Builder;
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ManualTransferMoneyRequest {
public class ManualDealRequest {
private long buyerOrderCode;
private long sellerOrderCode;
private long orderCode;
private Integer tradeBillsId;
private Integer operateUid;
// '3位场景(卖家不卖了,卖家不卖了(已被购买),鉴定不通过,鉴定通过,交易成功)+2位资金去向(卖家,买家,其他)+2资金类型(货款、保证金、其他)
private int sceneId;
... ...
... ... @@ -6,7 +6,7 @@ import com.yohoufo.order.model.PaymentData;
import com.yohoufo.order.model.PaymentOrderQueryBO;
import com.yohoufo.order.model.request.ManualTransferMoneyRequest;
import com.yohoufo.order.model.request.ManualDealRequest;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.model.response.PaymentConfirmRsp;
... ... @@ -49,5 +49,5 @@ public interface IPaymentService {
void transferMon(TransferMoneyRequest request);
void manualTransfer(ManualTransferMoneyRequest req);
void manualDeal(ManualDealRequest req);
}
... ...
... ... @@ -44,7 +44,7 @@ import com.yohoufo.order.model.OrderInfo;
import com.yohoufo.order.model.PayQueryBo;
import com.yohoufo.order.model.PayRefundBo;
import com.yohoufo.order.model.PaymentData;
import com.yohoufo.order.model.request.ManualTransferMoneyRequest;
import com.yohoufo.order.model.request.ManualDealRequest;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.model.response.PaymentConfirmRsp;
... ... @@ -528,6 +528,7 @@ public class PaymentServiceImpl implements IPaymentService {
logger.warn("transferMonErr transferMon计算费用结果为 null, 不合法的金额");
record.setTradeStatus(202);
addTradeBills(record);
// TODO alarm
throw new ServiceException(400, "计算金额错误!:");
}
transfer.setAmount(transferAmount);
... ... @@ -538,6 +539,7 @@ public class PaymentServiceImpl implements IPaymentService {
logger.warn("transferMonErr transferMon计算费用结果为 {}, 小于0.1", transferAmount);
record.setTradeStatus(202);
addTradeBills(record);
// TODO alarm
throw new ServiceException(400, "不合法的金额:" + transferAmount);
}
... ... @@ -548,7 +550,7 @@ public class PaymentServiceImpl implements IPaymentService {
if (jsonObject == null) {
logger.warn("transferMonErr 转账失败 , orderCode is {}", buyerOrderCode);
transfer.setStatus(3);
throw new ServiceException(500, "转账失败");
throw new ServiceException(500, "转账失败:阿里接口返回null");
}
transfer.setAlipayTradeResult(jsonObject.toJSONString());
// 成功
... ... @@ -563,12 +565,17 @@ public class PaymentServiceImpl implements IPaymentService {
transfer.setStatus(1);
} else {
logger.warn("transferMonErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
throw new ServiceException(500, "转账失败");
throw new ServiceException(500, "转账失败:code或者order_id不是成功状态");
}
} catch (Exception e) {
transfer.setStatus(3);
logger.warn("transferMonErr 转账失败 , orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
throw new ServiceException(500, "转账失败");
transfer.setStatus(3);
// TODO alarm
if(e instanceof ServiceException) {
throw new ServiceException(((ServiceException) e).getCode(), e.getMessage());
} else {
throw new ServiceException(500, "转账失败");
}
} finally {
logger.info("transferMon最后更新状态 status= {}", transfer.getStatus());
transfer.setUpdateTime(now);
... ... @@ -581,55 +588,91 @@ public class PaymentServiceImpl implements IPaymentService {
}
@Override
public synchronized void manualTransfer(ManualTransferMoneyRequest req) {
// 转账
logger.info("manualTransfer开始调用阿里接口参数req={}", req);
long orderCode = req.getBuyerOrderCode();
if (orderCode == 0) {
orderCode = req.getSellerOrderCode();
@Database(ForceMaster = true)
public void manualDeal(ManualDealRequest req) {
logger.info("manualDeal人工打款开始参数req={}", req);
Integer tradeBillsId = req.getTradeBillsId();
if (tradeBillsId == null || tradeBillsId < 1) {
throw new ServiceException(400, "manualDeal:流水Id不合法");
}
if (orderCode == 0) {
orderCode = System.nanoTime();
if (req.getOperateUid() == null || req.getOperateUid() < 1) {
throw new ServiceException(400, "manualDeal:客服uid不合法");
}
String alipayAccount = req.getAlipayAccount();
if (StringUtils.isBlank(alipayAccount)) {
throw new ServiceException(400, "manualTransfer alipayAccount is null");
throw new ServiceException(400, "manualDeal:支付宝账号不合法");
}
TradeBills tradeBills = tradeBillsMapper.selectByPrimaryKey(tradeBillsId);
if (tradeBills == null) {
throw new ServiceException(400, "manualDeal:流水不存在");
}
if (tradeBills.getTradeStatus() == 100) {
throw new ServiceException(400, "manualDeal:该流水不是失败的");
}
// 查询处理过的
if (tradeBillsMapper.selectByDealRelateId(tradeBillsId) != null) {
throw new ServiceException(400, "manualDeal:该流水已经处理过");
}
long orderCode = tradeBills.getOrderCode();
BigDecimal amount = null;
try {
amount = new BigDecimal(req.getAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
} catch (Exception e) {
throw new ServiceException(400, "manualTransfer amount is invalid");
throw new ServiceException(400, "manualDeal:金额不合法");
}
if (amount.compareTo(new BigDecimal("0.1")) < 0) {
throw new ServiceException(400, "manualTransfer amount less than 0.1");
throw new ServiceException(400, "manualDeal:金额小于0.1");
}
if (tradeBills.getAmount().compareTo(amount) != 0) {
throw new ServiceException(400, "manualDeal:金额有误");
}
int now = (int) (System.currentTimeMillis() / 1000);
ManualTransfer transfer = new ManualTransfer();
transfer.setAlipayAccount(alipayAccount);
transfer.setBuyerOrderCode(req.getBuyerOrderCode());
transfer.setSellerOrderCode(req.getSellerOrderCode());
if (req.getBuyerOrderCode() == 0 && req.getSellerOrderCode() == 0) {
transfer.setBuyerOrderCode(orderCode);
int beforeStatus = tradeBills.getTradeStatus();
// 预标记成功
TradeBills preSuccess = new TradeBills();
preSuccess.setId(tradeBills.getId());
preSuccess.setTradeStatus(100);
preSuccess.setDealTime(now);
preSuccess.setDealStatus(1);
preSuccess.setDealUid(req.getOperateUid());
if (tradeBillsMapper.updateToOkByPrimaryKey(preSuccess) == 0) {
throw new ServiceException(400, "manualDeal:流水已经处理过id=" + tradeBills.getId());
}
// '1:买家uid; 2:卖家uid','1:保证金;2:货款;3:补偿款', '1:用户收入; 2:用户支出'
try {
if (/* 退货款 */ (tradeBills.getUserType() == 1 && tradeBills.getTradeType() == 2 && tradeBills.getIncomeOutcome() == 1)
/* 退保证金 */ || (tradeBills.getUserType() == 2 && tradeBills.getTradeType() == 1 && tradeBills.getIncomeOutcome() == 1)) {
logger.warn("manualDeal人工退款 , tradeBills is {}", tradeBills);
PaymentRequest pReq = new PaymentRequest();
pReq.setOrderCode(orderCode);
pReq.setRefundAmount(amount.doubleValue());
refund(pReq);
// 加新流水
tradeBills.setDealRelateId(tradeBills.getId());
tradeBills.setId(null);
tradeBills.setTradeStatus(100);
tradeBills.setCreateTime(now);
tradeBillsMapper.insert(tradeBills);
return;
}
} catch (Exception e) {
logger.warn("manualDealErr 退款失败 , tradeBillsId is {}, msg is {}", tradeBillsId, e.getMessage());
// TODO alarm
preSuccess.setTradeStatus(beforeStatus);
preSuccess.setDealStatus(0);
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
throw new ServiceException(500, "退款失败id=" + tradeBillsId);
}
transfer.setAmount(amount);
transfer.setUid(req.getUid());
transfer.setCreateTime(now);
transfer.setStatus(0);
int transferType = (((req.getSceneId() << 2) | req.getMoneyTo()) << 2) | req.getMoneyType();
transfer.setTransferType(transferType);
manualTransferMapper.insert(transfer);
try {
JSONObject jsonObject = alipayService.transferMoney(Long.toString(orderCode), alipayAccount, amount);
if (jsonObject == null) {
logger.warn("manualTransfer 转账失败 , req is {}", req);
transfer.setStatus(3);
throw new ServiceException(500, "转账失败");
logger.warn("manualDeal 转账失败 , req is {}", req);
throw new ServiceException(500, "转账失败:阿里接口返回null");
}
transfer.setAlipayTradeResult(jsonObject.toJSONString());
logger.info("manualDeal 转账阿里接口返回 {}", jsonObject.toJSONString());
// 成功
Integer code = null;
String orderId = null;
... ... @@ -637,30 +680,43 @@ public class PaymentServiceImpl implements IPaymentService {
&& (code = jsonObject.getInteger("code")) == 10000
&& jsonObject.containsKey("order_id")
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
logger.info("manualTransfer转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
transfer.setAlipayTradeId(orderId);
transfer.setStatus(1);
logger.info("manualDeal转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
try {
// 加新流水
tradeBills.setDealRelateId(tradeBills.getId());
tradeBills.setId(null);
tradeBills.setTradeStatus(100);
tradeBills.setCreateTime(now);
tradeBillsMapper.insert(tradeBills);
} catch (Exception e) {
logger.error("manualDeal转账成功,但是记录结果失败,tradeBillsId is " + tradeBillsId, e);
throw new ServiceException(500, "manualDeal转账成功,但是记录结果失败,流水id=" + tradeBillsId);
}
} else {
logger.warn("manualTransferErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
throw new ServiceException(500, "转账失败");
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
// 上报
throw new ServiceException(500, "转账失败:返回code="+code+",order_id="+orderId);
}
} catch (Exception e) {
transfer.setStatus(3);
logger.warn("manualTransferErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
throw new ServiceException(500, "转账失败id=" + transfer.getId());
logger.warn("manualDealErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
// alarm
preSuccess.setTradeStatus(beforeStatus);
preSuccess.setDealStatus(0);
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
throw new ServiceException(500, "转账失败id=" + tradeBillsId);
} finally {
logger.info("manualTransfer最后更新状态 status= {}", transfer.getStatus());
transfer.setUpdateTime(now);
manualTransferMapper.updateByPrimaryKeySelective(transfer);
logger.info("manualTransfer最后更新状态完成,转账结束, buyerOrderCode is {}!", orderCode);
logger.info("manualDeal转账结束, tradeBillsId is {}!", tradeBillsId);
}
}
private void addTradeBills(TradeBills record) {
try {
tradeBillsMapper.insert(record);
} catch (Exception e) {
logger.error("PayRecordErr记录交易到数据库出错 err={}, rec = {}", e.getMessage(), record);
// TODO alarm
}
}
... ...