|
|
@@ -16,26 +16,189 @@
|
|
|
*/
|
|
|
package org.springblade.ldt.bills.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.yeepay.yop.sdk.service.common.response.YopResponse;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springblade.common.enums.AppConstant;
|
|
|
+import org.springblade.common.enums.OrderType;
|
|
|
+import org.springblade.common.enums.PaymentScene;
|
|
|
+import org.springblade.common.enums.PaymentType;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
+import org.springblade.core.secure.BladeUser;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springblade.ldt.bills.entity.Bills;
|
|
|
import org.springblade.ldt.bills.entity.PointBills;
|
|
|
-import org.springblade.ldt.bills.vo.PointBillsVO;
|
|
|
import org.springblade.ldt.bills.mapper.PointBillsMapper;
|
|
|
+import org.springblade.ldt.bills.service.IBillsService;
|
|
|
import org.springblade.ldt.bills.service.IPointBillsService;
|
|
|
-import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
+import org.springblade.ldt.bills.vo.PointBillCensusVO;
|
|
|
+import org.springblade.ldt.bills.vo.PointBillsVO;
|
|
|
+import org.springblade.ldt.bills.vo.YeePayAccountBalanceVO;
|
|
|
+import org.springblade.ldt.mall.entity.Mall;
|
|
|
+import org.springblade.ldt.mall.service.IMallService;
|
|
|
+import org.springblade.yeePay.common.YeePayConst;
|
|
|
+import org.springblade.yeePay.common.YeepayApiConstant;
|
|
|
+import org.springblade.yeePay.entity.saas.account.TransferOrderDto;
|
|
|
+import org.springblade.yeePay.service.YeepaySaasService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
- * 服务实现类
|
|
|
+ * 服务实现类
|
|
|
*
|
|
|
* @author BladeX
|
|
|
* @since 2021-09-23
|
|
|
*/
|
|
|
+@AllArgsConstructor
|
|
|
@Service
|
|
|
public class PointBillsServiceImpl extends BaseServiceImpl<PointBillsMapper, PointBills> implements IPointBillsService {
|
|
|
|
|
|
+ private IMallService mallService;
|
|
|
+ private YeepaySaasService yeepaySaasService;
|
|
|
+ private YeePayConst yeePayConst;
|
|
|
+ private IBillsService billsService;
|
|
|
+
|
|
|
@Override
|
|
|
public IPage<PointBillsVO> selectPointBillsPage(IPage<PointBillsVO> page, PointBillsVO pointBills) {
|
|
|
return page.setRecords(baseMapper.selectPointBillsPage(page, pointBills));
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public PointBillCensusVO censusData(BladeUser bladeUser) {
|
|
|
+ Mall mall = mallService.getOne(Wrappers.<Mall>lambdaQuery().select(Mall::getMerchantNo).eq(Mall::getTenantId, bladeUser.getTenantId()));
|
|
|
+
|
|
|
+ PointBillCensusVO result = new PointBillCensusVO();
|
|
|
+ //易宝余额
|
|
|
+ YeePayAccountBalanceVO balanceVO = getYeePayAccountBalance(mall.getMerchantNo());
|
|
|
+ result.setYeePayBalance(balanceVO.getBalance());
|
|
|
+
|
|
|
+ //总金额
|
|
|
+ QueryWrapper<PointBills> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.select("sum(total_price) as totalPrice");
|
|
|
+ result.setAccountsPayable(getData(queryWrapper));
|
|
|
+
|
|
|
+ //已核销
|
|
|
+ QueryWrapper<PointBills> queryYes = new QueryWrapper<>();
|
|
|
+ queryYes.select("sum(total_price) as totalPrice").lambda().eq(PointBills::getIsCheak, "1");
|
|
|
+ result.setHandledAmount(getData(queryYes));
|
|
|
+
|
|
|
+ //未核销
|
|
|
+ QueryWrapper<PointBills> queryNo = new QueryWrapper<>();
|
|
|
+ queryNo.select("sum(total_price) as totalPrice").lambda().eq(PointBills::getIsCheak, "0");
|
|
|
+ result.setUntreatedAmount(getData(queryNo));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getData(QueryWrapper<PointBills> queryWrapper) {
|
|
|
+ return ObjectUtil.isEmpty(getOne(queryWrapper)) ? "0.00" : getOne(queryWrapper).getTotalPrice().toPlainString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean handlePointBills(List<Long> ids, BladeUser bladeUser) {
|
|
|
+ Mall mall = mallService.getOne(Wrappers.<Mall>lambdaQuery().select(Mall::getMerchantNo).eq(Mall::getTenantId, bladeUser.getTenantId()));
|
|
|
+
|
|
|
+ YeePayAccountBalanceVO yeePayAccountBalance = getYeePayAccountBalance(mall.getMerchantNo());
|
|
|
+ if (ObjectUtil.isEmpty(yeePayAccountBalance) || ObjectUtil.isEmpty(yeePayAccountBalance.getBalance())) {
|
|
|
+ throw new ServiceException("余额不足,请充值处理!");
|
|
|
+ }
|
|
|
+ BigDecimal balance = BigDecimal.valueOf(Long.parseLong(yeePayAccountBalance.getBalance()));
|
|
|
+
|
|
|
+ QueryWrapper<PointBills> query = new QueryWrapper<>();
|
|
|
+ query.select("sum(total_price) as totalPrice").lambda()
|
|
|
+ .eq(PointBills::getIsCheak, "0")
|
|
|
+ .in(PointBills::getId, ids);
|
|
|
+
|
|
|
+ PointBills pointBills = getOne(query);
|
|
|
+ if (ObjectUtil.isEmpty(pointBills) && ObjectUtil.isEmpty(pointBills.getTotalPrice())) {
|
|
|
+ throw new ServiceException("未找到记录!");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal amount = pointBills.getTotalPrice();
|
|
|
+ //判断余额是否可以进行核销操作
|
|
|
+ if (balance.subtract(amount).compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ throw new ServiceException("余额不足,请充值处理!");
|
|
|
+ }
|
|
|
+
|
|
|
+ String requestNo = IdUtil.simpleUUID();
|
|
|
+
|
|
|
+ //余额充足调取易宝的转账接口进行转账操作
|
|
|
+ transfer(mall, amount, requestNo);
|
|
|
+
|
|
|
+ //保存转账信息
|
|
|
+ saveBills(bladeUser, amount, requestNo, ids, mall.getId());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存积分核销转账信息
|
|
|
+ */
|
|
|
+ private void saveBills(BladeUser bladeUser, BigDecimal amount, String requestNo, List<Long> ids, long mallId) {
|
|
|
+ Bills bills = new Bills();
|
|
|
+ bills.setPointNum(BigDecimal.ZERO);
|
|
|
+ bills.setFee(BigDecimal.ZERO);
|
|
|
+ bills.setPointFee(BigDecimal.ZERO);
|
|
|
+ bills.setBalanceNum(BigDecimal.ZERO);
|
|
|
+ bills.setPrice(amount);
|
|
|
+ bills.setTotalPrice(amount);
|
|
|
+ bills.setCost(amount);
|
|
|
+ bills.setPayPlugin(PaymentType.YEE_PAY.name());
|
|
|
+ bills.setTitle("积分核销");
|
|
|
+ bills.setPayStatus(AppConstant.BillPayStatus.待付款.name());
|
|
|
+ bills.setPayway(PaymentScene.TRANSFER.name());
|
|
|
+ bills.setCost(BigDecimal.ZERO);
|
|
|
+ bills.setType(OrderType.MALL_CHARGE.name());
|
|
|
+ bills.setDiscount(BigDecimal.ONE);
|
|
|
+ bills.setPayId(bladeUser.getUserId());
|
|
|
+ bills.setThirdOrderId(requestNo);
|
|
|
+ bills.setBillDesc(JSON.toJSONString(ids));
|
|
|
+ bills.setTenantId(bills.getTenantId());
|
|
|
+ bills.setMallId(mallId);
|
|
|
+ Assert.isTrue(billsService.saveOrUpdate(bills), () -> {
|
|
|
+ throw new ServiceException("订单异常!");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 积分核销,向易宝发起商户或商场账户扣费
|
|
|
+ */
|
|
|
+ private void transfer(Mall mall, BigDecimal amount, String requestNo) {
|
|
|
+ TransferOrderDto transferOrderDto = TransferOrderDto.builder()
|
|
|
+ .fromMerchantNo(mall.getMerchantNo())
|
|
|
+ .toMerchantNo(yeePayConst.getParentMerchantNo())
|
|
|
+ .orderAmount(amount.toPlainString())
|
|
|
+ .requestNo(requestNo)
|
|
|
+ .usage("积分交易核销!")
|
|
|
+ .feeChargeSide(YeepayApiConstant.feeChargeSide.INSIDE)
|
|
|
+ .notifyUrl(yeePayConst.getServiceUrl() + "/payment/callback/" + OrderType.MALL_CHARGE)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ YopResponse yopResponse = yeepaySaasService.transferOrder(transferOrderDto);
|
|
|
+ JSONObject res = JSON.parseObject(yopResponse.getStringResult());
|
|
|
+ if (!Objects.equals(res.getString("returnCode"), "UA00000")) {
|
|
|
+ throw new ServiceException(res.getString("returnMsg"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取易宝余额信息
|
|
|
+ */
|
|
|
+ private YeePayAccountBalanceVO getYeePayAccountBalance(String merchantNo) {
|
|
|
+ YeePayAccountBalanceVO balanceVO = new YeePayAccountBalanceVO();
|
|
|
+ YopResponse yopResponse = yeepaySaasService.accountBalanceQuery(merchantNo);
|
|
|
+ if (ObjectUtil.isNotEmpty(yopResponse)) {
|
|
|
+ balanceVO = JSON.parseObject(yopResponse.getStringResult(), YeePayAccountBalanceVO.class);
|
|
|
+ }
|
|
|
+ return balanceVO;
|
|
|
+ }
|
|
|
}
|