|  | @@ -1,10 +1,12 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | -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.entity.*;
 | 
	
		
			
				|  |  | +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.entity.SysUserCashAccountDetail;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dal.BaseDAO;
 | 
	
	
		
			
				|  | @@ -12,20 +14,18 @@ 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 org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | -import org.slf4j.Logger;
 | 
	
		
			
				|  |  | -import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | -import org.springframework.transaction.annotation.Propagation;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.util.Date;
 | 
	
		
			
				|  |  |  import java.util.HashMap;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  | -import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentInstrument> implements StudentInstrumentService {
 | 
	
	
		
			
				|  | @@ -48,6 +48,8 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |      private ContractService contractService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private SysUserCashAccountDetailService sysUserCashAccountDetailService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysMessageService sysMessageService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private final Logger logger = LoggerFactory.getLogger(this.getClass());
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -58,25 +60,23 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -    public Map pay(Long id, BigDecimal amount, boolean isUseBalance) throws Exception {
 | 
	
		
			
				|  |  | -        StudentInstrument studentInstrument = studentInstrumentDao.get(id);
 | 
	
		
			
				|  |  | +    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("用户账户找不到");
 | 
	
	
		
			
				|  | @@ -87,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");
 | 
	
	
		
			
				|  | @@ -94,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);
 | 
	
	
		
			
				|  | @@ -166,68 +168,73 @@ public class StudentInstrumentServiceImpl extends BaseServiceImpl<Long, StudentI
 | 
	
		
			
				|  |  |          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);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            repairInfo.setPayStatus(2);
 | 
	
		
			
				|  |  | -            repairInfo.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | -            if (this.update(repairInfo) <= 0) {
 | 
	
		
			
				|  |  | -                throw new BizException("维修单更新失败");
 | 
	
		
			
				|  |  | +            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();
 | 
	
		
			
				|  |  | -            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);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            sysMessageService.sendNoAuthPrivateMessage(repairInfo.getEmployeeId().toString(), userId.toString(), imContent);
 | 
	
		
			
				|  |  | +            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) {
 | 
	
		
			
				|  |  | -            repairInfo.setPayStatus(0);
 | 
	
		
			
				|  |  | -            repairInfo.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | -            if (this.update(repairInfo) <= 0) {
 | 
	
		
			
				|  |  | -                throw new BizException("维修单更新失败");
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | -                sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐器维修支付失败");
 | 
	
		
			
				|  |  | +                sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐保购买支付失败");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SMS_SPORADIC_PAYMENT_FAILED, map, null, 0, "", "STUDENT",
 | 
	
		
			
				|  |  | -                    studentPaymentOrder.getActualAmount(), "乐器维修");
 | 
	
		
			
				|  |  | +                    studentPaymentOrder.getActualAmount(), "乐保购买");
 | 
	
		
			
				|  |  |              return false;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return false;
 |