From 7310116a85fd0a6bf3e0b7ccd9387596e3236448 Mon Sep 17 00:00:00 2001 From: yaoxin <1752800946@qq.com> Date: Fri, 1 Mar 2024 19:25:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E6=A8=A1=E5=9D=97=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=BB=84=E5=A2=9E=E5=88=A0=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/web/domain/TreeEntity.java | 1 + muyu-modules/muyu-file/pom.xml | 7 + .../file/service/LocalSysFileServiceImpl.java | 5 +- .../java/com/muyu/file/utils/OssUtil.java | 155 ++++++++++++++++++ .../muyu/product/domain/AsAttributeGroup.java | 6 + .../com/muyu/product/domain/CategoryInfo.java | 2 + .../domain/req/AttributeGroupEditReq.java | 5 + .../domain/req/CategoryInfoSaveReq.java | 1 + .../controller/AttributeGroupController.java | 1 + .../controller/AttributeInfoController.java | 4 + .../mapper/AsAttributeGroupMapper.java | 4 + .../service/AsAttributeGroupService.java | 2 + .../service/AttributeGroupService.java | 2 + .../impl/AsAttributeGroupServiceImpl.java | 9 + .../impl/AttributeGroupServiceImpl.java | 22 +++ .../impl/AttributeInfoServiceImpl.java | 6 + .../mapper/product/AsAttributeGroupMapper.xml | 7 + 17 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 muyu-modules/muyu-file/src/main/java/com/muyu/file/utils/OssUtil.java diff --git a/muyu-common/muyu-common-core/src/main/java/com/muyu/common/core/web/domain/TreeEntity.java b/muyu-common/muyu-common-core/src/main/java/com/muyu/common/core/web/domain/TreeEntity.java index 3d9c1ef..1bad4d9 100644 --- a/muyu-common/muyu-common-core/src/main/java/com/muyu/common/core/web/domain/TreeEntity.java +++ b/muyu-common/muyu-common-core/src/main/java/com/muyu/common/core/web/domain/TreeEntity.java @@ -32,6 +32,7 @@ public class TreeEntity extends BaseEntity { /** * 父菜单ID */ + private Long parentId; /** diff --git a/muyu-modules/muyu-file/pom.xml b/muyu-modules/muyu-file/pom.xml index e1de9af..30c0ac0 100644 --- a/muyu-modules/muyu-file/pom.xml +++ b/muyu-modules/muyu-file/pom.xml @@ -17,6 +17,13 @@ + + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + + com.alibaba.cloud diff --git a/muyu-modules/muyu-file/src/main/java/com/muyu/file/service/LocalSysFileServiceImpl.java b/muyu-modules/muyu-file/src/main/java/com/muyu/file/service/LocalSysFileServiceImpl.java index f3ce089..b6c2bce 100644 --- a/muyu-modules/muyu-file/src/main/java/com/muyu/file/service/LocalSysFileServiceImpl.java +++ b/muyu-modules/muyu-file/src/main/java/com/muyu/file/service/LocalSysFileServiceImpl.java @@ -1,6 +1,7 @@ package com.muyu.file.service; import com.muyu.file.utils.FileUploadUtils; +import com.muyu.file.utils.OssUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; @@ -44,7 +45,9 @@ public class LocalSysFileServiceImpl implements ISysFileService { @Override public String uploadFile (MultipartFile file) throws Exception { String name = FileUploadUtils.upload(localFilePath, file); - String url = domain + localFilePrefix + name; + String s = OssUtil.uploadMultipartFile(file); + String url = s; + System.out.println("==="+url); return url; } } diff --git a/muyu-modules/muyu-file/src/main/java/com/muyu/file/utils/OssUtil.java b/muyu-modules/muyu-file/src/main/java/com/muyu/file/utils/OssUtil.java new file mode 100644 index 0000000..e3d1b96 --- /dev/null +++ b/muyu-modules/muyu-file/src/main/java/com/muyu/file/utils/OssUtil.java @@ -0,0 +1,155 @@ +package com.muyu.file.utils; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.GetObjectRequest; +import com.aliyun.oss.model.PutObjectRequest; +import lombok.extern.log4j.Log4j2; +import org.springframework.web.multipart.MultipartFile; + +import java.io.*; +import java.time.LocalDateTime; +import java.util.UUID; + +/** + * Oss服务调用 + */ +@Log4j2 +public class OssUtil { + + + + /** + * Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述 + */ + private static String endPoint = "oss-cn-shanghai.aliyuncs.com"; + private static String accessKeyId = "LTAI5tS2MAqRExUpPHy48woQ"; + private static String accessKeySecret = "tJDYdPm6YOec2glrz0Gq7rzkDdLbnw"; + private static String accessPre = "https://bwie-master.oss-cn-shanghai.aliyuncs.com/"; + + /** + * bucket名称 + * @return + */ + private static String bucketName = "bwie-master"; + + private static OSS ossClient ; + + static { + ossClient = new OSSClientBuilder().build( + endPoint, + accessKeyId, + accessKeySecret); + log.info("oss服务连接成功!"); + } + + /** + * 默认路径上传本地文件 + * @param filePath + */ + public static String uploadFile(String filePath){ + return uploadFileForBucket(bucketName,getOssFilePath(filePath) ,filePath); + } + + /** + * 默认路径上传multipartFile文件 + * @param multipartFile + */ + public static String uploadMultipartFile(MultipartFile multipartFile) { + return uploadMultipartFile(bucketName,getOssFilePath(multipartFile.getOriginalFilename()),multipartFile); + } + /** + * 上传 multipartFile 类型文件 + * @param bucketName + * @param ossPath + * @param multipartFile + */ + public static String uploadMultipartFile(String bucketName , String ossPath , MultipartFile multipartFile){ + InputStream inputStream = null; + try { + inputStream = multipartFile.getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + } + uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); + return accessPre+ossPath; + } + + /** + * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传 + * @param bucketName 实例名称 + * @param ossPath oss存储路径 + * @param filePath 本地文件路径 + */ + public static String uploadFileForBucket(String bucketName , String ossPath , String filePath) { + // 创建PutObjectRequest对象。 + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath)); + + // 上传 + ossClient.putObject(putObjectRequest); + return accessPre+ossPath; + } + + /** + * 使用文件流上传到指定的bucket实例 + * @param bucketName 实例名称 + * @param ossPath oss存储路径 + * @param filePath 本地文件路径 + */ + public static String uploadFileInputStreamForBucket(String bucketName , String ossPath , String filePath){ + + // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 + InputStream inputStream = null; + try { + inputStream = new FileInputStream(filePath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。 + uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); + return accessPre+ossPath; + } + + public static void uploadFileInputStreamForBucket(String bucketName , String ossPath , InputStream inputStream ){ + ossClient.putObject(bucketName, ossPath, inputStream); + } + + /** + * 下载 + * @param ossFilePath + * @param filePath + */ + public static void downloadFile(String ossFilePath , String filePath ){ + downloadFileForBucket(bucketName , ossFilePath , filePath); + } + /** + * 下载 + * @param bucketName 实例名称 + * @param ossFilePath oss存储路径 + * @param filePath 本地文件路径 + */ + public static void downloadFileForBucket(String bucketName , String ossFilePath , String filePath ){ + ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath)); + } + + /** + * + * @return + */ + public static String getOssDefaultPath(){ + LocalDateTime now = LocalDateTime.now(); + String url = + now.getYear()+"/"+ + now.getMonth()+"/"+ + now.getDayOfMonth()+"/"+ + now.getHour()+"/"+ + now.getMinute()+"/"; + return url; + } + + public static String getOssFilePath(String filePath){ + String fileSuf = filePath.substring(filePath.indexOf(".") + 1); + return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf; + } + +} \ No newline at end of file diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AsAttributeGroup.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AsAttributeGroup.java index d3d6cff..66c642b 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AsAttributeGroup.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AsAttributeGroup.java @@ -52,4 +52,10 @@ public class AsAttributeGroup extends BaseEntity { .attributeId(attributeId) .build(); } + + public static AsAttributeGroup buildGroup (Long attributeGroupId) { + return AsAttributeGroup.builder() + .groupId(attributeGroupId) + .build(); + } } diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java index dc06d43..c4450c6 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java @@ -78,6 +78,7 @@ public class CategoryInfo extends TreeEntity { .image(categoryInfoSaveReq.getImage()) .start(categoryInfoSaveReq.getStart()) .introduction(categoryInfoSaveReq.getIntroduction()) + .parentId(categoryInfoSaveReq.getParentId()) .build(); } @@ -91,6 +92,7 @@ public class CategoryInfo extends TreeEntity { .image(categoryInfoEditReq.getImage()) .start(categoryInfoEditReq.getStart()) .introduction(categoryInfoEditReq.getIntroduction()) + .parentId(categoryInfoEditReq.getParentId()) .build(); } diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/AttributeGroupEditReq.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/AttributeGroupEditReq.java index 35a1b47..8c703d9 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/AttributeGroupEditReq.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/AttributeGroupEditReq.java @@ -1,6 +1,8 @@ package com.muyu.product.domain.req; import java.util.Date; +import java.util.List; + import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; @@ -33,5 +35,8 @@ public class AttributeGroupEditReq extends BaseEntity { @ApiModelProperty(name = "状态", value = "状态", required = true) private String states; + private List attributeIdList; + + } diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/CategoryInfoSaveReq.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/CategoryInfoSaveReq.java index 62f78e9..4c60f1b 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/CategoryInfoSaveReq.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/req/CategoryInfoSaveReq.java @@ -48,4 +48,5 @@ public class CategoryInfoSaveReq extends TreeEntity { @ApiModelProperty(name = "介绍", value = "介绍") private String introduction; + } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeGroupController.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeGroupController.java index bd489a0..ec1266d 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeGroupController.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeGroupController.java @@ -99,6 +99,7 @@ public class AttributeGroupController extends BaseController { @PutMapping("/{id}") @ApiOperation("修改属性组") public Result edit(@PathVariable Long id, @RequestBody AttributeGroupEditReq attributeGroupEditReq) { + attributeGroupService.updateAsAttributeGrop(attributeGroupEditReq,id); return toAjax(attributeGroupService.updateById(AttributeGroup.editBuild(id,attributeGroupEditReq))); } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeInfoController.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeInfoController.java index 6f59dcb..3e777c3 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeInfoController.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/AttributeInfoController.java @@ -3,6 +3,7 @@ package com.muyu.product.controller; import java.util.List; import javax.servlet.http.HttpServletResponse; +import com.muyu.product.service.AsAttributeGroupService; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -39,6 +40,9 @@ public class AttributeInfoController extends BaseController { @Autowired private AttributeInfoService attributeInfoService; + @Autowired + private AsAttributeGroupService asAttributeGroupService; + /** * 查询商品属性列表 */ diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/mapper/AsAttributeGroupMapper.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/mapper/AsAttributeGroupMapper.java index 1b420df..207725b 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/mapper/AsAttributeGroupMapper.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/mapper/AsAttributeGroupMapper.java @@ -3,6 +3,8 @@ package com.muyu.product.mapper; import java.util.List; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.muyu.product.domain.AsAttributeGroup; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 属性与组中间Mapper接口 @@ -10,6 +12,8 @@ import com.muyu.product.domain.AsAttributeGroup; * @author DongZeLiang * @date 2024-02-27 */ +@Mapper public interface AsAttributeGroupMapper extends BaseMapper { + void deleteAsAttributeGroup(@Param("attributeIds") List attributeIds); } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AsAttributeGroupService.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AsAttributeGroupService.java index db672c4..e0377ea 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AsAttributeGroupService.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AsAttributeGroupService.java @@ -19,4 +19,6 @@ public interface AsAttributeGroupService extends IService { */ public List list(AsAttributeGroup asAttributeGroup); + void deleteAsAttributeGroup(List attributeId); + } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AttributeGroupService.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AttributeGroupService.java index 9527f88..455262a 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AttributeGroupService.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/AttributeGroupService.java @@ -7,6 +7,7 @@ import com.muyu.product.domain.AttributeGroup; import com.baomidou.mybatisplus.extension.service.IService; import com.muyu.product.domain.AttributeInfo; import com.muyu.product.domain.model.AttributeGroupSaveModel; +import com.muyu.product.domain.req.AttributeGroupEditReq; import com.muyu.product.domain.resp.AttributeGroupPageResp; /** @@ -39,4 +40,5 @@ public interface AttributeGroupService extends IService { */ public Boolean save(AttributeGroupSaveModel attributeGroupSaveModel); + void updateAsAttributeGrop(AttributeGroupEditReq attributeGroupEditReq,Long groupId); } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AsAttributeGroupServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AsAttributeGroupServiceImpl.java index 62e469e..83ec3a4 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AsAttributeGroupServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AsAttributeGroupServiceImpl.java @@ -4,6 +4,7 @@ import java.util.List; import com.muyu.common.core.utils.ObjUtils; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.muyu.product.mapper.AsAttributeGroupMapper; import com.muyu.product.domain.AsAttributeGroup; @@ -21,6 +22,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @Service public class AsAttributeGroupServiceImpl extends ServiceImpl implements AsAttributeGroupService { + @Autowired + private AsAttributeGroupMapper asAttributeGroupMapper; + /** * 查询属性与组中间列表 * @@ -46,4 +50,9 @@ public class AsAttributeGroupServiceImpl extends ServiceImpl attributeId) { + asAttributeGroupMapper.deleteAsAttributeGroup(attributeId); + } } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeGroupServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeGroupServiceImpl.java index 85f52d1..3e20118 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeGroupServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeGroupServiceImpl.java @@ -9,6 +9,7 @@ import com.muyu.product.domain.AsAttributeGroup; import com.muyu.product.domain.AttributeGroup; import com.muyu.product.domain.AttributeInfo; import com.muyu.product.domain.model.AttributeGroupSaveModel; +import com.muyu.product.domain.req.AttributeGroupEditReq; import com.muyu.product.domain.resp.AttributeGroupPageResp; import com.muyu.product.mapper.AttributeGroupMapper; import com.muyu.product.service.AsAttributeGroupService; @@ -19,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Stream; @@ -39,6 +41,9 @@ public class AttributeGroupServiceImpl extends ServiceImpl list = asAttributeGroupService.list(asAttributeGroup); + ArrayList asAttributeGroupIds = new ArrayList<>(); + for (AsAttributeGroup attributeGroup : list) { + asAttributeGroupIds.add(attributeGroup.getId()); + } + asAttributeGroupService.removeBatchByIds(asAttributeGroupIds); + List attributeIdList = attributeGroupEditReq.getAttributeIdList(); + attributeGroupService.saveBatch( + attributeIdList.stream() + .map(attributeId -> AsAttributeGroup.buildGroup(groupId, attributeId)) + .toList() + ); + } } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeInfoServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeInfoServiceImpl.java index cfa5de5..6795bea 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeInfoServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/AttributeInfoServiceImpl.java @@ -1,5 +1,6 @@ package com.muyu.product.service.impl; +import java.util.ArrayList; import java.util.List; import com.muyu.common.core.exception.ServiceException; @@ -64,6 +65,11 @@ public class AttributeInfoServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AsAttributeGroup::getGroupId, groupId); + List longs = asAttributeGroupService.list(queryWrapper).stream() + .map(AsAttributeGroup::getAttributeId) + .toList(); + System.out.println("====="+longs.toString()); + List longs1 = new ArrayList<>(); return this.listByIds( asAttributeGroupService.list(queryWrapper).stream() .map(AsAttributeGroup::getAttributeId) diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/resources/mapper/product/AsAttributeGroupMapper.xml b/muyu-modules/muyu-product/muyu-product-server/src/main/resources/mapper/product/AsAttributeGroupMapper.xml index 05c468d..286464f 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/resources/mapper/product/AsAttributeGroupMapper.xml +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/resources/mapper/product/AsAttributeGroupMapper.xml @@ -18,4 +18,11 @@ select id, group_id, attribute_id, remark, create_by, create_time, update_by, update_time from as_attribute_group + + delete from as_attribute_group where attribute_id in ( + + #{attributeId} + + ) +