feige
2024-11-10 f33f5d962eddbc75f3db8124b14f8a50d93cf625
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
 
package com.ruoyi.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EsModel;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.domain.dto.AncestorClan;
import com.ruoyi.mapper.ZfAncestorMapper;
import com.ruoyi.service.*;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
 
@Service
public class ZfAncestorServiceImpl extends ServiceImpl<ZfAncestorMapper, ZfAncestor> implements ZfAncestorService {
 
    @Resource
    private EsService esSer;
 
    @Resource
    private ZfAncestorMapper zfAncestorMapper;
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    @Resource
    private ZfAncestorService zfAncestorService;
 
    @Resource
    private  ZfClanService zfClanService;
 
 
    private LambdaQueryWrapper<ZfAncestor> buildCondition(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfAncestor::getId);
        lqw.eq(zfAncestor.getName()!=null, ZfAncestor::getName,zfAncestor.getName());
 
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfAncestor> uniqueCondition(ZfAncestor zfAncestor){
        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zfAncestor.getClanId() !=null , ZfAncestor::getClanId, zfAncestor.getClanId());
        lqw.eq(zfAncestor.getSysId() !=null , ZfAncestor::getSysId, zfAncestor.getSysId());
        return lqw;
    }
 
    /**
     *
     * 获取当前用户的sysUserId
     * @return
     */
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
 
        return zInfoUserService.getInfoBysysId( myself.getUserId()).getUserId();
 
    }
 
    @Override
    public boolean count(Integer clanId){
        ZfAncestor zfAncestor = new ZfAncestor();
        zfAncestor.setClanId(clanId);
        return list(buildCondition(zfAncestor)).size() < 2;
    }
 
    @Override
    public AjaxResult selectDataList(Integer clanId, Integer pageNo, Integer pageSize) {
//        ZfAncestor zfAncestor = new ZfAncestor();
//        zfAncestor.setClanId(clanId);
//        LambdaQueryWrapper<ZfAncestor> lqw = buildCondition(zfAncestor);
//        List<ZfAncestor> beanRecords = list(lqw);
//        List<HashMap<String, String>> zInfoUsers = new ArrayList<>();
//
//        for (ZfAncestor 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);
 
//        List<ZfAncestor> beanRecords = list(lqw);
        List<ZInfoUser> zInfoUsers = zInfoUserService.selectByClanId(clanId);
        HashMap<String,Long> members = new HashMap<>();
        for (ZInfoUser zInfoUser: zInfoUsers) {
            members.put(zInfoUser.getNickName(),zInfoUser.getRoleId());
        }
 
        return AjaxResult.success(members);
    }
 
    @Override
    public AjaxResult selectDataList(ZfAncestor zfAncestor, Integer pageNo, Integer pageSize) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
 
        queryWrapper.eq(zfAncestor.getClanId() !=null , " zf_ancestor.clan_id", zfAncestor.getClanId());
        queryWrapper.eq(zfAncestor.getSysId() !=null , " zf_ancestor.sys_id ", zfAncestor.getSysId());
        queryWrapper.like(StringUtils.isNotEmpty(zfAncestor.getName()),"zf_ancestor.name",zfAncestor.getName());
 
        Page<AncestorClan> info = zfAncestorMapper.selectInfo(new Page<>(pageNo, pageSize),queryWrapper);
        //page(pa, wrapper);
     //   List<AncestorClan> orders = page(pa, wrapper);
        List<AncestorClan>  list =info.getRecords();
        HashMap<String, Object> data = MapUtils.getResult(info, list);
        return AjaxResult.success(data);
    }
 
    @Override
    public List<AncestorClan> selectByCondition(ZfAncestor zfAncestor) {
        QueryWrapper<Object> lqw = new QueryWrapper<>();
        lqw.eq(zfAncestor.getClanId() !=null , " zf_ancestor.clan_id", zfAncestor.getClanId());
 
        lqw.like(StringUtils.isNotEmpty(zfAncestor.getName()),"zf_ancestor.name",zfAncestor.getName());
 
        List<AncestorClan>  lis = zfAncestorMapper.selectAllInfo(lqw);
 
        return lis;
    }
 
    @Override
    public List<AncestorClan> selectByIds(Long[] ids) {
        List<AncestorClan> list = new ArrayList<>();
        QueryWrapper<Object> lqw = new QueryWrapper<>();
       // lqw.like(StringUtils.isNotEmpty(zfAncestor.getName()),"zf_ancestor.name",zfAncestor.getName());
 
      //  if(ids.length!=0)
        //    list = listByIds(Arrays.asList(ids));
        //else
        list = zfAncestorMapper.selectAllInfo(lqw);
//        System.out.println(list.size());
//        System.out.println("---------+++++++++---------");
 
        List tmp = new ArrayList<>(Arrays.asList(ids));
        List res = new ArrayList();
        if(ids.length!=0)
        {
            for(AncestorClan ast : list)
            {
                if(tmp.contains(ast.getId().longValue()))
                {
                    res.add(ast);
                }
            }
            return res;
        }
        else
 
            return list;
    }
 
 
    @Override
    public Long addData(ZfAncestor zfAncestor) {
//        LambdaQueryWrapper<ZfAncestor> lqw = buildCondition(zfAncestor);
//        List<ZfAncestor> list = list(lqw);
//        Long sysID = 0L;
//        System.out.println("15dniwru/"+list);
//        if(list.size()>0){
//            throw new RuntimeException("请勿重复添加祖先");
//        }
 
//        else if (list.size() == 1){
//            if (list.get(0).getSex().equals(zfAncestor.getSex())){
//                throw new RuntimeException("两位祖先不能为同性");
//            }
 
 
          //  sysID = zInfoUserService.getInfoBysysId(list.get(0).getSysId()).getUserId();
//        }
//
 
//        if (!Long.valueOf(zfClanService.getById(zfClanManage.getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力添加成员");
//
//            }
 
 
        /**
         * 在用户信息表添加家族号
         */
//        ZInfoUser zInfoUser =  zInfoUserService.getInfoBysysId(zfAncestor.getSysId());
//        zInfoUser.setClanId(zfAncestor.getClanId());
//        zInfoUserService.saveOrUpdate(zInfoUser);
 
        save(zfAncestor);
//            EsModel esModel = new EsModel();
//            Integer inte = zfAncestor.getClanId();
//            String uuid = UUID.randomUUID().toString().replace("-","");
//            esModel.setId(uuid);
//            esModel.setCtId(Long.valueOf(inte));
//            esModel.setCtTableName("家族管理");
//            esModel.setBy1(zfAncestor.getName());
//
//            esModel.setBy5("/zfClanManage");
////            esModel.setFid(familyId);
//            esSer.insertTable(esModel);
//            System.out.println(esModel);
 
            return 1L;
 
    }
 
    /**
     *通过id删除
     */
    @Override
    public AjaxResult deleteData(Integer clanId,Integer id) {
//        ZfAncestor zfAncestor1 = new ZfAncestor();
//        zfAncestor1.setMemberId(id);
//        zfAncestor1.setClanId(clanId);
//        List<ZfAncestor> zfAncestors =list(buildCondition(zfAncestor1));
////        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(zfAncestors.get(0).getMemberId()));
//        zInfoUser.setClanId(0);
//        zInfoUserService.saveOrUpdate(zInfoUser);
//
//        if (zfAncestorService.removeByIds(Arrays.asList(zfAncestors.get(0).getId()))) {
//
//            //删除es中的数据
//            zfAncestors.stream().forEach(zfAncestor -> {
//                EsModel esModel = esSer.findByCtId((zfAncestor.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();
//        }
        return null;
    }
 
    @Override
    public ZfAncestor update(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor> lqw = uniqueCondition(zfAncestor);
        List<ZfAncestor> list = list(lqw);
//        System.out.println(list);
        ZInfoUser updateDate =zInfoUserService.getInfoBysysId(zfAncestor.getSysId());
        ZfAncestor zfAncestor1 = new ZfAncestor();
        ZfAncestor zfAncestor2 = new ZfAncestor();
        zfAncestor1.setId(list.get(0).getId());
        zfAncestor1.setName(zfAncestor.getName());
        System.out.println(updateDate);
        if (zfAncestor.getSex() != null){
        if(updateDate.getSpouseId() != null && updateDate.getSpouseId() != 0){
            Long spouseId=updateDate.getSpouseId();
            if (zInfoUserService.getById(spouseId).getSex().equals(zfAncestor.getSex())){
 
                Long spouseSysId = zInfoUserService.getById(spouseId).getSysId();
                zfAncestor2.setSysId(spouseSysId);
                Integer spId = list(uniqueCondition(zfAncestor2)).get(0).getId();
                zfAncestor2.setId(spId);
                zfAncestor2.setSex(zfAncestor.getSex() == 0? 1: 0);
                System.out.println(zfAncestor2);
                zfAncestorService.updateById(zfAncestor2);
            }}
            zfAncestor1.setSex(zfAncestor.getSex());}
//        System.out.println("1841vdndigrrr"+zfAncestor1);
 
            zfAncestorService.updateById(zfAncestor1);
 
        return zfAncestor2;
    }
 
    @Override
    public ZfAncestor selectZfAncestor(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor>  queryWraper = new LambdaQueryWrapper<>();
 
        queryWraper.eq(zfAncestor.getSysId() !=null , ZfAncestor::getSysId, zfAncestor.getSysId());
        List lis = this.list(queryWraper);
        if(lis.size()>0)
            return (ZfAncestor) lis.get(0);
        else
            return null;
    }
 
    /**
     *
     *通过家族id删除
     */
    @Override
    public void deleteMember(Long ids) {
        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfAncestor::getClanId,ids);
        List<ZfAncestor> zfAncestors =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 (ZfAncestor zfAncestor : zfAncestors) {
            ZInfoUser zInfoUser =  zInfoUserService.getInfoBysysId(zfAncestor.getSysId());
            zInfoUser.setClanId(0);
            zInfoUserService.saveOrUpdate(zInfoUser);
            id.add(zfAncestor.getId());
//
//        }
        /**
         * 在用户信息表删除家族号
         */
 
 
        if (zfAncestorService.removeByIds(id)) {
            //删除es中的数据
            zfAncestors.stream().forEach(zfAncestor1 -> {
                EsModel esModel = esSer.findByCtId((zfAncestor1.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);
                    }
                }
            });
 
 
        }}}
        }