zqy
2025-01-07 8ff279e89f7736c345f343b2b7f292786d5ffffe
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
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.*;
import com.ruoyi.mapper.ZfShareDataMapper;
import com.ruoyi.service.EsService;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfClanService;
import com.ruoyi.service.ZfShareDataService;
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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
 
@Service
public class ZfShareDataServiceImpl extends ServiceImpl<ZfShareDataMapper, ZfShareData> implements ZfShareDataService {
 
    @Resource
    private ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    @Resource
    private ZfClanService zfClanService;
 
 
    @Resource
    private EsService esSer;
    private LambdaQueryWrapper<ZfShareData> buildCondition(Long userId) {
        LambdaQueryWrapper<ZfShareData> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfShareData::getId);
        lqw.eq(ZfShareData::getUserId,userId);
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfShareData> uniqueCondition(ZfShareData zfShareData) {
        LambdaQueryWrapper<ZfShareData> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfShareData::getId);
        lqw.eq(ZfShareData::getSharedId,zfShareData.getSharedId());
        return lqw;
    }
 
    @Override
    public List<ZfShareData> selectList() {
        LambdaQueryWrapper<ZfShareData> lqw = buildCondition(getUserId());
 
        return list(lqw);
    }
 
    @Override
    public List<ZfShareData> selectByIds(Long[] ids) {
        List<ZfShareData> list = new ArrayList<>();
        if(ids.length!=0)
            list = listByIds(Arrays.asList(ids));
        else
            list = list();
        return list;
    }
 
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
        return myself.getUserId();
 
    }
 
    public Long getSource(Long userId){
        LambdaQueryWrapper<ZfShareData> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfShareData::getId);
        lqw.eq(ZfShareData::getSharedId,userId);
        if (list(lqw).size() == 0){
            return userId;
        }
        return getSource(list(lqw).get(0).getUserId().longValue());
    }
    @Override
    public AjaxResult addData(Long[] userIds) {
 
            for (Long userId : userIds) {
                ZfShareData zfShareData = new ZfShareData();
                zfShareData.setUserId(getUserId().intValue());
                zfShareData.setSharedId(userId.intValue());
                zfShareData.setMasterAccount(0);
                for(ZfShareData zfShareData1:list(buildCondition(getUserId()))){
                    System.out.println(zfShareData1);
                }
                if (list(uniqueCondition(zfShareData)).size() == 0) save(zfShareData);
                else return AjaxResult.error("请勿重复共享");
            }
            return AjaxResult.success("数据共享成功");
 
 
    }
 
    @Override
    public AjaxResult deleteData(Long[] ids) {
        List<ZfShareData> meetings = listByIds(Arrays.asList(ids));
 
        if (removeByIds(Arrays.asList(ids))) {
 
            //删除es中的数据
            meetings.forEach(meeting -> {
                EsModel esModel = esSer.findByCtId((meeting.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();
        }
    }
 
    @Override
    public AjaxResult setAdmin(Long userId,Long id) {
        List<ZfShareData> list = list(buildCondition(getSource(userId)));
        list.addAll(getShareId(userId));
        for (ZfShareData zfShareData1:list){
            zfShareData1.setMasterAccount(id.intValue());
            updateById(zfShareData1);
        }
        return AjaxResult.success();
    }
 
    @Override
    public AjaxResult allUser(Long id) {
        List<ZfShareData> list = list(buildCondition(getSource(id)));
        list.addAll(getShareId(id));
        for (ZfShareData zfShareData1:list){
            zfShareData1.setMasterAccount(0);
            updateById(zfShareData1);
        }
        return AjaxResult.success();
    }
 
    @Override
    public List<ZfShareData> getShareId(Long id) {
        LambdaQueryWrapper<ZfShareData> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfShareData::getId);
        lqw.eq(ZfShareData::getSharedId,id);
        return list(lqw);
    }
}