package com.ruoyi.collaborativeApproval.service.impl;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.collaborativeApproval.dto.HouseLeaseDTO;
|
import com.ruoyi.collaborativeApproval.mapper.HouseLeaseMapper;
|
import com.ruoyi.collaborativeApproval.pojo.HouseLease;
|
import com.ruoyi.collaborativeApproval.pojo.HouseLeaseCost;
|
import com.ruoyi.collaborativeApproval.service.HouseLeaseCostService;
|
import com.ruoyi.collaborativeApproval.service.HouseLeaseService;
|
import com.ruoyi.collaborativeApproval.vo.HouseLeaseVO;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.time.LocalDate;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* 房屋租赁管理 Service 业务层处理
|
*
|
* @author ruoyi
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class HouseLeaseServiceImpl extends ServiceImpl<HouseLeaseMapper, HouseLease> implements HouseLeaseService {
|
|
private final HouseLeaseCostService houseLeaseCostService;
|
|
@Override
|
public Page<HouseLeaseVO> listPage(Page<HouseLease> page, HouseLeaseDTO houseLeaseDTO) {
|
LambdaQueryWrapper<HouseLease> lqw = Wrappers.lambdaQuery();
|
|
if (StringUtils.isNotBlank(houseLeaseDTO.getOffice())) {
|
lqw.like(HouseLease::getOffice, houseLeaseDTO.getOffice());
|
}
|
if (StringUtils.isNotBlank(houseLeaseDTO.getUserName())) {
|
lqw.like(HouseLease::getUserName, houseLeaseDTO.getUserName());
|
}
|
|
// 处理到期预警状态
|
String warnStatus = houseLeaseDTO.getWarnStatus();
|
if (StringUtils.isNotBlank(warnStatus)) {
|
LocalDate today = LocalDate.now();
|
LocalDate in30Days = today.plusDays(30);
|
|
if ("overdue".equals(warnStatus)) {
|
// 已逾期: 合同到期日 < 今天
|
lqw.lt(HouseLease::getContractEndDate, today);
|
} else if ("expiring".equals(warnStatus)) {
|
// 即将到期: 0 <= 合同到期日 - 今天 <= 30
|
lqw.ge(HouseLease::getContractEndDate, today)
|
.le(HouseLease::getContractEndDate, in30Days);
|
} else if ("normal".equals(warnStatus)) {
|
// 正常: 合同到期日 > 今天 + 30天
|
lqw.gt(HouseLease::getContractEndDate, in30Days);
|
}
|
}
|
|
lqw.orderByDesc(HouseLease::getCreateTime);
|
|
Page<HouseLease> entityPage = this.page(page, lqw);
|
|
// 转换成VO分页对象
|
Page<HouseLeaseVO> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
|
List<HouseLeaseVO> voRecords = entityPage.getRecords().stream()
|
.map(entity -> BeanUtil.copyProperties(entity, HouseLeaseVO.class))
|
.collect(Collectors.toList());
|
voPage.setRecords(voRecords);
|
|
return voPage;
|
}
|
|
@Override
|
public List<HouseLease> getList(HouseLeaseDTO houseLeaseDTO) {
|
LambdaQueryWrapper<HouseLease> lqw = Wrappers.lambdaQuery();
|
|
if (StringUtils.isNotBlank(houseLeaseDTO.getOffice())) {
|
lqw.like(HouseLease::getOffice, houseLeaseDTO.getOffice());
|
}
|
if (StringUtils.isNotBlank(houseLeaseDTO.getUserName())) {
|
lqw.like(HouseLease::getUserName, houseLeaseDTO.getUserName());
|
}
|
|
// 处理到期预警状态
|
String warnStatus = houseLeaseDTO.getWarnStatus();
|
if (StringUtils.isNotBlank(warnStatus)) {
|
LocalDate today = LocalDate.now();
|
LocalDate in30Days = today.plusDays(30);
|
|
if ("overdue".equals(warnStatus)) {
|
lqw.lt(HouseLease::getContractEndDate, today);
|
} else if ("expiring".equals(warnStatus)) {
|
lqw.ge(HouseLease::getContractEndDate, today)
|
.le(HouseLease::getContractEndDate, in30Days);
|
} else if ("normal".equals(warnStatus)) {
|
lqw.gt(HouseLease::getContractEndDate, in30Days);
|
}
|
}
|
|
lqw.orderByDesc(HouseLease::getCreateTime);
|
|
return this.list(lqw);
|
}
|
|
@Override
|
public HouseLeaseVO getDetail(Long id) {
|
HouseLease houseLease = this.getById(id);
|
if (houseLease == null) {
|
return null;
|
}
|
|
HouseLeaseVO vo = BeanUtil.copyProperties(houseLease, HouseLeaseVO.class);
|
|
// 查询子表
|
List<HouseLeaseCost> costList = houseLeaseCostService.list(
|
Wrappers.<HouseLeaseCost>lambdaQuery()
|
.eq(HouseLeaseCost::getLeaseId, id)
|
.orderByAsc(HouseLeaseCost::getMonth)
|
);
|
vo.setCostList(costList);
|
|
return vo;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public boolean addOrUpdate(HouseLeaseDTO houseLeaseDTO) {
|
HouseLease houseLease = BeanUtil.copyProperties(houseLeaseDTO, HouseLease.class);
|
|
// 1. 保存主表
|
this.saveOrUpdate(houseLease);
|
|
// 2. 处理子表 (全删全插模式)
|
// 删除旧数据
|
houseLeaseCostService.remove(
|
Wrappers.<HouseLeaseCost>lambdaQuery()
|
.eq(HouseLeaseCost::getLeaseId, houseLease.getId())
|
);
|
|
// 插入新数据
|
List<HouseLeaseCost> costList = houseLeaseDTO.getCostList();
|
if (costList != null && !costList.isEmpty()) {
|
for (HouseLeaseCost cost : costList) {
|
cost.setId(null); // 确保是新增
|
cost.setLeaseId(houseLease.getId());
|
}
|
houseLeaseCostService.saveBatch(costList);
|
}
|
|
return true;
|
}
|
}
|