mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package com.panzhihua.service_dangjian.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.panzhihua.common.enums.ComPbCheckUnitTypeEnum;
import com.panzhihua.common.model.dtos.common.AddComPbCheckUnitDto;
import com.panzhihua.common.model.dtos.common.EditComPbCheckUnitDto;
import com.panzhihua.common.model.dtos.common.PageComPbCheckUnitDto;
import com.panzhihua.common.model.dtos.common.PagePbCheckUnitCommonDto;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.TreeListVO;
import com.panzhihua.common.model.vos.common.ComPbCheckUnitVo;
import com.panzhihua.common.model.vos.common.PbCheckUnitStatisticsVo;
import com.panzhihua.common.model.vos.partybuilding.excel.ComPbCheckUnitErrorExcelVO;
import com.panzhihua.common.model.vos.partybuilding.excel.ComPbCheckUnitExcelVO;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_dangjian.dao.ComPbCheckUnitMapper;
import com.panzhihua.service_dangjian.dao.ComPbMemberDAO;
import com.panzhihua.service_dangjian.dao.ComPbMemberRoleDAO;
import com.panzhihua.service_dangjian.entity.ComPbCheckUnit;
import com.panzhihua.service_dangjian.model.dos.ComPbMemberDO;
import com.panzhihua.service_dangjian.model.dos.ComPbMemberRoleDO;
import com.panzhihua.service_dangjian.service.ComPbCheckUnitService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * title: 党建-报到单位表服务实现类
 * <p>
 * projectName 成都呐喊信息技术有限公司-智慧社区项目
 * <p>
 * description: 党建-报到单位表服务实现类
 *
 * @author lyq
 * @date 2022-02-16 16:22:26
 */
@Service("comPbCheckUnitService")
public class ComPbCheckUnitServiceImpl extends ServiceImpl<ComPbCheckUnitMapper, ComPbCheckUnit> implements ComPbCheckUnitService {
 
    @Resource
    private ComPbMemberDAO comPbMemberDAO;
    @Resource
    private ComPbMemberRoleDAO comPbMemberRoleDAO;
 
 
    /**
     * description  queryByPage  分页查询
     *
     * @param comPbCheckUnit 请求参数
     * @return 分页查询列表数据
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    public R queryByPage(PageComPbCheckUnitDto comPbCheckUnit) {
 
        Long communityId = comPbCheckUnit.getCommunityId();
        if (communityId != null){
            List<Long> communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds = new ArrayList<>();
                communityIds.add(communityId);
            }
            comPbCheckUnit.setCommunityIds(communityIds);
        }
        IPage<ComPbCheckUnitVo> checkUnitVoPage= this.baseMapper.queryAllByLimit(comPbCheckUnit, new Page(comPbCheckUnit.getPageNum(), comPbCheckUnit.getPageSize()));
        checkUnitVoPage.getRecords().forEach(checkUnit -> {
            if(StringUtils.isEmpty(checkUnit.getOrgName())){
                checkUnit.setOrgName("待编辑党支部");
            }
        });
        return R.ok(checkUnitVoPage);
    }
 
    @Override
    public R treeList(TreeListVO treeListVO) {
        QueryWrapper<ComPbCheckUnit> wrapper = new QueryWrapper<>();
        if(treeListVO.getName() != null){
            wrapper.eq("belong_to",treeListVO.getName());
        } else if(treeListVO.getOrgName() != null){
            wrapper.eq("org_name",treeListVO.getOrgName());
        } else {
            return R.ok();
        }
        List<ComPbCheckUnit> comPbCheckUnits = this.baseMapper.selectList(wrapper);
        return R.ok(comPbCheckUnits);
    }
 
    @Override
    public R orgList() {
        QueryWrapper<ComPbCheckUnit> wrapper = new QueryWrapper<>();
        List<ComPbCheckUnitVo> voList = new ArrayList<>();
        List<ComPbCheckUnit> list = this.baseMapper.selectList(wrapper);
        Map<String, List<ComPbCheckUnit>> map = list.stream().filter(f -> f.getOrgName() != null).collect(Collectors.groupingBy(ComPbCheckUnit::getOrgName));
        for (Map.Entry<String, List<ComPbCheckUnit>> entry : map.entrySet()) {
            ComPbCheckUnitVo pbCheckUnitVo = new ComPbCheckUnitVo();
            pbCheckUnitVo.setOrgName(entry.getKey());
            voList.add(pbCheckUnitVo);
        }
        return R.ok(voList);
    }
 
    /**
     * description  insert  新增数据
     *
     * @param comPbCheckUnit 请求参数
     * @return 新增结果
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    @Transactional
    public R insert(AddComPbCheckUnitDto comPbCheckUnit) {
        Long communityId=this.baseMapper.selectCommunityId(comPbCheckUnit.getHelpCommunityName().split(",")[0],comPbCheckUnit.getHelpCommunityName().split(",")[1],comPbCheckUnit.getHelpCommunityName().split(",")[2]);
        ComPbCheckUnit entity = new ComPbCheckUnit();
        BeanUtils.copyProperties(comPbCheckUnit, entity);
        int a=0;
        if(!StringUtils.isEmpty(comPbCheckUnit.getAdminPhone()))
        {
            a=this.baseMapper.selectOrgAdmin(comPbCheckUnit.getAdminPhone());
        }
        if(a!=0)
        {
            return R.fail("该手机号已绑定管理员!");
        }
        if(communityId!=null){
            entity.setCommunityId(communityId);
        }
        if (this.baseMapper.insert(entity) > 0) {
            return R.ok();
        }
        return R.fail("添加失败");
    }
 
    /**
     * description  update  修改数据
     *
     * @param editDto 请求参数
     * @return 修改结果
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    public R update(EditComPbCheckUnitDto editDto) {
        ComPbCheckUnit entity = this.baseMapper.selectById(editDto.getId());
        if (entity == null) {
            return R.fail("未查询到该记录");
        }
        BeanUtils.copyProperties(editDto, entity);
        if (this.baseMapper.updateById(entity) > 0) {
            return R.ok();
        }
        return R.fail("修改失败");
    }
 
    /**
     * description  deleteById  通过主键删除数据
     *
     * @param id 主键id
     * @return 删除结果
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    public R deleteById(Long id) {
        Integer memberCount = comPbMemberDAO.selectCount(new QueryWrapper<ComPbMemberDO>().lambda()
                .eq(ComPbMemberDO::getCheckUnitId,id));
        if(memberCount > 0){
            return R.fail("该数据已被引用,无法删除!");
        }
        Integer memberRoleCount = comPbMemberRoleDAO.selectCount(new QueryWrapper<ComPbMemberRoleDO>().lambda()
                .eq(ComPbMemberRoleDO::getCheckUnitId,id));
        if(memberRoleCount > 0){
            return R.fail("该数据已被引用,无法删除!");
        }
        if (this.baseMapper.deleteById(id) > 0) {
            return R.ok();
        }
        return R.fail("删除失败");
    }
 
    /**
     * description  detailById  查询详情
     *
     * @param id 主键id
     * @return 详情数据
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    public R detailById(Long id) {
        ComPbCheckUnitVo checkUnitVo = this.baseMapper.queryById(id);
        if(checkUnitVo != null && StringUtils.isEmpty(checkUnitVo.getContacts())){
            checkUnitVo.setContacts("待编辑负责人");
        }
        if(checkUnitVo != null && StringUtils.isEmpty(checkUnitVo.getOrgName())){
            checkUnitVo.setOrgName("待编辑党支部");
        }
        return R.ok(checkUnitVo);
    }
 
    /**
     * description  queryByPage  查询列表
     *
     * @param comPbCheckUnit 请求参数
     * @return 列表数据
     * @author lyq
     * @date 2022-02-16 16:22:26
     */
    @Override
    public R queryByList(PageComPbCheckUnitDto comPbCheckUnit) {
        Long communityId = comPbCheckUnit.getCommunityId();
        if (communityId != null){
            List<Long> communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds = new ArrayList<>();
                communityIds.add(communityId);
            }
            comPbCheckUnit.setCommunityIds(communityIds);
        }
        IPage<ComPbCheckUnitVo> checkUnitList = this.baseMapper.queryAllByLimit(comPbCheckUnit,new Page(comPbCheckUnit.getPageNum(),comPbCheckUnit.getPageSize()));
        checkUnitList.getRecords().forEach(checkUnit -> {
            if(StringUtils.isEmpty(checkUnit.getOrgName())){
                checkUnit.setOrgName("待编辑党支部");
            }
            if(StringUtils.isNotEmpty(checkUnit.getAreaCode())){
                switch (checkUnit.getAreaCode()){
                    case "510423": checkUnit.setAreaName("西区"); break;
                    case "510402": checkUnit.setAreaName("东区"); break;
                    case "510411": checkUnit.setAreaName("仁和区"); break;
                    case "510421": checkUnit.setAreaName("米易县"); break;
                    case "510422": checkUnit.setAreaName("盐边"); break;
                }
            }
        });
        return R.ok(checkUnitList);
    }
 
    /**
     * 批量导入报道单位
     * @param list  导入数据
     * @param communityId   社区id
     * @param userId    用户id
     * @return  导入结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R importCheckUnit(List<ComPbCheckUnitExcelVO> list, Long communityId, Long userId){
        ArrayList<ComPbCheckUnitErrorExcelVO> mistakes = Lists.newArrayList();
        ArrayList<ComPbCheckUnit> saveList = Lists.newArrayList();
        Date nowDate = new Date();
        if(list != null && list.size() > 0){
            list.forEach(checkUnit -> {
                ComPbCheckUnit comPbCheckUnit = this.baseMapper.selectOne(new QueryWrapper<ComPbCheckUnit>().lambda()
                        .eq(ComPbCheckUnit::getCommunityId,communityId).eq(ComPbCheckUnit::getName,checkUnit.getName()));
                if(comPbCheckUnit != null){
                    ComPbCheckUnitErrorExcelVO checkUnitError = new ComPbCheckUnitErrorExcelVO();
                    BeanUtils.copyProperties(checkUnit,checkUnitError);
                    checkUnitError.setError("该单位已存在,不可重复导入");
                    mistakes.add(checkUnitError);
                }else{
                    comPbCheckUnit = new ComPbCheckUnit();
                    BeanUtils.copyProperties(checkUnit,comPbCheckUnit);
                    comPbCheckUnit.setType(ComPbCheckUnitTypeEnum.getCodeByName(checkUnit.getType()));
                    comPbCheckUnit.setCommunityId(communityId);
                    comPbCheckUnit.setCreateAt(nowDate);
                    comPbCheckUnit.setCreateBy(userId);
                    saveList.add(comPbCheckUnit);
                }
            });
        }
        if(saveList.size() > 0){
            this.saveBatch(saveList);
        }
        if(mistakes.size() > 0){
            return R.fail(mistakes);
        }
        return R.ok();
    }
 
    @Override
    public R detailByPhone(String phone) {
        List<ComPbCheckUnit> comPbCheckUnits=this.baseMapper.selectList(new QueryWrapper<ComPbCheckUnit>().lambda().eq(ComPbCheckUnit::getAdminPhone,phone));
        if(!comPbCheckUnits.isEmpty()){
            return R.ok(comPbCheckUnits.get(0));
        }
        return R.ok();
    }
 
    /**
     * 报到单位统计-顶部数据
     * @param communityId
     * @param belongTo
     * @param choice
     * @return
     */
    @Override
    public R unitStatisticsTop(Long communityId, String belongTo, String choice,Long[] unitIds,String yearTime) {
        List<Long> communityIds = new ArrayList<>();
        if (null != communityId){
             communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds.add(communityId);
            }
        }
        //查询 活动总积分
        PbCheckUnitStatisticsVo vo = this.baseMapper.unitStatisticsTop(communityIds, belongTo, choice,unitIds,yearTime);
        if(null != vo) {
            Integer sum = this.baseMapper.organSumIntegral(communityIds,yearTime);
            vo.setOrganSumIntegral(sum);
        }
        return R.ok(vo);
    }
 
    /**
     * 报到单位统计
     * @param commonDto
     * @return
     */
    @Override
    public R unitStatistics(PagePbCheckUnitCommonDto commonDto) {
        Long communityId = commonDto.getCommunityId();
        if (null != communityId) {
            List<Long> communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds = new ArrayList<>();
                communityIds.add(communityId);
            }
            commonDto.setCommunityIds(communityIds);
        }
        return R.ok(this.baseMapper.unitStatistics(commonDto, new Page(commonDto.getPageNum(), commonDto.getPageSize())));
    }
 
    /**
     * 报到党员统计-顶部数据
     * @param communityId
     * @param belongTo
     * @param choice
     * @param checkUnitId
     * @return
     */
    @Override
    public R pbStatisticsTop(Long communityId, String belongTo, String choice, Long checkUnitId,Long[] unitIds ,String yearTime) {
 
        List<Long> communityIds = new ArrayList<>();
        if (null != communityId){
            communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds.add(communityId);
            }
        }
        //查询 活动总积分
        PbCheckUnitStatisticsVo vo = this.baseMapper.pbStatisticsTop(communityIds, belongTo, choice, checkUnitId,unitIds,yearTime);
        if(null != vo) {
            Integer sum = this.baseMapper.pbOrganSumIntegral(communityIds,yearTime);
            vo.setOrganSumIntegral(sum);
        }
        return R.ok(vo);
    }
 
    /**
     * 报到党员统计-按单位归属统计
     * @param commonDto
     * @return
     */
    @Override
    public R pbStatisticsBelong(PagePbCheckUnitCommonDto commonDto) {
        Long communityId = commonDto.getCommunityId();
        if (null != communityId) {
            List<Long> communityIds = comPbMemberDAO.selectIds(communityId);
            if (CollUtil.isEmpty(communityIds)) {
                communityIds = new ArrayList<>();
                communityIds.add(communityId);
            }
            commonDto.setCommunityIds(communityIds);
        }
        return R.ok(this.baseMapper.pbStatisticsBelong(commonDto, new Page(commonDto.getPageNum(), commonDto.getPageSize())));
    }
 
    /**
     * 报到党员统计-按单位统计
     * @param commonDto
     * @return
     */
    @Override
    public R pbStatisticsUnit(PagePbCheckUnitCommonDto commonDto) {
        Long communityId = commonDto.getCommunityId();
        List<Long> communityIds = comPbMemberDAO.selectIds(communityId);
        if (CollUtil.isEmpty(communityIds)) {
            communityIds = new ArrayList<>();
            communityIds.add(communityId);
        }
        commonDto.setCommunityIds(communityIds);
        return R.ok(this.baseMapper.pbStatisticsUnit(commonDto, new Page(commonDto.getPageNum(), commonDto.getPageSize())));
    }
}