|
@@ -1,30 +1,49 @@
|
|
package edu.travel.tenant.service.impl;
|
|
package edu.travel.tenant.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.io.FileTypeUtil;
|
|
import cn.hutool.core.io.FileTypeUtil;
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import edu.travel.adapter.service.country.CountryAdapter;
|
|
import edu.travel.adapter.service.upload.UploadAdapter;
|
|
import edu.travel.adapter.service.upload.UploadAdapter;
|
|
import edu.travel.remote.feign.mode.dto.tenant.BannerDto;
|
|
import edu.travel.remote.feign.mode.dto.tenant.BannerDto;
|
|
import edu.travel.remote.feign.mode.vo.banner.BannerVo;
|
|
import edu.travel.remote.feign.mode.vo.banner.BannerVo;
|
|
|
|
+import edu.travel.remote.upload.dto.EduFileDTO;
|
|
|
|
+import edu.travel.remote.upload.vo.EduFileVo;
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
|
|
+import edu.travel.tenant.constant.BaseConstant;
|
|
import edu.travel.tenant.entity.ShopBanner;
|
|
import edu.travel.tenant.entity.ShopBanner;
|
|
import edu.travel.tenant.mapper.ShopBannerMapper;
|
|
import edu.travel.tenant.mapper.ShopBannerMapper;
|
|
import edu.travel.tenant.service.ShopBannerService;
|
|
import edu.travel.tenant.service.ShopBannerService;
|
|
|
|
+import edu.travel.tenant.utils.FIleUtil;
|
|
|
|
+import edu.travel.vo.BaseCountryServeVo;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+import static edu.travel.rpc.RPCBaseResponse.error;
|
|
|
|
+import static edu.travel.rpc.RPCBaseResponse.success;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class ShopBannerServiceImpl extends ServiceImpl<ShopBannerMapper, ShopBanner> implements ShopBannerService {
|
|
public class ShopBannerServiceImpl extends ServiceImpl<ShopBannerMapper, ShopBanner> implements ShopBannerService {
|
|
@Autowired
|
|
@Autowired
|
|
private UploadAdapter uploadAdapter;
|
|
private UploadAdapter uploadAdapter;
|
|
|
|
+ @Autowired
|
|
|
|
+ private CountryAdapter countryAdapter;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ShopBannerMapper shopBannerMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ShopBannerService shopBannerService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public List<BannerVo> getBanner(BannerDto mode) {
|
|
public List<BannerVo> getBanner(BannerDto mode) {
|
|
List<BannerVo> list = new ArrayList<>();
|
|
List<BannerVo> list = new ArrayList<>();
|
|
@@ -45,17 +64,242 @@ public class ShopBannerServiceImpl extends ServiceImpl<ShopBannerMapper, ShopBan
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public RPCBaseResponse<String> uploadBannerImage(MultipartFile file) {
|
|
public RPCBaseResponse<String> uploadBannerImage(MultipartFile file) {
|
|
- //判断文件是否为空
|
|
|
|
|
|
+ // 判断上传的文件是否为空或未选择
|
|
if (file == null || file.isEmpty()) {
|
|
if (file == null || file.isEmpty()) {
|
|
- return RPCBaseResponse.error();
|
|
|
|
- }
|
|
|
|
-// try{
|
|
|
|
-// String fileType = FileTypeUtil.getType(file.getInputStream());
|
|
|
|
-//
|
|
|
|
-// }catch (){
|
|
|
|
-// return RPCBaseResponse.error();
|
|
|
|
-// }
|
|
|
|
- return null;
|
|
|
|
|
|
+ return error();
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 获取文件类型(MIME 类型)
|
|
|
|
+ String fileType = FileTypeUtil.getType(file.getInputStream());
|
|
|
|
+
|
|
|
|
+ // 检查文件类型是否有效且是否在允许的横幅图片类型列表中
|
|
|
|
+ if (fileType != null && BaseConstant.getBannerImageTypes().contains(fileType.toLowerCase())) {
|
|
|
|
+ // 获取用户上传的文件名
|
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
|
+ // 检查文件名是否为空
|
|
|
|
+ if (ObjectUtil.isEmpty(filename)) {
|
|
|
|
+ return error(); // 返回错误响应
|
|
|
|
+ }
|
|
|
|
+ // 初始化上传,获取上传 ID
|
|
|
|
+ RPCBaseResponse<String> stringRPCBaseResponse = uploadAdapter.initializeUpload();
|
|
|
|
+ // 创建文件 DTO 对象,用于存储文件相关信息
|
|
|
|
+ EduFileDTO eduFileDTO = new EduFileDTO();
|
|
|
|
+ eduFileDTO.setUploadId(stringRPCBaseResponse.getData()); // 设置上传 ID
|
|
|
|
+ eduFileDTO.setFileType(fileType); // 设置文件类型
|
|
|
|
+ eduFileDTO.setFileName(filename); // 设置文件原始名称
|
|
|
|
+ // 生成系统文件名,确保唯一性
|
|
|
|
+ eduFileDTO.setFileSysName(IdUtil.fastSimpleUUID() + DateUtil.format(new Date(), "yyyyMMddHHmmss") + "." + fileType);
|
|
|
|
+ // 计算文件的 MD5 值以便后续验证
|
|
|
|
+ eduFileDTO.setFileMd5(FIleUtil.calculateFileMd5(file.getBytes()));
|
|
|
|
+ // 调用上传适配器进行文件上传
|
|
|
|
+ RPCBaseResponse rpcBaseResponse = uploadAdapter.uploadFile(eduFileDTO, file);
|
|
|
|
+ // 返回成功响应,包含上传后的数据
|
|
|
|
+ return success(rpcBaseResponse.getData().toString());
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace(); // 打印异常栈信息
|
|
|
|
+ return error("file upload fail"); // 返回表示文件上传失败的错误响应
|
|
|
|
+ }
|
|
|
|
+ return error(); // 如果文件类型不支持,返回错误响应
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增轮播图信息
|
|
|
|
+ * @param banner
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public RPCBaseResponse<BannerVo> saveBannerCountry(BannerDto banner) {
|
|
|
|
+ // 验证输入参数的有效性
|
|
|
|
+ if (ObjectUtil.isEmpty(banner)) {
|
|
|
|
+ return error("Banner data cannot be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 检查 isDefault 是否被输入
|
|
|
|
+ if (banner.getIsDefault() == null) {
|
|
|
|
+ return error("isDefault must be provided");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果 isDefault 为 0,检查 startTime 和 endTime
|
|
|
|
+ if (banner.getIsDefault() == 0) {
|
|
|
|
+ if (ObjectUtil.isEmpty(banner.getStartTime()) || ObjectUtil.isEmpty(banner.getEndTime())) {
|
|
|
|
+ return error("startTime and endTime must be provided when isDefault is false");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 使用 uploadAdapter.getFormId 查找出 file_path 和 fileType
|
|
|
|
+ RPCBaseResponse<EduFileVo> fileResponse = uploadAdapter.getFormId(banner.getImageId());
|
|
|
|
+ if (fileResponse.getData() == null) {
|
|
|
|
+ return error("Failed to retrieve file information");
|
|
|
|
+ }
|
|
|
|
+ EduFileVo fileData = fileResponse.getData();
|
|
|
|
+ String filePath = fileData.getFilePath(); // 假设 EduFileDTO 有 getFilePath 方法
|
|
|
|
+ String fileType = fileData.getFileType(); // 假设 EduFileDTO 有 getFileType 方法
|
|
|
|
+
|
|
|
|
+ // 创建新的 BannerVo 对象
|
|
|
|
+ BannerVo bannerVo = new BannerVo();
|
|
|
|
+ bannerVo.setUrl(filePath); // 设置文件路径
|
|
|
|
+ bannerVo.setUrlType("image".equalsIgnoreCase(fileType) ? 0 : 1); // 假设0为图片,1为视频
|
|
|
|
+ bannerVo.setContent(banner.getContent()); // 设置名称
|
|
|
|
+ bannerVo.setCountryId(banner.getCountryId()); // 设置国家
|
|
|
|
+ bannerVo.setLinkUrl(banner.getLinkUrl()); // 设置链接地址
|
|
|
|
+ bannerVo.setMode(banner.getMode());
|
|
|
|
+
|
|
|
|
+ // 这里可以进行其他必要的字段设置,比如与 banner 相关的其他信息
|
|
|
|
+ bannerVo.setId(banner.getId()); // 如果需要设置ID,可以从DTO中获取
|
|
|
|
+
|
|
|
|
+ // 保存横幅信息到数据库,假设我们有一个方法 saveBanner
|
|
|
|
+ ShopBanner shopBanner = new ShopBanner();
|
|
|
|
+ // 将 bannerVo 转换为 shopBanner (可以使用 BeanUtil.copyProperties 或自定义转换)
|
|
|
|
+ BeanUtil.copyProperties(bannerVo, shopBanner);
|
|
|
|
+ // 设置其他必要字段
|
|
|
|
+ shopBanner.setEnable(0); // 示例:设置为启用状态
|
|
|
|
+ shopBanner.setCreateTime(new Date()); // 设置当前时间为创建时间
|
|
|
|
+
|
|
|
|
+ // 设置 isDefault、startTime 和 endTime 字段
|
|
|
|
+ shopBanner.setIsDefault(banner.getIsDefault());
|
|
|
|
+ if (banner.getIsDefault() == 0) {
|
|
|
|
+ shopBanner.setStartTime(banner.getStartTime());
|
|
|
|
+ shopBanner.setEndTime(banner.getEndTime());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 调用 mapper 保存到数据库
|
|
|
|
+ this.save(shopBanner);
|
|
|
|
+
|
|
|
|
+ return success(bannerVo); // 返回成功的响应
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return error("Failed to save banner country");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新横幅信息
|
|
|
|
+ * @param banner
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public RPCBaseResponse<BannerVo> updateBannerCountry(BannerDto banner) {
|
|
|
|
+ // 验证输入参数的有效性
|
|
|
|
+ if (ObjectUtil.isEmpty(banner) || ObjectUtil.isEmpty(banner.getId())) {
|
|
|
|
+ return error("Banner data cannot be null and id must be provided");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 检查 isDefault 是否被输入
|
|
|
|
+ if (banner.getIsDefault() == null) {
|
|
|
|
+ return error("isDefault must be provided");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果 isDefault 为 0,检查 startTime 和 endTime
|
|
|
|
+ if (banner.getIsDefault() == 0) {
|
|
|
|
+ if (ObjectUtil.isEmpty(banner.getStartTime()) || ObjectUtil.isEmpty(banner.getEndTime())) {
|
|
|
|
+ return error("startTime and endTime must be provided when isDefault is 0");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 使用 uploadAdapter.getFormId 查找出 file_path 和 fileType
|
|
|
|
+ RPCBaseResponse<EduFileVo> fileResponse = uploadAdapter.getFormId(banner.getImageId());
|
|
|
|
+ if (fileResponse.getData() == null) {
|
|
|
|
+ return error("Failed to retrieve file information");
|
|
|
|
+ }
|
|
|
|
+ EduFileVo fileData = fileResponse.getData();
|
|
|
|
+ String filePath = fileData.getFilePath(); // 假设 EduFileDTO 有 getFilePath 方法
|
|
|
|
+ String fileType = fileData.getFileType(); // 假设 EduFileDTO 有 getFileType 方法
|
|
|
|
+
|
|
|
|
+ // 查找现有的 ShopBanner 对象进行更新
|
|
|
|
+ ShopBanner shopBanner = this.getById(banner.getId());
|
|
|
|
+ if (shopBanner == null) {
|
|
|
|
+ return error("Banner not found");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 更新 BannerVo 对象
|
|
|
|
+ BannerVo bannerVo = new BannerVo();
|
|
|
|
+ bannerVo.setUrl(filePath); // 设置文件路径
|
|
|
|
+ bannerVo.setUrlType("image".equalsIgnoreCase(fileType) ? 0 : 1); // 假设0为图片,1为视频
|
|
|
|
+ bannerVo.setContent(banner.getContent()); // 设置名称
|
|
|
|
+ bannerVo.setCountryId(banner.getCountryId()); // 设置国家
|
|
|
|
+ bannerVo.setLinkUrl(banner.getLinkUrl()); // 设置链接地址
|
|
|
|
+ bannerVo.setMode(banner.getMode());
|
|
|
|
+ bannerVo.setId(banner.getId()); // 保持ID一致
|
|
|
|
+
|
|
|
|
+ // 将 bannerVo 转换为 shopBanner,并设置其他字段
|
|
|
|
+ BeanUtil.copyProperties(bannerVo, shopBanner);
|
|
|
|
+ shopBanner.setEnable(0); // 示例:设置为启用状态
|
|
|
|
+ shopBanner.setUpdateTime(new Date()); // 设置当前时间为更新时间
|
|
|
|
+
|
|
|
|
+ // 设置 isDefault、startTime 和 endTime 字段
|
|
|
|
+ shopBanner.setIsDefault(banner.getIsDefault());
|
|
|
|
+ if (banner.getIsDefault() == 0) {
|
|
|
|
+ shopBanner.setStartTime(banner.getStartTime());
|
|
|
|
+ shopBanner.setEndTime(banner.getEndTime());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 调用 mapper 更新到数据库
|
|
|
|
+ this.updateById(shopBanner);
|
|
|
|
+
|
|
|
|
+ return success(bannerVo); // 返回成功的响应
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return error("Failed to update banner country");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取所有信息(分页-连表)
|
|
|
|
+ * @param bannerDto
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public RPCBaseResponse<IPage<BannerVo>> getBannerAllPage(BannerDto bannerDto) {
|
|
|
|
+ //查询轮播图数据,确保返回类型正确
|
|
|
|
+ List<BannerVo> bannerVos = shopBannerMapper.selectBannerWithCountry(bannerDto);
|
|
|
|
+ List<BannerVo> shopBannerVos = bannerVos;
|
|
|
|
+
|
|
|
|
+ int total = shopBannerMapper.countAll(bannerDto);
|
|
|
|
+
|
|
|
|
+ //提取CountryID
|
|
|
|
+ Set<String> countryIds = shopBannerVos.stream()
|
|
|
|
+ .map(BannerVo::getCountryId)
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
+ //查询国家信息
|
|
|
|
+ Map<String, BannerVo> countryMap = fetchCountryData(countryIds);
|
|
|
|
+
|
|
|
|
+ //遍历并设置国家信息
|
|
|
|
+ for (BannerVo shopBannerVo : shopBannerVos) {
|
|
|
|
+ if (shopBannerVo.getMap() == null) {
|
|
|
|
+ shopBannerVo.setMap(new HashMap<>());
|
|
|
|
+ }
|
|
|
|
+ if (countryMap.containsKey(shopBannerVo.getCountryId())) {
|
|
|
|
+ shopBannerVo.getMap().put("countryServe", countryMap.get(shopBannerVo.getCountryId()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //封装返回结果
|
|
|
|
+ IPage<BannerVo> pageVoLink = new Page<>();
|
|
|
|
+ pageVoLink.setRecords(shopBannerVos);
|
|
|
|
+ pageVoLink.setTotal(total);
|
|
|
|
+ pageVoLink.setCurrent(bannerDto.getCurrentPage());
|
|
|
|
+ pageVoLink.setSize(bannerDto.getPageSize());
|
|
|
|
+ return new RPCBaseResponse<>(200, "SUCCESS", pageVoLink);
|
|
}
|
|
}
|
|
|
|
+ //查询国家信息
|
|
|
|
+ private Map<String, BannerVo>fetchCountryData(Set<String> countryIds){
|
|
|
|
+ Map<String, BannerVo> countryMap = new HashMap<>();
|
|
|
|
+ for (String countryId : countryIds) {
|
|
|
|
+ BaseCountryServeVo countryServeVo = countryAdapter.getFormId(countryId).getData();
|
|
|
|
+ if (countryServeVo != null) {
|
|
|
|
+ BannerVo bannerVo = new BannerVo();
|
|
|
|
+ bannerVo.setCountryId(countryId);
|
|
|
|
+ bannerVo.setMap(new HashMap<>());
|
|
|
|
+ bannerVo.getMap().put("countryNameZh", countryServeVo.getCountryNameZh());
|
|
|
|
+ bannerVo.getMap().put("countryNameEn", countryServeVo.getCountryNameEn());
|
|
|
|
+ bannerVo.getMap().put("countryNameLocal", countryServeVo.getCountryNameLocal());
|
|
|
|
+ countryMap.put(countryId, bannerVo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return countryMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|