Pu Zhibing
2025-04-22 d138293736414a314467a2641e6116ff263ead48
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.ruoyi.bussiness.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bussiness.domain.Placement;
import com.ruoyi.bussiness.domain.PlacementBatchHousehold;
import com.ruoyi.bussiness.mapper.PlacementMapper;
import com.ruoyi.bussiness.object.request.placement.PlacementIdRequest;
import com.ruoyi.bussiness.object.request.placement.PlacementImportRequest;
import com.ruoyi.bussiness.object.request.placement.PlacementPageRequest;
import com.ruoyi.bussiness.object.response.placement.GetHouseHistoryRequest;
import com.ruoyi.bussiness.object.response.placement.PlacementPageResponse;
import com.ruoyi.bussiness.object.response.placement.PlacementTemplateResponse;
import com.ruoyi.bussiness.service.PlacementBatchHouseholdService;
import com.ruoyi.bussiness.service.PlacementService;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class PlacementServiceImpl extends ServiceImpl<PlacementMapper, Placement> implements PlacementService {
 
    @Lazy
    @Autowired
    private PlacementBatchHouseholdService placementBatchHouseholdService;
 
    @Override
    public PlacementPageResponse page(PlacementPageRequest request) {
        Page<Placement> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjUtil.isNotEmpty(request.getStreet())){
            queryWrapper.like(Placement::getStreet,request.getStreet());
        }
        if(ObjUtil.isNotEmpty(request.getProjectName())){
            queryWrapper.like(Placement::getProjectName,request.getProjectName());
        }
        if(ObjUtil.isNotEmpty(request.getCommunity())){
            queryWrapper.like(Placement::getCommunity,request.getCommunity());
        }
        if(ObjUtil.isNotEmpty(request.getHeadOrIdCard())){
            queryWrapper.and(query->query.like(Placement::getHouseholdHead,request.getHeadOrIdCard()).or().like(Placement::getIdCard,request.getHeadOrIdCard()));
        }
        if(ObjUtil.isNotEmpty(request.getStatus())){
            queryWrapper.eq(Placement::getStatus,request.getStatus());
        }
        if(ObjUtil.isNotEmpty(request.getFamilyNames())){
            queryWrapper.like(Placement::getFamilyName,request.getFamilyNames());
        }
        Page<Placement> pageResponse = this.baseMapper.selectPage(page, queryWrapper);
        PlacementPageResponse response = new PlacementPageResponse();
        response.setTotal(pageResponse.getTotal());
        response.setRecords(pageResponse.getRecords());
        return response;
    }
 
    @Override
    public void add(Placement placement) {
        //查询是否唯一
        int i = this.countPlacementByFamilyIdCard(placement.getFamilyIdCard());
        if (i > 1) {
            throw new GlobalException(placement.getFamilyIdCard() + "家庭成员身份证号已存在!");
        }
        this.save(placement);
    }
 
    @Override
    public void update(Placement placement) {
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Placement::getFamilyIdCard, placement.getFamilyIdCard());
        queryWrapper.ne(Placement::getId,placement.getId());
        List<Placement> oldPlacements = this.baseMapper.selectList(queryWrapper);
        if (!oldPlacements.isEmpty()) {
            throw new GlobalException(placement.getIdCard() + "身份证号已存在!");
        }
        this.updateById(placement);
    }
 
    @Override
    public Placement detail(PlacementIdRequest request) {
        Placement placement = this.baseMapper.selectById(request.getId());
        GetHouseHistoryRequest getHouseHistoryRequest = new GetHouseHistoryRequest();
        getHouseHistoryRequest.setFamilyIdCard(placement.getFamilyIdCard());
        getHouseHistoryRequest.setFamilyName(placement.getFamilyName());
        List<PlacementBatchHousehold> households = placementBatchHouseholdService.getHouseHistory(getHouseHistoryRequest);
        placement.setHouseholds(households);
        return placement;
    }
 
    @Override
    public void del(PlacementIdRequest request) {
        this.baseMapper.deleteById(request.getId());
    }
 
    @Override
    public void imports(PlacementImportRequest request) {
        File file = FileUtils.convertToFile(request.getFile());
        List<Map<Integer, String>> maps = FileUtils.readExcelHead(file);
        if (!Objects.equals(maps.get(0).get(0), "项目名称")
                || !Objects.equals(maps.get(0).get(1), "项目启动时间")
                || !Objects.equals(maps.get(0).get(2), "镇街")
                || !Objects.equals(maps.get(0).get(3), "村社区")
                || !Objects.equals(maps.get(0).get(4), "组")
                || !Objects.equals(maps.get(0).get(5), "户主姓名")
                || !Objects.equals(maps.get(0).get(6), "身份证号码")
        ) {
            throw new GlobalException("Excel表头格式不对");
        }
        List<PlacementTemplateResponse> dataList;
        try {
            dataList = EasyExcelFactory.read(file)
                    .head(PlacementTemplateResponse.class)
                    .sheet()
                    .doReadSync();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(e.getMessage());
        }
        if (CollectionUtils.isEmpty(dataList)) {
            throw new GlobalException("未解析出数据");
        }
        List<String> idCards = new ArrayList<>();
        for (PlacementTemplateResponse data : dataList) {
            if (ObjUtil.isEmpty(data.getIdCard())
                    || ObjUtil.isEmpty(data.getFamilyIdCard())) {
                throw new GlobalException("导入数据户主和家庭成员身份证号码不能为空!");
            }
            if(ObjUtil.isEmpty(data.getProjectName())){
                throw new GlobalException("导入数据项目名称不能为空!");
            }
            if (ObjUtil.isEmpty(data.getStartTime())) {
                throw new GlobalException("导入数据项目启动时间不能为空!");
            }
            if (ObjUtil.isEmpty(data.getStreet())) {
                throw new GlobalException("导入数据镇不能为空!");
            }
            if (ObjUtil.isEmpty(data.getHouseholdHead())) {
                throw new GlobalException("导入数据户主不能为空!");
            }
            if (ObjUtil.isEmpty(data.getFamilyName())) {
                throw new GlobalException("导入数据家庭成员不能为空!");
            }
            if (ObjUtil.isEmpty(data.getRelation())) {
                throw new GlobalException("导入数据关系不能为空!");
            }
            if (ObjUtil.isEmpty(data.getPersonType())) {
                throw new GlobalException("导入数据人员性质不能为空!");
            }
 
//            if (ObjUtil.isEmpty(data.getAge())) {
//                throw new GlobalException("导入数据年龄不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getSex())) {
//                throw new GlobalException("导入数据性别不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getBirthday())) {
//                throw new GlobalException("导入数据出生年月不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getWays())) {
//                throw new GlobalException("导入数据安置方式不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getMobile())) {
//                throw new GlobalException("导入数据联系方式不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getOriginCollectionTime())) {
//                throw new GlobalException("导入数据原始拆迁过渡时间不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getLastBeginTime())) {
//                throw new GlobalException("导入数据上次过渡时间不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getLastEndTime())) {
//                throw new GlobalException("导入数据上次过渡发放时间不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getNoHouseArea())) {
//                throw new GlobalException("导入数据剩余未安置面积不能为空!");
//            }
//            if (ObjUtil.isEmpty(data.getNoShopArea())) {
//                throw new GlobalException("导入数据剩余未安置商铺面积不能为空!");
//            }
            idCards.add(data.getFamilyIdCard());
        }
        List<Placement> exists = selectPlacementIdCard(idCards);
        if (ObjUtil.isNotEmpty(exists)) {
            String ids = exists.stream().map(Placement::getFamilyIdCard).collect(Collectors.joining(","));
            throw new GlobalException("身份证已存在:" + ids);
        }
 
        List<Placement> placements = BeanUtil.copyToList(dataList, Placement.class);
        this.saveBatch(placements);
    }
 
    @Override
    public int countPlacementByFamilyIdCard(String familyIdCard) {
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Placement::getFamilyIdCard,familyIdCard);
        return this.baseMapper.selectCount(queryWrapper).intValue();
    }
 
    @Override
    public int countPlacementByIdCard(String idCard) {
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Placement::getIdCard,idCard);
        return this.baseMapper.selectCount(queryWrapper).intValue();
    }
 
    @Override
    public List<Placement> selectPlacementIdCard(List<String> idCard) {
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Placement::getFamilyIdCard,idCard);
        return this.baseMapper.selectList(queryWrapper);
    }
 
    @Override
    public int countNamesExists(String name) {
        LambdaQueryWrapper<Placement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Placement::getFamilyName,name.trim());
        return this.baseMapper.selectCount(queryWrapper).intValue();
    }
 
}