diff --git a/src/main/java/com/cultural/heritage/config/MyBatisPlusConfig.java b/src/main/java/com/cultural/heritage/config/MyBatisPlusConfig.java index 11a6254..d0c904b 100644 --- a/src/main/java/com/cultural/heritage/config/MyBatisPlusConfig.java +++ b/src/main/java/com/cultural/heritage/config/MyBatisPlusConfig.java @@ -22,4 +22,7 @@ public class MyBatisPlusConfig { interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } + + + } \ No newline at end of file diff --git a/src/main/java/com/cultural/heritage/controller/book/PhotoCategoryController.java b/src/main/java/com/cultural/heritage/controller/book/PhotoCategoryController.java index 74f6831..f1d7236 100644 --- a/src/main/java/com/cultural/heritage/controller/book/PhotoCategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/book/PhotoCategoryController.java @@ -36,6 +36,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.ArrayList; import java.util.List; @RestController @@ -101,10 +102,10 @@ public class PhotoCategoryController { if (photoCategoryUpdateRequest == null || photoCategoryUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - // 获取原有写真类别名称 Long id = photoCategoryUpdateRequest.getId(); PhotoCategory originPhotoCategory = photoCategoryService.getById(id); + ThrowUtils.throwIf(originPhotoCategory == null, ErrorCode.OPERATION_ERROR, "写真类别不存在"); String originName = originPhotoCategory.getName(); // 获取目标写真预约名称 String targetName = photoCategoryUpdateRequest.getName(); @@ -112,8 +113,7 @@ public class PhotoCategoryController { // 更新原有写真类别下所有的写真产品类名 UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("categoryName", originName).set("categoryName", targetName); - boolean update = photoProductsService.update(updateWrapper); - ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "写真产品类名更新失败"); + photoProductsService.update(updateWrapper); // 更新写真类别名称 PhotoCategory photoCategory = new PhotoCategory(); BeanUtils.copyProperties(photoCategoryUpdateRequest, photoCategory); @@ -141,16 +141,23 @@ public class PhotoCategoryController { } Long id = commonRequest.getId(); PhotoCategory photoCategory = photoCategoryService.getById(id); + ThrowUtils.throwIf(photoCategory == null, ErrorCode.OPERATION_ERROR, "写真类别不存在"); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("categoryName", photoCategory.getName()); List photoProductsList = photoProductsService.list(queryWrapper); - // 获取预约日期列表 - List bookingDateList = commonService.getItemsByIds(photoProductsList, bookingDateService, PhotoProducts::getId); - // 获取预约时间列表 - List bookingTimes = commonService.getItemsByIds(bookingDateList, bookingTimeService, BookingDate::getId); + // 获取预约日期表 + List photoProductIds = photoProductsList.stream().map(PhotoProducts::getId).toList(); + QueryWrapper bookingDateQueryWrapper = new QueryWrapper<>(); + bookingDateQueryWrapper.in("photoProductId", photoProductIds); + List bookingDateList = bookingDateService.list(bookingDateQueryWrapper); + // 获取预约时间表 + List bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); + QueryWrapper bookingTimeQueryWrapper = new QueryWrapper<>(); + bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds); + List bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper); // 批量删除预约时间 - boolean removeTime = bookingTimeService.removeBatchByIds(bookingTimes); + boolean removeTime = bookingTimeService.removeBatchByIds(bookingTimeList); ThrowUtils.throwIf(!removeTime, ErrorCode.OPERATION_ERROR, "预约时间删除失败"); // 批量删除预约日期 @@ -203,9 +210,11 @@ public class PhotoCategoryController { List typeNameList = photoProductsList.stream().map(PhotoProducts::getCategoryName).toList(); QueryWrapper categoryWrapper = new QueryWrapper<>(); - categoryWrapper.in("name", typeNameList); - List categoryList = photoCategoryService.list(categoryWrapper); - + List categoryList = new ArrayList<>(); + if (!typeNameList.isEmpty()) { + categoryWrapper.in("name", typeNameList); + categoryList = photoCategoryService.list(categoryWrapper); + } List photoCategoryVOS = categoryList.stream().map(photoCategory -> { PhotoCategoryVO photoCategoryVO = new PhotoCategoryVO(); BeanUtils.copyProperties(photoCategory, photoCategoryVO); diff --git a/src/main/java/com/cultural/heritage/controller/book/PhotoProductsController.java b/src/main/java/com/cultural/heritage/controller/book/PhotoProductsController.java index a177fbc..b92ea89 100644 --- a/src/main/java/com/cultural/heritage/controller/book/PhotoProductsController.java +++ b/src/main/java/com/cultural/heritage/controller/book/PhotoProductsController.java @@ -246,22 +246,26 @@ public class PhotoProductsController { // 获取写真产品列表 List records = page.getRecords(); // 获取预约日期表 - List bookingDateList = commonService.getItemsByIds(records, bookingDateService, PhotoProducts::getId); + List photoProductIds = records.stream().map(PhotoProducts::getId).toList(); + QueryWrapper bookingDateQueryWrapper = new QueryWrapper<>(); + bookingDateQueryWrapper.in("photoProductId", photoProductIds); + List bookingDateList = bookingDateService.list(bookingDateQueryWrapper); // 获取预约时间表 - List bookingTimeList = commonService.getItemsByIds(bookingDateList, bookingTimeService, BookingDate::getId); - // 封装成BookingTimeVO列表 + List bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); + QueryWrapper bookingTimeQueryWrapper = new QueryWrapper<>(); + bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds); + List bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper); + // 封装成BookingTimeVO列表, 并将预约时间从小到大排序 List bookingTimeVOList = bookingTimeList.stream().map(bookingTime -> { BookingTimeVO bookingTimeVO = new BookingTimeVO(); BeanUtils.copyProperties(bookingTime, bookingTimeVO); return bookingTimeVO; - }).toList(); - - // 将预约时间从小到大排序 - bookingTimeVOList.sort((b1, b2) -> { + }).sorted((b1, b2) -> { DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm"); DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm"); return time1.compareTo(time2); // 按照时间升序排序 - }); + }).toList(); + // 封装成BookingDateVO列表 List bookingDateVOS = new ArrayList<>(); @@ -320,6 +324,8 @@ public class PhotoProductsController { } Long id = commonRequest.getId(); PhotoProducts photoProducts = photoProductsService.getById(id); + System.out.println("\n\n\n\n\n"); + System.out.println(photoProducts); ThrowUtils.throwIf(photoProducts == null || photoProducts.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "该商品已被删除或者已下架"); // 获取预约日期表 @@ -327,20 +333,20 @@ public class PhotoProductsController { queryWrapper.eq("photoProductId", id); List bookingDateList = bookingDateService.list(queryWrapper); // 获取预约时间表 - List bookingTimeList = commonService.getItemsByIds(bookingDateList, bookingTimeService, BookingDate::getId); - // 封装成BookingTimeVO列表 + List bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); + QueryWrapper bookingTimeQueryWrapper = new QueryWrapper<>(); + bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds); + List bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper); + // 封装成BookingTimeVO列表, 并将预约时间从小到大排序 List bookingTimeVOList = bookingTimeList.stream().map(bookingTime -> { BookingTimeVO bookingTimeVO = new BookingTimeVO(); BeanUtils.copyProperties(bookingTime, bookingTimeVO); return bookingTimeVO; - }).toList(); - - // 将预约时间从小到大排序 - bookingTimeVOList.sort((b1, b2) -> { + }).sorted((b1, b2) -> { DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm"); DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm"); return time1.compareTo(time2); // 按照时间升序排序 - }); + }).toList(); // 封装成BookingDateVO列表 List bookingDateVOS = new ArrayList<>(); @@ -389,7 +395,7 @@ public class PhotoProductsController { // 封装成BookingPhotoProductsVO列表 PhotoProductsVO photoProductsVO = new PhotoProductsVO(); - BeanUtils.copyProperties(photoProductsVO, photoProductsVO); + BeanUtils.copyProperties(photoProducts, photoProductsVO); photoProductsVO.setBookingDateVOList(bookingDateVOList); return ResultUtils.success(photoProductsVO); @@ -414,6 +420,7 @@ public class PhotoProductsController { // 获取当前服务类商品的上(下)架状态 Long id = commonRequest.getId(); PhotoProducts photoProducts = photoProductsService.getById(id); + ThrowUtils.throwIf(photoProducts == null, ErrorCode.OPERATION_ERROR, "写真产品不存在"); Integer status = photoProducts.getIsShelves() == 0 ? 1 : 0; UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id", id); diff --git a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java index f1d1a9e..3a2c62f 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -167,6 +167,7 @@ public class CategoryController { // 获取原有的类别名称 Long id = categoryUpdateRequest.getId(); Category originCategory = categoryService.getById(id); + ThrowUtils.throwIf(originCategory == null, ErrorCode.OPERATION_ERROR, "类别不存在"); String originTypeName = originCategory.getTypeName(); // 获取目标的类别名称 String targetTypeName = categoryUpdateRequest.getTypeName(); diff --git a/src/main/java/com/cultural/heritage/handler/StringArrayTypeHandler.java b/src/main/java/com/cultural/heritage/handler/StringArrayTypeHandler.java new file mode 100644 index 0000000..afe09e6 --- /dev/null +++ b/src/main/java/com/cultural/heritage/handler/StringArrayTypeHandler.java @@ -0,0 +1,34 @@ +package com.cultural.heritage.handler; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class StringArrayTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException { + ps.setString(i, String.join(",", parameter)); // 将字符串数组拼接为一个逗号分隔的字符串 + } + + @Override + public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException { + String result = rs.getString(columnName); + return result != null ? result.split(",") : null; // 反向解析为字符串数组 + } + + @Override + public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + String result = rs.getString(columnIndex); + return result != null ? result.split(",") : null; // 反向解析为字符串数组 + } + + @Override + public String[] getNullableResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException { + String result = cs.getString(columnIndex); + return result != null ? result.split(",") : null; // 反向解析为字符串数组 + } +} diff --git a/src/main/java/com/cultural/heritage/model/entity/PhotoProducts.java b/src/main/java/com/cultural/heritage/model/entity/PhotoProducts.java index 01a8d22..2c1c5fe 100644 --- a/src/main/java/com/cultural/heritage/model/entity/PhotoProducts.java +++ b/src/main/java/com/cultural/heritage/model/entity/PhotoProducts.java @@ -1,8 +1,10 @@ package com.cultural.heritage.model.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.cultural.heritage.handler.StringArrayTypeHandler; import lombok.Data; import java.io.Serial; @@ -17,7 +19,7 @@ import java.util.Date; @Data @TableName("photo_products") -public class PhotoProducts extends Good implements Serializable { +public class PhotoProducts implements Serializable { /** @@ -42,6 +44,7 @@ public class PhotoProducts extends Good implements Serializable { /** * 效果图 */ + @TableField(typeHandler = StringArrayTypeHandler.class) private String [] effectImg; diff --git a/src/main/java/com/cultural/heritage/service/common/CommonService.java b/src/main/java/com/cultural/heritage/service/common/CommonService.java index 3ddaf26..4b6a860 100644 --- a/src/main/java/com/cultural/heritage/service/common/CommonService.java +++ b/src/main/java/com/cultural/heritage/service/common/CommonService.java @@ -19,4 +19,17 @@ public interface CommonService { * 根据 ID 列表获取对应的实体列表 */ List getListByIds(List ids, IService genericService); + + + + /** + * 从第一个集合中提取某个属性值,并用这些值作为查询条件去查询第二个集合的数据。 + * @param sourceList 原始集合(源数据) + * @param service 要执行查询的 Service + * @param sourceField 在原始集合中提取的字段名 + * @param targetField 在目标集合中的查询字段名 + * @param 目标查询实体类型 + * @return 查询结果集合 + */ + List findListByField(List sourceList, IService service, String sourceField, String targetField); } diff --git a/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java b/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java index 4eaef1e..713ac46 100644 --- a/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.cultural.heritage.service.common.CommonService; import org.springframework.stereotype.Service; +import java.lang.reflect.Field; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; @@ -44,4 +45,52 @@ public class CommonServiceImpl implements CommonService { // 调用具体的服务执行查询 return genericService.list(queryWrapper); } + + + + + /** + * 从第一个集合中提取某个属性值,并用这些值作为查询条件去查询第二个集合的数据。 + * @param sourceList 原始集合(源数据) + * @param service 要执行查询的 Service + * @param sourceField 在原始集合中提取的字段名 + * @param targetField 在目标集合中的查询字段名 + * @param 目标查询实体类型 + * @return 查询结果集合 + */ + @Override + public List findListByField(List sourceList, IService service, String sourceField, String targetField) { + // 使用反射获取源集合中对应字段的值 + List fieldValues = sourceList.stream() + .map(item -> getFieldValue(item, sourceField)) // 获取字段值 + .collect(Collectors.toList()); + + // 如果 fieldValues 为空,直接返回空集合 + if (fieldValues.isEmpty()) { + return List.of(); // 返回空集合 + } + // 创建查询条件 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in(targetField, fieldValues); // 根据字段值进行查询 + + return service.list(queryWrapper); // 执行查询并返回结果 + } + + /** + * 使用反射获取对象的字段值 + * @param object 对象 + * @param fieldName 字段名 + * @return 字段的值 + */ + private Object getFieldValue(Object object, String fieldName) { + try { + Field field = object.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(object); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException("字段获取失败", e); + } + } + + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2d55b57..ef78540 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -21,8 +21,6 @@ spring: - - # 测试环境 # driver-class-name: com.mysql.cj.jdbc.Driver # url: jdbc:mysql://123.249.108.160:3306/feiyi?serverTimezone=Asia/Shanghai @@ -54,7 +52,6 @@ server: cookie: max-age: 2592000 - mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: @@ -65,6 +62,7 @@ mybatis-plus: logic-delete-field: isDelete #全局逻辑删除的实体字段名 logic-delete-value: 1 #逻辑已删除值(默认为1) logic-not-delete-value: 0 #逻辑未删除值(默认为0) + type-handlers-package: com.cultural.heritage.handler hwyun: