zqy
2024-07-07 780fa6d4016c6e616bbb4b3d29d33dbf3a40cbd6
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
package com.ruoyi.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.StringUtils;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfDoctorDownload;
import com.ruoyi.mapper.ZfDoctorDownloadMapper;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
 
@Service
public class ZfDoctorDownloadServiceImpl extends ServiceImpl<ZfDoctorDownloadMapper, ZfDoctorDownload> implements ZfDoctorDownloadService {
 
    @Resource
    EsService esService;
 
    @Resource
    ZfDoctorShareService zfDoctorShareService;
 
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    private RestHighLevelClient restHighLevelClient;
 
    private LambdaQueryWrapper<ZfDoctorDownload> buildCondition(ZfDoctorDownload zfDoctorDownload,Long userId) {
        LambdaQueryWrapper<ZfDoctorDownload> lqw = new LambdaQueryWrapper<>();
        lqw.in(ZfDoctorDownload::getDownloadId,userId);
        lqw.orderByDesc(ZfDoctorDownload::getDownloadId);
        lqw.like(zfDoctorDownload.getDownloadId() != null, ZfDoctorDownload::getDownloadId, zfDoctorDownload.getDownloadId())
            .like(zfDoctorDownload.getDownloadContent() != null, ZfDoctorDownload::getDownloadContent, zfDoctorDownload.getDownloadContent());
        return lqw;
}
    private LambdaQueryWrapper<ZfDoctorDownload> uniqueCondition(ZfDoctorDownload zfDoctorDownload) {
        LambdaQueryWrapper<ZfDoctorDownload> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(String.valueOf(zfDoctorDownload.getDownloadId())), ZfDoctorDownload::getDownloadId, zfDoctorDownload.getDownloadId())
            .eq(StringUtils.isNotEmpty(String.valueOf(zfDoctorDownload.getDownloadContent())), ZfDoctorDownload::getDownloadContent, zfDoctorDownload.getDownloadContent());
        return lqw;
    }
    @Override
    public List<ZfDoctorDownload> selectDoctorList(ZfDoctorDownload zfDoctorDownload , Integer pageNum, Integer pageSize) {
        ZInfoUser myself = zInfoUserService.getMyself();
        LambdaQueryWrapper<ZfDoctorDownload> lqw = buildCondition(zfDoctorDownload,myself.getUserId());
        List<ZfDoctorDownload> beanRecords = list(lqw);
        return new ArrayList<>(beanRecords);
    }
 
 
    @Override
    public AjaxResult addData(ZfDoctorDownload zfDoctorDownload) {
    zfDoctorDownload.setDownloadId(getUserId());
    LambdaQueryWrapper<ZfDoctorDownload> lqw = uniqueCondition(zfDoctorDownload);
    List<ZfDoctorDownload> list = list(lqw);
        if (list.size() > 0) {
        throw new RuntimeException("请勿新增重复数据");
    }
 
        zfDoctorDownload.setDownloadId(getUserId());
        zfDoctorDownload.setShareId(zfDoctorShareService.getShareId(zfDoctorDownload));
        if (save(zfDoctorDownload)) {
        EsModel esModel = new EsModel();
        Integer inte = zfDoctorDownload.getId().intValue();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        esModel.setId(uuid);
        esModel.setCtId(Long.valueOf(inte));
        esModel.setCtTableName("家庭小医生");
 
        esModel.setBy1(String.valueOf(zfDoctorDownload.getDownloadId()));
        esModel.setBy2(String.valueOf(zfDoctorDownload.getDownloadContent()));
        esModel.setBy3(String.valueOf(zfDoctorDownload.getShareId()));
        //这里存储查询详情的路径
        esService.insertTable(esModel);
        return AjaxResult.success();
    } else {
        return AjaxResult.error();
    }
}
    @Override
    public AjaxResult deleteData(Long[] ids) {
        List<ZfDoctorDownload> zfDoctorDownloads = listByIds(Arrays.asList(ids));
 
        if (zfDoctorShareService.removeByIds(Arrays.asList(ids))) {
 
            //删除es中的数据
            zfDoctorDownloads.stream().forEach(zfDoctorDownload -> {
                EsModel esModel = esService.findByCtId(zfDoctorDownload.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();
        }
    }
 
 
    public Long getUserId() {
        ZInfoUser myself = zInfoUserService.getMyself();
        return myself.getUserId();
    }
}