999
whywhyo
2023-05-15 b5c4def616b7509ee86c8df91e42ae8bbc3a391f
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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.SysUser;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfEvent;
import com.ruoyi.domain.ZfEvent;
import com.ruoyi.domain.ZfEvent;
import com.ruoyi.mapper.ZfEventMapper;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfEventService;
import com.ruoyi.util.ArraysUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
 
import static com.ruoyi.constant.MenuAuthority.EVENT_LIST;
import static com.ruoyi.constant.MenuAuthority.PROPERTY_LIST;
 
/**
 * <p>
 * 家庭大事件表 服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-12
 */
@Slf4j
@Service
public class ZfEventServiceImpl extends ServiceImpl<ZfEventMapper, ZfEvent> implements ZfEventService {
 
    @Resource
    ZInfoUserService zInfoUserService;
 
    @Resource
    ZfEventService zfEventService;
 
    private LambdaQueryWrapper<ZfEvent> buildCondition(ZfEvent zfEvent) {
        LambdaQueryWrapper<ZfEvent> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(zfEvent.getAddress()), ZfEvent::getAddress, zfEvent.getAddress());
        lqw.like(!StringUtils.isEmpty(zfEvent.getPeople()), ZfEvent::getPeople, zfEvent.getPeople());
        lqw.like(!StringUtils.isEmpty(zfEvent.getTitle()), ZfEvent::getTitle, zfEvent.getTitle());
        lqw.like(!StringUtils.isEmpty(zfEvent.getRemark()), ZfEvent::getRemark, zfEvent.getRemark());
        lqw.eq(zfEvent.getHappenTime() != null, ZfEvent::getHappenTime, zfEvent.getHappenTime());
        lqw.between(zfEvent.getHappenStartTime() != null && zfEvent.getHappenEndTime() != null, ZfEvent::getHappenTime, zfEvent.getHappenStartTime(), zfEvent.getHappenEndTime());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfEvent> uniqueCondition(ZfEvent zfEvent){
        LambdaQueryWrapper<ZfEvent> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(zfEvent.getAddress()), ZfEvent::getAddress, zfEvent.getAddress());
        lqw.eq(!StringUtils.isEmpty(zfEvent.getPeople()), ZfEvent::getPeople, zfEvent.getPeople());
        lqw.eq(!StringUtils.isEmpty(zfEvent.getTitle()), ZfEvent::getTitle, zfEvent.getTitle());
        lqw.eq(!StringUtils.isEmpty(zfEvent.getRemark()), ZfEvent::getRemark, zfEvent.getRemark());
        lqw.eq(zfEvent.getHappenTime() != null, ZfEvent::getHappenTime, zfEvent.getHappenTime());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfEvent> buildCondition(ZfEvent zfEvent, String familyIds, String secondFamilyAuthority) {
        //模糊查询的条件
        LambdaQueryWrapper<ZfEvent> lqw = buildCondition(zfEvent);
 
        //定义一个布尔标记,标记第一家庭号是否有逗号
        boolean flag = false;
 
        StringBuilder secondFamilyIdsBuffer = new StringBuilder();
 
        //处理传入的secondFamilyAuthority   3{2007 2018 2015},4{2007 2019}
 
        if (StringUtils.isNotEmpty(secondFamilyAuthority)) {
 
            if (secondFamilyAuthority.contains(",")) {
                //逗号隔开得到如下串:3{2007 2018 2015}
                String[] authorityList = secondFamilyAuthority.split(",");
                for (String authorityAndId : authorityList) {
                    setSecondFamilyAuthority(secondFamilyIdsBuffer, authorityAndId);
                }
 
                //把后面的逗号去掉
                if (secondFamilyIdsBuffer.length() > 1) {
                    secondFamilyIdsBuffer.deleteCharAt(secondFamilyIdsBuffer.length() - 1);
                }
 
            } else {
                setSecondFamilyAuthority(secondFamilyIdsBuffer, secondFamilyAuthority);
                //把后面的逗号去掉
                if (secondFamilyIdsBuffer.length() > 1) {
                    secondFamilyIdsBuffer.deleteCharAt(secondFamilyIdsBuffer.length() - 1);
                }
            }
        }
        //将StringBuffer转成String
        String secondFamilyIds = secondFamilyIdsBuffer.toString();
 
        //第一家庭号的条件
        if (familyIds.contains(",")) {
            flag = true;
        }
 
        //第二家庭号的条件
        if (StringUtils.isNotEmpty(secondFamilyIds)) {
            if (secondFamilyIds.contains(",")) {
                String[] secondFamilyIdList = secondFamilyIds.split(",");
                if (flag) {
                    //如果第一家庭号和第二家庭号都有逗号
                    String[] familyList = familyIds.split(",");
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(familyList, secondFamilyIdList);
                    lqw.in(ZfEvent::getFamilyId, totalFamilyIdList);
                } else {
                    //如果第一家庭号没有逗号但是第二家庭号有逗号
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(secondFamilyIdList, familyIds);
                    lqw.in(ZfEvent::getFamilyId, totalFamilyIdList);
                }
 
            } else {
                if (flag) {
                    //如果第一家庭号有逗号但第二家庭号没有逗号
                    String[] familyList = familyIds.split(",");
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(familyList, secondFamilyIds);
                    lqw.in(ZfEvent::getFamilyId, totalFamilyIdList);
                } else {
                    //如果第一家庭号和第二家庭号都没有逗号
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(familyIds, secondFamilyIds);
                    lqw.in(ZfEvent::getFamilyId, totalFamilyIdList);
                }
            }
        } else {
            //如果没有第二家庭号
            if (flag) {
                String[] familyList = familyIds.split(",");
                lqw.in(ZfEvent::getFamilyId, familyList);
            } else {
                lqw.eq(ZfEvent::getFamilyId, familyIds);
            }
 
        }
 
        lqw.orderByDesc(ZfEvent::getCreateTime);
        return lqw;
    }
 
    private void setSecondFamilyAuthority(StringBuilder secondFamilyIdsBuffer, String authorityAndId) {
        //得到权限串
        String authorities = authorityAndId.substring(2, authorityAndId.length() - 1);
 
        if (authorities.contains(" ")) {
            //空格隔开得到菜单id,看是否有对应的权限
            String[] authorityIdList = authorities.split(" ");
            for (String s : authorityIdList) {
                if (s.equals(EVENT_LIST)) {
                    secondFamilyIdsBuffer.append(authorityAndId.charAt(0)).append(",");
                    break;
                }
            }
        } else {
            if (authorities.equals(EVENT_LIST)) {
                secondFamilyIdsBuffer.append(authorityAndId.charAt(0)).append(",");
            }
        }
    }
 
    private ZInfoUser getMySelf() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        LambdaQueryWrapper<ZInfoUser> zInfoUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zInfoUserLambdaQueryWrapper.eq(ZInfoUser::getUserId, userId);
        return zInfoUserService.getOne(zInfoUserLambdaQueryWrapper);
    }
 
    private String listFamilyIds() {
        ZInfoUser mySelf = getMySelf();
        return mySelf.getFamilyId();
    }
 
    private String listSecondFamilyIds() {
        ZInfoUser mySelf = getMySelf();
        return mySelf.getSecondFamilyId();
    }
 
    @Override
    public List<ZfEvent> selectByCondition(ZfEvent zfEvent) {
        String familyIds = listFamilyIds();
        String secondFamilyIds = listSecondFamilyIds();
        LambdaQueryWrapper<ZfEvent> lambdaQueryWrapper = buildCondition(zfEvent, familyIds, secondFamilyIds);
        List<ZfEvent> beanRecords = list(lambdaQueryWrapper);
        log.info("从数据库中查到的为:{}", beanRecords);
        return markOwnData(familyIds, beanRecords);
    }
 
    private static List<ZfEvent> markOwnData(String familyIds, List<ZfEvent> beanRecords) {
        return beanRecords.stream().peek(record -> {
            String recordFamilyId = record.getFamilyId();
            if (familyIds.contains(",")) {
                String[] familyIdList = familyIds.split(",");
                for (String familyId : familyIdList) {
                    if (familyId.equals(recordFamilyId)) {
                        record.setOwnData(1);
                    }
                }
            } else {
                if (familyIds.equals(recordFamilyId)) {
                    record.setOwnData(1);
                }
            }
        }).collect(Collectors.toList());
    }
 
    @Override
    public AjaxResult selectDataList(ZfEvent zfEvent, Integer pageNum, Integer pageSize) {
        String familyIds = listFamilyIds();
        String secondFamilyAuthority = listSecondFamilyIds();
        LambdaQueryWrapper<ZfEvent> lqw = buildCondition(zfEvent, familyIds, secondFamilyAuthority);
 
        Page<ZfEvent> zfEventPage = new Page<>(pageNum, pageSize);
        Page<ZfEvent> pageResult = page(zfEventPage, lqw);
 
        List<ZfEvent> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        List<ZfEvent> dtoResult = markOwnData(familyIds, beanRecords);
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult, dtoResult);
        return AjaxResult.success(data);
    }
 
    @Override
    public AjaxResult addData(ZfEvent zfEvent) {
 
        String familyIds = listFamilyIds();
        boolean flag = false;//判断当前用户的id是否有权加入当前家庭id的对象
        if (familyIds.contains(",")) {
            String[] familyList = familyIds.split(",");
            for (String familyId : familyList) {
                if (familyId.equals(zfEvent.getFamilyId())) {
                    flag = true;
                }
            }
        } else {
            if (zfEvent.getFamilyId().equals(familyIds)) {
                flag = true;
            }
        }
        if (flag) {
            if (save(zfEvent)) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
 
        } else {
            throw new RuntimeException("你没有操作该家庭号为" + zfEvent.getFamilyId() + "数据的权限");
        }
    }
 
    @Override
    public AjaxResult addData2(ZfEvent zfEvent) {
        ZInfoUser mySelf = getMySelf();
        String myFamilyId = mySelf.getFamilyId();
 
        if (StringUtils.isNotEmpty(zfEvent.getFamilyId())) {
            throw new RuntimeException("导入数据时您不能指定家庭号");
        }
 
        if (StringUtils.isEmpty(myFamilyId)) {
            throw new RuntimeException("您还未加入任何家庭");
        }
 
        if (myFamilyId.contains(",")) {
            String[] myFamilyIds = myFamilyId.split(",");
            //默认添加第一个家庭的数据
            zfEvent.setFamilyId(myFamilyIds[0]);
        } else {
            zfEvent.setFamilyId(myFamilyId);
        }
 
        //判断是否有重复数据
        LambdaQueryWrapper<ZfEvent> zfEventLambdaQueryWrapper = uniqueCondition(zfEvent);
        List<ZfEvent> list = list(zfEventLambdaQueryWrapper);
        if(list.size()>0){
            throw new RuntimeException("请勿新增重复数据");
        }
 
        if (save(zfEvent)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
 
 
    @Override
    @Transactional
    public AjaxResult importExcel(MultipartFile file) {
        ExcelUtil<ZfEvent> util = new ExcelUtil<>(ZfEvent.class);
        List<ZfEvent> propertyList = null;
        try {
            propertyList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("资产列表为:{}", propertyList);
 
        for (ZfEvent zfEvent : propertyList) {
            zfEventService.addData2(zfEvent);
        }
 
        return AjaxResult.success("导入数据成功");
 
    }
    
    
    
 
}