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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.panzhihua.service_community.service.impl;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
import javax.annotation.Resource;
 
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.panzhihua.common.model.vos.DateVO;
import com.panzhihua.common.model.vos.community.building.admin.BuildingListHeaderStatisticsAdminVo;
import com.panzhihua.common.model.vos.community.building.admin.PageBuildingListAdminVo;
import com.panzhihua.common.model.vos.puda.BasicDataVO;
import com.panzhihua.common.model.vos.puda.CitizenInfoVO;
import com.panzhihua.common.model.vos.puda.DynamicVO;
import com.panzhihua.common.model.vos.puda.SpecialPeopleVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.service_community.service.ComActBuildingService;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.screen.*;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.*;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ComActDynDO;
import com.panzhihua.service_community.service.ScreenWorkService;
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
 
/**
 * @author xyh
 * @date 2021/6/15 15:14
 */
@Service
public class ScreenWorkServiceImpl implements ScreenWorkService {
 
    private static final Integer pageSize = 200;
    private final String moth_format_str = "yyyy-MM";
    private final String[] monthStr = new String[] {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    @Resource
    private ComActDAO comActDAO;
    @Resource
    private ComActNeighborCircleDAO comActNeighborCircleDAO;
    @Resource
    private ComActDynDAO comActDynDAO;
    @Resource
    private ComActMicroWishDAO comActMicroWishDAO;
    @Resource
    private ComActActivityDAO actActivityDAO;
    @Resource
    private ComActDiscussDAO comActDiscussDAO;
    @Resource
    private ComPbDynDAO comPbDynDAO;
    @Resource
    private ComActEasyPhotoDAO comActEasyPhotoDAO;
    @Resource
    private ComScreenDao comScreenDao;
    @Resource
    private ComActBuildingService comActBuildingService;
 
    @Override
    public R statisticNeighborCircle(Long communityId) {
 
        ComActNeighborCircleScreenVO comActNeighborCircleScreenVO = new ComActNeighborCircleScreenVO();
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Long> countMap = comActNeighborCircleDAO.countByCommunityId(communityId, date);
        comActNeighborCircleScreenVO.setCurrentNum(countMap.get("currentNum"));
        comActNeighborCircleScreenVO.setTotalNum(countMap.get("totalNum"));
        Map<String, Object> sumMap = comActNeighborCircleDAO.sumScreenNum(communityId);
        comActNeighborCircleScreenVO.setFabulousNum(
            sumMap.get("fabulousNum") == null ? 0L : Long.valueOf(sumMap.get("fabulousNum").toString()));
        comActNeighborCircleScreenVO
            .setCommentNum(sumMap.get("commentNum") == null ? 0L : Long.valueOf(sumMap.get("commentNum").toString()));
        comActNeighborCircleScreenVO
            .setSharedNum(sumMap.get("forwardNum") == null ? 0L : Long.valueOf(sumMap.get("forwardNum").toString()));
        // 查询最近发布的邻里圈
        comActNeighborCircleScreenVO.setList(comActNeighborCircleDAO.screenNeighborCircle(communityId, pageSize));
        // 查询最近发布的邻里圈图片
        comActNeighborCircleScreenVO
            .setImgs(convertStringList(comActNeighborCircleDAO.screenNeighborCircleImgs(communityId, pageSize)));
        return R.ok(comActNeighborCircleScreenVO);
    }
 
    @Override
    public R wish(Long communityId) {
        ComActMicroWishScreenVO wishScreenVO = new ComActMicroWishScreenVO();
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        // 统计本月新增,累计实现,待实现
        Map<String, Long> countMap = comActMicroWishDAO.countByCommunityId(communityId, date);
        wishScreenVO.setCompletedNum(countMap.get("completedNum") == null ? 0L : countMap.get("completedNum"));
        wishScreenVO.setWillNum(countMap.get("willNum") == null ? 0L : countMap.get("willNum"));
        wishScreenVO.setCurrentNum(countMap.get("currentNum") == null ? 0L : countMap.get("currentNum"));
        // 微心愿轮播图片
        wishScreenVO.setImgs(convertStringList(comActMicroWishDAO.getAllImgs(communityId, pageSize)));
        return R.ok(wishScreenVO);
    }
 
    private List<String> convertStringList(List<String> imgList) {
        List<String> imgs = new ArrayList<>();
        for (String img : imgList) {
            if (StringUtils.isEmpty(img)) {
                continue;
            }
            String[] arrs = img.split(",");
            if (arrs.length > 1) {
                for (String arr : arrs) {
                    imgs.add(arr);
                }
            } else {
                imgs.add(img);
            }
        }
        return imgs;
    }
 
    @Override
    public R activity(Long communityId) {
        ComActActivityScreenVO comActActivityScreenVO = new ComActActivityScreenVO();
 
        comActActivityScreenVO.setImgs(convertStringList(actActivityDAO.listImg(communityId)));
        // 统计本月新增、居民活动,志愿者活动
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Long> countMap = actActivityDAO.countByCommunityId(communityId, date);
        comActActivityScreenVO.setTotalNum(countMap.get("totalNum") == null ? 0L : countMap.get("totalNum"));
        comActActivityScreenVO.setCurrentNum(countMap.get("currentNum") == null ? 0L : countMap.get("currentNum"));
        comActActivityScreenVO.setCommonNum(countMap.get("commonNum") == null ? 0L : countMap.get("commonNum"));
        comActActivityScreenVO
            .setCommonPeopleNum(countMap.get("commonPeopleNum") == null ? 0L : countMap.get("commonPeopleNum"));
        comActActivityScreenVO
            .setVolunteerNum(countMap.get("volunteerNum") == null ? 0L : countMap.get("volunteerNum"));
        comActActivityScreenVO.setVolunteerPeopleNum(
            countMap.get("volunteerPeopleNum") == null ? 0L : countMap.get("volunteerPeopleNum"));
        // 最近活动
        comActActivityScreenVO.setList(actActivityDAO.screenActivity(communityId, pageSize));
        return R.ok(comActActivityScreenVO);
    }
 
    @Override
    public R discuss(Long communityId) {
        ComActDiscussScreenVO discussScreenVO = new ComActDiscussScreenVO();
        // 统计本月新增,总共,图文,投票
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Long> countMap = comActDiscussDAO.countByCommunityId(communityId, date);
        discussScreenVO.setCurrentNum(countMap.get("currentNum") == null ? 0L : countMap.get("currentNum"));
        discussScreenVO.setTotalNum(countMap.get("totalNum") == null ? 0L : countMap.get("totalNum"));
        discussScreenVO.setImgNum(countMap.get("imgNum") == null ? 0L : countMap.get("imgNum"));
        discussScreenVO.setImgPeopleNum(countMap.get("imgPeopleNum") == null ? 0L : countMap.get("imgPeopleNum"));
        discussScreenVO.setVoteNum(discussScreenVO.getTotalNum() - discussScreenVO.getImgNum());
        discussScreenVO.setVotePeopleNum(countMap.get("votePeopleNum") == null ? 0L : countMap.get("votePeopleNum"));
        discussScreenVO.setList(comActDiscussDAO.screenDiscuss(communityId, pageSize));
        return R.ok(discussScreenVO);
    }
 
    @Override
    public R pbWork(Long communityId) {
        ComPbWorkScreenVO pbWorkScreenVO = new ComPbWorkScreenVO();
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Long> countMap = comPbDynDAO.countByCommunityId(communityId, date);
        pbWorkScreenVO.setActivityNum(countMap.get("activityNum") == null ? 0L : countMap.get("activityNum"));
        pbWorkScreenVO.setDynNum(countMap.get("dynNum") == null ? 0L : countMap.get("dynNum"));
        pbWorkScreenVO.setTotalNum(pbWorkScreenVO.getActivityNum() + pbWorkScreenVO.getDynNum());
        pbWorkScreenVO.setCurrentNum(countMap.get("currentNum") == null ? 0L : countMap.get("currentNum"));
        // 统计近半年数据
        List<PbWorkVO> list = new ArrayList<>();
        for (PbWorkVO workVO : listHalfYear()) {
            PbWorkVO result = comPbDynDAO.countByTime(workVO.getStart(), workVO.getEnd(), communityId);
            result.setMonth(workVO.getMonth());
            list.add(result);
        }
        pbWorkScreenVO.setList(list);
        return R.ok(pbWorkScreenVO);
    }
 
    private List<PbWorkVO> listHalfYear() {
        List<PbWorkVO> dateList = new ArrayList<>();
        Date now = new Date();
        for (int i = 6; i >= 1; i--) {
            Date date = DateUtils.getDateM(now, -i);
            DateTime endDay = DateUtil.endOfMonth(date);
 
            int m = DateUtil.month(endDay);
            if (m == 0) {
                m = 12;
            }
            m--;
            int day = DateUtil.dayOfMonth(endDay);
            int half = day / 2;
            String month = DateUtil.format(date, moth_format_str);
            PbWorkVO pbWorkVO = new PbWorkVO();
            pbWorkVO.setMonth(monthStr[m] + "月上旬");
            pbWorkVO.setStart(month + "-01 00:00:00");
            pbWorkVO.setEnd(month + "-" + half + " 23:59:58");
            dateList.add(pbWorkVO);
            PbWorkVO pbWorkVO2 = new PbWorkVO();
            pbWorkVO2.setMonth(monthStr[m] + "月下旬");
            pbWorkVO2.setStart(month + "-" + half + " 23:59:58");
            pbWorkVO2.setEnd(DateUtils.getDateFormatString(endDay, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(pbWorkVO2);
        }
        return dateList;
 
    }
 
    @Override
    public R easyPhoto(Long communityId) {
        ComActEasyPhotoScreenVO comActEasyPhotoScreenVO = new ComActEasyPhotoScreenVO();
        comActEasyPhotoScreenVO.setDeal(comActEasyPhotoDAO.countDeal(communityId));
        comActEasyPhotoScreenVO.setPub(comActEasyPhotoDAO.countPub(communityId));
        comActEasyPhotoScreenVO.setApproved(comActEasyPhotoDAO.countApproved(communityId));
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Object> countMap = comActEasyPhotoDAO.countByCommunityId(communityId, date);
        comActEasyPhotoScreenVO.setCurrentNum(
            countMap.get("currentNum") == null ? 0L : Long.valueOf(countMap.get("currentNum").toString()));
        comActEasyPhotoScreenVO
            .setNoneDeal(countMap.get("noneDeal") == null ? 0L : Long.valueOf(countMap.get("noneDeal").toString()));
        comActEasyPhotoScreenVO
            .setPubPoint(countMap.get("pubPoint") == null ? 0 : Integer.valueOf(countMap.get("pubPoint").toString()));
        comActEasyPhotoScreenVO.setDealTime(
            countMap.get("dealTime") == null ? 0 : Double.valueOf(countMap.get("dealTime").toString()).intValue());
        List<EastPhotoVO> list = new ArrayList<>();
        for (PbWorkVO workVO : listHalfYear()) {
            String nowDate = DateUtils.getDateFormatString(DateUtil.parseDate(workVO.getStart()), moth_format_str);
            EastPhotoVO result =
                comActEasyPhotoDAO.countByTime(workVO.getStart(), workVO.getEnd(), nowDate, communityId);
            result.setMonth(workVO.getMonth());
            list.add(result);
        }
        comActEasyPhotoScreenVO.setList(list);
        return R.ok(comActEasyPhotoScreenVO);
    }
 
    private List<EasyPhotoPointVO> getList(Map<String, Long> dealMap) {
        Set<Map.Entry<String, Long>> entrySet = dealMap.entrySet();
 
        List<EasyPhotoPointVO> list = new ArrayList<>();
        for (Map.Entry<String, Long> en : entrySet) {
            EasyPhotoPointVO easyPhotoPointVO = new EasyPhotoPointVO();
            easyPhotoPointVO.setTypeName(en.getKey());
            easyPhotoPointVO.setPoint(en.getValue().intValue());
            list.add(easyPhotoPointVO);
        }
        return list;
 
    }
 
    @Override
    public R workCount(Long communityId) {
        ComActWorkScreenVO comActWorkScreenVO = new ComActWorkScreenVO();
        // 已完成
        comActWorkScreenVO.setCompletetList(comActNeighborCircleDAO.countAllCompletedWorkByCommunityId(communityId));
 
        // 未完成
        comActWorkScreenVO.setNoneList(comActNeighborCircleDAO.countAllNoneCompletedWorkByCommunityId(communityId));
        comActWorkScreenVO
            .setTotalCompleted(comActWorkScreenVO.getCompletetList().stream().mapToInt(PieElementVO::getNum).sum());
        comActWorkScreenVO
            .setTotalNoneCompleted(comActWorkScreenVO.getNoneList().stream().mapToInt(PieElementVO::getNum).sum());
 
        // 统计平均耗时、平均每天完成个数
        Map<String, Object> avgMap = comActNeighborCircleDAO.countAvgByCommunityId(communityId);
        comActWorkScreenVO.setAvgTime(avgMap.get("avgTime") == null ? "0h"
            : convertTimeStr(Double.valueOf(avgMap.get("avgTime").toString()).intValue()));
 
        ComActDO comActDO = comActDAO.selectById(communityId);
        BigDecimal days = new BigDecimal(DateUtil.betweenDay(comActDO.getCreateAt(), new Date(), false));
        BigDecimal num = new BigDecimal(comActWorkScreenVO.getTotalCompleted());
        BigDecimal rt = num.divide(days, 1, RoundingMode.HALF_UP);
        comActWorkScreenVO.setEveryDayNum(rt.toString());
        return R.ok(comActWorkScreenVO);
    }
 
    private String convertTimeStr(int minute) {
        int hour = minute / 60;
        int min = minute % 60;
        StringBuilder timestr = new StringBuilder();
        if (hour > 0) {
            timestr.append(hour).append("h");
        }
        if (min > 0) {
            timestr.append(min).append("min");
        }
        return timestr.toString();
    }
 
    @Override
    public R dynamicWork(Long communityId) {
        ComDynamicWorkScreenVO workScreenVO = new ComDynamicWorkScreenVO();
        String date = DateUtils.getDateFormatString(new Date(), moth_format_str);
        Map<String, Long> countMap = comActDynDAO.countByCommunityId(communityId, date);
        if (!countMap.isEmpty()) {
            workScreenVO
                .setTotalNum(countMap.get("dynTotal") == null ? 0L : Long.valueOf(countMap.get("dynTotal").toString()));
            workScreenVO.setCurrentNum(
                countMap.get("currentNum") == null ? 0L : Long.valueOf(countMap.get("currentNum").toString()));
        }
 
        // 计算平均每天动态
        DateScreenVO countAvg = comActDynDAO.countByAvgCommunityId(communityId);
        if (countAvg != null) {
            int day = DateUtils.differentDays(countAvg.getStartTime(), countAvg.getEndTime());
            if (day > 0) {
                workScreenVO.setCurrentAvgNum(workScreenVO.getTotalNum().intValue() / day);
            }
        }
 
        // 查询最新的10条社区动态
        List<String> dynList = new ArrayList<>();
        List<ComActDynDO> actDynDOList = comActDynDAO.selectList(new QueryWrapper<ComActDynDO>().lambda()
            .eq(ComActDynDO::getCommunityId, communityId).orderByDesc(ComActDynDO::getCreateAt).last("limit 10"));
        if (!actDynDOList.isEmpty()) {
            actDynDOList.forEach(actDyn -> {
                dynList.add(actDyn.getTitle());
            });
        }
        workScreenVO.setDynList(dynList);
 
        // 统计近半年数据
        List<DynamicWorkVO> list = new ArrayList<>();
        for (DynamicWorkVO dynamicWorkVO : listHalfYearByDyn()) {
            DynamicWorkVO result =
                comActDynDAO.countByTime(dynamicWorkVO.getStart(), dynamicWorkVO.getEnd(), communityId);
            result.setMonth(dynamicWorkVO.getMonth());
            list.add(result);
        }
        workScreenVO.setList(list);
 
        return R.ok(workScreenVO);
    }
 
    @Override
    public R autonomyDynamic(Long communityId) {
        DynamicVO dynamicVO =comScreenDao.autonomyDynamic(communityId);
        return R.ok(dynamicVO);
    }
 
    @Override
    public R property(Long communityId) {
        return R.ok(comScreenDao.propertyData(communityId));
    }
 
    @Override
    public R partyData(Long communityId) {
        return R.ok(comScreenDao.partyData(communityId));
    }
 
    @Override
    public R yard(Long communityId) {
        return R.ok(comScreenDao.yard(communityId));
    }
 
    @Override
    public R citizenInfo(Long communityId) {
        DateVO vo=new DateVO();
        vo.setCommunityId(communityId);
        //当前日期
        vo.setNow(DateUtil.now());
        //16年前
        vo.setOffset16(DateUtil.offsetMonth(DateUtil.date(), -192).toDateStr());
        //27年前
        vo.setOffset27(DateUtil.offsetMonth(DateUtil.date(), -324).toDateStr());
        //35年前
        vo.setOffset35(DateUtil.offsetMonth(DateUtil.date(), -420).toDateStr());
        //45年前
        vo.setOffset45(DateUtil.offsetMonth(DateUtil.date(), -540).toDateStr());
        //55年前
        vo.setOffset55(DateUtil.offsetMonth(DateUtil.date(), -660).toDateStr());
        CitizenInfoVO citizenInfoVO = comScreenDao.ageRange(vo);
        citizenInfoVO.setTotal(comScreenDao.buildingListHeaderStatisticsAdmin(communityId).getPopulationNum());
        citizenInfoVO.setMaleRate(NumberUtil.div(citizenInfoVO.getMaleNum(), citizenInfoVO.getTotal(), 2));
        citizenInfoVO.setFemaleRate(NumberUtil.div(citizenInfoVO.getFemaleNum(), citizenInfoVO.getTotal(), 2));
        //男性占比
        return R.ok(citizenInfoVO);
    }
 
    @Override
    public R specialPeople(Long communityId) {
        SpecialPeopleVO vo=new SpecialPeopleVO();
        vo.setTotal(0);
        List<SpecialPeopleVO> specialPeopleVOS = comScreenDao.specialPeople(communityId);
        //求和
        for (SpecialPeopleVO specialPeopleVO : specialPeopleVOS) {
            vo.setTotal(vo.getTotal()+specialPeopleVO.getCount());
        }
        //计算占比
        for (SpecialPeopleVO specialPeopleVO : specialPeopleVOS) {
            specialPeopleVO.setRate(NumberUtil.div(specialPeopleVO.getCount(),vo.getTotal() ,2 ));
        }
        vo.setChildList(specialPeopleVOS);
        return R.ok(vo);
    }
 
    @Override
    public R basicData(Long communityId) {
        BuildingListHeaderStatisticsAdminVo buildingListHeaderStatisticsAdminVo=comScreenDao.buildingListHeaderStatisticsAdmin(communityId);
        BasicDataVO vo=new BasicDataVO();
        vo.setVillage(buildingListHeaderStatisticsAdminVo.getBuildNum());
        vo.setPeople(buildingListHeaderStatisticsAdminVo.getPopulationNum());
        vo.setBuildings(buildingListHeaderStatisticsAdminVo.getHouseNum());
//        vo.setBuildings(comScreenDao.assetAnalysis(communityId));
        return R.ok(vo);
    }
 
    private List<DynamicWorkVO> listHalfYearByDyn() {
        List<DynamicWorkVO> dateList = new ArrayList<>();
        Date now = new Date();
        for (int i = 5; i >= 0; i--) {
            Date date = DateUtils.getDateM(now, -i);
            DateTime endDay = DateUtil.endOfMonth(date);
 
            int m = DateUtil.month(endDay);
            m++;
            int day = DateUtil.dayOfMonth(endDay);
            int half = day / 2;
            String month = DateUtil.format(date, moth_format_str);
            DynamicWorkVO dynamicWorkVO = new DynamicWorkVO();
            dynamicWorkVO.setMonth(monthStr[m-1] + "月上旬");
            dynamicWorkVO.setStart(month + "-01 00:00:00");
            dynamicWorkVO.setEnd(month + "-" + half + " 23:59:58");
            dateList.add(dynamicWorkVO);
            DynamicWorkVO dynamicWorkVO1 = new DynamicWorkVO();
            dynamicWorkVO1.setMonth(monthStr[m-1] + "月下旬");
            dynamicWorkVO1.setStart(month + "-" + half + " 23:59:58");
            dynamicWorkVO1.setEnd(DateUtils.getDateFormatString(endDay, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(dynamicWorkVO1);
        }
        return dateList;
    }
 
 
}