|  | @@ -1,22 +1,26 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dao.StudentDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.StudentInstrumentDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.SysConfigDao;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.dto.MaintenancePayDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.StudentInstrument;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.SysUserCashAccount;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.enums.DealStatusEnum;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.enums.GroupType;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.enums.OrderTypeEnum;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.enums.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dal.BaseDAO;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
		
			
				|  |  | +import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 | 
	
		
			
				|  |  | +import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.util.Date;
 | 
	
	
		
			
				|  | @@ -41,7 +45,13 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private StudentPaymentOrderService studentPaymentOrderService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  | -    private StudentDao studentDao;
 | 
	
		
			
				|  |  | +    private ContractService contractService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysUserCashAccountDetailService sysUserCashAccountDetailService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysMessageService sysMessageService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public BaseDAO<Long, StudentInstrument> getDAO() {
 | 
	
	
		
			
				|  | @@ -49,25 +59,24 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public Map pay(Long id, BigDecimal amount, boolean isUseBalance) throws Exception {
 | 
	
		
			
				|  |  | -        StudentInstrument studentInstrument = studentInstrumentDao.get(id);
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public Map pay(MaintenancePayDto maintenancePayDto) throws Exception {
 | 
	
		
			
				|  |  | +        StudentInstrument studentInstrument = studentInstrumentDao.get(maintenancePayDto.getId());
 | 
	
		
			
				|  |  |          if (studentInstrument == null) {
 | 
	
		
			
				|  |  |              throw new BizException("所选乐器不存在,请核查");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          Integer userId = studentInstrument.getStudentId();
 | 
	
		
			
				|  |  |          BigDecimal orderAmount = new BigDecimal(400);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        BigDecimal amount = maintenancePayDto.getAmount(); //扣除余额之前的金额
 | 
	
		
			
				|  |  |          if (amount.compareTo(orderAmount) != 0) {
 | 
	
		
			
				|  |  |              throw new BizException("商品价格不符");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          String channelType = "";
 | 
	
		
			
				|  |  |          Date date = new Date();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          BigDecimal balance = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | -        if (isUseBalance && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +        if (maintenancePayDto.getUseBalance() && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  |              SysUserCashAccount userCashAccount = sysUserCashAccountService.getLocked(userId);
 | 
	
		
			
				|  |  |              if (userCashAccount == null) {
 | 
	
		
			
				|  |  |                  throw new BizException("用户账户找不到");
 | 
	
	
		
			
				|  | @@ -78,6 +87,8 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |                  sysUserCashAccountService.updateBalance(userId, balance.negate(), PlatformCashAccountDetailTypeEnum.PAY_FEE, "乐保购买");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  |          StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          studentPaymentOrder.setPaymentChannel("BALANCE");
 | 
	
	
		
			
				|  | @@ -85,7 +96,7 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |          studentPaymentOrder.setGroupType(GroupType.MAINTENANCE);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setOrderNo(orderNo);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setType(OrderTypeEnum.MAINTENANCE);
 | 
	
		
			
				|  |  | -        studentPaymentOrder.setExpectAmount(amount);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setExpectAmount(orderAmount);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setActualAmount(amount);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setBalancePaymentAmount(balance);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setStatus(DealStatusEnum.ING);
 | 
	
	
		
			
				|  | @@ -134,4 +145,98 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |          studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  |          return payMap;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public Boolean orderCallback(StudentPaymentOrder studentPaymentOrder) {
 | 
	
		
			
				|  |  | +        Date nowDate = new Date();
 | 
	
		
			
				|  |  | +        //更新订单信息
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | +        int updateCount = studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +        if (updateCount <= 0) {
 | 
	
		
			
				|  |  | +            throw new BizException("订单更新失败");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //更新维修单信息
 | 
	
		
			
				|  |  | +        StudentInstrument studentInstrument = get(Long.parseLong(studentPaymentOrder.getMusicGroupId()));
 | 
	
		
			
				|  |  | +        if (studentInstrument == null) {
 | 
	
		
			
				|  |  | +            throw new BizException("乐保信息不存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Integer userId = studentPaymentOrder.getUserId();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<Integer, String> map = new HashMap<>();
 | 
	
		
			
				|  |  | +        map.put(userId, userId.toString());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //支付成功
 | 
	
		
			
				|  |  | +        if (studentPaymentOrder.getStatus() == DealStatusEnum.SUCCESS) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                contractService.transferProduceContract(userId, null);
 | 
	
		
			
				|  |  | +            } catch (Exception e) {
 | 
	
		
			
				|  |  | +                logger.error("产品协议生成失败", e);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            Date startTime = nowDate;
 | 
	
		
			
				|  |  | +            Date endTime = DateUtil.addYears(nowDate, 1);
 | 
	
		
			
				|  |  | +            if (studentInstrument.getEndTime() != null &&
 | 
	
		
			
				|  |  | +                    DateUtil.getLastTimeWithDay(studentInstrument.getEndTime()).compareTo(nowDate) >= 0) {
 | 
	
		
			
				|  |  | +                startTime = studentInstrument.getStartTime();
 | 
	
		
			
				|  |  | +                endTime = DateUtil.addYears(studentInstrument.getStartTime(), 1);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            studentInstrument.setStartTime(startTime);
 | 
	
		
			
				|  |  | +            studentInstrument.setEndTime(endTime);
 | 
	
		
			
				|  |  | +            studentInstrument.setStatus(1);
 | 
	
		
			
				|  |  | +            if (this.update(studentInstrument) <= 0) {
 | 
	
		
			
				|  |  | +                throw new BizException("乐保信息更新失败");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //插入交易明细
 | 
	
		
			
				|  |  | +            BigDecimal amount = studentPaymentOrder.getActualAmount();
 | 
	
		
			
				|  |  | +            if (amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +                SysUserCashAccount cashAccount = sysUserCashAccountService.get(userId);
 | 
	
		
			
				|  |  | +                //充值
 | 
	
		
			
				|  |  | +                SysUserCashAccountDetail rechargeDetail = new SysUserCashAccountDetail();
 | 
	
		
			
				|  |  | +                rechargeDetail.setAmount(amount);
 | 
	
		
			
				|  |  | +                rechargeDetail.setBalance(cashAccount.getBalance().add(amount));
 | 
	
		
			
				|  |  | +                rechargeDetail.setComment("缴费前充值");
 | 
	
		
			
				|  |  | +                rechargeDetail.setCreateTime(nowDate);
 | 
	
		
			
				|  |  | +                rechargeDetail.setStatus(DealStatusEnum.SUCCESS);
 | 
	
		
			
				|  |  | +                rechargeDetail.setTransNo(studentPaymentOrder.getTransNo());
 | 
	
		
			
				|  |  | +                rechargeDetail.setType(PlatformCashAccountDetailTypeEnum.RECHARGE);
 | 
	
		
			
				|  |  | +                rechargeDetail.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | +                rechargeDetail.setUserId(userId);
 | 
	
		
			
				|  |  | +                rechargeDetail.setChannel(studentPaymentOrder.getPaymentChannel());
 | 
	
		
			
				|  |  | +                rechargeDetail.setComAmount(studentPaymentOrder.getComAmount());
 | 
	
		
			
				|  |  | +                rechargeDetail.setPerAmount(studentPaymentOrder.getPerAmount());
 | 
	
		
			
				|  |  | +                sysUserCashAccountDetailService.insert(rechargeDetail);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                //缴费
 | 
	
		
			
				|  |  | +                SysUserCashAccountDetail paymentDetail = new SysUserCashAccountDetail();
 | 
	
		
			
				|  |  | +                paymentDetail.setAmount(amount.negate());
 | 
	
		
			
				|  |  | +                paymentDetail.setBalance(cashAccount.getBalance());
 | 
	
		
			
				|  |  | +                paymentDetail.setComment("乐保购买");
 | 
	
		
			
				|  |  | +                paymentDetail.setCreateTime(nowDate);
 | 
	
		
			
				|  |  | +                paymentDetail.setStatus(DealStatusEnum.SUCCESS);
 | 
	
		
			
				|  |  | +                paymentDetail.setTransNo(studentPaymentOrder.getTransNo());
 | 
	
		
			
				|  |  | +                paymentDetail.setType(PlatformCashAccountDetailTypeEnum.PAY_FEE);
 | 
	
		
			
				|  |  | +                paymentDetail.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | +                paymentDetail.setUserId(userId);
 | 
	
		
			
				|  |  | +                rechargeDetail.setChannel(studentPaymentOrder.getPaymentChannel());
 | 
	
		
			
				|  |  | +                sysUserCashAccountDetailService.insert(paymentDetail);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            return true;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +                sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐保购买支付失败");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SMS_SPORADIC_PAYMENT_FAILED, map, null, 0, "", "STUDENT",
 | 
	
		
			
				|  |  | +                    studentPaymentOrder.getActualAmount(), "乐保购买");
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |