zqy
2024-07-25 6f78cfae24b940229bd282ce3b3d0e6f5b337fa7
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
 
package com.ruoyi.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.EsModel;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfClanManage;
import com.ruoyi.mapper.ZfClanManageMapper;
import com.ruoyi.service.*;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class ZfClanManageServiceImpl extends ServiceImpl<ZfClanManageMapper, ZfClanManage> implements ZfClanManageService {
 
    @Resource
    private EsService esSer;
 
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    @Resource
    private ZfClanManageService zfClanManageService;
 
    @Resource
    private  ZfClanService zfClanService;
 
//    @Resource
//    private ISysUserService iSysUserService;
 
    private LambdaQueryWrapper<ZfClanManage> buildCondition(ZfClanManage zfClanManage) {
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfClanManage::getId);
        lqw.like(zfClanManage.getClanId() !=null , ZfClanManage::getClanId, zfClanManage.getClanId())
                .like(zfClanManage.getMemberId() !=null, ZfClanManage::getClanId, zfClanManage.getClanId());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfClanManage> uniqueCondition(ZfClanManage zfClanManage){
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zfClanManage.getClanId() !=null , ZfClanManage::getClanId, zfClanManage.getClanId());
        lqw.eq(zfClanManage.getMemberId() !=null, ZfClanManage::getMemberId, zfClanManage.getMemberId());
        return lqw;
    }
 
    /**
     *
     * 获取当前用户的sysUserId
     * @return
     */
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
 
        return zInfoUserService.getInfoBysysId( myself.getUserId()).getUserId();
 
    }
 
 
    @Override
    public AjaxResult selectDataList(Integer clanId, Integer pageNo, Integer pageSize) {
        ZfClanManage zfClanManage = new ZfClanManage();
        zfClanManage.setClanId(clanId);
        LambdaQueryWrapper<ZfClanManage> lqw = buildCondition(zfClanManage);
        List<ZfClanManage> beanRecords = list(lqw);
        List<HashMap<String, String>> zInfoUsers = new ArrayList<>();
 
        for (ZfClanManage beanRecord:beanRecords) {
            HashMap<String,String> idAndName = new HashMap<>();
            String name = zInfoUserService.getById(Long.valueOf(beanRecord.getMemberId())).getNickName();
            idAndName.put("名字",name);
            idAndName.put("角色类型",String.valueOf(beanRecord.getRoleId()));
            zInfoUsers.add(idAndName);
        }
 
        List<HashMap<String, String>>  record =  zInfoUsers.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());
 
        int totalPage = (record.size() -1) / pageSize +1;
 
        Page<String> zInfoUserPage = new Page<>(pageNo, pageSize,totalPage);
        HashMap<String, Object> data = MapUtils.getShareResult(zInfoUserPage, record,zInfoUsers.size());
        return AjaxResult.success(data);
    }
 
    @Override
    public void addA(Integer clanId,Integer memberId,Integer roleId){
        ZfClanManage zfClanManage = new ZfClanManage();
        zfClanManage.setClanId(clanId);
        zfClanManage.setMemberId(memberId);
        zfClanManage.setRoleId(roleId);
        addData(zfClanManage);
    }
 
    @Override
    public AjaxResult addData(ZfClanManage zfClanManage) {
//        LambdaQueryWrapper<ZfClanManage> lqw = uniqueCondition(zfClanManage);
//        List<ZfClanManage> list = list(lqw);
//        if(list.size()>0){
//            throw new RuntimeException("请勿重复添加同一成员");
//        }
 
 
//        if (!Long.valueOf(zfClanService.getById(zfClanManage.getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力添加成员");
//
//            }
        /**
         * 在用户信息表添加家族号
         */
        ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManage.getMemberId()));
        zInfoUser.setClanId(zfClanManage.getClanId());
        zInfoUserService.saveOrUpdate(zInfoUser);
 
        if (save(zfClanManage)) {
            EsModel esModel = new EsModel();
            Integer inte =zfClanManage.getClanId();
            String uuid = UUID.randomUUID().toString().replace("-","");
            esModel.setId(uuid);
            esModel.setCtId(Long.valueOf(inte));
            esModel.setCtTableName("家族管理");
            esModel.setBy10(zfClanManage.getMemberId());
 
            esModel.setBy5("/zfClanManage");
//            esModel.setFid(familyId);
            esSer.insertTable(esModel);
//            System.out.println(esModel);
 
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
 
        }
    }
 
    /**
     *通过id删除
     */
    @Override
    public AjaxResult deleteData(Integer clanId,Integer id) {
        ZfClanManage zfClanManage1 = new ZfClanManage();
        zfClanManage1.setMemberId(id);
        zfClanManage1.setClanId(clanId);
        List<ZfClanManage> zfClanManages =list(buildCondition(zfClanManage1));
//        for (ZfClanManage zfClanManage : zfClanManages) {
//            if (!zfClanManages.getAdminId().equals(getUserId())) {
//                throw new RuntimeException("您不是管理员,没有权力删除该家族");
//            }
//        }
 
//        if (!Long.valueOf(zfClanService.getById(zfClanManages.get(0).getClanId()).getAdminId()).equals(getUserId())) {
//                throw new RuntimeException("您不是管理员,没有权力删除成员");
//            }
        /**
         * 在用户信息表删除家族号
         */
        ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManages.get(0).getMemberId()));
        zInfoUser.setClanId(0);
        zInfoUserService.saveOrUpdate(zInfoUser);
 
        if (zfClanManageService.removeByIds(Arrays.asList(zfClanManages.get(0).getId()))) {
 
            //删除es中的数据
            zfClanManages.stream().forEach(zfClanManage -> {
                EsModel esModel = esSer.findByCtId((zfClanManage.getId().intValue()), "家族管理");
                if (esModel != null) {
                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
                    try {
                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
 
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
 
    /**
     *通过家族id删除
     */
    @Override
    public void deleteMember(Long ids) {
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfClanManage::getClanId,ids);
        List<ZfClanManage> zfClanManages =list(lqw) ;
//        for (ZfClanManage zfClanManage : zfClanManages) {
//            if (!zfClanManages.getAdminId().equals(getUserId())) {
//                throw new RuntimeException("您不是管理员,没有权力删除该家族");
//            }
//        }
 
//        if (!Long.valueOf(zfClanService.getById(zfClanManages.get(0).getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力删除成员");
//        }
        List<Integer> id = new ArrayList<>() ;
        for (ZfClanManage zfClanManage: zfClanManages) {
            ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManage.getMemberId()));
            zInfoUser.setClanId(0);
            System.out.println(zInfoUser);
            zInfoUserService.saveOrUpdate(zInfoUser);
            id.add(zfClanManage.getId());
        }
        /**
         * 在用户信息表删除家族号
         */
 
 
 
        if (zfClanManageService.removeByIds(id)) {
            //删除es中的数据
            zfClanManages.stream().forEach(zfClanManage -> {
                EsModel esModel = esSer.findByCtId((zfClanManage.getId().intValue()), "家族管理");
                if (esModel != null) {
                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
                    try {
                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
 
 
        }}}