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.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 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; 
 | 
  
 | 
    @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 (StringUtils.isBlank(s.getInfo())){ 
 | 
                    wxMiniUtilService.generateWXMiniCode(s,systemDictData,prePath,code); 
 | 
                    s.setEditor(loginUserInfo.getId()); 
 | 
                    s.setEditDate(new Date()); 
 | 
                    updateById(s); 
 | 
                } 
 | 
                s.setImgfullurl(fullPath+s.getInfo()); 
 | 
            }); 
 | 
            return locksList; 
 | 
        } 
 | 
        return new ArrayList<>(); 
 | 
    } 
 | 
  
 | 
  
 | 
    @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); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |