zqy
2025-04-22 c4ec501d4280f34c1abcf18015b1b9605626677a
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
 
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.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.ZfClanMapper;
import com.ruoyi.service.*;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.update.UpdateRequest;
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.sql.SQLIntegrityConstraintViolationException;
import java.time.LocalDateTime;
import java.util.*;
 
@Service
public class ZfClanServiceImpl extends ServiceImpl<ZfClanMapper,ZfClan> implements ZfClanService {
 
    @Resource
    private EsService esSer;
 
    @Resource
    ZfLogService zfLogService;
 
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    @Resource
    private ZfClanService zfClanService;
 
    @Resource
    private ZfAncestorService zfAncestorService;
 
 
    private LambdaQueryWrapper<ZfClan> buildCondition(ZfClan zfClan) {
        LambdaQueryWrapper<ZfClan> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfClan::getClanId);
        lqw.like(StringUtils.isNotEmpty(zfClan.getClanName()), ZfClan::getClanName, zfClan.getClanName());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfClan> uniqueCondition(ZfClan zfClan){
        LambdaQueryWrapper<ZfClan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(zfClan.getClanName()), ZfClan::getClanName, zfClan.getClanName());
        return lqw;
    }
 
 
    /**
     *
     * 获取当前用户的sysUserId
     * @return
     */
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
 
        return myself.getUserId();
 
    }
 
 
    @Override
    public AjaxResult selectDataList(ZfClan zfClan, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ZfClan> lqw = buildCondition(zfClan);
 
 
        Page<ZfClan> zfClanPage = new Page<>(pageNum, pageSize);
        Page<ZfClan> pageResult = page(zfClanPage, lqw);
 
        List<ZfClan> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult, beanRecords);
        return AjaxResult.success(data);    }
 
    @Override
    public AjaxResult selectData() {
        List<ZfClan> lis = this.list();
        return AjaxResult.success(lis);
    }
 
    @Override
    public AjaxResult selectDataList(ZfClan zfClan){
        LambdaQueryWrapper<ZfClan> lqw = buildCondition(zfClan);
        List<ZfClan> zfClans = list(lqw);
        List<ZfClan>  satisfyList = new ArrayList<>();
        for (ZfClan zfClan1: zfClans) {
            if (zfAncestorService.count(zfClan1.getClanId())){
               satisfyList.add(zfClan1);}
        }
        return AjaxResult.success(satisfyList);
    }
 
    @Override
    public List<ZfClan> selectByCondition(ZfClan zfClan) {
 
        System.out.println("8008888//"+zfClan.getClanId());
        LambdaQueryWrapper<ZfClan> lambdaQueryWrapper = buildCondition(zfClan);
        lambdaQueryWrapper.eq(zfClan.getClanId()!=null,ZfClan::getClanId,zfClan.getClanId());
 
        return list(lambdaQueryWrapper);
    }
 
    @Override
    public List<ZfClan> selectByIds(Long[] ids) {
        List<ZfClan> list = new ArrayList<>();
        if(ids.length!=0)
            list = listByIds(Arrays.asList(ids));
        else
            list = list();
        return list;
    }
 
    @Override
    public AjaxResult addData(ZfClan zfClan) {
        LambdaQueryWrapper<ZfClan> lqw = uniqueCondition(zfClan);
        List<ZfClan> list = list(lqw);
 
        if(list.size()>0){
            throw new RuntimeException("请勿新增重复家族");
        }
 
 
 
 
        if (save(zfClan)) {
            EsModel esModel = new EsModel();
            Integer inte =zfClan.getClanId();
            String uuid = UUID.randomUUID().toString().replace("-","");
            esModel.setId(uuid);
            esModel.setCtId(Long.valueOf(inte));
            esModel.setCtTableName("家族");
            esModel.setBy1(zfClan.getClanName());
 
            esModel.setBy5("/zfClan");
//            esModel.setFid(familyId);
            esSer.insertTable(esModel);
//            System.out.println(esModel);
 
 
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
 
        }
    }
 
 
    @Override
    public AjaxResult updateData(ZfClan zfClan) {
 
//        if (!Long.valueOf(zfClanService.getById(zfClan.getClanId()).getAdminId()).equals(getUserId())) {
//                throw new RuntimeException("您不是管理员,没有权力修改该家族信息");
//
//        }
        ZfLog zfLog = new ZfLog();
        zfLog.setUpdateTime(LocalDateTime.now());
        zfLog.setModule("家族");
//        zfLog.setUpdater(zInfoUserService.getMyself().getNickName());
        zfLogService.save(zfLog);
 
        LambdaQueryWrapper<ZfClan> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfClan::getClanId);
        lqw.eq(StringUtils.isNotEmpty(zfClan.getClanName()), ZfClan::getClanName, zfClan.getClanName());
        List<ZfClan> lis = this.list(lqw);
//        System.out.println("===================================="+meeting);
        if(lis.size()>0)
        {
            return AjaxResult.success("家族名称不能重复!");
 
        }
        else
        {
            boolean res = updateById(zfClan);
            return AjaxResult.success();
        }
 
          //  if(res){
 
          //  }else {
          //      return AjaxResult.success("家族名称不能重复!");
          //  }
 
 
 
      //  return AjaxResult.success("家族名称不能重复!");
//            //到数据库中查询对应的数据
//            ZfClan dataById = getById(zfClan.getClanId());
//
//            //先到es中查询到对应那条数据在es的id
//            EsModel esResult = esSer.findByCtId(dataById.getClanId(), "家族");
////            System.out.println("===================================="+meeting);
//            if (esResult == null){
//                return AjaxResult.success();
//            }
//
//            //操作es修改数据
//            EsModel newModel = new EsModel();
//            if(zfClan.getClanName()!=null){
//                newModel.setBy1(zfClan.getClanName());
//            }else {
//                newModel.setBy1(dataById.getClanName());
//            }
////
//
//            UpdateRequest updateRequest = new UpdateRequest("allsearchdata", esResult.getId());
//            updateRequest.doc(
//                    "by1",newModel.getBy1()
//
//            );
 
//            try {
//                restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
 
 
    }
 
    @Override
    public AjaxResult deleteData(Long ids) {
        List<ZfClan> zfClans = listByIds(Arrays.asList(ids));
 
        zfAncestorService.deleteMember(ids);
        if (zfClanService.removeByIds(Arrays.asList(ids))) {
 
            //删除es中的数据
            zfClans.stream().forEach(zfClan -> {
                EsModel esModel = esSer.findByCtId((zfClan.getClanId().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();
        }
    }}