|  |  |  | 
|---|
|  |  |  | package com.ruoyi.system.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.ruoyi.common.basic.PageInfo; | 
|---|
|  |  |  | import com.ruoyi.common.core.domain.AjaxResult; | 
|---|
|  |  |  | import com.ruoyi.common.core.domain.entity.SysRole; | 
|---|
|  |  |  | import com.ruoyi.common.constant.AmountConstant; | 
|---|
|  |  |  | import com.ruoyi.common.constant.CacheConstants; | 
|---|
|  |  |  | import com.ruoyi.common.core.redis.RedisCache; | 
|---|
|  |  |  | import com.ruoyi.common.enums.DisabledEnum; | 
|---|
|  |  |  | import com.ruoyi.common.exception.ServiceException; | 
|---|
|  |  |  | import com.ruoyi.common.utils.DateUtils; | 
|---|
|  |  |  | import com.ruoyi.common.utils.OrderNos; | 
|---|
|  |  |  | import com.ruoyi.common.utils.SmsUtil; | 
|---|
|  |  |  | import com.ruoyi.common.utils.StringUtils; | 
|---|
|  |  |  | import com.ruoyi.common.utils.TencentMailUtil; | 
|---|
|  |  |  | import com.ruoyi.common.utils.uuid.UUID; | 
|---|
|  |  |  | import com.ruoyi.system.dto.BillStatisticsDto; | 
|---|
|  |  |  | import com.ruoyi.system.dto.CachPayDto; | 
|---|
|  |  |  | import com.ruoyi.system.dto.OfflinePayCheckDto; | 
|---|
|  |  |  | import com.ruoyi.system.dto.SmsByBillDto; | 
|---|
|  |  |  | import com.ruoyi.system.dto.TBillDto; | 
|---|
|  |  |  | import com.ruoyi.system.dto.TbillSaveDto; | 
|---|
|  |  |  | import com.ruoyi.system.mapper.TBillMapper; | 
|---|
|  |  |  | import com.ruoyi.system.model.*; | 
|---|
|  |  |  | import com.ruoyi.system.model.TBankFlow; | 
|---|
|  |  |  | import com.ruoyi.system.model.TBill; | 
|---|
|  |  |  | import com.ruoyi.system.model.TBillDetail; | 
|---|
|  |  |  | import com.ruoyi.system.model.TFlowManagement; | 
|---|
|  |  |  | import com.ruoyi.system.model.TInvoiceToBill; | 
|---|
|  |  |  | import com.ruoyi.system.model.TOrderBill; | 
|---|
|  |  |  | import com.ruoyi.system.model.TPayOrder; | 
|---|
|  |  |  | import com.ruoyi.system.query.TBillQuery; | 
|---|
|  |  |  | import com.ruoyi.system.service.*; | 
|---|
|  |  |  | import com.ruoyi.system.query.TInvoiceToBillQuery; | 
|---|
|  |  |  | import com.ruoyi.system.service.TBankFlowService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TBillConfirmService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TBillDetailService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TBillService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TFlowManagementService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TInvoiceToBillService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TOrderBillService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TPayOrderService; | 
|---|
|  |  |  | import com.ruoyi.system.vo.ScreenRentRankVO; | 
|---|
|  |  |  | import com.taxi591.bankapi.dto.ChargeBillRequest; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.jetbrains.annotations.NotNull; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import javax.validation.constraints.NotEmpty; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Comparator; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.function.Consumer; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  | import java.util.stream.Stream; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * <p> | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | TOrderBillService orderBillService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | TInvoiceToBillService tInvoiceToBillService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | SmsUtil smsUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | TencentMailUtil mailUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public PageInfo<TBillDto> queryPage(TBillQuery query){ | 
|---|
|  |  |  | PageInfo<TBill> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (isok){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | TBill save = new TBill(); | 
|---|
|  |  |  | save.setId(tBill.getId()); | 
|---|
|  |  |  | TBill presist = getById(tBill.getId()); | 
|---|
|  |  |  | //如果账单是已缴费状态,本方法不再进行更新账单 | 
|---|
|  |  |  | if (presist.getPayFeesStatus().equals("3")){ | 
|---|
|  |  |  | 
|---|
|  |  |  | BigDecimal result = presist.getPayFeesMoney().add(tBill.getPayFeesMoney()); | 
|---|
|  |  |  | save.setPayFeesMoney(result); | 
|---|
|  |  |  | //缴费后的欠费 =(应缴费+违约金)-已缴费金额 | 
|---|
|  |  |  | BigDecimal outstand = save.getPayableFeesMoney() | 
|---|
|  |  |  | BigDecimal outstand = presist.getPayableFeesMoney() | 
|---|
|  |  |  | .add(presist.getPayableFeesPenalty()) | 
|---|
|  |  |  | .subtract(save.getPayFeesMoney()); | 
|---|
|  |  |  | save.setOutstandingMoney(outstand); | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void saveBill(TbillSaveDto bill) { | 
|---|
|  |  |  | bill.setManualAddition(DisabledEnum.YES.getCode()); | 
|---|
|  |  |  | save(bill); | 
|---|
|  |  |  | if (bill.getBillType().equals("3")){ | 
|---|
|  |  |  | if (bill.getDetails()==null || bill.getDetails().size()==0){ | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public boolean checkOfflinePay(OfflinePayCheckDto dto) { | 
|---|
|  |  |  | List<TBill> tBills = listByIds(dto.getBillIds()); | 
|---|
|  |  |  | TBillConfirm confirm = tBillConfirmService.getById(dto.getConfirmId()); | 
|---|
|  |  |  | lockAndUpdateByAmountBatch(tBills,dto.getAmount(),(bill)->{ | 
|---|
|  |  |  | TBillDto bill = getDetailByBillId(dto.getBillId()); | 
|---|
|  |  |  | if (dto.getPayType()==1){ //银行 | 
|---|
|  |  |  | if (StringUtils.isEmpty(dto.getFlowId())){ | 
|---|
|  |  |  | throw new ServiceException("银行流水ID不能为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | TBankFlow bankflow = tBankFlowService.getById(dto.getFlowId()); | 
|---|
|  |  |  | if (bankflow.getRemainingMoney().compareTo(BigDecimal.ZERO)<=0){ | 
|---|
|  |  |  | throw new ServiceException("该流水已无可抵扣剩余金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (bankflow.getRemainingMoney().compareTo(dto.getAmount())<0){ | 
|---|
|  |  |  | throw new ServiceException("实付金额不能高于于流水可抵扣剩余金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //如果实付金额大于欠费金额 | 
|---|
|  |  |  | if (dto.getAmount().compareTo(bill.getOutstandingMoney())>0){ | 
|---|
|  |  |  | throw new ServiceException("实付金额不能高于该账单欠费金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | TBill billSave = new TBill(); | 
|---|
|  |  |  | billSave.setId(bill.getId()); | 
|---|
|  |  |  | billSave.setPayFeesMoney(dto.getAmount()); | 
|---|
|  |  |  | billSave.setBankSerialNumber(bankflow.getBankSerialNumber()); | 
|---|
|  |  |  | billSave.setPayFeesTime(bankflow.getPayTime()); | 
|---|
|  |  |  | billSave.setVoucher(dto.getVoucher()); | 
|---|
|  |  |  | billSave.setPayFeesType(2); | 
|---|
|  |  |  | TBill back = lockAndUpdateInfo(billSave, 2); | 
|---|
|  |  |  | TBankFlow saveBankFlow = new TBankFlow(); | 
|---|
|  |  |  | saveBankFlow.setId(bankflow.getId()); | 
|---|
|  |  |  | saveBankFlow.setDeductionMoney(bankflow.getDeductionMoney().add(dto.getAmount())); | 
|---|
|  |  |  | BigDecimal subtract = bankflow.getRemainingMoney().subtract(dto.getAmount()); | 
|---|
|  |  |  | saveBankFlow.setRemainingMoney(subtract); | 
|---|
|  |  |  | if (BigDecimal.ZERO.compareTo(subtract) == 0){ | 
|---|
|  |  |  | saveBankFlow.setFlowStatus(1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | tBankFlowService.updateById(saveBankFlow); | 
|---|
|  |  |  | //更新银行流水的已抵扣金额和剩余可抵扣金额 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //存流水 | 
|---|
|  |  |  | TFlowManagement save = new TFlowManagement(); | 
|---|
|  |  |  | save.setPayType(3); | 
|---|
|  |  |  | save.setPayer(dto.getPayer()); | 
|---|
|  |  |  | save.setPayTime(dto.getPayTime()==null?DateUtils.dateToLocalDateTime(confirm.getPayTime()):dto.getPayTime()); | 
|---|
|  |  |  | save.setBankSerialNumber(dto.getBankSerilNum()); | 
|---|
|  |  |  | save.setBusinessDeptId(bill.getBusinessDeptId()); | 
|---|
|  |  |  | save.setPayTime(bankflow.getPayTime()); | 
|---|
|  |  |  | save.setSysSerialNumber(OrderNos.getDid(30)); | 
|---|
|  |  |  | save.setBankSerialNumber(bankflow.getBankSerialNumber()); | 
|---|
|  |  |  | save.setFlowType(2); | 
|---|
|  |  |  | save.setPaymentBillId(bill.getId()); | 
|---|
|  |  |  | save.setDeductionMoney(bill.getDeductionMoney()); | 
|---|
|  |  |  | save.setPaymentBillId(back.getId()); | 
|---|
|  |  |  | save.setDeductionMoney(back.getDeductionMoney()); | 
|---|
|  |  |  | save.setFlowMoney(dto.getAmount()); | 
|---|
|  |  |  | save.setRemainingMoney(bill.getOutstandingMoney()); | 
|---|
|  |  |  | save.setPreOutstand(bill.getPreOutstand()); | 
|---|
|  |  |  | save.setRemainingMoney(back.getOutstandingMoney()); | 
|---|
|  |  |  | save.setPreOutstand(back.getPreOutstand()); | 
|---|
|  |  |  | tFlowManagementService.save(save); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | TBankFlow bankFlow = new TBankFlow(); | 
|---|
|  |  |  | bankFlow.setPayType(3); | 
|---|
|  |  |  | bankFlow.setPayer(dto.getPayer()); | 
|---|
|  |  |  | bankFlow.setPayTime(dto.getPayTime()==null?DateUtils.dateToLocalDateTime(confirm.getPayTime()):dto.getPayTime()); | 
|---|
|  |  |  | bankFlow.setBankSerialNumber(dto.getBankSerilNum()); | 
|---|
|  |  |  | bankFlow.setFlowMoney(dto.getAmount()); | 
|---|
|  |  |  | bankFlow.setFlowStatus(1); | 
|---|
|  |  |  | tBankFlowService.save(bankFlow); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //现金支付 | 
|---|
|  |  |  | TBill billSave = new TBill(); | 
|---|
|  |  |  | billSave.setId(bill.getId()); | 
|---|
|  |  |  | billSave.setPayFeesMoney(dto.getAmount()); | 
|---|
|  |  |  | billSave.setPayFeesTime(dto.getPayTime()!=null?dto.getPayTime():DateUtils.dateToLocalDateTime(new Date())); | 
|---|
|  |  |  | billSave.setVoucher(dto.getVoucher()); | 
|---|
|  |  |  | billSave.setPayFeesType(2); | 
|---|
|  |  |  | TBill back = lockAndUpdateInfo(billSave, 2); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TFlowManagement save = new TFlowManagement(); | 
|---|
|  |  |  | save.setPayType(3); | 
|---|
|  |  |  | save.setPayer(dto.getPayer()); | 
|---|
|  |  |  | save.setPayTime(billSave.getPayFeesTime()); | 
|---|
|  |  |  | save.setSysSerialNumber(OrderNos.getDid(30)); | 
|---|
|  |  |  | save.setFlowType(1); | 
|---|
|  |  |  | save.setPaymentBillId(back.getId()); | 
|---|
|  |  |  | save.setDeductionMoney(back.getDeductionMoney()); | 
|---|
|  |  |  | save.setFlowMoney(dto.getAmount()); | 
|---|
|  |  |  | save.setRemainingMoney(back.getOutstandingMoney()); | 
|---|
|  |  |  | save.setPreOutstand(back.getPreOutstand()); | 
|---|
|  |  |  | tFlowManagementService.save(save); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | String uuid = UUID.fastUUID().toString(); | 
|---|
|  |  |  | boolean lock = redisCache.trylockLoop(CacheConstants.COMPLETE_PAY_LOCK_KEY + orderNo, uuid, 60); | 
|---|
|  |  |  | if (lock){ | 
|---|
|  |  |  | TPayOrder order = tPayOrderService.getById(orderNo); | 
|---|
|  |  |  | if (order==null){ | 
|---|
|  |  |  | throw new ServiceException("订单不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isNotEmpty(order.getPayNo())){ | 
|---|
|  |  |  | log.info("订单号已处理:{}",orderNo); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 更新订单状态 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | TPayOrder save = new TPayOrder(); | 
|---|
|  |  |  | save.setId(order.getId()); | 
|---|
|  |  |  | save.setStatus(1); | 
|---|
|  |  |  | save.setPayNo(billRequest.getMessage().getInfo().getTraceNo()); | 
|---|
|  |  |  | save.setPayType(billRequest.getMessage().getHead().getChannel()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | save.setPayTime(DateUtils.parseDate(billRequest.getMessage().getHead().getTimeStamp(),"yyyyMMddHHmmssSSS")); | 
|---|
|  |  |  | } catch (ParseException e) { | 
|---|
|  |  |  | throw new ServiceException("日期格式化错误"); | 
|---|
|  |  |  | TPayOrder order = tPayOrderService.getById(orderNo); | 
|---|
|  |  |  | if (order==null){ | 
|---|
|  |  |  | throw new ServiceException("订单不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isNotEmpty(order.getPayNo())){ | 
|---|
|  |  |  | log.info("订单号已处理:{}",orderNo); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 更新订单状态 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | TPayOrder save = new TPayOrder(); | 
|---|
|  |  |  | save.setId(order.getId()); | 
|---|
|  |  |  | save.setStatus(1); | 
|---|
|  |  |  | save.setPayNo(billRequest.getMessage().getInfo().getTraceNo()); | 
|---|
|  |  |  | save.setPayType(billRequest.getMessage().getHead().getChannel()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | save.setPayTime(DateUtils.parseDate(billRequest.getMessage().getHead().getTimeStamp(),"yyyyMMddHHmmssSSS")); | 
|---|
|  |  |  | } catch (ParseException e) { | 
|---|
|  |  |  | throw new ServiceException("日期格式化错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | save.setCallbackTime(new Date()); | 
|---|
|  |  |  | BigDecimal payAmount = new BigDecimal(billRequest.getMessage().getInfo().getPayBillAmt()); | 
|---|
|  |  |  | save.setActPayAmount(payAmount | 
|---|
|  |  |  | .multiply(AmountConstant.b100).longValue()); | 
|---|
|  |  |  | save.setStatus(1); | 
|---|
|  |  |  | save.setPayInfo(billRequest.getMessage().toString()); | 
|---|
|  |  |  | tPayOrderService.updateById(save); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 更新账单状态 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | List<TOrderBill> orderBills = orderBillService.getByOrderNo(order.getId()); | 
|---|
|  |  |  | List<TBill> bills = orderBills.stream().map(ob -> getById(ob.getBillId())).collect(Collectors.toList()); | 
|---|
|  |  |  | lockAndUpdateByAmountBatch(bills,payAmount,(bill)->{ | 
|---|
|  |  |  | TFlowManagement saveFlow = new TFlowManagement(); | 
|---|
|  |  |  | saveFlow.setPayType(1); | 
|---|
|  |  |  | saveFlow.setPayer(order.getUserId()); | 
|---|
|  |  |  | saveFlow.setBusinessDeptId(bill.getBusinessDeptId()); | 
|---|
|  |  |  | saveFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime())); | 
|---|
|  |  |  | saveFlow.setSysSerialNumber(OrderNos.getDid(30)); | 
|---|
|  |  |  | saveFlow.setBankSerialNumber(save.getPayNo()); | 
|---|
|  |  |  | saveFlow.setFlowType(2); | 
|---|
|  |  |  | saveFlow.setPaymentBillId(bill.getId()); | 
|---|
|  |  |  | saveFlow.setDeductionMoney(bill.getDeductionMoney()); | 
|---|
|  |  |  | saveFlow.setFlowMoney(payAmount); | 
|---|
|  |  |  | saveFlow.setRemainingMoney(bill.getOutstandingMoney()); | 
|---|
|  |  |  | saveFlow.setPreOutstand(bill.getPreOutstand()); | 
|---|
|  |  |  | tFlowManagementService.save(saveFlow); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | //                TBankFlow bankFlow = new TBankFlow(); | 
|---|
|  |  |  | //                bankFlow.setPayType(1); | 
|---|
|  |  |  | //                bankFlow.setPayer(order.getUserId()); | 
|---|
|  |  |  | //                bankFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime())); | 
|---|
|  |  |  | //                bankFlow.setBankSerialNumber(save.getPayNo()); | 
|---|
|  |  |  | //                bankFlow.setFlowMoney(payAmount); | 
|---|
|  |  |  | //                bankFlow.setFlowStatus(1); | 
|---|
|  |  |  | //                tBankFlowService.save(bankFlow); | 
|---|
|  |  |  | }finally { | 
|---|
|  |  |  | redisCache.unlock(CacheConstants.COMPLETE_PAY_LOCK_KEY + orderNo,uuid); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | save.setCallbackTime(new Date()); | 
|---|
|  |  |  | BigDecimal payAmount = new BigDecimal(billRequest.getMessage().getInfo().getPayBillAmt()); | 
|---|
|  |  |  | save.setActPayAmount(payAmount | 
|---|
|  |  |  | .multiply(AmountConstant.b100).longValue()); | 
|---|
|  |  |  | save.setStatus(1); | 
|---|
|  |  |  | save.setPayInfo(billRequest.getMessage().toString()); | 
|---|
|  |  |  | tPayOrderService.updateById(save); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 更新账单状态 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | List<TOrderBill> orderBills = orderBillService.getByOrderNo(order.getId()); | 
|---|
|  |  |  | List<TBill> bills = orderBills.stream().map(ob -> getById(ob.getBillId())).collect(Collectors.toList()); | 
|---|
|  |  |  | lockAndUpdateByAmountBatch(bills,payAmount,(bill)->{ | 
|---|
|  |  |  | TFlowManagement saveFlow = new TFlowManagement(); | 
|---|
|  |  |  | saveFlow.setPayType(1); | 
|---|
|  |  |  | saveFlow.setPayer(order.getUserId()); | 
|---|
|  |  |  | saveFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime())); | 
|---|
|  |  |  | saveFlow.setBankSerialNumber(save.getPayNo()); | 
|---|
|  |  |  | saveFlow.setFlowType(2); | 
|---|
|  |  |  | saveFlow.setPaymentBillId(bill.getId()); | 
|---|
|  |  |  | saveFlow.setDeductionMoney(bill.getDeductionMoney()); | 
|---|
|  |  |  | saveFlow.setFlowMoney(payAmount); | 
|---|
|  |  |  | saveFlow.setRemainingMoney(bill.getOutstandingMoney()); | 
|---|
|  |  |  | saveFlow.setPreOutstand(bill.getPreOutstand()); | 
|---|
|  |  |  | tFlowManagementService.save(saveFlow); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | TBankFlow bankFlow = new TBankFlow(); | 
|---|
|  |  |  | bankFlow.setPayType(1); | 
|---|
|  |  |  | bankFlow.setPayer(order.getUserId()); | 
|---|
|  |  |  | bankFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime())); | 
|---|
|  |  |  | bankFlow.setBankSerialNumber(save.getPayNo()); | 
|---|
|  |  |  | bankFlow.setFlowMoney(payAmount); | 
|---|
|  |  |  | bankFlow.setFlowStatus(1); | 
|---|
|  |  |  | tBankFlowService.save(bankFlow); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据发票编号查询账单列表 | 
|---|
|  |  |  | * @param invoiceId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageInfo<TBillDto> getBillByInvoiceId(String invoiceId){ | 
|---|
|  |  |  | PageInfo<TBillDto> pageInfo = new PageInfo<>(); | 
|---|
|  |  |  | ArrayList<TBillDto> bills = new ArrayList<>(); | 
|---|
|  |  |  | TInvoiceToBillQuery query = new TInvoiceToBillQuery(); | 
|---|
|  |  |  | query.setInvoiceId(invoiceId); | 
|---|
|  |  |  | List<TInvoiceToBill> tInvoiceToBills = tInvoiceToBillService.makeQuery(query); | 
|---|
|  |  |  | for (TInvoiceToBill tInvoiceToBill : tInvoiceToBills) { | 
|---|
|  |  |  | TBill bill = getById(tInvoiceToBill.getBillId()); | 
|---|
|  |  |  | if (bill != null && bill.getId() != null){ | 
|---|
|  |  |  | TBillDto detailByBillId = getDetailByBillId(bill.getId()); | 
|---|
|  |  |  | bills.add(detailByBillId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | pageInfo.setRecords(bills); | 
|---|
|  |  |  | return pageInfo; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer sendSmsByBillIds(SmsByBillDto dto) { | 
|---|
|  |  |  | int failNum = 0; | 
|---|
|  |  |  | for (String billId : dto.getBillIds()) { | 
|---|
|  |  |  | TBillDto bill = getDetailByBillId(billId); | 
|---|
|  |  |  | if (bill.getSmsLastTime()!=null && bill.getSmsStatus()==1 | 
|---|
|  |  |  | && (System.currentTimeMillis()-bill.getSmsLastTime().getTime()<smsUtil.getPro().getBillSmsDelayPeriod()*60*1000L)){ | 
|---|
|  |  |  | throw new ServiceException("有账单最近一次发送的时间是:"+DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,bill.getSmsLastTime())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isEmpty(bill.getPhone())){ | 
|---|
|  |  |  | failNum++; | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | TBill save = new TBill(); | 
|---|
|  |  |  | save.setId(bill.getId()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String name = bill.getPartyTwoName().length()>5?bill.getPartyTwoName().substring(0,5):bill.getPartyTwoName(); | 
|---|
|  |  |  | smsUtil.sendSms(bill.getPhone(), "2365726", new String[]{name}); | 
|---|
|  |  |  | save.setSmsStatus(1); | 
|---|
|  |  |  | }catch (ServiceException e){ | 
|---|
|  |  |  | failNum++; | 
|---|
|  |  |  | save.setSmsStatus(2); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | save.setSmsLastTime(new Date()); | 
|---|
|  |  |  | save.setSmsSendUserid(dto.getSendUserId()); | 
|---|
|  |  |  | lockAndUpdateInfo(save,1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return failNum; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer sendMailBatchByBillIds(SmsByBillDto dto) { | 
|---|
|  |  |  | int failNum = 0; | 
|---|
|  |  |  | for (String billId : dto.getBillIds()) { | 
|---|
|  |  |  | TBillDto bill = getDetailByBillId(billId); | 
|---|
|  |  |  | if (bill.getMailLastTime()!=null && bill.getMailStatus()==1 | 
|---|
|  |  |  | && (System.currentTimeMillis()-bill.getMailLastTime().getTime()<mailUtil.getPro().getBillMailDelayPeriod()*60*1000L)){ | 
|---|
|  |  |  | throw new ServiceException("有账单最近一次发送的时间是:"+DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,bill.getMailLastTime())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isEmpty(bill.getEmail())){ | 
|---|
|  |  |  | failNum++; | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | TBill save = new TBill(); | 
|---|
|  |  |  | save.setId(bill.getId()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | mailUtil.send(bill.getEmail(),bill.getHouseName()); | 
|---|
|  |  |  | save.setMailStatus(1); | 
|---|
|  |  |  | }catch (ServiceException e){ | 
|---|
|  |  |  | failNum++; | 
|---|
|  |  |  | save.setMailStatus(2); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | save.setMailLastTime(new Date()); | 
|---|
|  |  |  | save.setMailSendUserid(dto.getSendUserId()); | 
|---|
|  |  |  | lockAndUpdateInfo(save,1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return failNum; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public TBillDto getDetailByBillId(@NotEmpty String billId) { | 
|---|
|  |  |  | return getBaseMapper().selectDetailByBillId(billId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 收款、类型可能是现金、银行 | 
|---|
|  |  |  | * @param dto | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Boolean cashPay(CachPayDto dto) { | 
|---|
|  |  |  | TBill back = null; | 
|---|
|  |  |  | TBankFlow bankflow = null; | 
|---|
|  |  |  | if (dto.getPayType()==1){ | 
|---|
|  |  |  | if (StringUtils.isEmpty(dto.getFlowId())){ | 
|---|
|  |  |  | throw new ServiceException("银行流水不能为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | bankflow = tBankFlowService.getById(dto.getFlowId()); | 
|---|
|  |  |  | TBillDto bill = getDetailByBillId(dto.getBillId()); | 
|---|
|  |  |  | if (bankflow.getRemainingMoney().compareTo(BigDecimal.ZERO)<=0){ | 
|---|
|  |  |  | throw new ServiceException("该流水已无可抵扣剩余金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (bankflow.getRemainingMoney().compareTo(dto.getAmount())<0){ | 
|---|
|  |  |  | throw new ServiceException("实付金额不能高于于流水可抵扣剩余金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //如果实付金额大于欠费金额 | 
|---|
|  |  |  | if (dto.getAmount().compareTo(bill.getOutstandingMoney())>0){ | 
|---|
|  |  |  | throw new ServiceException("实付金额不能高于该账单欠费金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | TBillDto bill = getDetailByBillId(dto.getBillId()); | 
|---|
|  |  |  | TBill billSave = new TBill(); | 
|---|
|  |  |  | billSave.setId(bill.getId()); | 
|---|
|  |  |  | billSave.setPayFeesMoney(dto.getAmount()); | 
|---|
|  |  |  | billSave.setBankSerialNumber(bankflow!=null?bankflow.getBankSerialNumber():null); | 
|---|
|  |  |  | billSave.setPayFeesTime(bankflow!=null?bankflow.getPayTime():DateUtils.dateToLocalDateTime(new Date())); | 
|---|
|  |  |  | billSave.setVoucher(dto.getVoucher()); | 
|---|
|  |  |  | billSave.setPayFeesType(2); | 
|---|
|  |  |  | back = lockAndUpdateInfo(billSave, 2); | 
|---|
|  |  |  | if (dto.getPayType()==1){ | 
|---|
|  |  |  | //更新银行流水的已抵扣金额和剩余可抵扣金额 | 
|---|
|  |  |  | TBankFlow saveBankFlow = new TBankFlow(); | 
|---|
|  |  |  | saveBankFlow.setId(bankflow.getId()); | 
|---|
|  |  |  | saveBankFlow.setDeductionMoney(bankflow.getDeductionMoney().add(dto.getAmount())); | 
|---|
|  |  |  | BigDecimal subtract = bankflow.getRemainingMoney().subtract(dto.getAmount()); | 
|---|
|  |  |  | saveBankFlow.setRemainingMoney(subtract); | 
|---|
|  |  |  | if (BigDecimal.ZERO.compareTo(subtract) == 0){ | 
|---|
|  |  |  | saveBankFlow.setFlowStatus(1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | tBankFlowService.updateById(saveBankFlow); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //存流水 | 
|---|
|  |  |  | TFlowManagement save = new TFlowManagement(); | 
|---|
|  |  |  | save.setPayType(3); | 
|---|
|  |  |  | save.setPayer(dto.getPayer()); | 
|---|
|  |  |  | save.setBusinessDeptId(bill.getBusinessDeptId()); | 
|---|
|  |  |  | save.setPayTime(bankflow!=null?bankflow.getPayTime():DateUtils.dateToLocalDateTime(new Date())); | 
|---|
|  |  |  | save.setSysSerialNumber(OrderNos.getDid()); | 
|---|
|  |  |  | save.setBankSerialNumber(bankflow!=null?bankflow.getBankSerialNumber():null); | 
|---|
|  |  |  | save.setFlowType(dto.getPayType()==1?2:1); | 
|---|
|  |  |  | save.setPaymentBillId(back.getId()); | 
|---|
|  |  |  | save.setDeductionMoney(back.getDeductionMoney()); | 
|---|
|  |  |  | save.setFlowMoney(dto.getAmount()); | 
|---|
|  |  |  | save.setRemainingMoney(back.getOutstandingMoney()); | 
|---|
|  |  |  | save.setPreOutstand(back.getPreOutstand()); | 
|---|
|  |  |  | tFlowManagementService.save(save); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public BillStatisticsDto statistics(String busDeptId) { | 
|---|
|  |  |  | BillStatisticsDto dto = new BillStatisticsDto(); | 
|---|
|  |  |  | dto.setRent(getBaseMapper().statisticsAllRent(busDeptId)); | 
|---|
|  |  |  | dto.setNopay(getBaseMapper().statisticsNoPay(busDeptId)); | 
|---|
|  |  |  | dto.setPayed(getBaseMapper().statisticsPayed(busDeptId)); | 
|---|
|  |  |  | dto.setOverdue(getBaseMapper().statisticsOverdue(busDeptId)); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer batchBillCount(String userId, List<String> billIds) { | 
|---|
|  |  |  | return this.baseMapper.batchBillCount(userId,billIds); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 街道租金排行 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<ScreenRentRankVO> getStreetRentRank(String businessDeptId) { | 
|---|
|  |  |  | return baseMapper.getStreetRentRank(businessDeptId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询季付账单 | 
|---|
|  |  |  | * @param businessDeptId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<TBill> getJiFuBillList(String businessDeptId) { | 
|---|
|  |  |  | return baseMapper.getJiFuBillList(businessDeptId,null,null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询当前季度的季付账单 | 
|---|
|  |  |  | * @param businessDeptId | 
|---|
|  |  |  | * @param first | 
|---|
|  |  |  | * @param last | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<TBill> getJiFuBillListByTime(String businessDeptId, Date first, Date last) { | 
|---|
|  |  |  | return baseMapper.getJiFuBillList(businessDeptId,first,last); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|