package com.zzyl.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.TradingConstant;
import com.zzyl.dto.BillDto;
import com.zzyl.dto.PrepaidRechargeRecordDto;
import com.zzyl.dto.RetreatClearingBillDto;
import com.zzyl.entity.*;
import com.zzyl.enums.*;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.*;
import com.zzyl.service.*;
import com.zzyl.utils.CodeUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.*;
import com.zzyl.vo.retreat.Arrearage;
import com.zzyl.vo.retreat.DueBack;
import com.zzyl.vo.retreat.RetreatBillVo;
import com.zzyl.vo.retreat.Unpaid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BillServiceImpl implements BillService {
@Resource
private BillMapper billMapper;
@Resource
private BalanceService balanceService;
@Resource
private CheckInConfigMapper checkInConfigMapper;
@Resource
private CheckInMapper checkInMapper;
@Resource
private PrepaidRechargeRecordMapper prepaidRechargeRecordMapper;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Autowired
private TradingService tradingService;
@Resource
private RefundRecordService refundRecordService;
@Resource
private RefundRecordMapper refundRecordMapper;
@Resource
private MemberService memberService;
@Resource
private MemberElderService memberElderService;
@Resource
private NursingLevelService nursingLevelService;
@Resource
private RoomService roomService;
/**
* 根据id删除账单
*
* @param id 账单id
* @return 删除结果
*/
@Override
public int deleteByElderId(Long id) {
return billMapper.deleteByElderId(id);
}
/**
* 生成月度账单
*
* @param billDto 账单实体
* @return 插入结果
*/
@Override
public int createMonthBill(BillDto billDto) {
CheckIn checkIn = checkInMapper.selectByElderIdAndStatus(billDto.getElderId(), CheckInStatusEnum.PROGRESSING.getOrdinal());
Bill bill1 = billMapper.selectByElderAndMonth(billDto.getElderId(), billDto.getBillMonth(), checkIn.getCheckInCode());
if (ObjectUtil.isNotEmpty(bill1)) {
throw new BaseException(BasicEnum.MONTH_BILL_DUPLICATE_EXCEPTION);
}
Bill bill = BeanUtil.toBean(billDto, Bill.class);
//入住编码
bill.setCheckInCode(checkIn.getCheckInCode());
// 生成账单
bill.setBillType(BillType.MONTH.getOrdinal());
bill.setTransactionStatus(BillStatus.UN_PAY.getOrdinal());
// 编号
String zd = CodeUtil.generateCode("ZD", stringRedisTemplate, 5);
bill.setBillNo(zd);
// 查询入住配置
CheckInConfig checkInConfig = checkInConfigMapper.findCurrentConfigByElderId(bill.getElderId());
NursingLevelVo byId = nursingLevelService.getById(checkInConfig.getNursingLevelId());
bill.setLname(byId.getName());
RoomVo room = roomService.getRoom(Long.parseLong(checkInConfig.getRemark().split(":")[1]));
bill.setTypeName(room.getTypeName());
// 账单开始结束时间
int year = Integer.parseInt(bill.getBillMonth().substring(0, 4));
int monthOfYear = Integer.parseInt(bill.getBillMonth().substring(5, 7));
LocalDateTime firstDayOfMonth = LocalDateTime.of(year, monthOfYear, 1, 0, 0, 0);
if (checkInConfig.getCostEndTime().isBefore(firstDayOfMonth)) {
throw new BaseException(BasicEnum.MONTH_OUT_CHECKIN_TERM);
}
// 押金
BigDecimal depositAmount = new BigDecimal(0);
if (checkInConfig.getCostStartTime().isAfter(firstDayOfMonth) || checkInConfig.getCostStartTime().isEqual(firstDayOfMonth)) {
// 首月
bill.setBillStartTime(checkInConfig.getCostStartTime());
depositAmount = checkInConfig.getDepositAmount();
} else {
bill.setBillStartTime(firstDayOfMonth);
}
// 支付截止时间
bill.setPaymentDeadline(bill.getBillStartTime().plusDays(6));
// 押金金额
bill.setDepositAmount(depositAmount);
LocalDateTime lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.toLocalDate().lengthOfMonth())
.withHour(23)
.withMinute(59)
.withSecond(59);
if (checkInConfig.getCostEndTime().isAfter(lastDayOfMonth)) {
bill.setBillEndTime(lastDayOfMonth);
} else {
// 末月
bill.setBillEndTime(checkInConfig.getCostEndTime());
}
// 费用共计天数
int i = bill.getBillEndTime().getDayOfMonth() - bill.getBillStartTime().getDayOfMonth() + 1;
bill.setTotalDays(i);
// 每月应付
BigDecimal cost = checkInConfig.getBedCost().add(checkInConfig.getOtherCost()).add(checkInConfig.getNursingCost())
.subtract(checkInConfig.getMedicalInsurancePayment()).subtract(checkInConfig.getGovernmentSubsidy());
// 当月天数
int days = lastDayOfMonth.getDayOfMonth() - firstDayOfMonth.getDayOfMonth() + 1;
// 本期应付 = (每月应付 / 当月天数)* 共计天数
// 首月和最后一个月需要扣减
BigDecimal currentCost = cost.divide(new BigDecimal(days), 60, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(i));
bill.setCurrentCost(currentCost);
// 账单金额 = 本期应付 + 押金
BigDecimal billAmount = currentCost.add(depositAmount);
bill.setBillAmount(billAmount);
if (billAmount.compareTo(new BigDecimal(0)) == 0) {
bill.setPayableAmount(BigDecimal.ZERO);
bill.setTransactionStatus(BillStatus.PAY.ordinal());
billMapper.insert(bill);
}
// 应付金额 = 本期应付 - 预交款
// 查询余额表
Balance balance = balanceService.selectByElderId(bill.getElderId());
if (balance.getPrepaidBalance().compareTo(billAmount) >= 0) {
// 预交款足够 应付金额 = 0
bill.setPayableAmount(new BigDecimal(0));
// 预交款扣减
balance.setPrepaidBalance(balance.getPrepaidBalance().subtract(billAmount));
bill.setPrepaidAmount(billAmount);
bill.setTransactionStatus(BillStatus.PAY.ordinal());
} else {
// 预交款不足
BigDecimal payableAmount = billAmount.subtract(balance.getPrepaidBalance());
bill.setPayableAmount(payableAmount);
bill.setPrepaidAmount(balance.getPrepaidBalance());
// 预交款扣减
balance.setPrepaidBalance(new BigDecimal(0));
bill.setTransactionStatus(BillStatus.UN_PAY.ordinal());
}
// 预交款扣减后回写数据库
balanceService.save(balance);
return billMapper.insert(bill);
}
/**
* 生成费用账单
*
* @param billDto 账单实体
* @return 插入结果
*/
@Override
public int createProjectBill(BillDto billDto) {
//查询入住信息
CheckIn checkIn = checkInMapper.selectByElderIdAndStatus(billDto.getElderId(), CheckInStatusEnum.PROGRESSING.getOrdinal());
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的养老院管理系统后端代码设计源码 (432个子文件)
.gitignore 162B
BillServiceImpl.java 46KB
UUID.java 15KB
DeviceServiceImpl.java 13KB
NursingTaskServiceImpl.java 12KB
CheckInServiceImpl.java 12KB
AlertRuleServiceImpl.java 12KB
RetreatServiceImpl.java 11KB
AmqpClient.java 11KB
WeChatPayUtil.java 10KB
StringUtils.java 10KB
OrderServiceImpl.java 9KB
TradingServiceImpl.java 9KB
BillMapper.java 9KB
ResourceServiceImpl.java 9KB
DeptServiceImpl.java 9KB
UserServiceImpl.java 8KB
PageResponse.java 8KB
BillController.java 8KB
ReservationServiceImpl.java 8KB
CustomOrderController.java 7KB
RoomServiceImpl.java 7KB
ElderServiceImpl.java 7KB
PostServiceImpl.java 6KB
TradingVo.java 6KB
MemberServiceImpl.java 6KB
NursingTaskController.java 6KB
UserController.java 5KB
SwaggerConfig.java 5KB
ReservationController.java 5KB
BasicEnum.java 5KB
RoomTypeServiceImpl.java 5KB
WebMvcConfig.java 5KB
AutoFillInterceptor.java 5KB
CustomerReservationController.java 5KB
PostController.java 5KB
RefundRecordServiceImpl.java 5KB
OrderController.java 4KB
VisitController.java 4KB
VisitServiceImpl.java 4KB
SnowflakeIdWorker.java 4KB
MemberElderServiceImpl.java 4KB
BillService.java 4KB
NursingLevelController.java 4KB
ContractServiceImpl.java 4KB
NursingPlanServiceImpl.java 4KB
RoleServiceImpl.java 4KB
LoginServiceImpl.java 4KB
ContractController.java 4KB
ResponseResult.java 4KB
CheckInVo.java 4KB
ElderController.java 4KB
RetreatController.java 4KB
DeptController.java 3KB
CustomerUserController.java 3KB
FloorController.java 3KB
NursingPlanController.java 3KB
OrderMapper.java 3KB
JwtAuthorizationManager.java 3KB
AlertRuleController.java 3KB
Retreat.java 3KB
MemberElderController.java 3KB
RefundRecordVo.java 3KB
DeviceController.java 3KB
BalanceServiceImpl.java 3KB
WechatServiceImpl.java 3KB
RoomTypeController.java 3KB
UserTokenInterceptor.java 3KB
HttpUtilTest.java 3KB
TradingConstant.java 3KB
CheckInConfigVo.java 3KB
ElderService.java 3KB
OSSAliyunFileStorageService.java 3KB
NursingProjectServiceImpl.java 3KB
BillVo.java 3KB
CostomerContractController.java 3KB
Trading.java 3KB
ContractVo.java 3KB
AlertDataVo.java 3KB
ContractDto.java 3KB
RoomController.java 3KB
Constants.java 3KB
NursingLevelServiceImpl.java 3KB
RoleController.java 3KB
CheckInController.java 3KB
ContractMapper.java 3KB
ElderMapper.java 3KB
RetreatElderVo.java 3KB
OrderJob.java 2KB
EmptyUtil.java 2KB
UserAuth.java 2KB
DeviceMapper.java 2KB
FloorServiceImpl.java 2KB
RetreatElderDto.java 2KB
ResourceController.java 2KB
OrderService.java 2KB
AlertJob.java 2KB
UserDto.java 2KB
JwtUtil.java 2KB
ResourceVo.java 2KB
共 432 条
- 1
- 2
- 3
- 4
- 5
资源评论
wjs2024
- 粉丝: 2283
- 资源: 5463
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功