hsy
2026-08-28 a8e4132c3e2e9c3b3fcb0360901b35571b6464ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
    }
}