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
package com.panzhihua.service_community.service.impl;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
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.panzhihua.common.model.dtos.community.ComSwRotaPageDTO;
import com.panzhihua.common.model.dtos.community.ComSwRotaSaveDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComSwRotaExcelVO;
import com.panzhihua.common.model.vos.community.ComSwRotaPersonVO;
import com.panzhihua.common.model.vos.community.ComSwRotaVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.ComSwPatrolRecordDAO;
import com.panzhihua.service_community.dao.ComSwRotaDAO;
import com.panzhihua.service_community.model.dos.ComSwRotaDO;
import com.panzhihua.service_community.service.ComSwRotaService;
 
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @auther txb
 * @create 2021-07-30 09:48:54
 * @describe 社区》安全工作》值班表服务实现类
 */
@Slf4j
@Service
public class ComSwRotaServiceImpl extends ServiceImpl<ComSwRotaDAO, ComSwRotaDO> implements ComSwRotaService {
 
    @Resource
    private ComSwRotaDAO comSwRotaDAO;
    @Resource
    private UserService userService;
 
    @Resource
    private ComSwPatrolRecordDAO comSwPatrolRecordDAO;
 
    @Override
    public R pageRota(ComSwRotaPageDTO comSwRotaPageDTO) {
        Page page = new Page<>();
        Long pageNum = comSwRotaPageDTO.getPageNum();
        Long pageSize = comSwRotaPageDTO.getPageSize();
        if (null == pageNum || 0 == pageNum) {
            pageNum = 1L;
        }
        if (null == pageSize || 0 == pageSize) {
            pageSize = 10L;
        }
        page.setSize(pageSize);
        page.setCurrent(pageNum);
 
        IPage<ComSwRotaVO> iPage = comSwRotaDAO.pageRota(page, comSwRotaPageDTO);
        if (null != iPage.getRecords()) {
            iPage.getRecords().forEach(comSwRotaVO -> {
                comSwRotaVO.setWeekDay(DateUtils.dateToWeek(comSwRotaVO.getRotaDate()));
                StringBuilder leaderNameString = new StringBuilder();
                StringBuilder leaderPhoneString = new StringBuilder();
                // 值班领导id查询值班领导的名字和电话
                if (StringUtils.isNotEmpty(comSwRotaVO.getRotaLeader())) {
                    String[] leaderIds = comSwRotaVO.getRotaLeader().split(",");
                    for (int i = 0; i < leaderIds.length; i++) {
                        Map<String, String> leader = comSwPatrolRecordDAO.getPbServiceTeamById(leaderIds[i]);
                        if (leader != null) {
                            if (i != 0) {
                                leaderNameString.append(",");
                                leaderPhoneString.append(",");
                            }
                            leaderNameString.append(leader.get("name"));
                            leaderPhoneString.append(leader.get("phone"));
                        }
                    }
                    comSwRotaVO.setLeaderName(leaderNameString.toString());
                    comSwRotaVO.setLeaderPhone(leaderPhoneString.toString());
                }
                // 值班人员id查询值班人员的名字和电话
                StringBuilder personNameString = new StringBuilder();
                StringBuilder personPhoneString = new StringBuilder();
                if (StringUtils.isNotEmpty(comSwRotaVO.getRotaPerson())) {
                    String[] personIds = comSwRotaVO.getRotaPerson().split(",");
                    for (int i = 0; i < personIds.length; i++) {
                        Map<String, String> person = comSwPatrolRecordDAO.getPbServiceTeamById(personIds[i]);
                        if (person != null) {
                            if (i != 0) {
                                personNameString.append(",");
                                personPhoneString.append(",");
                            }
                            personNameString.append(person.get("name"));
                            personPhoneString.append(person.get("phone"));
                        }
                    }
                    comSwRotaVO.setPersonName(personNameString.toString());
                    comSwRotaVO.setPersonPhone(personPhoneString.toString());
                }
            });
        }
        return R.ok(iPage);
    }
 
    @Override
    public R detailRota(Long rotaId) {
        ComSwRotaVO comSwRotaVO = new ComSwRotaVO();
        ComSwRotaDO comSwRotaDO = comSwRotaDAO.selectById(rotaId);
        if (null == comSwRotaDO) {
            return R.fail("该值班表不存在");
        }
        BeanUtils.copyProperties(comSwRotaDO, comSwRotaVO);
        if (null != comSwRotaDO.getRotaDate()) {
            comSwRotaVO.setRotaDate(new SimpleDateFormat("yyyy-MM-dd").format(comSwRotaDO.getRotaDate()));
        }
        comSwRotaVO.setWeekDay(DateUtils.dateToWeek(comSwRotaVO.getRotaDate()));
        StringBuilder leaderNameString = new StringBuilder();
        StringBuilder leaderPhoneString = new StringBuilder();
        // 值班领导id查询值班领导的名字和电话
        if (StringUtils.isNotEmpty(comSwRotaDO.getRotaLeader())) {
            String[] leaderIds = comSwRotaDO.getRotaLeader().split(",");
            for (int i = 0; i < leaderIds.length; i++) {
                Map<String, String> leader = comSwPatrolRecordDAO.getPbServiceTeamById(leaderIds[i]);
                if (leader != null) {
                    if (i != 0) {
                        leaderNameString.append(",");
                        leaderPhoneString.append(",");
                    }
                    leaderNameString.append(leader.get("name"));
                    leaderPhoneString.append(leader.get("phone"));
                }
            }
            comSwRotaVO.setLeaderName(leaderNameString.toString());
            comSwRotaVO.setLeaderPhone(leaderPhoneString.toString());
        }
        // 值班人员id查询值班人员的名字和电话
        StringBuilder personNameString = new StringBuilder();
        StringBuilder personPhoneString = new StringBuilder();
        if (StringUtils.isNotEmpty(comSwRotaDO.getRotaPerson())) {
            String[] personIds = comSwRotaDO.getRotaPerson().split(",");
            for (int i = 0; i < personIds.length; i++) {
                Map<String, String> person = comSwPatrolRecordDAO.getPbServiceTeamById(personIds[i]);
                if (person != null) {
                    if (i != 0) {
                        personNameString.append(",");
                        personPhoneString.append(",");
                    }
                    personNameString.append(person.get("name"));
                    personPhoneString.append(person.get("phone"));
                }
            }
            comSwRotaVO.setPersonName(personNameString.toString());
            comSwRotaVO.setPersonPhone(personPhoneString.toString());
        }
        return R.ok(comSwRotaVO);
    }
 
    @Override
    public R addRota(List<ComSwRotaSaveDTO> comSwRotaSaveDTOs) {
        for (ComSwRotaSaveDTO comSwRotaSaveDTO : comSwRotaSaveDTOs) {
            if (comSwRotaSaveDTO.getId() != null && comSwRotaSaveDTO.getId() != 0) {
                ComSwRotaDO comSwRotaDO = comSwRotaDAO.selectById(comSwRotaSaveDTO.getId());
                if (null == comSwRotaDO) {
                    return R.fail("该值班表不存在");
                }
                ComSwRotaDO checkCreditCode = comSwRotaDAO.selectOne(new QueryWrapper<ComSwRotaDO>().lambda()
                    .eq(ComSwRotaDO::getRotaDate, comSwRotaSaveDTO.getRotaDate()).eq(ComSwRotaDO::getCommunityId,comSwRotaSaveDTO.getCommunityId()));
                if (null != checkCreditCode && !comSwRotaDO.getId().equals(checkCreditCode.getId())) {
                    return R.fail("该值班表已存在,值班日期重复");
                }
                BeanUtils.copyProperties(comSwRotaSaveDTO, comSwRotaDO);
                try {
                    comSwRotaDO.setRotaDate(new SimpleDateFormat("yyyy-MM-dd").parse(comSwRotaSaveDTO.getRotaDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                comSwRotaDAO.updateById(comSwRotaDO);
            } else {
                Integer count = comSwRotaDAO.selectCount(new QueryWrapper<ComSwRotaDO>().lambda()
                    .eq(ComSwRotaDO::getRotaDate, comSwRotaSaveDTO.getRotaDate()).eq(ComSwRotaDO::getCommunityId,comSwRotaSaveDTO.getCommunityId()));
                if (count > 0) {
                    return R.fail("该值班表已存在,值班日期重复");
                }
                ComSwRotaDO comSwRotaDO = new ComSwRotaDO();
                BeanUtils.copyProperties(comSwRotaSaveDTO, comSwRotaDO);
                comSwRotaDO.setId(Snowflake.getId());
                try {
                    comSwRotaDO.setRotaDate(new SimpleDateFormat("yyyy-MM-dd").parse(comSwRotaSaveDTO.getRotaDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                comSwRotaDAO.insert(comSwRotaDO);
            }
        }
        return R.ok();
    }
 
    @Override
    public R deleteRota(List<Long> rotaIds) {
        int delete = comSwRotaDAO.deleteBatchIds(rotaIds);
        if (delete > 0) {
            return R.ok();
        }
        return R.fail("删除失败");
    }
 
    @Override
    public R getRotaPersonByDate(String rotaDate,Long communityId) {
        ComSwRotaDO comSwRotaDO = new ComSwRotaDO();
        try {
            comSwRotaDO = comSwRotaDAO.selectOne(new QueryWrapper<ComSwRotaDO>().lambda().eq(ComSwRotaDO::getRotaDate,
                new SimpleDateFormat("yyyy-MM-dd").parse(rotaDate)).eq(ComSwRotaDO::getCommunityId,communityId));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (comSwRotaDO == null) {
            return R.fail(rotaDate + "值班表不存在");
        }
        List<ComSwRotaPersonVO> rotaPersons = new ArrayList<>();
        if (null != comSwRotaDO) {
            String rotaPerson = comSwRotaDO.getRotaPerson();
            String[] rotaPersonArray = rotaPerson.split(",");
            for (int i = 0; i <= rotaPersonArray.length - 1; i++) {
                ComSwRotaPersonVO comSwRotaPersonVO = new ComSwRotaPersonVO();
                comSwRotaPersonVO.setId(Long.valueOf(rotaPersonArray[i]));
                Map<String, String> userMap = this.comSwRotaDAO.getImageUrl(Long.valueOf(rotaPersonArray[i]));
                if (userMap != null) {
                    comSwRotaPersonVO.setPhone(userMap.get("phone"));
                    comSwRotaPersonVO.setImageUrl(userMap.get("url"));
                    comSwRotaPersonVO.setName(userMap.get("name"));
                }
                rotaPersons.add(comSwRotaPersonVO);
            }
        }
        return R.ok(rotaPersons);
    }
 
    @Override
    public R getRotaLeaderByDate(String rotaDate,Long communityId) {
        ComSwRotaDO comSwRotaDO = new ComSwRotaDO();
        try {
            comSwRotaDO = comSwRotaDAO.selectOne(new QueryWrapper<ComSwRotaDO>().lambda().eq(ComSwRotaDO::getRotaDate,
                new SimpleDateFormat("yyyy-MM-dd").parse(rotaDate)).eq(ComSwRotaDO::getCommunityId,communityId));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (comSwRotaDO == null) {
            return R.fail(rotaDate + "值班表不存在");
        }
        List<ComSwRotaPersonVO> rotaLeaders = new ArrayList<>();
        if (null != comSwRotaDO) {
            String rotaPerson = comSwRotaDO.getRotaLeader();
            String[] rotaPersonArray = rotaPerson.split(",");
            for (int i = 0; i <= rotaPersonArray.length - 1; i++) {
                ComSwRotaPersonVO comSwRotaPersonVO = new ComSwRotaPersonVO();
                comSwRotaPersonVO.setId(Long.valueOf(rotaPersonArray[i]));
                Map<String, String> userMap = this.comSwRotaDAO.getImageUrl(Long.valueOf(rotaPersonArray[i]));
                if (userMap != null) {
                    comSwRotaPersonVO.setPhone(userMap.get("phone"));
                    comSwRotaPersonVO.setImageUrl(userMap.get("url"));
                    comSwRotaPersonVO.setName(userMap.get("name"));
                }
                rotaLeaders.add(comSwRotaPersonVO);
            }
        }
        return R.ok(rotaLeaders);
    }
 
    @Override
    public R getTotlePerson(String param, Long communityId) {
        return userService.listActivityManager(param, communityId);
    }
 
    @Override
    public R listSaveSwRotaExcelVO(List<ComSwRotaExcelVO> list, Long communityId) {
        String[] parsePatterns = new String[] {"yyyy-MM-dd", "yyyy-M-dd", "yyyy-MM-d", "yyyy-M-d", "yyyy/MM/dd",
            "yyyy/M/dd", "yyyy/MM/d", "yyyy/M/d"};
        List<ComSwRotaDO> comSwRotaDOS = new ArrayList<>();
        for (ComSwRotaExcelVO comSwRotaExcelVO : list) {
            Date date = DateUtil.parse(comSwRotaExcelVO.getRotaDate(), parsePatterns);
            Integer count =
                comSwRotaDAO.selectCount(new QueryWrapper<ComSwRotaDO>().lambda().eq(ComSwRotaDO::getRotaDate, date)
                        .eq(ComSwRotaDO::getCommunityId,communityId));
            if (count > 0) {
                return R.fail("值班日期已存在," + comSwRotaExcelVO.getRotaDate() + "重复");
            }
            ComSwRotaDO comSwRotaDO = new ComSwRotaDO();
            String[] leaderPhones = comSwRotaExcelVO.getLeaderPhone().split("、");
            String[] leaderNames = comSwRotaExcelVO.getLeaderName().split("、");
            List<String> leaderIds = new ArrayList<>();
            for (int i = 0; i < leaderPhones.length; i++) {
                String id = comSwRotaDAO.getTeamIdsByTeamPhone(leaderPhones[i]);
                if (StringUtils.isEmpty(id)) {
                    return R.fail(leaderNames[i] + "为非社区团队人员或该人员电话未维护!");
                }
                leaderIds.add(id);
            }
 
            String[] personPhones = comSwRotaExcelVO.getPersonPhone().split("、");
            String[] personNames = comSwRotaExcelVO.getPersonName().split("、");
            List<String> personIds = new ArrayList<>();
            for (int i = 0; i < personPhones.length; i++) {
                String id = comSwRotaDAO.getTeamIdsByTeamPhone(personPhones[i]);
                if (StringUtils.isEmpty(id)) {
                    return R.fail(personNames[i] + "为非社区团队人员或该人员电话未维护!");
                }
                personIds.add(id);
            }
            BeanUtils.copyProperties(comSwRotaExcelVO, comSwRotaDO);
            comSwRotaDO.setId(Snowflake.getId());
            comSwRotaDO.setCommunityId(communityId);
            // if (StringUtils.isNotEmpty(comSwRotaExcelVO.getRotaDate())) {
            // String el =
            // "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
            // \n" +
            // "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
            // ";
            // Pattern p = Pattern.compile(el);
            // Matcher m = p.matcher(comSwRotaExcelVO.getRotaDate());
            // if (!m.matches()) {
            // return R.fail(comSwRotaExcelVO.getRotaDate() + "日期格式不正确");
            // }
            // }
            // try {
            // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // comSwRotaDO.setRotaDate(sdf.parse(comSwRotaExcelVO.getRotaDate()));
            // } catch (ParseException e) {
            // e.printStackTrace();
            // }
            comSwRotaDO.setRotaDate(date);
            comSwRotaDO.setRotaLeader(StringUtils.join(leaderIds, ","));
            comSwRotaDO.setRotaPerson(StringUtils.join(personIds, ","));
            comSwRotaDO.setLeaderName(comSwRotaExcelVO.getLeaderName().replace("、", ","));
            comSwRotaDO.setPersonName(comSwRotaExcelVO.getPersonName().replace("、", ","));
            comSwRotaDOS.add(comSwRotaDO);
        }
        this.saveBatch(comSwRotaDOS);
        return R.ok();
    }
}