zhongrj
2024-01-31 60b96e9221632debb08aaf5fb7abd8361a90c0fb
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.grid.service.impl;
 
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.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.springblade.common.cache.SysCache;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.grid.entity.GridEntity;
import org.springblade.modules.grid.entity.GridmanEntity;
import org.springblade.modules.grid.excel.GridmanExcel;
import org.springblade.modules.grid.mapper.GridmanMapper;
import org.springblade.modules.grid.service.IGridService;
import org.springblade.modules.grid.service.IGridmanService;
import org.springblade.modules.grid.vo.GridmanVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IRegionService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * 网格员表 服务实现类
 *
 * @author BladeX
 * @since 2023-11-27
 */
@Service
public class GridmanServiceImpl extends ServiceImpl<GridmanMapper, GridmanEntity> implements IGridmanService {
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private IGridService gridService;
 
    @Override
    public IPage<GridmanVO> selectGridmanPage(IPage<GridmanVO> page, GridmanVO gridman) {
        List<String> regionChildCodesList = SysCache.getRegionChildCodesByDeptId(AuthUtil.getDeptId());
        Integer isAdministrator = AuthUtil.isAdministrator() == true ? 1 : 2;
        return page.setRecords(baseMapper.selectGridmanPage(page, gridman, regionChildCodesList, isAdministrator));
    }
 
    /**
     * 网格员表 自定义新增或修改
     *
     * @param gridman
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateGridman(GridmanEntity gridman) {
        boolean flag = false;
        // 修改
        if (null != gridman.getId()) {
            // 更新网格员信息
            flag = updateById(gridman);
        } else {
            // 新增
            // 先判断用户表中是否已存在该用户,如果已存在则不新增,需要更新角色
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("is_deleted", 0).eq("account", gridman.getMobile());
            List<User> list = userService.list(wrapper);
            // 更新用户,查询是否需要更新角色
            // 查询网格id
            IGridService bean = SpringUtil.getBean(IGridService.class);
            GridEntity gridEntity = bean.getOne(Wrappers.<GridEntity>lambdaQuery().eq(GridEntity::getGridCode, gridman.getGridCode()));
            gridman.setGridId(gridEntity.getId());
            if (list.size() > 0) {
                // 默认取出第一个
                User user = list.get(0);
                // 判断角色是否包好网格员的角色,如果没有则加入网格员的角色,有则不更改
                if (!user.getRoleId().contains("1717429261910528001")) {
                    user.setRoleId(user.getRoleId() + "," + "1717429261910528001");
                    // 更新用户信息
                    userService.updateById(user);
                }
                //插入网格员信息
                gridman.setUserId(user.getId());
                //匹配
                QueryWrapper<GridmanEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("is_deleted", 0)
                    .eq("grid_id", gridman.getGridId())
                    .eq("user_id", gridman.getUserId());
                GridmanEntity one = getOne(queryWrapper);
                if (null == one) {
                    flag = save(gridman);
                }
            } else {
                saveUser(gridman);
                //匹配
                QueryWrapper<GridmanEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("is_deleted", 0)
                    .eq("grid_id", gridman.getGridId())
                    .eq("user_id", gridman.getUserId());
                GridmanEntity one = getOne(queryWrapper);
                if (null == one) {
                    flag = save(gridman);
                }
            }
        }
        // 返回
        return flag;
    }
 
    /**
     * 用户新增
     *
     * @param gridman
     */
    public void saveUser(GridmanEntity gridman) {
        // 新增用户
        User userInfo = new User();
        // 设置机构
        GridEntity gridEntity = gridService.getById(gridman.getGridId());
        if (null != gridEntity) {
            userInfo.setDeptId(gridEntity.getDeptId().toString());
        }
        // 设置账号信息
        userInfo.setAccount(gridman.getMobile());
        userInfo.setPhone(gridman.getMobile());
        userInfo.setName(gridman.getGridmanName());
        userInfo.setRealName(gridman.getGridmanName());
        // 设置网格员角色
        userInfo.setRoleId("1717429261910528001");
        // 设置密码,默认密码为 123456
        userInfo.setPassword("123456");
        // 插入用户
        userService.submit(userInfo);
        // 设置网格信息
        gridman.setUserId(userInfo.getId());
    }
 
    /**
     * 网格员导入
     *
     * @param data
     * @param isCovered
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importGridman(List<GridmanExcel> data, Boolean isCovered) {
        // 遍历
        for (GridmanExcel gridmanExcel : data) {
            // 先查询是否有对应的用户
            List<User> userList = userService.getUserListByPhoneOrAccount(gridmanExcel.getMobile());
            if (userList.size()>0){
                User user = userList.get(0);
                // 判断角色是否包好网格员的角色,如果没有则加入网格员的角色,有则不更改
                if (!user.getRoleId().contains("1717429261910528001")) {
                    // 更新角色
                    user.setRoleId(user.getRoleId() + "," + "1717429261910528001");
                    // 更新机构
                    setDeptId(gridmanExcel,user);
                    // 更新用户信息
                    userService.updateById(user);
                    // 更新网格员绑定
                    updateGridmanBind(gridmanExcel,user);
                }
            }else {
                // 创建用户
                User user = new User();
                user.setTenantId("000000");
                user.setUserType(1);
                user.setRoleId("1717429261910528001");
                user.setName(gridmanExcel.getGridmanName());
                user.setRealName(gridmanExcel.getGridmanName());
                user.setPhone(gridmanExcel.getMobile());
                user.setAccount(gridmanExcel.getMobile());
                // 更新机构
                setDeptId(gridmanExcel,user);
                // 设置默认密码
                user.setPassword("123456");
                // 保存
                userService.submit(user);
                // 更新网格员绑定
                updateGridmanBind(gridmanExcel,user);
            }
 
        }
    }
 
    /**
     * 更新网格员绑定
     * @param gridmanExcel
     * @param user
     */
    public void updateGridmanBind(GridmanExcel gridmanExcel, User user) {
        // 保存更新网格员信息
        GridmanEntity gridmanEntity = Objects.requireNonNull(BeanUtil.copy(gridmanExcel, GridmanEntity.class));
        // 通过社区名称和网格名称获取网格id
        QueryWrapper<GridEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0)
            .eq("grid_code", gridmanExcel.getGridCode());
        GridEntity gridEntity = gridService.getOne(queryWrapper);
        // 设置网格id
        gridmanEntity.setGridId(gridEntity.getId());
        gridmanEntity.setUserId(user.getId());
        // 插入网格员
        // 判断是否已存,已存在则更新,否则插入
        QueryWrapper<GridmanEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0).eq("user_id",user.getId()).eq("grid_code",gridmanExcel.getGridCode());
        GridmanEntity entity = getOne(wrapper);
        if (null!=entity){
            gridmanEntity.setId(entity.getId());
            updateById(gridmanEntity);
        }else {
            save(gridmanEntity);
        }
    }
 
    /**
     * 更新机构
     * @param gridmanExcel
     * @param user
     */
    public void setDeptId(GridmanExcel gridmanExcel, User user) {
        // 通过社区编号查询对应的机构id
        Region region = SpringUtil.getBean(IRegionService.class).getById(gridmanExcel.getCommunityCode());
        // 查询社区对应的机构
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0).eq("dept_name",region.getName());
        Dept dept = SpringUtil.getBean(IDeptService.class).getOne(wrapper);
        // 查询网格对应的机构
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0).
            eq("parent_id",dept.getId()).
            eq("dept_name",gridmanExcel.getGridName());
        System.out.println("网格名称 = " + gridmanExcel.getGridName());
        Dept deptGrid = SpringUtil.getBean(IDeptService.class).getOne(queryWrapper);
        // 设置机构
        user.setDeptId(deptGrid.getId().toString());
    }
 
    /**
     * 网格员查询
     *
     * @param gridman
     * @return
     */
    @Override
    public List<GridmanVO> getGridmanList(GridmanVO gridman) {
        return baseMapper.getGridmanList(gridman);
    }
 
    @Override
    public Object getGridStatistics(String code, String roleType) {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        if (roleType.equals("2")) {
            Integer gridStatistics = baseMapper.getGridStatistics(code, null, roleType);
            Integer companyStatistics = baseMapper.getCompanyStatistics(code, null, roleType);
            Integer ownersCommitteeStatistics = baseMapper.getOwnersCommitteeStatistics(code, null, roleType);
            objectObjectHashMap.put("gridStatistics", gridStatistics);
            objectObjectHashMap.put("companyStatistics", companyStatistics);
            objectObjectHashMap.put("ownersStatistics", ownersCommitteeStatistics);
        } else {
            Integer gridStatistics = baseMapper.getGridStatistics(code, AuthUtil.getUserId(), roleType);
            Integer companyStatistics = baseMapper.getCompanyStatistics(code, AuthUtil.getUserId(), roleType);
            Integer ownersCommitteeStatistics = baseMapper.getOwnersCommitteeStatistics(code, AuthUtil.getUserId(), roleType);
            objectObjectHashMap.put("gridStatistics", gridStatistics);
            objectObjectHashMap.put("companyStatistics", companyStatistics);
            objectObjectHashMap.put("ownersStatistics", ownersCommitteeStatistics);
 
        }
        return objectObjectHashMap;
    }
 
    /**
     * 网格员表 自定义详情
     */
    @Override
    public GridmanVO getDetail(GridmanEntity gridman) {
        return baseMapper.getDetail(gridman);
    }
 
    /**
     * 查询网格id
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getGridIdByUserId(Long userId) {
        return baseMapper.getGridIdByUserId(userId);
    }
 
    @Override
    public List<UserEntity> getGridManByCode(String houseCode) {
        return baseMapper.getGridManByCode(houseCode);
    }
}