jiangping
2025-02-20 f80fb683f62639ec7848cc82d37e8313c7ffdb0c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package com.doumee.service.business.impl;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.wx.WxMiniConfig;
import com.doumee.dao.business.BikesMapper;
import com.google.common.collect.Lists;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Utils;
import com.doumee.core.wx.WxMiniUtilService;
import com.doumee.dao.business.LocksMapper;
import com.doumee.dao.business.join.LocksJoinMapper;
import com.doumee.dao.business.model.BaseParam;
import com.doumee.dao.business.model.Bikes;
import com.doumee.dao.business.model.Locks;
import com.doumee.dao.business.model.Sites;
import com.doumee.dao.system.model.SystemDictData;
import com.doumee.service.business.LocksService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.service.business.SitesService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 锁头信息表Service实现
 * @author 江蹄蹄
 * @date 2023/09/27 18:06
 */
@Service
public class LocksServiceImpl implements LocksService {
 
    @Autowired
    private LocksMapper locksMapper;
 
    @Autowired
    private LocksJoinMapper locksJoinMapper;
 
 
    @Autowired
    private WxMiniUtilService wxMiniUtilService;
 
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private BikesMapper bikesMapper;
 
    @Override
    public String create(Locks locks) {
        locksMapper.insert(locks);
        return locks.getId();
    }
 
    @Override
    public void deleteById(String id) {
        locksMapper.deleteById(id);
    }
 
    @Override
    public void delete(Locks locks) {
        UpdateWrapper<Locks> deleteWrapper = new UpdateWrapper<>(locks);
        locksMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        locksMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Locks locks) {
        locksMapper.updateById(locks);
    }
 
    @Override
    public void updateByIdInBatch(List<Locks> lockss) {
        if (CollectionUtils.isEmpty(lockss)) {
            return;
        }
        for (Locks locks: lockss) {
            this.updateById(locks);
        }
    }
 
    @Override
    public Locks findById(String id) {
        return locksMapper.selectById(id);
    }
 
    @Override
    public Locks findOne(Locks locks) {
        QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks);
        return locksMapper.selectOne(wrapper.last(" limit 1"));
    }
 
    @Override
    public List<Locks> findList(Locks locks) {
        QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks);
        return locksMapper.selectList(wrapper);
    }
 
    @Override
    public List<Locks> findLockBase64List(Locks locks) {
        LoginUserInfo loginUserInfo =  (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        String fullPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode() +
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode() ;
 
        QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks);
        wrapper.lambda().orderByAsc(Locks::getCode);
        List<Locks> locksList = locksMapper.selectList(wrapper);
        SystemDictData systemDictData = systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.ACCESS_TOKEN);
        String code = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        String prePath = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.IMG_DIR).getCode();
        if (!CollectionUtils.isEmpty(locksList)){
            // 检查是否有图片信息没有更新保存图片
            locksList.forEach(s-> {
                if (Constants.equalsInteger(locks.getFource(),Constants.ONE) ||StringUtils.isBlank(s.getInfo())){
                    try {
                        wxMiniUtilService.generateWXMiniCode(s, WxMiniConfig.wxMaService.getAccessToken(),prePath,code);
                    }catch (Exception e){
                    }
                    s.setEditor(loginUserInfo.getId());
                    s.setEditDate(new Date());
                    updateById(s);
                }
                s.setImgfullurl(fullPath+s.getInfo());
            });
            return locksList;
        }
        return new ArrayList<>();
    }
 
    /**
     * 获取所有电测小程序二维码
     * @param force 0不强制刷新 1强制刷新
     * @return
     */
    @Override
    public     List<Bikes> findAllBikeBase(Integer force) {
        List<Bikes> bikesList = bikesMapper.selectList(new QueryWrapper<Bikes>().lambda()
                .eq(Bikes::getIsdeleted,Constants.ZERO)
                .eq(Bikes::getType,Constants.ONE)
        );
        if(bikesList!=null && bikesList.size()>0){
            LoginUserInfo loginUserInfo =  (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
            String fullPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode() +
                    systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode() ;
            String code = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
            String prePath = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.IMG_DIR).getCode();
            for(Bikes bikes : bikesList){
                if (Constants.equalsInteger(force,Constants.ONE) || StringUtils.isBlank(bikes.getImgurl())){
                    try {
                        wxMiniUtilService.generateEbikeWXMiniCode(bikes, WxMiniConfig.wxMaService.getAccessToken(),prePath,code);
                    }catch (Exception e){
                    }
                    if (StringUtils.isNotBlank(bikes.getImgurl())){
                        bikes.setEditor(loginUserInfo.getId());
                        bikes.setEditDate(new Date());
                        bikesMapper.updateById(bikes);
                    }
                }
                if (StringUtils.isNotBlank(bikes.getImgurl())){
                    bikes.setImgurl(fullPath+bikes.getImgurl());
                }
            }
 
        }
 
        return bikesList;
    }
    @Override
    public    Bikes findBikeBase(String bikeId, Integer force) {
       Bikes bikes = bikesMapper.selectById(bikeId);
       if(bikes == null
               || Constants.equalsInteger(bikes.getIsdeleted(),Constants.ONE)
               || !Constants.equalsInteger(bikes.getType(),Constants.ONE)){
           throw new BusinessException(ResponseStatus.DATA_EMPTY);
       }
        LoginUserInfo loginUserInfo =  (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        String fullPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode() +
                systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode() ;
        String code = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        String prePath = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.IMG_DIR).getCode();
            // 检查是否有图片信息没有更新保存图片
        if (Constants.equalsInteger(force,Constants.ONE) || StringUtils.isBlank(bikes.getImgurl())){
            try {
                wxMiniUtilService.generateEbikeWXMiniCode(bikes, WxMiniConfig.wxMaService.getAccessToken(),prePath,code);
            }catch (Exception e){
            }
            if (StringUtils.isNotBlank(bikes.getImgurl())){
                bikes.setEditor(loginUserInfo.getId());
                bikes.setEditDate(new Date());
                bikesMapper.updateById(bikes);
            }
        }
        if (StringUtils.isBlank(bikes.getImgurl())){
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"获取小程序码失败,请稍后重试!");
        }
        bikes.setImgurl(fullPath+bikes.getImgurl());
        return bikes;
    }
 
 
    @Override
    public PageData<Locks> findPage(PageWrap<Locks> pageWrap) {
        IPage<Locks> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Locks> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
 
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.like(Locks::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getSiteId() != null) {
            queryWrapper.like(Locks::getSiteId, pageWrap.getModel().getSiteId());
        }
        if (pageWrap.getModel().getSiteName() != null) {
            queryWrapper.like(Sites::getName, pageWrap.getModel().getSiteName());
        }
        queryWrapper.leftJoin(Bikes.class,Bikes::getCode,Locks::getBikeCode)
                    .leftJoin(BaseParam.class,BaseParam::getId,Bikes::getParamId)
                    .leftJoin(Sites.class,Sites::getCode,Locks::getSiteId);
        queryWrapper.orderByDesc(Locks::getBikeCode);
        queryWrapper.selectAll(Locks.class)
                    .selectAs(Sites::getName,Locks::getSiteName)
                    .selectAs(BaseParam::getName,Locks::getBikeType);
        return PageData.from(locksJoinMapper.selectJoinPage(page, Locks.class,queryWrapper));
    }
 
    @Override
    public long count(Locks locks) {
        QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks);
        return locksMapper.selectCount(wrapper);
    }
    public static void packFilesToZip(List<File> files,    ServletOutputStream os) throws IOException {
        try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) {
            for (File file : files) {
                ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());
                zipOutputStream.putArchiveEntry(entry);
                try (FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fileInputStream.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, length);
                    }
                }
                zipOutputStream.closeArchiveEntry();
            }
        }
    }
    @Override
    public void  exportImages(String siteId, HttpServletResponse response) {
        try {
            List<File> fileList = new ArrayList<>();
            List<Locks> locks = locksJoinMapper.selectList(new QueryWrapper<Locks>().lambda().eq(Locks::getSiteId,siteId).isNotNull(Locks::getInfo));
            if(locks== null || locks.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.IMG_DIR).getCode()+systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode();
            for(Locks l : locks){
                if(StringUtils.isNotBlank(l.getInfo())){
                    File file = new File(path + l.getInfo());
                    if(file!=null && file.isFile()){
                        fileList.add(file);
                    }
                }
            }
            if(fileList == null || fileList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String fileName =  "站点【"+siteId+"】小程序码批量导出_"+System.currentTimeMillis()+".zip" ;
            String encodeFileName = URLEncoder.encode(fileName);
            response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName);
            response.setContentType("application/octet-stream");
            response.setHeader("eva-opera-type", "download");
            response.setHeader("eva-download-filename", encodeFileName);
            packFilesToZip(fileList,response.getOutputStream());
        } catch (IOException e) {
            throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e);
        }
    }
    @Override
    public void exportEbikeImages(String bikeid, HttpServletResponse response){
        try {
            List<File> fileList = new ArrayList<>();
            List<Bikes> bikesList = bikesMapper.selectList(new QueryWrapper<Bikes>().lambda()
                    .eq(Bikes::getIsdeleted,Constants.ZERO)
                    .eq(StringUtils.isNotBlank(bikeid) ,Bikes::getId,bikeid)
                    .eq(Bikes::getType,Constants.ONE)
            );
            if(bikesList== null || bikesList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.IMG_DIR).getCode()+systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode();
            for(Bikes l : bikesList){
                if(StringUtils.isNotBlank(l.getImgurl())){
                    File file = new File(path + l.getImgurl());
                    if(file!=null && file.isFile()){
                        fileList.add(file);
                    }
                }
            }
            if(fileList == null || fileList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String fileName =  "电车小程序码批量导出_"+System.currentTimeMillis()+".zip" ;
            String encodeFileName = URLEncoder.encode(fileName);
            response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName);
            response.setContentType("application/octet-stream");
            response.setHeader("eva-opera-type", "download");
            response.setHeader("eva-download-filename", encodeFileName);
            packFilesToZip(fileList,response.getOutputStream());
        } catch (IOException e) {
            throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e);
        }
    }
 
}