whywhyo
2023-05-03 ab06f7043e4192556c4d9881b0430bcb6cc75f50
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
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.ZfCollection;
import com.ruoyi.domain.ZfCollection;
import com.ruoyi.domain.ZfCollection;
import com.ruoyi.mapper.ZfCollectionMapper;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfCollectionService;
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.COLLECTION_LIST;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-12
 */
@Service
@Slf4j
public class ZfCollectionServiceImpl extends ServiceImpl<ZfCollectionMapper, ZfCollection> implements ZfCollectionService {
 
    @Resource
    ZfCollectionService zfCollectionService;
    
    @Resource
    ZInfoUserService zInfoUserService;
 
    private LambdaQueryWrapper<ZfCollection> buildCondition(ZfCollection zfCollection) {
        LambdaQueryWrapper<ZfCollection> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(zfCollection.getType()),ZfCollection::getType,zfCollection.getType());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getName()),ZfCollection::getName,zfCollection.getName());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getSource()),ZfCollection::getSource,zfCollection.getSource());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getOwner()),ZfCollection::getOwner,zfCollection.getOwner());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getPrice()),ZfCollection::getPrice,zfCollection.getPrice());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getLocation()),ZfCollection::getLocation,zfCollection.getLocation());
        lqw.like(StringUtils.isNotEmpty(zfCollection.getRemark()),ZfCollection::getRemark,zfCollection.getRemark());
        lqw.eq(zfCollection.getHappenTime() != null, ZfCollection::getHappenTime, zfCollection.getHappenTime());
        lqw.between(zfCollection.getHappenStartTime() != null && zfCollection.getHappenEndTime() != null, ZfCollection::getHappenTime, zfCollection.getHappenStartTime(), zfCollection.getHappenEndTime());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZfCollection> buildCondition(ZfCollection zfCollection, String familyIds, String secondFamilyAuthority) {
        //模糊查询的条件
        LambdaQueryWrapper<ZfCollection> lqw = buildCondition(zfCollection);
 
        //定义一个布尔标记,标记第一家庭号是否有逗号
        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(ZfCollection::getFamilyId, totalFamilyIdList);
                } else {
                    //如果第一家庭号没有逗号但是第二家庭号有逗号
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(secondFamilyIdList, familyIds);
                    lqw.in(ZfCollection::getFamilyId, totalFamilyIdList);
                }
 
            } else {
                if (flag) {
                    //如果第一家庭号有逗号但第二家庭号没有逗号
                    String[] familyList = familyIds.split(",");
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(familyList, secondFamilyIds);
                    lqw.in(ZfCollection::getFamilyId, totalFamilyIdList);
                } else {
                    //如果第一家庭号和第二家庭号都没有逗号
                    String[] totalFamilyIdList = ArraysUtil.appendToArray(familyIds, secondFamilyIds);
                    lqw.in(ZfCollection::getFamilyId, totalFamilyIdList);
                }
            }
        } else {
            //如果没有第二家庭号
            if (flag) {
                String[] familyList = familyIds.split(",");
                lqw.in(ZfCollection::getFamilyId, familyList);
            } else {
                lqw.eq(ZfCollection::getFamilyId, familyIds);
            }
 
        }
 
        lqw.orderByDesc(ZfCollection::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(COLLECTION_LIST)) {
                    secondFamilyIdsBuffer.append(authorityAndId.charAt(0)).append(",");
                    break;
                }
            }
        } else {
            if (authorities.equals(COLLECTION_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<ZfCollection> selectByCondition(ZfCollection zfCollection) {
        String familyIds = listFamilyIds();
        String secondFamilyIds = listSecondFamilyIds();
        LambdaQueryWrapper<ZfCollection> lambdaQueryWrapper = buildCondition(zfCollection, familyIds, secondFamilyIds);
        List<ZfCollection> beanRecords = list(lambdaQueryWrapper);
        log.info("从数据库中查到的为:{}", beanRecords);
        return markOwnData(familyIds, beanRecords);
    }
 
    private List<ZfCollection> markOwnData(String familyIds, List<ZfCollection> 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(ZfCollection zfCollection, Integer pageNum, Integer pageSize) {
        String familyIds = listFamilyIds();
        String secondFamilyAuthority = listSecondFamilyIds();
        LambdaQueryWrapper<ZfCollection> lqw = buildCondition(zfCollection, familyIds, secondFamilyAuthority);
 
        Page<ZfCollection> zfCollectionPage = new Page<>(pageNum, pageSize);
        Page<ZfCollection> pageResult = page(zfCollectionPage, lqw);
 
        List<ZfCollection> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        List<ZfCollection> dtoResult = markOwnData(familyIds, beanRecords);
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult, dtoResult);
        return AjaxResult.success(data);
    }
 
    
    @Override
    public AjaxResult addData2(ZfCollection zfCollection){
        ZInfoUser mySelf = getMySelf();
        String myFamilyId = mySelf.getFamilyId();
 
        if (StringUtils.isNotEmpty(zfCollection.getFamilyId())){
            throw new RuntimeException("导入数据时您不能指定家庭号");
        }
 
        if(StringUtils.isEmpty(myFamilyId)){
            throw new RuntimeException("您还未加入任何家庭");
        }
 
        if (myFamilyId.contains(",")){
            String[] myFamilyIds = myFamilyId.split(",");
            //默认添加第一个家庭的数据
            zfCollection.setFamilyId(myFamilyIds[0]);
        }else {
            zfCollection.setFamilyId(myFamilyId);
        }
 
        if (save(zfCollection)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
    
 
    @Override
    @Transactional
    public AjaxResult importExcel(MultipartFile file) {
        ExcelUtil<ZfCollection> util = new ExcelUtil<>(ZfCollection.class);
        List<ZfCollection> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("准备要导入的数据列表为:{}", dataList);
 
        for (ZfCollection zfCollection : dataList) {
            zfCollectionService.addData2(zfCollection);
        }
 
        return AjaxResult.success("导入数据成功");
 
    }
 
}