feige
2024-11-19 63240c3b7edb2a149511fdbe4108755ad60db91f
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
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.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.*;
import com.ruoyi.mapper.ZfShareMapper;
import com.ruoyi.service.*;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.stream.Stream;
 
import static com.alibaba.druid.sql.PagerUtils.limit;
import static com.ruoyi.constant.MenuAuthority.DOCTOR_LIST_UPDATE;
 
@Slf4j
@Service
public class ZfDoctorShareServiceImpl extends ServiceImpl<ZfShareMapper, ZfShare> implements ZfDoctorShareService {
    @Resource
    private ZfDoctorService zfDoctorService;
 
    @Resource
    private ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    @Resource
    EsService esService;
 
    @Resource
    private ZfEventService zfEventService;
 
 
    private LambdaQueryWrapper<ZfShare> uniqueCondition(ZfShare zfShare) {
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(String.valueOf(zfShare.getShareId())), ZfShare::getShareId, zfShare.getShareId());
        lqw.eq(StringUtils.isNotEmpty(String.valueOf(zfShare.getShareContent())), ZfShare::getShareContent, zfShare.getShareContent());
        lqw.eq(StringUtils.isNotEmpty(String.valueOf(zfShare.getSysMenuId())), ZfShare::getSysMenuId, zfShare.getSysMenuId());
        return lqw;
    }
 
 
 
    @Override
    public List<ZfShare> getByUserId() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfShare::getUserId,userId);
 
        return list(lqw);
    }
 
 
    @Resource
    ZfDoctorShareService zfDoctorShareService;
 
    public void addData(ZfShare za)
    {
        LambdaQueryWrapper<ZfShare> lqw = uniqueCondition(za);
        List<ZfShare> list = list(lqw);
 
        if(list.size()>0){
            throw new RuntimeException("请勿分享重复数据");
        }
        else {
            zfDoctorShareService.save(za);
        }
 
    }
 
 
    @Override
    public AjaxResult saveZa(ShareMore zfDoctor) {
        //  boolean bl = zAuthorityService.saveOrUpdate(zAuthority);
        Long [] shareIds = zfDoctor.getShareIds();
        Long [] shareContents =  zfDoctor.getShareContents();
        for (Long shareId : shareIds) {
            for (Long shareContent : shareContents) {
                ZfShare za = new ZfShare();
                za.setUserId(getUserId());
                za.setShareContent(shareContent);
                za.setShareId(shareId);
                za.setSysMenuId(2019L);
                addData(za);
            }
        }
        //  Long []
        //  if(bl)
        return AjaxResult.success("分享成功!");
        // else
        //    return  AjaxResult.error("权限新增失败!");
 
 
    }
    /**
     * 根据UserId和分享人shareId查看已经授权给那些人那些数据
     */
    @Override
    public AjaxResult listByFidAid(ShareMore zfDoctor,Integer pageNo ,Integer pageSize) {
        //找到对应的赋予数据的用户以及数据内容
        Long [] shareIds = zfDoctor.getShareIds();
        List<ZfDoctor> beanRecord3 = new ArrayList<>();
        for (Long shareId : shareIds) {
            {
                LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ZfShare::getShareId,shareId)
                    .eq(ZfShare::getSysMenuId,zfDoctor.getSysMenuId())
                        .eq(ZfShare::getUserId, getUserId());
                List<ZfShare> beanRecords = list(lqw);
                for (ZfShare beanRecord : beanRecords ) {
                    beanRecord3.add(zfDoctorService.getById(beanRecord.getShareContent()));
                }
            }
        }
        List<ZfDoctor>  record =  beanRecord3.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());
 
        int totalPage = (beanRecord3.size() -1) / pageSize +1;
 
        Page<ZfDoctor> zfDoctorPage = new Page<>(pageNo, pageSize,totalPage);
        HashMap<String, Object> data = MapUtils.getShareResult(zfDoctorPage, record,beanRecord3.size());
 
        return AjaxResult.success(data);
    }
 
    @Override
    public AjaxResult deleteZa(ShareMore zfDoctor) {
        Long [] shareIds = zfDoctor.getShareIds();
        Long [] shareContents =  zfDoctor.getShareContents();
        for(Long shareId : shareIds)
            for(Long shareContent: shareContents)
            {
                LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ZfShare::getShareId,shareId)
                        .eq(ZfShare::getShareContent, shareContent)
                        .eq(ZfShare::getSysMenuId, zfDoctor.getSysMenuId())
                        .eq(ZfShare::getUserId, getUserId());
 
                zfDoctorShareService.remove(lqw);
                //   addData(za);
            }
        return AjaxResult.success("数据收回成功!");
 
    }
 
    /**
     * 用户自己查看别人分享的数据和分享人
     */
    @Override
    public AjaxResult listByUserId() {
        //找到对应的赋予数据的用户以及数据内容
        HashMap<Long,List<ZfDoctor>>  bs = new HashMap<>();
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfShare::getShareId,getUserId());
        lqw.eq(ZfShare::getSysMenuId,2019L);
        List<ZfShare> beanRecords = list(lqw);
        List<ZfDoctor> b1 = new ArrayList<>();
        for (ZfShare beanRecord : beanRecords ) {
                b1.add(zfDoctorService.getById(beanRecord.getShareContent()));
                bs.put(beanRecord.getUserId(),b1);
        }
        log.info("从数据库中查到的为:{}", bs);
        return AjaxResult.success(bs);
 
    }
    /**
     * 用户自己查看别人分享的数据==用户自己查看别人分享的数据和分享人
     */
    @Override
    public AjaxResult listUserId(Integer pageNo ,Integer pageSize) {
        //找到对应的赋予数据的用户以及数据内容
        List<ZfDoctor>  bs = new ArrayList<>();
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfShare::getShareId,getUserId());
        lqw.eq(ZfShare::getSysMenuId,2019L);
        List<ZfShare> beanRecords = list(lqw);
        for (ZfShare beanRecord : beanRecords ) {
            ZfDoctor bs2 = zfDoctorService.getById(beanRecord.getShareContent());
            bs2.setShareId(beanRecord.getUserId());
            bs.add(bs2);
        }
        log.info("从数据库中查到的为:{}", bs);
        List<ZfDoctor>  record = bs.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());
 
        int totalPage = (bs.size() -1) / pageSize +1;
 
        Page<ZfDoctor> zfDoctorPage = new Page<>(pageNo, pageSize,totalPage);
        HashMap<String, Object> data = MapUtils.getShareResult(zfDoctorPage, record,bs.size());
        return AjaxResult.success(data);
 
    }
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
        return myself.getUserId();
 
    }
    @Override
    public Long getShareId(ZfDownload zfDownload){
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfShare::getShareId, zfDownload.getDownloadId())
                .eq(ZfShare::getShareContent, zfDownload.getDownloadContent())
                .eq(ZfShare::getSysMenuId, zfDownload.getSysMenuId());
        List<ZfShare> beanRecords = list(lqw);
        if (beanRecords.size() != 0){
            return beanRecords.get(0).getUserId();
        }
        else {
            return null;
        }
    }
 
    @Override
    public Boolean deleteByContentId(Long[] ids) {
         List<Long> id = new ArrayList<>();
 
        LambdaQueryWrapper<ZfShare> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, ZfShare::getShareContent,  ids)
                .eq(ZfShare::getSysMenuId, 2019L);
 
        List<ZfShare> zfShares = list(lqw);
        if (zfShares.size() == 0){
            return true;
        }
        for (ZfShare zfShare: zfShares) {
            id.add(zfShare.getId());
        }
 
        System.out.println(id);
        if (removeByIds(id)) {
            //删除es中的数据
            zfShares.forEach(zfDoctorDownload -> {
                EsModel esModel = esService.findByCtId(zfDoctorDownload.getId().intValue(), "小医生分享删除");
                if (esModel != null) {
                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
                    System.out.println(deleteRequest);
                    try {
                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            return true;
        } else {
            return false;
        }
 
    }
 
}