luoxinwu
2023-12-21 a80d56ef9f4d2fd3f1bc08dda23771c652021014
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
package com.panzhihua.service_community.service.impl;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
 
 
import javax.annotation.Resource;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.dtos.community.building.BuildingDto;
import com.panzhihua.common.model.vos.StreetVO;
import com.panzhihua.common.model.vos.area.StreetAndBuildVO;
import com.panzhihua.common.model.vos.community.bigscreen.ScreenStatics;
import com.panzhihua.common.model.vos.community.screen.event.EventPopulationSpecialStatisticsVO;
import com.panzhihua.common.model.vos.neighbor.ActivityAnalysisVO;
import com.panzhihua.common.model.vos.neighbor.UnitActivityAnalysisVO;
import com.panzhihua.common.model.vos.user.AdministratorsUserVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.service_community.dao.*;
 
import com.panzhihua.service_community.entity.ComEvent;
 
import com.panzhihua.service_community.service.ComAreaTownCommunityService;
import com.panzhihua.service_community.util.MyAESUtil;
import com.panzhihua.service_community.util.WxDataUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.panzhihua.common.model.dtos.community.PageComStreetDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComActVO;
import com.panzhihua.common.model.vos.community.ComStreetVO;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ComStreetDO;
import com.panzhihua.service_community.service.ComStreetService;
 
/**
 * @author: llming
 * @description: 街道
 **/
@Service
public class ComStreetServiceImpl implements ComStreetService {
    @Resource
    private ComActDAO comActDAO;
 
    @Resource
    private ComStreetDAO comStreetDAO;
 
    @Resource
    private UserService userService;
 
    @Resource
    private ComEventMapper comEventMapper;
 
    @Autowired
    private CommunityService communityService;
 
    /**
     * 新增社区
     *
     * @param comStreetVO
     *            街道信息
     * @return 新增结果
     */
    @Override
    @Transactional
    public R<ComStreetVO> addStreet(ComStreetVO comStreetVO) {
        ComStreetDO comStreetDO = new ComStreetDO();
        LambdaQueryWrapper<ComStreetDO> param = new QueryWrapper<ComStreetDO>().lambda();
        param.eq(ComStreetDO::getName, comStreetVO.getName());
        param.eq(ComStreetDO::getProvinceCode, comStreetVO.getProvinceCode());
        param.eq(ComStreetDO::getCityCode, comStreetVO.getCityCode());
        param.eq(ComStreetDO::getAreaCode, comStreetVO.getAreaCode());
        Integer integer = comStreetDAO.selectCount(param);
        if (integer > 0) {
            return R.fail(500, "街道已经存在");
        }
        AdministratorsUserVO administratorsUserVO=new AdministratorsUserVO();
        administratorsUserVO.setType(3);
        administratorsUserVO.setAccount(comStreetVO.getAccount());
        administratorsUserVO.setPassword(comStreetVO.getPassword());
        administratorsUserVO.setSocialType(1);
        administratorsUserVO.setRoleId(777777777L);
        administratorsUserVO.setStreetId(0L);
        administratorsUserVO.setName(comStreetVO.getName());
        administratorsUserVO.setUserId(Snowflake.getId());
        R r=userService.addUserBackstageProperty(administratorsUserVO);
        if(R.isOk(r)){
            BeanUtils.copyProperties(comStreetVO, comStreetDO);
            int insert = comStreetDAO.insert(comStreetDO);
            if (insert > 0) {
                AdministratorsUserVO administratorsUserVO1=new AdministratorsUserVO();
                administratorsUserVO1.setUserId(Long.parseLong(r.getData().toString()));
                administratorsUserVO1.setStreetId(comStreetDO.getStreetId());
                userService.updateStreet(administratorsUserVO1);
                ComStreetDO comStreetDO1 = comStreetDAO.selectOne(param);
                BeanUtils.copyProperties(comStreetDO1, comStreetVO);
                return R.ok(comStreetVO);
            }
            return R.fail(500, "");
        }
        return R.fail("账号已存在");
    }
 
    /**
     * 查询街道
     *
     * @param comStreetVO
     *            街道名
     * @return 社区集合
     */
    @Override
    public R listStreet(ComStreetVO comStreetVO) {
        LambdaQueryWrapper<ComStreetDO> param = new QueryWrapper<ComStreetDO>().lambda();
        if (comStreetVO.getProvinceCode() != null) {
            param.eq(ComStreetDO::getProvinceCode, comStreetVO.getProvinceCode());
        }
        if (comStreetVO.getCityCode() != null) {
            param.eq(ComStreetDO::getCityCode, comStreetVO.getCityCode());
        }
        if (comStreetVO.getAreaCode() != null) {
            param.eq(ComStreetDO::getAreaCode, comStreetVO.getAreaCode());
        }
        List<ComStreetVO> voList = Lists.newArrayList();
        List<ComStreetDO> comStreetDOS = comStreetDAO.selectList(param);
        comStreetDOS.forEach(dos -> {
            ComStreetVO vo = new ComStreetVO();
            BeanUtils.copyProperties(dos, vo);
            voList.add(vo);
        });
        return R.ok(voList);
    }
 
    /**
     * 社区详情
     *
     * @param streetId
     *            街道id
     * @return 社区详情
     */
    @Override
    public R detailStreet(Long streetId) {
        ComStreetDO comStreetDO = comStreetDAO.selectById(streetId);
        if (ObjectUtils.isEmpty(comStreetDO)) {
            return R.fail();
        }
        ComStreetVO comStreetVO = new ComStreetVO();
        BeanUtils.copyProperties(comStreetDO, comStreetVO);
        comStreetVO.setAdministrativeRegions(comStreetDAO.retrieveRegions(comStreetVO.getAreaCode().toString()));
        return R.ok(comStreetVO);
    }
 
    /**
     * 分页查询社区
     *
     * @param pageComStreetDTO
     *            查询参数
     * @return 分页集合
     */
    @Override
    public R pageStreet(PageComStreetDTO pageComStreetDTO) {
        Page page = new Page<>();
        Long pageNum = pageComStreetDTO.getPageNum();
        Long pageSize = pageComStreetDTO.getPageSize();
        if (null == pageNum || 0 == pageNum) {
            pageNum = 1L;
        }
        if (null == pageSize || 0 == pageSize) {
            pageSize = 10L;
        }
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        LambdaQueryWrapper<ComStreetDO> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (pageComStreetDTO.getName() != null) {
            userLambdaQueryWrapper.like(ComStreetDO::getName, pageComStreetDTO.getName());
        }
        if (pageComStreetDTO.getAreaCode() != null) {
            userLambdaQueryWrapper.like(ComStreetDO::getAreaCode, pageComStreetDTO.getAreaCode());
        }
        Page userPage = new Page(pageNum, pageSize);
        IPage<ComStreetDO> doPager =
            comStreetDAO.selectPage(userPage, userLambdaQueryWrapper.orderByDesc(ComStreetDO::getCreateAt));
        return R.ok(doPager);
    }
 
    /**
     * 批量删除街道
     *
     * @param streetIds
     *            社区id
     * @return 删除结果
     */
    @Override
    public R delectStreat(List<Long> streetIds) {
        // 不能删除,已经绑定了社区的街道
        LambdaQueryWrapper<ComActDO> comActDOParam =
            new QueryWrapper<ComActDO>().lambda().in(ComActDO::getStreetId, streetIds);
        List<ComActDO> comActDOS = comActDAO.selectList(comActDOParam);
        if (comActDOS != null && comActDOS.size() > 0) {
            return R.fail("删除失败,批量删除街道中,街道下有社区无法删除!");
        }
        int delete = comStreetDAO.deleteBatchIds(streetIds);
        if (delete > 0) {
            return R.ok();
        }
        return R.fail();
    }
 
 
    /**
     * 修改街道信息
     * @param comStreetVO 街道信息
     * */
    @Override
    public R update(ComStreetVO comStreetVO) {
        ComStreetDO comStreetDO=new ComStreetDO();
        BeanUtils.copyProperties(comStreetVO,comStreetDO );
        return R.ok(comStreetDAO.updateById(comStreetDO));
    }
 
    /**
     * 删除街道
     * */
    @Override
    public R remove(Long id) {
        //是否有未结事件
        List<Long> longs = comActDAO.selectCommunityByStreetId(id);
        List<ComEvent> comEvents = comEventMapper.selectList(new QueryWrapper<ComEvent>().lambda().in(ComEvent::getEventProcessStatus, (4), (6), (7), (8)).in(ComEvent::getRequestUserCommunity, longs));
        if (comEvents.size()!=0){
            return R.fail("有未结事件不可删除");
        }
        comStreetDAO.deleteById(id);
        return R.ok();
    }
 
    @Override
    public R removeCommunity(Long id) {
        //是否有未结案件
        List<ComEvent> comEvents = comEventMapper.selectList(new QueryWrapper<ComEvent>().lambda().in(ComEvent::getEventProcessStatus, (4), (6), (7), (8)).eq(ComEvent::getRequestUserCommunity, id));
        if (comEvents.size()!=0){
            return R.fail("有未结事件不可删除");
        }
        comActDAO.deleteById(id);
        return R.ok();
    }
 
    @Override
    public R repassCommunity(ComActVO comActVO) {
        ComActDO comActDO=new ComActDO();
        try {
            comActDO.setPassword(MyAESUtil.Encrypt(comActVO.getPassword(),"Ryo7M3n8loC5Abcd" ));
        } catch (Exception e) {
            e.printStackTrace();
        }
        comActDO.setCommunityId(comActVO.getCommunityId());
        return R.ok(comActDAO.updateById(comActDO));
    }
 
    @Override
    public R repssStreet(ComStreetVO comActVO) {
        ComStreetDO comStreetDO=new ComStreetDO();
        try {
            comStreetDO.setPassword(MyAESUtil.Encrypt(comActVO.getPassword(),"Ryo7M3n8loC5Abcd" ));
        } catch (Exception e) {
            e.printStackTrace();
        }
        comStreetDO.setStreetId(comActVO.getStreetId());
        return R.ok(comStreetDAO.updateById(comStreetDO));
    }
 
    @Override
    public R panzhihuaMap(String areaCode) {
 
        List<ComActVO> comActVOS = comActDAO.panzhihuaMap(areaCode);
        return R.ok(comActVOS);
    }
 
    @Override
    public R searchStreet(BuildingDto buildingDto) {
        QueryWrapper<ComStreetDO> wrapper = new QueryWrapper<>();
        wrapper.eq(buildingDto.getAreaCode()!= null,"area_code",buildingDto.getAreaCode());
        wrapper.eq("app_id","wx118de8a734d269f0");
        List<ComStreetDO> comStreetDOS = comStreetDAO.selectList(wrapper);
//        ArrayList<ComStreetDO> collect = comStreetDOS.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
//                Comparator.comparing(ComStreetDO::getAreaCode))), ArrayList::new));
        List<StreetVO> list = comStreetDOS.stream().map(v -> {
            StreetVO vo = new StreetVO();
            vo.setStreetId(String.valueOf(v.getStreetId()));
            vo.setName(v.getName());
            return vo;
        }).collect(Collectors.toList());
        return R.ok(list);
    }
 
    @Override
    public R oneTagThreeReal(Long street) {
        List<StreetAndBuildVO> streetAndBuildVOS = comActDAO.oneTagThreeReal(street);
        return R.ok(streetAndBuildVOS);
    }
 
    @Override
    public R getScreenStatics() {
        ScreenStatics screenStatics = comActDAO.getScreenStatics();
        WxDataUtil wxDataUtil=new WxDataUtil();
        screenStatics.setDayUser(wxDataUtil.getDayUser());
        screenStatics.setCountUser(wxDataUtil.getMonthUser());
        return R.ok(screenStatics);
    }
 
    @Override
    public R populationStatics(Long streetId) {
        StreetAndBuildVO vo = new StreetAndBuildVO();
        List<StreetAndBuildVO> list = comActDAO.oneTagThreeReal(streetId);
        if(list != null){
            for (StreetAndBuildVO basics : list) {
                vo.setPeopleNum(vo.getPeopleNum() + basics.getPeopleNum());
                vo.setVillageNum(vo.getVillageNum() + basics.getVillageNum());
                vo.setBuildNum(vo.getBuildNum() + basics.getBuildNum());
            }
        }
        EventPopulationSpecialStatisticsVO populationSpecial = comActDAO.getPopulationSpecial(streetId);
        if(populationSpecial != null){
            populationSpecial.setOtherTotal(populationSpecial.getZjTotal() + populationSpecial.getSfTotal()
                    + populationSpecial.getXsTotal() + populationSpecial.getXjTotal()
                    + populationSpecial.getJzTotal() + populationSpecial.getXdTotal());
            populationSpecial.setLnTotal(comActDAO.getPopulationAge(streetId,60));
            populationSpecial.setGlTotal(comActDAO.getPopulationAge(streetId,80));
            vo.setSpecialStatisticsVo(populationSpecial);
        }
        vo.setSpecialStatisticsVo(populationSpecial);
        return R.ok(vo);
    }
 
    @Override
    public R probably(Integer year,String belongTo) {
        ActivityAnalysisVO activityVO=new ActivityAnalysisVO();
//        String beginTime=null;
//        String endTime=null;
//        if (null != year){
//            //设置开始,截止时间
//            beginTime=year.toString().concat("-01-01");
//            endTime=year.toString().concat("-12-31");
//        }
        activityVO.setUnitCount(comActDAO.arriveUnit(belongTo));
        R r = communityService.institutionalUnitServiceAnalysis(year, null, null, null, null, null, belongTo, null, null);
        Object data = r.getData();
        String jsonString = JSON.toJSONString(data);
        ActivityAnalysisVO activityAnalysisVO = JSON.parseObject(jsonString, ActivityAnalysisVO.class);
        activityVO.setUnitServiceTotalTime(activityAnalysisVO.getUnitServiceTotalTime());
        return R.ok(activityVO);
    }
 
}