559
whywhyo
2023-07-16 8e93f63d7eab5c4f5d193763cbb2e81ea5476923
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
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.utils.MapUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.ZfDoctorMapper;
import com.ruoyi.service.ZAuthorityService;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfDoctorService;
import com.ruoyi.service.ZfDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
 
import static com.ruoyi.constant.MenuAuthority.*;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-12
 */
@Service
@Slf4j
public class ZfDoctorServiceImpl extends ServiceImpl<ZfDoctorMapper, ZfDoctor> implements ZfDoctorService {
 
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    ZfDoctorService zfDoctorService;
 
    @Resource
    ZAuthorityService zAuthorityService;
 
    @Override
    public AjaxResult selectDoctorList(ZfDoctor zfDoctor, Integer pageNum, Integer pageSize) {
//        LambdaQueryWrapper<ZfDoctor> lqw = buildCondition(zfDoctor);
//        lqw.orderByDesc(ZfDoctor::getCreateTime);
//
//        Page<ZfDoctor> ZfDoctorPage = new Page<>(pageNum, pageSize);
//        Page<ZfDoctor> pageResult = page(ZfDoctorPage, lqw);
//
//        HashMap<String, Object> data = MapUtils.getResult(pageResult);
//        return AjaxResult.success(data);
        //要查自己家庭的
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        //也要查别人授权的
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
        //加上自己家庭的id
        idList.add(familyId);
//        String familyIds = listFamilyIds();
//        String secondFamilyAuthority = listSecondFamilyIds();
        LambdaQueryWrapper<ZfDoctor> lqw = buildCondition(zfDoctor, idList);
 
 
        Page<ZfDoctor> zfDoctorPage = new Page<>(pageNum, pageSize);
        Page<ZfDoctor> pageResult = page(zfDoctorPage, lqw);
 
        List<ZfDoctor> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        List<ZfDoctor> dtoResult = markOwnData(familyId, beanRecords);
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult, dtoResult);
        return AjaxResult.success(data);
    }
 
    @Override
    public List<ZfDoctor> selectByCondition(ZfDoctor zfDoctor) {
//        LambdaQueryWrapper<ZfDoctor> lambdaQueryWrapper = buildCondition(zfDoctor);
//        lambdaQueryWrapper.orderByDesc(ZfDoctor::getCreateTime);
//        List<ZfDoctor> list = list(lambdaQueryWrapper);
//        log.info("返回的数据为:{}", list);
//        return list;
 
        //要查自己家庭的
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
        //也要查别人授权的
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
        //加上自己家庭的id
        idList.add(familyId);
 
        LambdaQueryWrapper<ZfDoctor> lambdaQueryWrapper = buildCondition(zfDoctor, idList);
        List<ZfDoctor> beanRecords = list(lambdaQueryWrapper);
        log.info("从数据库中查到的为:{}", beanRecords);
        return markOwnData(familyId, beanRecords);
    }
 
    public List<ZfDoctor> markOwnData(Long familyId,List<ZfDoctor> beanRecords){
        return beanRecords.stream().peek(
                bean -> {
                    if (bean.getFamilyId() == familyId) {
                        bean.setOwnData(1);
                    } else {
                        bean.setOwnData(0);
                    }
                }
        ).collect(Collectors.toList());
    }
 
    @Override
    public List<ZfDoctor> selectByIds(Long[] ids) {
        List<ZfDoctor> list = new ArrayList<>();
        if (ids.length != 0)
            list = listByIds(Arrays.asList(ids));
        else
            list = list();
        return list;
    }
 
    @Override
    public AjaxResult importExcel(MultipartFile file) {
        ExcelUtil<ZfDoctor> util = new ExcelUtil<>(ZfDoctor.class);
        List<ZfDoctor> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("数据列表为:{}", dataList);
 
        for (ZfDoctor zfDoctor : dataList) {
            zfDoctorService.addData(zfDoctor);
        }
 
        return AjaxResult.success("导入数据成功");
 
    }
 
    @Override
    public AjaxResult addData(ZfDoctor zfDoctor) {
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
 
        if(familyId == null){
            throw new RuntimeException("您还未加入任何家庭");
        }
 
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_ADD)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
 
        if (zfDoctor.getFamilyId()!=null && !familyIdList.contains(zfDoctor.getFamilyId())) {
            throw new RuntimeException("你没有权限操作此家庭的数据");
        }
 
 
        if(zfDoctor.getFamilyId() == null){
            //默认添加自己家庭的数据
            zfDoctor.setFamilyId(familyId);
        }
 
        //判断是否有重复数据
        LambdaQueryWrapper<ZfDoctor> lqw = uniqueCondition(zfDoctor);
        List<ZfDoctor> list = list(lqw);
 
        if(list.size()>0){
            throw new RuntimeException("请勿新增重复数据");
        }
 
        if (save(zfDoctor)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
 
    @Override
    public AjaxResult updateData(ZfDoctor zfDoctor) {
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
 
        //先根据id查询出数据的familyId,看看有没有权限操作
        Long dataFamilyId = getById(zfDoctor.getId()).getFamilyId();
 
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_UPDATE)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
 
        if (dataFamilyId!=null && !familyIdList.contains(dataFamilyId)) {
            throw new RuntimeException("你没有权限操作此家庭的数据");
        }
 
        if(updateById(zfDoctor)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
 
    @Override
    public AjaxResult deleteData(Long[] ids) {
        List<ZfDoctor> dataList = zfDoctorService.listByIds(Arrays.asList(ids));
 
        ZInfoUser myself = zInfoUserService.getMyself();
        Long familyId = myself.getFamilyId();
 
        List<ZAuthority> authority = zAuthorityService.getAuthority();
        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(DOCTOR_LIST_REMOVE)).map(ZAuthority::getFid).collect(Collectors.toList());
        familyIdList.add(familyId);
 
        for (ZfDoctor data : dataList) {
            if (!familyIdList.contains(data.getFamilyId())){
                throw new RuntimeException("你没有权限操作此家庭的数据");
            }
        }
 
        if (zfDoctorService.removeByIds(Arrays.asList(ids))) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
 
//    @Override
//    public AjaxResult mySave(ZfDoctor zfDoctor) {
//        //判断是否有重复数据
//        LambdaQueryWrapper<ZfDoctor> lqw = uniqueCondition(zfDoctor);
//        List<ZfDoctor> list = list(lqw);
//        if (list.size() > 0) {
//            throw new RuntimeException("请勿新增重复数据");
//        }
//
//        if (save(zfDoctor)) {
//            return AjaxResult.success();
//        } else {
//            return AjaxResult.error();
//        }
//
//
//    }
 
    private LambdaQueryWrapper<ZfDoctor> uniqueCondition(ZfDoctor zfDoctor) {
        LambdaQueryWrapper<ZfDoctor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getType()), ZfDoctor::getType, zfDoctor.getType());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getSymptom()), ZfDoctor::getSymptom, zfDoctor.getSymptom());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getDuration()), ZfDoctor::getDuration, zfDoctor.getDuration());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getCmedical()), ZfDoctor::getCmedical, zfDoctor.getCmedical());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getWmedical()), ZfDoctor::getWmedical, zfDoctor.getWmedical());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getEffect()), ZfDoctor::getEffect, zfDoctor.getEffect());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getSuitable()), ZfDoctor::getSuitable, zfDoctor.getSuitable());
        lqw.eq(StringUtils.isNotEmpty(zfDoctor.getRemark()), ZfDoctor::getRemark, zfDoctor.getRemark());
        lqw.eq(zfDoctor.getFamilyId()!=null,ZfDoctor::getFamilyId,zfDoctor.getFamilyId());
        return lqw;
 
    }
 
    private LambdaQueryWrapper<ZfDoctor> buildCondition(ZfDoctor zfDoctor,List<Long> familyIdList) {
        LambdaQueryWrapper<ZfDoctor> lqw = new LambdaQueryWrapper<>();
        lqw.in(ZfDoctor::getFamilyId,familyIdList);
        lqw.orderByDesc(ZfDoctor::getCreateTime);
        lqw.like(StringUtils.isNotEmpty(zfDoctor.getType()), ZfDoctor::getType, zfDoctor.getType())
                .like(StringUtils.isNotEmpty(zfDoctor.getSymptom()), ZfDoctor::getSymptom, zfDoctor.getSymptom())
                .like(StringUtils.isNotEmpty(zfDoctor.getDuration()), ZfDoctor::getDuration, zfDoctor.getDuration())
                .like(StringUtils.isNotEmpty(zfDoctor.getEffect()), ZfDoctor::getEffect, zfDoctor.getEffect())
                .like(StringUtils.isNotEmpty(zfDoctor.getSuitable()), ZfDoctor::getSuitable, zfDoctor.getSuitable())
                .like(StringUtils.isNotEmpty(zfDoctor.getRemark()), ZfDoctor::getRemark, zfDoctor.getRemark());
        if (StringUtils.isNotEmpty(zfDoctor.getPrescription())){
            lqw.and(wrapper -> {
                wrapper.like(StringUtils.isNotEmpty(zfDoctor.getPrescription()),ZfDoctor::getWmedical,zfDoctor.getPrescription())
                        .or()
                        .like(StringUtils.isNotEmpty(zfDoctor.getPrescription()),ZfDoctor::getCmedical,zfDoctor.getPrescription());
            });
        }
        return lqw;
    }
}