更改目录结构

This commit is contained in:
liuyingfang
2024-03-02 18:31:44 +08:00
parent 8f7acca8e6
commit 0a70a66807
603 changed files with 2256 additions and 6114 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service;
import cn.ysk.cashier.domain.AlipayConfig;
import cn.ysk.cashier.domain.vo.TradeVo;
/**
* @author Zheng Jie
* @date 2018-12-31
*/
public interface AliPayService {
/**
* 查询配置
* @return AlipayConfig
*/
AlipayConfig find();
/**
* 更新配置
* @param alipayConfig 支付宝配置
* @return AlipayConfig
*/
AlipayConfig config(AlipayConfig alipayConfig);
/**
* 处理来自PC的交易请求
* @param alipay 支付宝配置
* @param trade 交易详情
* @return String
* @throws Exception 异常
*/
String toPayAsPc(AlipayConfig alipay, TradeVo trade) throws Exception;
/**
* 处理来自手机网页的交易请求
* @param alipay 支付宝配置
* @param trade 交易详情
* @return String
* @throws Exception 异常
*/
String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception;
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service;
import cn.ysk.cashier.domain.EmailConfig;
import cn.ysk.cashier.domain.vo.EmailVo;
/**
* @author Zheng Jie
* @date 2018-12-26
*/
public interface EmailService {
/**
* 更新邮件配置
* @param emailConfig 邮箱配置
* @param old /
* @return /
* @throws Exception /
*/
EmailConfig config(EmailConfig emailConfig, EmailConfig old) throws Exception;
/**
* 查询配置
* @return EmailConfig 邮件配置
*/
EmailConfig find();
/**
* 发送邮件
* @param emailVo 邮件发送的内容
* @param emailConfig 邮件配置
* @throws Exception /
*/
void send(EmailVo emailVo, EmailConfig emailConfig);
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service;
import cn.ysk.cashier.domain.LocalStorage;
import cn.ysk.cashier.service.dto.LocalStorageDto;
import cn.ysk.cashier.service.dto.LocalStorageQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author Zheng Jie
* @date 2019-09-05
*/
public interface LocalStorageService {
/**
* 分页查询
* @param criteria 条件
* @param pageable 分页参数
* @return /
*/
Object queryAll(LocalStorageQueryCriteria criteria, Pageable pageable);
/**
* 查询全部数据
* @param criteria 条件
* @return /
*/
List<LocalStorageDto> queryAll(LocalStorageQueryCriteria criteria);
/**
* 根据ID查询
* @param id /
* @return /
*/
LocalStorageDto findById(Long id);
/**
* 上传
* @param name 文件名称
* @param file 文件
* @return
*/
LocalStorage create(String name, MultipartFile file);
/**
* 编辑
* @param resources 文件信息
*/
void update(LocalStorage resources);
/**
* 多选删除
* @param ids /
*/
void deleteAll(Long[] ids);
/**
* 导出数据
* @param localStorageDtos 待导出的数据
* @param response /
* @throws IOException /
*/
void download(List<LocalStorageDto> localStorageDtos, HttpServletResponse response) throws IOException;
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service;
import cn.ysk.cashier.domain.QiniuConfig;
import cn.ysk.cashier.domain.QiniuContent;
import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author Zheng Jie
* @date 2018-12-31
*/
public interface QiNiuService {
/**
* 查配置
* @return QiniuConfig
*/
QiniuConfig find();
CloudStorageConfig findCloud();
/**
* 修改配置
* @param qiniuConfig 配置
* @return QiniuConfig
*/
QiniuConfig config(QiniuConfig qiniuConfig);
/**
* 分页查询
* @param criteria 条件
* @param pageable 分页参数
* @return /
*/
Object queryAll(QiniuQueryCriteria criteria, Pageable pageable);
/**
* 查询全部
* @param criteria 条件
* @return /
*/
List<QiniuContent> queryAll(QiniuQueryCriteria criteria);
/**
* 上传文件
* @param file 文件
* @param cloudStorageConfig 配置
* @return QiniuContent
*/
QiniuContent upload(MultipartFile file, CloudStorageConfig cloudStorageConfig);
/**
* 查询文件
* @param id 文件ID
* @return QiniuContent
*/
QiniuContent findByContentId(Long id);
/**
* 下载文件
* @param content 文件信息
* @param config 配置
* @return String
*/
String download(QiniuContent content, QiniuConfig config);
/**
* 删除文件
* @param content 文件
* @param config 配置
*/
void delete(QiniuContent content, QiniuConfig config);
/**
* 同步数据
* @param config 配置
*/
void synchronize(QiniuConfig config);
/**
* 删除文件
* @param ids 文件ID数组
* @param config 配置
*/
void deleteAll(Long[] ids, QiniuConfig config);
/**
* 更新数据
* @param type 类型
*/
void update(String type);
/**
* 导出数据
* @param queryAll /
* @param response /
* @throws IOException /
*/
void downloadList(List<QiniuContent> queryAll, HttpServletResponse response) throws IOException;
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.dto;
import lombok.Getter;
import lombok.Setter;
import cn.ysk.cashier.base.BaseDTO;
import java.io.Serializable;
/**
* @author Zheng Jie
* @date 2019-09-05
*/
@Getter
@Setter
public class LocalStorageDto extends BaseDTO implements Serializable {
private Long id;
private String realName;
private String name;
private String suffix;
private String type;
private String size;
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.dto;
import lombok.Data;
import java.sql.Timestamp;
import java.util.List;
import cn.ysk.cashier.annotation.Query;
/**
* @author Zheng Jie
* @date 2019-09-05
*/
@Data
public class LocalStorageQueryCriteria{
@Query(blurry = "name,suffix,type,createBy,size")
private String blurry;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.dto;
import lombok.Data;
import cn.ysk.cashier.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/**
* sm.ms图床
*
* @author Zheng Jie
* @date 2019-6-4 09:52:09
*/
@Data
public class PictureQueryCriteria{
@Query(type = Query.Type.INNER_LIKE)
private String filename;
@Query(type = Query.Type.INNER_LIKE)
private String username;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.dto;
import lombok.Data;
import cn.ysk.cashier.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
/**
* @author Zheng Jie
* @date 2019-6-4 09:54:37
*/
@Data
public class QiniuQueryCriteria{
@Query(type = Query.Type.INNER_LIKE)
private String key;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.impl;
import cn.ysk.cashier.domain.AlipayConfig;
import cn.ysk.cashier.domain.vo.TradeVo;
import cn.ysk.cashier.exception.BadRequestException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.AliPayRepository;
import cn.ysk.cashier.service.AliPayService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author Zheng Jie
* @date 2018-12-31
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "aliPay")
public class AliPayServiceImpl implements AliPayService {
private final AliPayRepository alipayRepository;
@Override
@Cacheable(key = "'config'")
public AlipayConfig find() {
Optional<AlipayConfig> alipayConfig = alipayRepository.findById(1L);
return alipayConfig.orElseGet(AlipayConfig::new);
}
@Override
@CachePut(key = "'config'")
@Transactional(rollbackFor = Exception.class)
public AlipayConfig config(AlipayConfig alipayConfig) {
alipayConfig.setId(1L);
return alipayRepository.save(alipayConfig);
}
@Override
public String toPayAsPc(AlipayConfig alipay, TradeVo trade) throws Exception {
if(alipay.getId() == null){
throw new BadRequestException("请先添加相应配置,再操作");
}
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
// 创建API对应的request(电脑网页版)
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
// 订单完成后返回的页面和异步通知地址
request.setReturnUrl(alipay.getReturnUrl());
request.setNotifyUrl(alipay.getNotifyUrl());
// 填充订单参数
request.setBizContent("{" +
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
" \"total_amount\":"+trade.getTotalAmount()+"," +
" \"subject\":\""+trade.getSubject()+"\"," +
" \"body\":\""+trade.getBody()+"\"," +
" \"extend_params\":{" +
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
" }"+
" }");//填充业务参数
// 调用SDK生成表单, 通过GET方式口可以获取url
return alipayClient.pageExecute(request, "GET").getBody();
}
@Override
public String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception {
if(alipay.getId() == null){
throw new BadRequestException("请先添加相应配置,再操作");
}
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
double money = Double.parseDouble(trade.getTotalAmount());
double maxMoney = 5000;
if(money <= 0 || money >= maxMoney){
throw new BadRequestException("测试金额过大");
}
// 创建API对应的request(手机网页版)
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.setReturnUrl(alipay.getReturnUrl());
request.setNotifyUrl(alipay.getNotifyUrl());
request.setBizContent("{" +
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
" \"total_amount\":"+trade.getTotalAmount()+"," +
" \"subject\":\""+trade.getSubject()+"\"," +
" \"body\":\""+trade.getBody()+"\"," +
" \"extend_params\":{" +
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
" }"+
" }");
return alipayClient.pageExecute(request, "GET").getBody();
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.impl;
import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import cn.ysk.cashier.domain.EmailConfig;
import cn.ysk.cashier.domain.vo.EmailVo;
import cn.ysk.cashier.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.EmailRepository;
import cn.ysk.cashier.service.EmailService;
import cn.ysk.cashier.utils.EncryptUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @author Zheng Jie
* @date 2018-12-26
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "email")
public class EmailServiceImpl implements EmailService {
private final EmailRepository emailRepository;
@Override
@CachePut(key = "'config'")
@Transactional(rollbackFor = Exception.class)
public EmailConfig config(EmailConfig emailConfig, EmailConfig old) throws Exception {
emailConfig.setId(1L);
if(!emailConfig.getPass().equals(old.getPass())){
// 对称加密
emailConfig.setPass(EncryptUtils.desEncrypt(emailConfig.getPass()));
}
return emailRepository.save(emailConfig);
}
@Override
@Cacheable(key = "'config'")
public EmailConfig find() {
Optional<EmailConfig> emailConfig = emailRepository.findById(1L);
return emailConfig.orElseGet(EmailConfig::new);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void send(EmailVo emailVo, EmailConfig emailConfig){
if(emailConfig.getId() == null){
throw new BadRequestException("请先配置,再操作");
}
// 封装
MailAccount account = new MailAccount();
// 设置用户
String user = emailConfig.getFromUser().split("@")[0];
account.setUser(user);
account.setHost(emailConfig.getHost());
account.setPort(Integer.parseInt(emailConfig.getPort()));
account.setAuth(true);
try {
// 对称解密
account.setPass(EncryptUtils.desDecrypt(emailConfig.getPass()));
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
}
account.setFrom(emailConfig.getUser()+"<"+emailConfig.getFromUser()+">");
// ssl方式发送
account.setSslEnable(true);
// 使用STARTTLS安全连接
account.setStarttlsEnable(true);
String content = emailVo.getContent();
// 发送
try {
int size = emailVo.getTos().size();
Mail.create(account)
.setTos(emailVo.getTos().toArray(new String[size]))
.setTitle(emailVo.getSubject())
.setContent(content)
.setHtml(true)
//关闭session
.setUseGlobalSession(false)
.send();
}catch (Exception e){
throw new BadRequestException(e.getMessage());
}
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.domain.LocalStorage;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.utils.*;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.config.FileProperties;
import cn.ysk.cashier.service.dto.LocalStorageDto;
import cn.ysk.cashier.service.dto.LocalStorageQueryCriteria;
import cn.ysk.cashier.service.mapstruct.LocalStorageMapper;
import cn.ysk.cashier.utils.*;
import cn.ysk.cashier.repository.LocalStorageRepository;
import cn.ysk.cashier.service.LocalStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* @author Zheng Jie
* @date 2019-09-05
*/
@Service
@RequiredArgsConstructor
public class LocalStorageServiceImpl implements LocalStorageService {
private final LocalStorageRepository localStorageRepository;
private final LocalStorageMapper localStorageMapper;
private final FileProperties properties;
@Override
public Object queryAll(LocalStorageQueryCriteria criteria, Pageable pageable){
Page<LocalStorage> page = localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(localStorageMapper::toDto));
}
@Override
public List<LocalStorageDto> queryAll(LocalStorageQueryCriteria criteria){
return localStorageMapper.toDto(localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
}
@Override
public LocalStorageDto findById(Long id){
LocalStorage localStorage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
ValidationUtil.isNull(localStorage.getId(),"LocalStorage","id",id);
return localStorageMapper.toDto(localStorage);
}
@Override
@Transactional(rollbackFor = Exception.class)
public LocalStorage create(String name, MultipartFile multipartFile) {
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
String type = FileUtil.getFileType(suffix);
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
if(ObjectUtil.isNull(file)){
throw new BadRequestException("上传失败");
}
try {
name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
LocalStorage localStorage = new LocalStorage(
file.getName(),
name,
suffix,
file.getPath(),
type,
FileUtil.getSize(multipartFile.getSize())
);
return localStorageRepository.save(localStorage);
}catch (Exception e){
FileUtil.del(file);
throw e;
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(LocalStorage resources) {
LocalStorage localStorage = localStorageRepository.findById(resources.getId()).orElseGet(LocalStorage::new);
ValidationUtil.isNull( localStorage.getId(),"LocalStorage","id",resources.getId());
localStorage.copy(resources);
localStorageRepository.save(localStorage);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAll(Long[] ids) {
for (Long id : ids) {
LocalStorage storage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
FileUtil.del(storage.getPath());
localStorageRepository.delete(storage);
}
}
@Override
public void download(List<LocalStorageDto> queryAll, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (LocalStorageDto localStorageDTO : queryAll) {
Map<String,Object> map = new LinkedHashMap<>();
map.put("文件名", localStorageDTO.getRealName());
map.put("备注名", localStorageDTO.getName());
map.put("文件类型", localStorageDTO.getType());
map.put("文件大小", localStorageDTO.getSize());
map.put("创建者", localStorageDTO.getCreateBy());
map.put("创建日期", localStorageDTO.getCreateTime());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
}

View File

@@ -0,0 +1,253 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.domain.QiniuConfig;
import cn.ysk.cashier.exception.BadRequestException;
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import com.dianguang.cloud.ossservice.service.OSSFactory;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.domain.QiniuContent;
import cn.ysk.cashier.repository.QiniuContentRepository;
import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
import cn.ysk.cashier.utils.QiNiuUtil;
import cn.ysk.cashier.repository.QiNiuConfigRepository;
import cn.ysk.cashier.service.QiNiuService;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
* @author Zheng Jie
* @date 2018-12-31
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "qiNiu")
public class QiNiuServiceImpl implements QiNiuService {
private final QiNiuConfigRepository qiNiuConfigRepository;
private final QiniuContentRepository qiniuContentRepository;
@Value("${qiniu.max-size}")
private Long maxSize;
@Override
@Cacheable(key = "'config'")
public QiniuConfig find() {
Optional<QiniuConfig> qiniuConfig = qiNiuConfigRepository.findById(1L);
QiniuConfig config= qiniuConfig.orElseGet(QiniuConfig::new);
return config;
}
@Override
public CloudStorageConfig findCloud() {
Optional<QiniuConfig> qiniuConfig = qiNiuConfigRepository.findById(1L);
QiniuConfig config= qiniuConfig.orElseGet(QiniuConfig::new);
if(ObjectUtil.isNotEmpty(config)){
CloudStorageConfig cloudStorageConfig=new CloudStorageConfig();
cloudStorageConfig.setType(2);
cloudStorageConfig.setUrl(config.getBucket().concat(".").concat(config.getHost()));
cloudStorageConfig.setPrefix("upload");
cloudStorageConfig.setAccessKey(config.getAccessKey());
cloudStorageConfig.setSecretKey(config.getSecretKey());
cloudStorageConfig.setBucketName(config.getBucket());
cloudStorageConfig.setEndPoint(config.getHost());
return cloudStorageConfig;
}
return null;
}
@Override
@CachePut(key = "'config'")
@Transactional(rollbackFor = Exception.class)
public QiniuConfig config(QiniuConfig qiniuConfig) {
qiniuConfig.setId(1L);
//String http = "http://", https = "https://";
////if (!(qiniuConfig.getHost().toLowerCase().startsWith(http)||qiniuConfig.getHost().toLowerCase().startsWith(https))) {
// throw new BadRequestException("外链域名必须以http://或者https://开头");
//}
return qiNiuConfigRepository.save(qiniuConfig);
}
@Override
public Object queryAll(QiniuQueryCriteria criteria, Pageable pageable){
return PageUtil.toPage(qiniuContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable));
}
@Override
public List<QiniuContent> queryAll(QiniuQueryCriteria criteria) {
return qiniuContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
}
@Override
@Transactional(rollbackFor = Exception.class)
public QiniuContent upload(MultipartFile file, CloudStorageConfig qiniuConfig) {
FileUtil.checkSize(maxSize, file.getSize());
if(qiniuConfig== null){
throw new BadRequestException("请先添加相应配置,再操作");
}
// 构造一个带指定Zone对象的配置类
try {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String url = OSSFactory.build(qiniuConfig).uploadSuffix(file.getBytes(), extension);
if(url != null){
//存入数据库
String[] str=url.split("/");
String fileName=str[str.length-1];
QiniuContent qiniuContent = new QiniuContent();
qiniuContent.setKey(fileName);
qiniuContent.setSuffix(fileName.split("\\.")[1]);
qiniuContent.setBucket(qiniuConfig.getBucketName());
qiniuContent.setSize(file.getSize()+"");
qiniuContent.setType("公开");
qiniuContent.setUrl(url);
return qiniuContentRepository.save(qiniuContent);
}
return new QiniuContent();
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
}
}
@Override
public QiniuContent findByContentId(Long id) {
QiniuContent qiniuContent = qiniuContentRepository.findById(id).orElseGet(QiniuContent::new);
ValidationUtil.isNull(qiniuContent.getId(),"QiniuContent", "id",id);
return qiniuContent;
}
@Override
public String download(QiniuContent content,QiniuConfig config){
String finalUrl;
String type = "公开";
if(type.equals(content.getType())){
finalUrl = content.getUrl();
} else {
Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
// 1小时可以自定义链接过期时间
long expireInSeconds = 3600;
finalUrl = auth.privateDownloadUrl(content.getUrl(), expireInSeconds);
}
return finalUrl;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(QiniuContent content, QiniuConfig config) {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(content.getBucket(), content.getKey() + "." + content.getSuffix());
qiniuContentRepository.delete(content);
} catch (QiniuException ex) {
qiniuContentRepository.delete(content);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void synchronize(QiniuConfig config) {
if(config.getId() == null){
throw new BadRequestException("请先添加相应配置,再操作");
}
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
BucketManager bucketManager = new BucketManager(auth, cfg);
//文件名前缀
String prefix = "";
//每次迭代的长度限制最大1000推荐值 1000
int limit = 1000;
//指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
String delimiter = "";
//列举空间文件列表
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(config.getBucket(), prefix, limit, delimiter);
while (fileListIterator.hasNext()) {
//处理获取的file list结果
QiniuContent qiniuContent;
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
if(qiniuContentRepository.findByKey(FileUtil.getFileNameNoEx(item.key)) == null){
qiniuContent = new QiniuContent();
qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize+"")));
qiniuContent.setSuffix(FileUtil.getExtensionName(item.key));
qiniuContent.setKey(FileUtil.getFileNameNoEx(item.key));
qiniuContent.setType(config.getType());
qiniuContent.setBucket(config.getBucket());
qiniuContent.setUrl(config.getHost()+"/"+item.key);
qiniuContentRepository.save(qiniuContent);
}
}
}
}
@Override
public void deleteAll(Long[] ids, QiniuConfig config) {
for (Long id : ids) {
delete(findByContentId(id), config);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(String type) {
qiNiuConfigRepository.update(type);
}
@Override
public void downloadList(List<QiniuContent> queryAll, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (QiniuContent content : queryAll) {
Map<String,Object> map = new LinkedHashMap<>();
map.put("文件名", content.getKey());
map.put("文件类型", content.getSuffix());
map.put("空间名称", content.getBucket());
map.put("文件大小", content.getSize());
map.put("空间类型", content.getType());
map.put("创建日期", content.getUpdateTime());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.mapstruct;
import cn.ysk.cashier.base.BaseMapper;
import cn.ysk.cashier.domain.LocalStorage;
import cn.ysk.cashier.service.dto.LocalStorageDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author Zheng Jie
* @date 2019-09-05
*/
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface LocalStorageMapper extends BaseMapper<LocalStorageDto, LocalStorage> {
}