|
|
@@ -0,0 +1,365 @@
|
|
|
+package com.uas.platform.b2c.fa.payment.service.impl;
|
|
|
+
|
|
|
+import com.uas.platform.b2c.common.account.dao.EnterpriseDao;
|
|
|
+import com.uas.platform.b2c.common.account.dao.UserBaseInfoDao;
|
|
|
+import com.uas.platform.b2c.common.account.model.Enterprise;
|
|
|
+import com.uas.platform.b2c.common.account.model.UserBaseInfo;
|
|
|
+import com.uas.platform.b2c.common.base.service.SendMessageService;
|
|
|
+import com.uas.platform.b2c.core.constant.Status;
|
|
|
+import com.uas.platform.b2c.core.constant.Type;
|
|
|
+import com.uas.platform.b2c.core.support.SystemSession;
|
|
|
+import com.uas.platform.b2c.core.utils.NumberUtil;
|
|
|
+import com.uas.platform.b2c.fa.payment.dao.InstallmentDao;
|
|
|
+import com.uas.platform.b2c.fa.payment.dao.InstallmentDetailDao;
|
|
|
+import com.uas.platform.b2c.fa.payment.dao.InstallmentStoreDao;
|
|
|
+import com.uas.platform.b2c.fa.payment.model.*;
|
|
|
+import com.uas.platform.b2c.fa.payment.service.BankInfoService;
|
|
|
+import com.uas.platform.b2c.fa.payment.service.InstallmentService;
|
|
|
+import com.uas.platform.b2c.trade.order.dao.OrderDao;
|
|
|
+import com.uas.platform.b2c.trade.order.dao.PurchaseDao;
|
|
|
+import com.uas.platform.b2c.trade.order.dao.PurchaseDetailDao;
|
|
|
+import com.uas.platform.b2c.trade.order.model.Order;
|
|
|
+import com.uas.platform.b2c.trade.order.model.Purchase;
|
|
|
+import com.uas.platform.b2c.trade.order.model.PurchaseDetail;
|
|
|
+import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
+import com.uas.platform.core.util.mq.MessageType;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 分期支付service实现类
|
|
|
+ * @author wangyc
|
|
|
+ *
|
|
|
+ * @version 2017/9/7 9:07 wangyc
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class InstallmentServiceImpl implements InstallmentService{
|
|
|
+
|
|
|
+ private final InstallmentStoreDao installmentStoreDao;
|
|
|
+
|
|
|
+ private final PurchaseDao purchaseDao;
|
|
|
+
|
|
|
+ private final OrderDao orderDao;
|
|
|
+
|
|
|
+ private final InstallmentDao installmentDao;
|
|
|
+
|
|
|
+ private final BankInfoService bankInfoService;
|
|
|
+
|
|
|
+ private final InstallmentDetailDao installmentDetailDao;
|
|
|
+
|
|
|
+ private final PurchaseDetailDao purchaseDetailDao;
|
|
|
+
|
|
|
+ private final SendMessageService sendMessageService;
|
|
|
+
|
|
|
+ private final EnterpriseDao enterpriseDao;
|
|
|
+
|
|
|
+ private final UserBaseInfoDao userBaseInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public InstallmentServiceImpl(InstallmentStoreDao installmentStoreDao, PurchaseDao purchaseDao, OrderDao orderDao, InstallmentDao installmentDao, BankInfoService bankInfoService, InstallmentDetailDao installmentDetailDao, PurchaseDetailDao purchaseDetailDao, SendMessageService sendMessageService, EnterpriseDao enterpriseDao, UserBaseInfoDao userBaseInfoDao) {
|
|
|
+ this.installmentStoreDao = installmentStoreDao;
|
|
|
+ this.purchaseDao = purchaseDao;
|
|
|
+ this.orderDao = orderDao;
|
|
|
+ this.installmentDao = installmentDao;
|
|
|
+ this.bankInfoService = bankInfoService;
|
|
|
+ this.installmentDetailDao = installmentDetailDao;
|
|
|
+ this.purchaseDetailDao = purchaseDetailDao;
|
|
|
+ this.sendMessageService = sendMessageService;
|
|
|
+ this.enterpriseDao = enterpriseDao;
|
|
|
+ this.userBaseInfoDao = userBaseInfoDao;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Installment createInstallment(Installment installment, Long purchaseId) {
|
|
|
+ // 基础验证
|
|
|
+ validationEnableInstallment(purchaseId);
|
|
|
+
|
|
|
+ Purchase purchase = purchaseDao.findOne(purchaseId);
|
|
|
+
|
|
|
+ // 如果之前具有分期信息,删除分期信息
|
|
|
+ if (purchase.getInstallmentId() != null) {
|
|
|
+ Installment oldInstallment = installmentDao.findOne(purchase.getInstallmentId());
|
|
|
+ if (oldInstallment != null)
|
|
|
+ installmentDao.delete(oldInstallment.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<InstallmentDetail> installmentDetails = installment.getInstallmentDetails();
|
|
|
+ if (CollectionUtils.isEmpty(installmentDetails))
|
|
|
+ throw new IllegalOperatorException("分期详情为空,请您设置分期详情");
|
|
|
+
|
|
|
+ for(InstallmentDetail installmentDetail : installmentDetails) {
|
|
|
+ installmentDetail.setStatus(Status.TOBEPAID.value());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置分期基本信息
|
|
|
+ installment.setSellerenuu(SystemSession.getUser().getEnterprise().getUu());
|
|
|
+ installment.setStatus(Status.TOBEPAID.value());
|
|
|
+ installment.setCount(installmentDetails.size());
|
|
|
+ installment.setCurrentNo((short) 1);
|
|
|
+ installment.setOrderNum(purchase.getOrderid());
|
|
|
+ installment.setPurchaseId(purchaseId);
|
|
|
+
|
|
|
+ Order order = orderDao.findOrderByOrderid(purchase.getOrderid());
|
|
|
+ if (order == null)
|
|
|
+ throw new IllegalOperatorException("此订单不存在,请重新确认信息");
|
|
|
+
|
|
|
+ installment.setOrderId(order.getId());
|
|
|
+ installment.setPrice(purchase.getPrice());
|
|
|
+ installment.setInstallmentDetails(installmentDetails);
|
|
|
+
|
|
|
+ // 保存分期信息
|
|
|
+ installment = installmentDao.save(installment);
|
|
|
+
|
|
|
+ for (InstallmentDetail installmentDetail : installmentDetails) {
|
|
|
+ installmentDetail.setInstallment(installment);
|
|
|
+ installmentDetail.setInstallmentId(installment.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ installmentDetailDao.save(installmentDetails);
|
|
|
+
|
|
|
+ // 订单、采购单保存分期id
|
|
|
+ purchase.setInstallmentId(installment.getId());
|
|
|
+ order.setInstallmentId(installment.getId());
|
|
|
+
|
|
|
+ purchaseDao.save(purchase);
|
|
|
+ orderDao.save(order);
|
|
|
+
|
|
|
+ return installment;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Installment updateInstallment(Installment installment, Long purchaseId) {
|
|
|
+ // 基础验证
|
|
|
+ validationEnableInstallment(purchaseId);
|
|
|
+
|
|
|
+ Purchase purchase = purchaseDao.findOne(purchaseId);
|
|
|
+
|
|
|
+ Set<InstallmentDetail> installmentDetails = installment.getInstallmentDetails();
|
|
|
+ if (CollectionUtils.isEmpty(installmentDetails))
|
|
|
+ throw new IllegalOperatorException("分期详情为空,请您设置分期详情");
|
|
|
+
|
|
|
+ Installment oldInstallment = installmentDao.findByPurchaseId(purchaseId);
|
|
|
+ if (oldInstallment == null)
|
|
|
+ throw new IllegalOperatorException("此订单分期信息不存在,请重新确认信息");
|
|
|
+
|
|
|
+
|
|
|
+// installmentDao.delete(oldInstallment.getId());
|
|
|
+
|
|
|
+ for(InstallmentDetail installmentDetail : installmentDetails) {
|
|
|
+ installmentDetail.setStatus(Status.TOBEPAID.value());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置分期基本信息
|
|
|
+ installment.setSellerenuu(SystemSession.getUser().getEnterprise().getUu());
|
|
|
+ installment.setStatus(Status.TOBEPAID.value());
|
|
|
+ installment.setCount(installmentDetails.size());
|
|
|
+ installment.setCurrentNo((short) 1);
|
|
|
+ installment.setOrderNum(purchase.getOrderid());
|
|
|
+ installment.setPurchaseId(purchaseId);
|
|
|
+
|
|
|
+ Order order = orderDao.findByOrderid(purchase.getOrderid());
|
|
|
+ if (order == null)
|
|
|
+ throw new IllegalOperatorException("此订单不存在,请重新确认信息");
|
|
|
+
|
|
|
+ installment.setOrderId(order.getId());
|
|
|
+ installment.setPrice(purchase.getPrice());
|
|
|
+ installment.setInstallmentDetails(installmentDetails);
|
|
|
+
|
|
|
+ oldInstallment.getInstallmentDetails().clear();
|
|
|
+
|
|
|
+ // 保存分期信息
|
|
|
+ BeanUtils.copyProperties(installment, oldInstallment, "installmentDetails");
|
|
|
+ oldInstallment = installmentDao.save(oldInstallment);
|
|
|
+ for (InstallmentDetail installmentDetail : installment.getInstallmentDetails()) {
|
|
|
+ installmentDetail.setInstallmentId(oldInstallment.getId());
|
|
|
+ }
|
|
|
+ installmentDetailDao.save(installment.getInstallmentDetails());
|
|
|
+
|
|
|
+ return oldInstallment;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Installment paidInstallment(BankTransfer bankTransfer, Long orderId) {
|
|
|
+ // 验证分期明细合法性
|
|
|
+ InstallmentDetail installmentDetail = installmentDetailDao.findOne(bankTransfer.getInstallmentDetailId());
|
|
|
+ if (installmentDetail == null)
|
|
|
+ throw new IllegalOperatorException("该订单分期信息不存在,请重新确认订单");
|
|
|
+ if (!orderId.equals(installmentDetail.getInstallment().getOrderId()))
|
|
|
+ throw new IllegalOperatorException("此分期信息与该订单不符,请重新确认分期信息");
|
|
|
+ if (Status.TOBEPAID.value() != installmentDetail.getStatus().intValue() && Status.PAID.value() != installmentDetail.getStatus().intValue())
|
|
|
+ throw new IllegalOperatorException("此次付款分期不为待付款状态,请重新确认分期信息");
|
|
|
+
|
|
|
+ // 更新分期信息状态为付款中
|
|
|
+ Installment installment = installmentDetail.getInstallment();
|
|
|
+ if (!installment.getCurrentNo().equals(installmentDetail.getDetno())) {
|
|
|
+ throw new IllegalOperatorException("此次付款分期与应付分期信息不符,请重新确认分期信息");
|
|
|
+ }
|
|
|
+ if ((Status.TOBEPAID.value() != installment.getStatus().intValue()) && (Status.PAID.value() != installment.getStatus().intValue())) {
|
|
|
+ throw new IllegalOperatorException("此分期不为待付款状态,请重新确认分期信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ installment.setStatus(com.uas.platform.b2c.core.constant.Status.PAID.value());
|
|
|
+
|
|
|
+ installmentDetail.setPaidDate(new Date());
|
|
|
+ installmentDetail.setImgs(bankTransfer.getImgUrl());
|
|
|
+ installmentDetail.setPricesArr(bankTransfer.getPricesArr());
|
|
|
+ installmentDetail.setStatus(com.uas.platform.b2c.core.constant.Status.PAID.value());
|
|
|
+
|
|
|
+ installmentDetailDao.save(installmentDetail);
|
|
|
+
|
|
|
+ Order order = orderDao.findOne(orderId);
|
|
|
+
|
|
|
+ if (order == null)
|
|
|
+ throw new IllegalOperatorException("订单不存在,请重新确认订单信息");
|
|
|
+ List<Purchase> purchases = purchaseDao.findByOrderid(order.getOrderid());
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(purchases)) {
|
|
|
+ for (Purchase purchase : purchases) {
|
|
|
+ Enterprise enterprise = enterpriseDao.findByUu(purchase.getSellerenuu());
|
|
|
+ if (enterprise == null)
|
|
|
+ throw new IllegalOperatorException("卖家企业不存在,请重新确认");
|
|
|
+ UserBaseInfo admin = userBaseInfoDao.findUserByUserUU(enterprise.getEnAdminuu());
|
|
|
+ if (admin == null)
|
|
|
+ throw new IllegalOperatorException("卖家管理员不存在,请联系卖家");
|
|
|
+ // 发送信息
|
|
|
+ sendMessageService.sendMessageForInstallmentPay(purchase.getSellername(), purchase.getBuyername(), installmentDetail.getPrice(),
|
|
|
+ admin.getUserTel(), MessageType.orderpay);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return installmentDao.save(installment);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteInstallment(Long purchaseId) {
|
|
|
+ validatePurchase(purchaseId);
|
|
|
+ Purchase purchase = purchaseDao.findOne(purchaseId);
|
|
|
+ Order order = orderDao.findByOrderid(purchase.getOrderid());
|
|
|
+
|
|
|
+ Installment installment = installmentDao.findByPurchaseId(purchaseId);
|
|
|
+ if (installment == null)
|
|
|
+ throw new IllegalOperatorException("分期信息不存在,请重新确认分期信息");
|
|
|
+
|
|
|
+ order.setInstallmentId(null);
|
|
|
+ purchase.setInstallmentId(null);
|
|
|
+
|
|
|
+ orderDao.save(order);
|
|
|
+ purchaseDao.save(purchase);
|
|
|
+
|
|
|
+ installmentDao.delete(installment.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Installment updateAfterModifyPurchasePrice(String purchaseDetailId) {
|
|
|
+ PurchaseDetail purchaseDetail = purchaseDetailDao.findByDetailid(purchaseDetailId);
|
|
|
+ if (purchaseDetail == null)
|
|
|
+ throw new IllegalOperatorException("订单改价明细数据不存在,请重新确认订单信息");
|
|
|
+ Purchase purchase = purchaseDetail.getPurchase();
|
|
|
+
|
|
|
+ if (purchase != null && purchase.getInstallmentId() != null)
|
|
|
+ validationEnableInstallment(purchase.getId());
|
|
|
+
|
|
|
+ Installment installment = installmentDao.findByPurchaseId(purchase.getId());
|
|
|
+
|
|
|
+ Double total = purchase.getPrice();
|
|
|
+ Double installTotal = installment.getPrice();
|
|
|
+ Double dvalue = 0d;
|
|
|
+
|
|
|
+ if (NumberUtil.compare(total, installTotal) == 1) {
|
|
|
+ InstallmentDetail installmentDetail = installmentDetailDao.findByInstallmentIdAndDetno(installment.getId(), installment.getCount().shortValue());
|
|
|
+ installmentDetail.setPrice(NumberUtil.pricesScaleTwo(total - installTotal + installmentDetail.getPrice()));
|
|
|
+ installmentDetailDao.save(installmentDetail);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (NumberUtil.compare(total, installTotal) == -1) {
|
|
|
+ for (short i = 1; i < installment.getCount() + 1; i++) {
|
|
|
+ InstallmentDetail installmentDetail = installmentDetailDao.findByInstallmentIdAndDetno(installment.getId(), i);
|
|
|
+ if (installmentDetail == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ dvalue = total - installmentDetail.getPrice();
|
|
|
+
|
|
|
+ if (dvalue <= 0) {
|
|
|
+ installmentDetail.setPrice(NumberUtil.pricesScaleTwo(total));
|
|
|
+ installmentDetailDao.save(installmentDetail);
|
|
|
+ installmentDetailDao.deleteGtDetno(installment.getId(), i);
|
|
|
+
|
|
|
+ Iterator<InstallmentDetail> it = installment.getInstallmentDetails().iterator();
|
|
|
+ if (it.hasNext()) {
|
|
|
+ InstallmentDetail installmentDetail1 = it.next();
|
|
|
+ if (installmentDetail.getDetno() > i) {
|
|
|
+ it.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ total = dvalue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ installment.setPrice(purchase.getPrice());
|
|
|
+ installment.setCount(installment.getInstallmentDetails().size());
|
|
|
+
|
|
|
+ return installmentDao.save(installment);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String validationEnableInstallment(Long purchaseId) {
|
|
|
+ validatePurchase(purchaseId);// 验证采购单、订单信息
|
|
|
+ validateBankInfo(Type.SUP.value(), Status.ALLOW.value());// 验证银行账户信息
|
|
|
+ validateInstallmentStore();// 验证是否设置分期权限
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+ void validateInstallmentStore() {
|
|
|
+ InstallmentStore installmentStore = installmentStoreDao.findByEnuuAndEnable(SystemSession.getUser().getEnterprise().getUu(), (short) 1);
|
|
|
+ if (installmentStore == null)
|
|
|
+ throw new IllegalOperatorException("当前企业没有设置分期功能权限,如有需要请联系客服");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证是否有收款账户
|
|
|
+ * @param type
|
|
|
+ * @param status
|
|
|
+ */
|
|
|
+ void validateBankInfo(Integer type, Integer status) {
|
|
|
+ List<BankInfo> bankInfos = bankInfoService.getEnterpriseBankInfoContainsStatus(type, status);
|
|
|
+ if (CollectionUtils.isEmpty(bankInfos))
|
|
|
+ throw new IllegalOperatorException("当前企业没有设置收款账户,请在 结算中心--收款账户信息 管理您的收款账户信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证采购单、订单信息
|
|
|
+ * @param purchaseId
|
|
|
+ */
|
|
|
+ void validatePurchase(Long purchaseId) {
|
|
|
+ if (SystemSession.getUser().getEnterprise() == null)
|
|
|
+ throw new IllegalOperatorException("当前账户为个人账户,请选择企业后再执行此操作");
|
|
|
+
|
|
|
+ Purchase purchase = purchaseDao.findOne(purchaseId);
|
|
|
+ if (purchase == null)
|
|
|
+ throw new IllegalOperatorException("订单不存在,请重新确认订单信息");
|
|
|
+ if (!SystemSession.getUser().getEnterprise().getUu().equals(purchase.getSellerenuu()))
|
|
|
+ throw new IllegalOperatorException("此订单不属于当前企业,请重新确认订单信息");
|
|
|
+ if (Status.TOBECONFIRMED.value() != purchase.getStatus().intValue())
|
|
|
+ throw new IllegalOperatorException("此订单状态不为待付款,不可修改分期信息");
|
|
|
+
|
|
|
+ Order order = orderDao.findByOrderid(purchase.getOrderid());
|
|
|
+ if (order == null)
|
|
|
+ throw new IllegalOperatorException("订单不存在,请重新确认订单信息");
|
|
|
+ if (Status.TOBEPAID.value() != order.getStatus().intValue() && Status.TOBECONFIRMED.value() != order.getStatus().intValue())
|
|
|
+ throw new IllegalOperatorException("此订单状态不为待付款,不可修改分期信息");
|
|
|
+ }
|
|
|
+}
|