Jinquan_Ou
2023-04-13 9aff69d38fa75981560ce21807be4b30122ef8f4
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
package com.ruoyi.service.impl;
 
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfEvent;
import com.ruoyi.domain.ZfRelation;
import com.ruoyi.domain.dto.EmpowerDto;
import com.ruoyi.domain.dto.UserRelationDto;
import com.ruoyi.mapper.ZInfoUserMapper;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-14
 */
@Service
@Slf4j
public class ZInfoUserServiceImpl extends ServiceImpl<ZInfoUserMapper, ZInfoUser> implements ZInfoUserService {
 
    @Autowired
    private ZfRelationService zfRelationService;
 
    @Override
    public AjaxResult selectInfoList(ZInfoUser zInfoUser, Integer pageNum, Integer pageSize) {
 
        LambdaQueryWrapper<ZInfoUser> lqw = buildCondition(zInfoUser);
 
        Page<ZInfoUser> zInfoUserPage = new Page<>(pageNum,pageSize);
        Page<ZInfoUser> pageResult = page(zInfoUserPage, lqw);
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult);
        return AjaxResult.success(data);
 
    }
 
    private LambdaQueryWrapper<ZInfoUser> buildCondition(ZInfoUser zInfoUser) {
 
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zInfoUser.getUserId()!=null,ZInfoUser::getUserId,zInfoUser.getUserId());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getAlwaysAddress()),ZInfoUser::getAlwaysAddress,zInfoUser.getAlwaysAddress());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getAlwaysPolice()),ZInfoUser::getAlwaysPolice,zInfoUser.getAlwaysPolice());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getIdNum()),ZInfoUser::getIdNum,zInfoUser.getIdNum());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getLocationAddress()),ZInfoUser::getLocationAddress,zInfoUser.getLocationAddress());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getLocationPolice()),ZInfoUser::getLocationPolice,zInfoUser.getLocationPolice());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getNation()),ZInfoUser::getNation,zInfoUser.getNation());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getNationality()),ZInfoUser::getNationality,zInfoUser.getNationality());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getNickName()),ZInfoUser::getNickName,zInfoUser.getNickName());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getOldName()),ZInfoUser::getOldName,zInfoUser.getOldName());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getPhoneNumber()),ZInfoUser::getPhoneNumber,zInfoUser.getPhoneNumber());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getPoliticalOutlook()),ZInfoUser::getPoliticalOutlook,zInfoUser.getPoliticalOutlook());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getPosition()),ZInfoUser::getPosition,zInfoUser.getPosition());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getUnit()),ZInfoUser::getUnit,zInfoUser.getUnit());
        lqw.like(zInfoUser.getSex()!=null,ZInfoUser::getSex,zInfoUser.getSex());
        lqw.like(zInfoUser.getMaritalStatus()!=null,ZInfoUser::getMaritalStatus,zInfoUser.getMaritalStatus());
        lqw.like(zInfoUser.getBirth()!=null,ZInfoUser::getBirth,zInfoUser.getBirth());
        return lqw;
 
    }
 
    @Override
    public List<ZInfoUser> selectByCondition(ZInfoUser zInfoUser) {
        LambdaQueryWrapper<ZInfoUser> lambdaQueryWrapper = buildCondition(zInfoUser);
        List<ZInfoUser> list = list(lambdaQueryWrapper);
        log.info("返回的数据为:{}",list);
        return list;
 
    }
 
    @Override
    public AjaxResult searchMyRelation(Long userId) {
 
        LambdaQueryWrapper<ZfRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfRelation::getMyId,userId);
        List<ZfRelation> relationList = zfRelationService.list(lqw);
 
        ArrayList<UserRelationDto> otherUserList = new ArrayList<>();
 
        relationList.stream().map(
                (relation)->{
                    Long otherId = relation.getOtherId();
                    ZInfoUser otherUser = getById(otherId);
                    UserRelationDto userRelationDto = new UserRelationDto();
                    BeanUtils.copyProperties(otherUser,userRelationDto);
                    userRelationDto.setRelation(relation.getRelation());
                    userRelationDto.setId(relation.getId());
                    otherUserList.add(userRelationDto);
                    return relation;
                }
        ).collect(Collectors.toList());
 
        return AjaxResult.success(otherUserList);
    }
 
    /**
     * 检查传入对方的id是否存在
     * @param zfRelation
     */
    private void checkRelation(ZfRelation zfRelation){
 
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZInfoUser::getUserId, zfRelation.getOtherId());
 
        ZInfoUser zInfoUser2 = getOne(lqw);
        if (zInfoUser2==null){
            throw new RuntimeException("对方的id不存在");
        }
    }
 
    @Override
    @Transactional
    public AjaxResult addRelation(ZfRelation zfRelation) {
        checkRelation(zfRelation);
 
        //获取自己的id并传入
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        zfRelation.setMyId(userId);
 
        zfRelationService.save(zfRelation);
        return AjaxResult.success();
    }
 
    @Override
    @Transactional
    public AjaxResult updateRelation(ZfRelation zfRelation) {
        checkRelation(zfRelation);
        zfRelationService.updateById(zfRelation);
        return AjaxResult.success();
    }
 
    @Override
    public AjaxResult deleteRelation(Long[] ids) {
        boolean flag = zfRelationService.removeByIds(Arrays.asList(ids));
        if (flag){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
 
    /**
     * 给用户授权的方法
     * @param empowerDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult empower(EmpowerDto empowerDto) {
 
        //得到dto中的信息
        String otherUserId = empowerDto.getUserId();
        String destinationFamilyId = empowerDto.getFamilyId();
        List<String> authorityList = empowerDto.getAuthorityList();
 
        String familyId = getMyFamilyId();
        checkAuthorization(familyId, destinationFamilyId,true);
 
        //授权  //3{2007 1988 1004}
        String finalStr = getFinalStr(destinationFamilyId, authorityList);
 
        //更新被授权用户的权限
        LambdaQueryWrapper<ZInfoUser> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(ZInfoUser::getUserId,otherUserId);
        ZInfoUser otherUser = getOne(lqw2);
        String secondFamilyId = otherUser.getSecondFamilyId();
 
        String replaceFamilyId=null;
 
        boolean save=true;
 
        if (StringUtils.isNotEmpty(secondFamilyId)){
            //如果原来就已经有被赋予权限了,那么需要判断是不是跟这个家庭号有关的权限
 
            //这个secondFamilyId是当前用户的完整权限字段
            if(secondFamilyId.contains(",")){
                String[] split = secondFamilyId.split(",");
                for (int i = 0; i < split.length; i++) {
                    String fid = split[i].substring(0, split[i].indexOf("{"));//从权限块中拿到被授权的家庭号
                    if(fid.equals(finalStr.substring(0,finalStr.indexOf("{")))){
                        //如果匹配到了对应的家庭号,那么就替换掉对应的权限块就可以了,不需要追加
                        save=false;
                        split[i]=finalStr;
                        break;
                    }
                }
                List<String> finalList = Arrays.asList(split);
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : finalList) {
                    stringBuilder.append(s).append(",");
                }
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
 
                replaceFamilyId = stringBuilder.toString();
 
            }else {
                String fid = secondFamilyId.substring(0, secondFamilyId.indexOf("{"));//从权限块中拿到被授权的家庭号
                if(fid.equals(finalStr.substring(0,finalStr.indexOf("{")))){
                    //如果匹配到了对应的家庭号,那么就替换掉对应的权限块就可以了,不需要追加
                    save=false;
                    replaceFamilyId=finalStr;
                }
            }
 
            if(save){
                //需要追加,没有匹配到对应的家庭号
                replaceFamilyId=secondFamilyId+","+finalStr;
            }else {
                //不需要追加
            }
 
        }else {
            //原来就没有权限了
            replaceFamilyId=finalStr;
        }
 
        LambdaUpdateWrapper<ZInfoUser> uw = new LambdaUpdateWrapper<>();
        uw.eq(ZInfoUser::getUserId,otherUserId).set(ZInfoUser::getSecondFamilyId,replaceFamilyId);
        if (update(null,uw)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
 
    }
 
    private void checkAuthorization(String familyId, String destinationFamilyId,boolean flag) {
        String text=null;
        if(flag){
            text="授权";
        }else {
            text="取消授权";
        }
 
        //对本人的家庭号进行处理
        if(familyId.contains(",")){
            String[] familyIds = familyId.split(",");
            List<String> idList = Arrays.asList(familyIds);
            if (!idList.contains(destinationFamilyId)) {
                throw new RuntimeException("不能"+text+"不属于自己家庭的数据");
            }
        }else {
            if (!familyId.equals(destinationFamilyId)){
                throw new RuntimeException("不能"+text+"不属于自己家庭的数据");
            }
        }
    }
 
    private String getMyFamilyId() {
        //得到自己的家庭id信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long myUserId = user.getUserId();
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZInfoUser::getUserId,myUserId);
        ZInfoUser zInfoUser = getOne(lqw);
        String familyId = zInfoUser.getFamilyId();
        return familyId;
    }
 
 
 
    private String getFinalStr(String destinationFamilyId, List<String> authorityList) {
        String authorityListStr = authorityList.stream().collect(Collectors.joining(" ", "{", "}"));
        String finalStr= destinationFamilyId +authorityListStr;  //3{2007 1988 1004}
        return finalStr;
    }
 
 
}