|
|
@@ -26,7 +26,6 @@ import shop.alien.mapper.StoreImgMapper;
|
|
|
import shop.alien.store.service.FitnessEquipmentInfoService;
|
|
|
import shop.alien.store.service.SportsEquipmentFacilityService;
|
|
|
import shop.alien.store.service.SportsFacilityAreaService;
|
|
|
-import shop.alien.entity.store.SportsFacilityArea;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
@@ -137,7 +136,7 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<SportsEquipmentFacilityVo> getListByAreaId(Integer storeId, Integer areaId) {
|
|
|
+ public List<FitnessEquipmentInfo> getListByAreaId(Integer storeId, Integer areaId) {
|
|
|
log.info("根据区域ID查询设备列表,storeId={},areaId={}", storeId, areaId);
|
|
|
|
|
|
// 参数验证
|
|
|
@@ -152,20 +151,64 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
+ // 1. 根据区域ID查询该区域下的所有设施记录
|
|
|
LambdaQueryWrapper<SportsEquipmentFacility> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
queryWrapper.eq(SportsEquipmentFacility::getStoreId, storeId)
|
|
|
.eq(SportsEquipmentFacility::getAreaId, areaId)
|
|
|
- .eq(SportsEquipmentFacility::getDeleteFlag, DELETE_FLAG_NOT_DELETED)
|
|
|
- .orderByDesc(SportsEquipmentFacility::getCreatedTime);
|
|
|
+ .eq(SportsEquipmentFacility::getDeleteFlag, DELETE_FLAG_NOT_DELETED);
|
|
|
|
|
|
List<SportsEquipmentFacility> facilityList = facilityMapper.selectList(queryWrapper);
|
|
|
- List<SportsEquipmentFacilityVo> voList = facilityList.stream()
|
|
|
- .map(this::convertToVo)
|
|
|
+
|
|
|
+ // 2. 收集所有设备ID(从fitnessEquipmentIds字段中提取)
|
|
|
+ List<Integer> equipmentIdList = new ArrayList<>();
|
|
|
+ for (SportsEquipmentFacility facility : facilityList) {
|
|
|
+ String fitnessEquipmentIds = facility.getFitnessEquipmentIds();
|
|
|
+ if (StringUtils.isNotBlank(fitnessEquipmentIds)) {
|
|
|
+ String[] idArray = fitnessEquipmentIds.split(ID_SEPARATOR);
|
|
|
+ for (String idStr : idArray) {
|
|
|
+ String trimmedId = idStr.trim();
|
|
|
+ if (StringUtils.isNotBlank(trimmedId)) {
|
|
|
+ try {
|
|
|
+ Integer equipmentId = Integer.parseInt(trimmedId);
|
|
|
+ if (equipmentId > 0 && !equipmentIdList.contains(equipmentId)) {
|
|
|
+ equipmentIdList.add(equipmentId);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析设备ID失败,跳过无效ID:{},storeId={},areaId={}",
|
|
|
+ trimmedId, storeId, areaId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 如果没有设备ID,返回空列表
|
|
|
+ if (CollectionUtils.isEmpty(equipmentIdList)) {
|
|
|
+ log.info("根据区域ID查询设备列表,未找到设备ID,storeId={},areaId={}", storeId, areaId);
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 根据设备ID列表查询设备信息
|
|
|
+ List<FitnessEquipmentInfo> equipmentList = new ArrayList<>(
|
|
|
+ fitnessEquipmentInfoService.listByIds(equipmentIdList));
|
|
|
+
|
|
|
+ // 5. 过滤掉已删除和禁用的设备,并按sortOrder排序
|
|
|
+ List<FitnessEquipmentInfo> resultList = equipmentList.stream()
|
|
|
+ .filter(equipment -> equipment != null
|
|
|
+ && equipment.getDeleteFlag() != null
|
|
|
+ && equipment.getDeleteFlag() == DELETE_FLAG_NOT_DELETED
|
|
|
+ && equipment.getStatus() != null
|
|
|
+ && equipment.getStatus() == STATUS_ENABLED)
|
|
|
+ .sorted((e1, e2) -> {
|
|
|
+ Integer sort1 = e1.getSortOrder() != null ? e1.getSortOrder() : 0;
|
|
|
+ Integer sort2 = e2.getSortOrder() != null ? e2.getSortOrder() : 0;
|
|
|
+ return sort1.compareTo(sort2);
|
|
|
+ })
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
log.info("根据区域ID查询设备列表成功,storeId={},areaId={},设备数量:{}",
|
|
|
- storeId, areaId, voList.size());
|
|
|
- return voList;
|
|
|
+ storeId, areaId, resultList.size());
|
|
|
+ return resultList;
|
|
|
} catch (Exception e) {
|
|
|
log.error("根据区域ID查询设备列表异常,storeId={},areaId={},异常信息:{}",
|
|
|
storeId, areaId, e.getMessage(), e);
|
|
|
@@ -175,10 +218,54 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
|
|
|
@Override
|
|
|
public SportsEquipmentFacilityVo getDetail(Integer id) {
|
|
|
+ log.info("查询设备设施详情,id={}", id);
|
|
|
+
|
|
|
+ if (id == null || id <= 0) {
|
|
|
+ log.warn("查询设备设施详情失败,ID无效:{}", id);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 先尝试根据设施ID查询(sports_equipment_facility表的主键ID)
|
|
|
SportsEquipmentFacility facility = facilityMapper.selectById(id);
|
|
|
+
|
|
|
+ // 2. 如果查询不到,尝试根据设备ID查询(fitness_equipment_info表的主键ID)
|
|
|
if (facility == null) {
|
|
|
- return null;
|
|
|
+ log.info("根据设施ID查询不到记录,尝试根据设备ID查询,equipmentId={}", id);
|
|
|
+ // 根据设备ID查询包含该设备的设施记录
|
|
|
+ LambdaQueryWrapper<SportsEquipmentFacility> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.like(SportsEquipmentFacility::getFitnessEquipmentIds, id)
|
|
|
+ .orderByDesc(SportsEquipmentFacility::getCreatedTime); // 按创建时间降序,取最新的
|
|
|
+ List<SportsEquipmentFacility> facilityList = facilityMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(facilityList)) {
|
|
|
+ // 优先取未删除的记录
|
|
|
+ for (SportsEquipmentFacility fac : facilityList) {
|
|
|
+ if (fac.getDeleteFlag() == null || fac.getDeleteFlag() == DELETE_FLAG_NOT_DELETED) {
|
|
|
+ facility = fac;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果没有未删除的记录,取第一个(可能是已删除的)
|
|
|
+ if (facility == null) {
|
|
|
+ facility = facilityList.get(0);
|
|
|
+ }
|
|
|
+ log.info("根据设备ID查询到设施记录,equipmentId={},facilityId={},deleteFlag={}",
|
|
|
+ id, facility.getId(), facility.getDeleteFlag());
|
|
|
+ } else {
|
|
|
+ // 如果仍然查询不到,尝试查询已删除的记录(绕过逻辑删除)
|
|
|
+ facility = facilityMapper.selectByEquipmentIdIncludeDeleted(id);
|
|
|
+ if (facility != null) {
|
|
|
+ log.info("根据设备ID查询到已删除的设施记录,equipmentId={},facilityId={},deleteFlag={}",
|
|
|
+ id, facility.getId(), facility.getDeleteFlag());
|
|
|
+ } else {
|
|
|
+ log.warn("根据设施ID和设备ID都查询不到记录,id={}", id);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("根据设施ID查询到记录,facilityId={},deleteFlag={}", id, facility.getDeleteFlag());
|
|
|
}
|
|
|
+
|
|
|
return convertToVo(facility);
|
|
|
}
|
|
|
|
|
|
@@ -192,10 +279,25 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean saveFacility(SportsEquipmentFacility facility, List<String> imageList) {
|
|
|
- // 校验图片数量(最多20张)
|
|
|
- if (!CollectionUtils.isEmpty(imageList) && imageList.size() > 20) {
|
|
|
- throw new RuntimeException("实景图片最多上传20张");
|
|
|
+ public boolean saveFacility(SportsEquipmentFacility facility, List<String> imageList, SportsEquipmentFacilityVo vo) {
|
|
|
+ log.info("保存设备设施,facility={},imageList大小={},vo={}", facility,
|
|
|
+ imageList != null ? imageList.size() : 0, vo);
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (facility == null) {
|
|
|
+ log.warn("保存设备设施失败,设备信息为空");
|
|
|
+ throw new IllegalArgumentException("设备信息不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (facility.getStoreId() == null || facility.getStoreId() <= 0) {
|
|
|
+ log.warn("保存设备设施失败,门店ID无效:{}", facility.getStoreId());
|
|
|
+ throw new IllegalArgumentException("门店ID不能为空且必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验图片数量(最多9张,根据UI需求)
|
|
|
+ if (!CollectionUtils.isEmpty(imageList) && imageList.size() > 9) {
|
|
|
+ log.warn("保存设备设施失败,图片数量超过限制:{}", imageList.size());
|
|
|
+ throw new RuntimeException("图片最多上传9张");
|
|
|
}
|
|
|
|
|
|
// 如果提供了areaId,验证区域是否存在且未删除
|
|
|
@@ -207,26 +309,103 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 保存设施信息
|
|
|
+ // 如果facilityCategory为null,设置默认值0(数据库要求不能为空)
|
|
|
+ if (facility.getFacilityCategory() == null) {
|
|
|
+ facility.setFacilityCategory(0);
|
|
|
+ log.info("新增设备时facilityCategory为null,已设置默认值0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果facilityName为null,设置默认值为空字符串(数据库要求不能为空)
|
|
|
+ if (facility.getFacilityName() == null) {
|
|
|
+ facility.setFacilityName("");
|
|
|
+ log.info("新增设备时facilityName为null,已设置默认值为空字符串");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 如果提供了设备信息(equipmentName),先创建FitnessEquipmentInfo记录
|
|
|
+ Integer equipmentId = null;
|
|
|
+ if (vo != null && StringUtils.isNotBlank(vo.getEquipmentName())) {
|
|
|
+ FitnessEquipmentInfo equipmentInfo = new FitnessEquipmentInfo();
|
|
|
+ equipmentInfo.setEquipmentName(vo.getEquipmentName().trim());
|
|
|
+ // 设备类型默认为6(其他),因为UI中没有选择设备类型
|
|
|
+ equipmentInfo.setEquipmentType(6); // 6:其他
|
|
|
+ if (vo.getEquipmentNums() != null && vo.getEquipmentNums() > 0) {
|
|
|
+ equipmentInfo.setEquipmentNums(vo.getEquipmentNums());
|
|
|
+ } else {
|
|
|
+ equipmentInfo.setEquipmentNums(1); // 默认数量为1
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(vo.getEquipmentImage())) {
|
|
|
+ equipmentInfo.setEquipmentImage(vo.getEquipmentImage());
|
|
|
+ }
|
|
|
+ // 设置默认值
|
|
|
+ equipmentInfo.setSortOrder(0);
|
|
|
+ equipmentInfo.setStatus(STATUS_ENABLED);
|
|
|
+ equipmentInfo.setDeleteFlag(DELETE_FLAG_NOT_DELETED);
|
|
|
+
|
|
|
+ boolean saveEquipmentResult = fitnessEquipmentInfoService.saveEquipment(equipmentInfo);
|
|
|
+ if (saveEquipmentResult && equipmentInfo.getId() != null) {
|
|
|
+ equipmentId = equipmentInfo.getId();
|
|
|
+ log.info("创建设备信息成功,equipmentId={},equipmentName={}", equipmentId, vo.getEquipmentName());
|
|
|
+ } else {
|
|
|
+ log.warn("创建设备信息失败,equipmentName={}", vo.getEquipmentName());
|
|
|
+ throw new RuntimeException("创建设备信息失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 保存设施信息
|
|
|
+ // 如果创建了设备信息,将设备ID保存到fitnessEquipmentIds字段
|
|
|
+ if (equipmentId != null) {
|
|
|
+ facility.setFitnessEquipmentIds(String.valueOf(equipmentId));
|
|
|
+ }
|
|
|
+
|
|
|
boolean result = this.save(facility);
|
|
|
- if (result && !CollectionUtils.isEmpty(imageList) && facility.getFacilityCategory() != null) {
|
|
|
- // 删除该分类下的旧图片(如果存在)
|
|
|
+ if (!result) {
|
|
|
+ log.error("保存设备设施失败,保存数据库失败:facility={}", facility);
|
|
|
+ throw new RuntimeException("保存设备设施失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 保存图片,使用facility.id作为business_id
|
|
|
+ if (!CollectionUtils.isEmpty(imageList) && facility.getId() != null) {
|
|
|
+ // 删除该设备下的旧图片(如果存在)
|
|
|
LambdaQueryWrapper<StoreImg> deleteWrapper = new LambdaQueryWrapper<>();
|
|
|
deleteWrapper.eq(StoreImg::getStoreId, facility.getStoreId())
|
|
|
- .eq(StoreImg::getBusinessId, facility.getFacilityCategory())
|
|
|
+ .eq(StoreImg::getBusinessId, facility.getId())
|
|
|
.eq(StoreImg::getImgType, IMG_TYPE_SPORTS_EQUIPMENT);
|
|
|
storeImgMapper.delete(deleteWrapper);
|
|
|
- // 保存新图片,business_id存储facility_category
|
|
|
- saveImages(facility.getFacilityCategory(), facility.getStoreId(), imageList);
|
|
|
+ // 保存新图片,business_id存储facility.id
|
|
|
+ saveImagesByFacilityId(facility.getId(), facility.getStoreId(), imageList);
|
|
|
}
|
|
|
+
|
|
|
+ log.info("保存设备设施成功,facilityId={},storeId={},areaId={},equipmentId={}",
|
|
|
+ facility.getId(), facility.getStoreId(), facility.getAreaId(), equipmentId);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean updateFacility(SportsEquipmentFacility facility, List<String> imageList) {
|
|
|
- // 校验图片数量(最多20张)
|
|
|
- if (!CollectionUtils.isEmpty(imageList) && imageList.size() > 20) {
|
|
|
- throw new RuntimeException("实景图片最多上传20张");
|
|
|
+ public boolean updateFacility(SportsEquipmentFacility facility, List<String> imageList, SportsEquipmentFacilityVo vo) {
|
|
|
+ log.info("修改设备设施,facility={},imageList大小={},vo={}", facility,
|
|
|
+ imageList != null ? imageList.size() : 0, vo);
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (facility == null) {
|
|
|
+ log.warn("修改设备设施失败,设备信息为空");
|
|
|
+ throw new IllegalArgumentException("设备信息不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证:必须提供设施ID或设备ID之一
|
|
|
+ if (facility.getId() == null && (vo == null || vo.getEquipmentId() == null || vo.getEquipmentId() <= 0)) {
|
|
|
+ log.warn("修改设备设施失败,设施ID和设备ID都为空");
|
|
|
+ throw new IllegalArgumentException("设施ID或设备ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (facility.getStoreId() == null || facility.getStoreId() <= 0) {
|
|
|
+ log.warn("修改设备设施失败,门店ID无效:{}", facility.getStoreId());
|
|
|
+ throw new IllegalArgumentException("门店ID不能为空且必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验图片数量(最多9张,根据UI需求)
|
|
|
+ if (!CollectionUtils.isEmpty(imageList) && imageList.size() > 9) {
|
|
|
+ log.warn("修改设备设施失败,图片数量超过限制:{}", imageList.size());
|
|
|
+ throw new RuntimeException("图片最多上传9张");
|
|
|
}
|
|
|
|
|
|
// 如果提供了areaId,验证区域是否存在且未删除
|
|
|
@@ -238,26 +417,256 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 更新设施信息
|
|
|
+ // 查询现有设施记录
|
|
|
+ // 如果vo中提供了equipmentId(fitness_equipment_info的ID),则根据设备ID查询设施记录
|
|
|
+ SportsEquipmentFacility existingFacility = null;
|
|
|
+ if (vo != null && vo.getEquipmentId() != null && vo.getEquipmentId() > 0) {
|
|
|
+ // 根据设备ID查询包含该设备的设施记录(包括已删除的记录,因为可能需要恢复)
|
|
|
+ // 由于@TableLogic的存在,先查询未删除的记录
|
|
|
+ LambdaQueryWrapper<SportsEquipmentFacility> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.like(SportsEquipmentFacility::getFitnessEquipmentIds, vo.getEquipmentId())
|
|
|
+ .orderByDesc(SportsEquipmentFacility::getCreatedTime); // 按创建时间降序,取最新的
|
|
|
+ List<SportsEquipmentFacility> facilityList = facilityMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(facilityList)) {
|
|
|
+ // 找到未删除的记录,取第一个
|
|
|
+ existingFacility = facilityList.get(0);
|
|
|
+ } else {
|
|
|
+ // 如果没有找到未删除的记录,查询已删除的记录(需要绕过@TableLogic)
|
|
|
+ // 使用自定义SQL查询包括已删除的记录
|
|
|
+ existingFacility = facilityMapper.selectByEquipmentIdIncludeDeleted(vo.getEquipmentId());
|
|
|
+ if (existingFacility != null) {
|
|
|
+ log.info("根据设备ID查询到已删除的设施记录,equipmentId={},facilityId={},deleteFlag={}",
|
|
|
+ vo.getEquipmentId(), existingFacility.getId(), existingFacility.getDeleteFlag());
|
|
|
+ } else {
|
|
|
+ log.warn("根据设备ID查询设施记录失败(包括已删除的记录),equipmentId={}", vo.getEquipmentId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (existingFacility != null) {
|
|
|
+ // 更新facility的ID为查询到的设施ID
|
|
|
+ facility.setId(existingFacility.getId());
|
|
|
+ log.info("根据设备ID查询到设施记录,equipmentId={},facilityId={},deleteFlag={}",
|
|
|
+ vo.getEquipmentId(), existingFacility.getId(), existingFacility.getDeleteFlag());
|
|
|
+ }
|
|
|
+ } else if (facility.getId() != null) {
|
|
|
+ // 如果没有提供equipmentId,则根据设施ID查询
|
|
|
+ existingFacility = this.getById(facility.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (existingFacility == null) {
|
|
|
+ log.warn("修改设备设施失败,设施不存在:facilityId={},equipmentId={}",
|
|
|
+ facility.getId(), vo != null ? vo.getEquipmentId() : null);
|
|
|
+ throw new IllegalArgumentException("设施不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果设施已被删除,恢复它(设置deleteFlag为0)
|
|
|
+ if (existingFacility.getDeleteFlag() != null && existingFacility.getDeleteFlag() != DELETE_FLAG_NOT_DELETED) {
|
|
|
+ log.info("设施已被删除,恢复设施记录:facilityId={}", existingFacility.getId());
|
|
|
+ existingFacility.setDeleteFlag(DELETE_FLAG_NOT_DELETED);
|
|
|
+ facility.setDeleteFlag(DELETE_FLAG_NOT_DELETED);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果facilityCategory为null,设置默认值0(数据库要求不能为空)
|
|
|
+ if (facility.getFacilityCategory() == null) {
|
|
|
+ facility.setFacilityCategory(0);
|
|
|
+ log.info("修改设备时facilityCategory为null,已设置默认值0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果facilityName为null,设置默认值为空字符串(数据库要求不能为空)
|
|
|
+ if (facility.getFacilityName() == null) {
|
|
|
+ facility.setFacilityName("");
|
|
|
+ log.info("修改设备时facilityName为null,已设置默认值为空字符串");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 如果提供了设备信息(equipmentName),更新或创建FitnessEquipmentInfo记录
|
|
|
+ Integer equipmentId = null;
|
|
|
+ if (vo != null && StringUtils.isNotBlank(vo.getEquipmentName())) {
|
|
|
+ // 如果fitnessEquipmentIds不为空,说明已有设备,更新现有设备
|
|
|
+ String existingEquipmentIds = existingFacility.getFitnessEquipmentIds();
|
|
|
+ if (StringUtils.isNotBlank(existingEquipmentIds)) {
|
|
|
+ // 解析现有设备ID列表,取第一个设备ID进行更新
|
|
|
+ String[] equipmentIdArray = existingEquipmentIds.split(ID_SEPARATOR);
|
|
|
+ if (equipmentIdArray.length > 0) {
|
|
|
+ try {
|
|
|
+ equipmentId = Integer.parseInt(equipmentIdArray[0].trim());
|
|
|
+ // 更新现有设备信息
|
|
|
+ FitnessEquipmentInfo equipmentInfo = fitnessEquipmentInfoService.getById(equipmentId);
|
|
|
+ if (equipmentInfo != null && equipmentInfo.getDeleteFlag() != null
|
|
|
+ && equipmentInfo.getDeleteFlag() == DELETE_FLAG_NOT_DELETED) {
|
|
|
+ equipmentInfo.setEquipmentName(vo.getEquipmentName().trim());
|
|
|
+ if (vo.getEquipmentNums() != null && vo.getEquipmentNums() > 0) {
|
|
|
+ equipmentInfo.setEquipmentNums(vo.getEquipmentNums());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(vo.getEquipmentImage())) {
|
|
|
+ equipmentInfo.setEquipmentImage(vo.getEquipmentImage());
|
|
|
+ }
|
|
|
+ boolean updateEquipmentResult = fitnessEquipmentInfoService.updateEquipment(equipmentInfo);
|
|
|
+ if (updateEquipmentResult) {
|
|
|
+ log.info("更新设备信息成功,equipmentId={},equipmentName={}", equipmentId, vo.getEquipmentName());
|
|
|
+ } else {
|
|
|
+ log.warn("更新设备信息失败,equipmentId={}", equipmentId);
|
|
|
+ throw new RuntimeException("更新设备信息失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 设备不存在或已删除,创建新设备
|
|
|
+ equipmentId = null;
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析设备ID失败,existingEquipmentIds={},异常信息:{}", existingEquipmentIds, e.getMessage());
|
|
|
+ equipmentId = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有现有设备或设备已删除,创建新设备
|
|
|
+ if (equipmentId == null) {
|
|
|
+ FitnessEquipmentInfo equipmentInfo = new FitnessEquipmentInfo();
|
|
|
+ equipmentInfo.setEquipmentName(vo.getEquipmentName().trim());
|
|
|
+ // 设备类型默认为6(其他),因为UI中没有选择设备类型
|
|
|
+ equipmentInfo.setEquipmentType(6); // 6:其他
|
|
|
+ if (vo.getEquipmentNums() != null && vo.getEquipmentNums() > 0) {
|
|
|
+ equipmentInfo.setEquipmentNums(vo.getEquipmentNums());
|
|
|
+ } else {
|
|
|
+ equipmentInfo.setEquipmentNums(1); // 默认数量为1
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(vo.getEquipmentImage())) {
|
|
|
+ equipmentInfo.setEquipmentImage(vo.getEquipmentImage());
|
|
|
+ }
|
|
|
+ // 设置默认值
|
|
|
+ equipmentInfo.setSortOrder(0);
|
|
|
+ equipmentInfo.setStatus(STATUS_ENABLED);
|
|
|
+ equipmentInfo.setDeleteFlag(DELETE_FLAG_NOT_DELETED);
|
|
|
+
|
|
|
+ boolean saveEquipmentResult = fitnessEquipmentInfoService.saveEquipment(equipmentInfo);
|
|
|
+ if (saveEquipmentResult && equipmentInfo.getId() != null) {
|
|
|
+ equipmentId = equipmentInfo.getId();
|
|
|
+ log.info("创建设备信息成功,equipmentId={},equipmentName={}", equipmentId, vo.getEquipmentName());
|
|
|
+ } else {
|
|
|
+ log.warn("创建设备信息失败,equipmentName={}", vo.getEquipmentName());
|
|
|
+ throw new RuntimeException("创建设备信息失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 更新设施信息
|
|
|
+ // 如果更新了设备信息,更新fitnessEquipmentIds字段
|
|
|
+ if (equipmentId != null) {
|
|
|
+ facility.setFitnessEquipmentIds(String.valueOf(equipmentId));
|
|
|
+ }
|
|
|
+
|
|
|
boolean result = this.updateById(facility);
|
|
|
- if (result && !CollectionUtils.isEmpty(imageList) && facility.getFacilityCategory() != null) {
|
|
|
- // 删除该分类下的旧图片
|
|
|
+ if (!result) {
|
|
|
+ log.error("修改设备设施失败,更新数据库失败:facility={}", facility);
|
|
|
+ throw new RuntimeException("修改设备设施失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 保存图片,使用facility.id作为business_id
|
|
|
+ if (!CollectionUtils.isEmpty(imageList) && facility.getId() != null) {
|
|
|
+ // 删除该设施下的旧图片(如果存在)
|
|
|
LambdaQueryWrapper<StoreImg> deleteWrapper = new LambdaQueryWrapper<>();
|
|
|
deleteWrapper.eq(StoreImg::getStoreId, facility.getStoreId())
|
|
|
- .eq(StoreImg::getBusinessId, facility.getFacilityCategory())
|
|
|
+ .eq(StoreImg::getBusinessId, facility.getId())
|
|
|
.eq(StoreImg::getImgType, IMG_TYPE_SPORTS_EQUIPMENT);
|
|
|
storeImgMapper.delete(deleteWrapper);
|
|
|
- // 保存新图片,business_id存储facility_category
|
|
|
- saveImages(facility.getFacilityCategory(), facility.getStoreId(), imageList);
|
|
|
+ // 保存新图片,business_id存储facility.id
|
|
|
+ saveImagesByFacilityId(facility.getId(), facility.getStoreId(), imageList);
|
|
|
}
|
|
|
+
|
|
|
+ log.info("修改设备设施成功,facilityId={},storeId={},areaId={},equipmentId={}",
|
|
|
+ facility.getId(), facility.getStoreId(), facility.getAreaId(), equipmentId);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean deleteFacility(Integer id) {
|
|
|
- // 注意:图片和设施没有关系,只和facility_category有关系,所以删除设施时不删除图片
|
|
|
- // 删除设施
|
|
|
- return this.removeById(id);
|
|
|
+ public boolean deleteFacility(Integer equipmentId) {
|
|
|
+ log.info("删除设备设施,equipmentId={}", equipmentId);
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (equipmentId == null || equipmentId <= 0) {
|
|
|
+ log.warn("删除设备设施失败,设备ID无效:{}", equipmentId);
|
|
|
+ throw new IllegalArgumentException("设备ID不能为空且必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证设备是否存在
|
|
|
+ FitnessEquipmentInfo equipmentInfo = fitnessEquipmentInfoService.getById(equipmentId);
|
|
|
+ if (equipmentInfo == null || equipmentInfo.getDeleteFlag() == null
|
|
|
+ || equipmentInfo.getDeleteFlag() != DELETE_FLAG_NOT_DELETED) {
|
|
|
+ log.warn("删除设备设施失败,设备不存在或已删除:equipmentId={}", equipmentId);
|
|
|
+ throw new IllegalArgumentException("设备不存在或已删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 查询所有包含该设备ID的设施记录
|
|
|
+ LambdaQueryWrapper<SportsEquipmentFacility> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(SportsEquipmentFacility::getDeleteFlag, DELETE_FLAG_NOT_DELETED)
|
|
|
+ .like(SportsEquipmentFacility::getFitnessEquipmentIds, equipmentId);
|
|
|
+ List<SportsEquipmentFacility> facilityList = this.list(queryWrapper);
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(facilityList)) {
|
|
|
+ log.warn("删除设备设施失败,未找到包含该设备ID的设施记录:equipmentId={}", equipmentId);
|
|
|
+ // 即使没有找到设施记录,也删除设备信息
|
|
|
+ boolean deleteEquipmentResult = fitnessEquipmentInfoService.deleteEquipment(equipmentId);
|
|
|
+ return deleteEquipmentResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 对每个设施记录,从fitnessEquipmentIds中移除该设备ID
|
|
|
+ for (SportsEquipmentFacility facility : facilityList) {
|
|
|
+ String fitnessEquipmentIds = facility.getFitnessEquipmentIds();
|
|
|
+ if (StringUtils.isNotBlank(fitnessEquipmentIds)) {
|
|
|
+ // 解析设备ID列表
|
|
|
+ List<String> equipmentIdList = new ArrayList<>();
|
|
|
+ String[] equipmentIdArray = fitnessEquipmentIds.split(ID_SEPARATOR);
|
|
|
+ for (String idStr : equipmentIdArray) {
|
|
|
+ String trimmedId = idStr.trim();
|
|
|
+ if (StringUtils.isNotBlank(trimmedId)) {
|
|
|
+ try {
|
|
|
+ Integer id = Integer.parseInt(trimmedId);
|
|
|
+ // 如果不是要删除的设备ID,保留
|
|
|
+ if (!id.equals(equipmentId)) {
|
|
|
+ equipmentIdList.add(trimmedId);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析设备ID失败,idStr={},异常信息:{}", idStr, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设施的fitnessEquipmentIds
|
|
|
+ if (equipmentIdList.isEmpty()) {
|
|
|
+ // 如果移除后设备列表为空,删除该设施
|
|
|
+ boolean deleteFacilityResult = this.removeById(facility.getId());
|
|
|
+ if (deleteFacilityResult) {
|
|
|
+ log.info("删除设施成功(设备列表为空),facilityId={},equipmentId={}",
|
|
|
+ facility.getId(), equipmentId);
|
|
|
+ } else {
|
|
|
+ log.warn("删除设施失败(设备列表为空),facilityId={},equipmentId={}",
|
|
|
+ facility.getId(), equipmentId);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果移除后设备列表不为空,更新设施的设备ID列表
|
|
|
+ String newFitnessEquipmentIds = String.join(ID_SEPARATOR, equipmentIdList);
|
|
|
+ facility.setFitnessEquipmentIds(newFitnessEquipmentIds);
|
|
|
+ boolean updateFacilityResult = this.updateById(facility);
|
|
|
+ if (updateFacilityResult) {
|
|
|
+ log.info("更新设施设备ID列表成功,facilityId={},equipmentId={},新设备ID列表:{}",
|
|
|
+ facility.getId(), equipmentId, newFitnessEquipmentIds);
|
|
|
+ } else {
|
|
|
+ log.warn("更新设施设备ID列表失败,facilityId={},equipmentId={}",
|
|
|
+ facility.getId(), equipmentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 逻辑删除设备信息
|
|
|
+ boolean deleteEquipmentResult = fitnessEquipmentInfoService.deleteEquipment(equipmentId);
|
|
|
+ if (deleteEquipmentResult) {
|
|
|
+ log.info("删除设备信息成功,equipmentId={}", equipmentId);
|
|
|
+ } else {
|
|
|
+ log.warn("删除设备信息失败,equipmentId={}", equipmentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return deleteEquipmentResult;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -861,7 +1270,27 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存图片
|
|
|
+ * 保存图片(使用facility.id作为business_id)
|
|
|
+ *
|
|
|
+ * @param facilityId 设施ID
|
|
|
+ * @param storeId 门店ID
|
|
|
+ * @param imageList 图片列表
|
|
|
+ */
|
|
|
+ private void saveImagesByFacilityId(Integer facilityId, Integer storeId, List<String> imageList) {
|
|
|
+ for (int i = 0; i < imageList.size(); i++) {
|
|
|
+ StoreImg storeImg = new StoreImg();
|
|
|
+ storeImg.setStoreId(storeId);
|
|
|
+ storeImg.setImgType(IMG_TYPE_SPORTS_EQUIPMENT);
|
|
|
+ storeImg.setBusinessId(facilityId); // business_id存储facility.id
|
|
|
+ storeImg.setImgUrl(imageList.get(i));
|
|
|
+ storeImg.setImgSort(i + 1);
|
|
|
+ storeImg.setImgDescription("运动器材设施实景图片");
|
|
|
+ storeImgMapper.insert(storeImg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存图片(旧方法,保留用于兼容,使用facility_category作为business_id)
|
|
|
*
|
|
|
* @param facilityCategory 设施分类
|
|
|
* @param storeId 门店ID
|
|
|
@@ -895,17 +1324,58 @@ public class SportsEquipmentFacilityServiceImpl extends ServiceImpl<SportsEquipm
|
|
|
if (facility.getDisplayInStoreDetail() != null) {
|
|
|
vo.setDisplayInStoreDetailText(facility.getDisplayInStoreDetail() == 1 ? "显示" : "隐藏");
|
|
|
}
|
|
|
- // 查询图片列表,根据store_id和facility_category查询
|
|
|
- LambdaQueryWrapper<StoreImg> imageWrapper = new LambdaQueryWrapper<>();
|
|
|
- imageWrapper.eq(StoreImg::getStoreId, facility.getStoreId())
|
|
|
- .eq(StoreImg::getBusinessId, facility.getFacilityCategory())
|
|
|
- .eq(StoreImg::getImgType, IMG_TYPE_SPORTS_EQUIPMENT);
|
|
|
- imageWrapper.orderByAsc(StoreImg::getImgSort);
|
|
|
- List<StoreImg> imageList = storeImgMapper.selectList(imageWrapper);
|
|
|
- if (!CollectionUtils.isEmpty(imageList)) {
|
|
|
- vo.setImageList(imageList.stream().map(StoreImg::getImgUrl)
|
|
|
- .collect(Collectors.toList()));
|
|
|
+
|
|
|
+ // 查询图片列表,优先使用facility.id作为business_id,兼容历史数据使用facility_category
|
|
|
+ List<String> imageList = new ArrayList<>();
|
|
|
+ if (facility.getId() != null) {
|
|
|
+ // 优先查询使用facility.id作为business_id的图片(新数据)
|
|
|
+ LambdaQueryWrapper<StoreImg> imageWrapper = new LambdaQueryWrapper<>();
|
|
|
+ imageWrapper.eq(StoreImg::getStoreId, facility.getStoreId())
|
|
|
+ .eq(StoreImg::getBusinessId, facility.getId())
|
|
|
+ .eq(StoreImg::getImgType, IMG_TYPE_SPORTS_EQUIPMENT)
|
|
|
+ .orderByAsc(StoreImg::getImgSort);
|
|
|
+ List<StoreImg> imageListByFacilityId = storeImgMapper.selectList(imageWrapper);
|
|
|
+ if (!CollectionUtils.isEmpty(imageListByFacilityId)) {
|
|
|
+ imageList = imageListByFacilityId.stream().map(StoreImg::getImgUrl)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ } else if (facility.getFacilityCategory() != null) {
|
|
|
+ // 如果没有找到,尝试使用facility_category查询(兼容历史数据)
|
|
|
+ LambdaQueryWrapper<StoreImg> imageWrapperByCategory = new LambdaQueryWrapper<>();
|
|
|
+ imageWrapperByCategory.eq(StoreImg::getStoreId, facility.getStoreId())
|
|
|
+ .eq(StoreImg::getBusinessId, facility.getFacilityCategory())
|
|
|
+ .eq(StoreImg::getImgType, IMG_TYPE_SPORTS_EQUIPMENT)
|
|
|
+ .orderByAsc(StoreImg::getImgSort);
|
|
|
+ List<StoreImg> imageListByCategory = storeImgMapper.selectList(imageWrapperByCategory);
|
|
|
+ if (!CollectionUtils.isEmpty(imageListByCategory)) {
|
|
|
+ imageList = imageListByCategory.stream().map(StoreImg::getImgUrl)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ vo.setImageList(imageList);
|
|
|
+
|
|
|
+ // 如果fitnessEquipmentIds不为空,查询关联的设备信息
|
|
|
+ if (StringUtils.isNotBlank(facility.getFitnessEquipmentIds())) {
|
|
|
+ // 解析设备ID列表
|
|
|
+ List<Integer> equipmentIdList = new ArrayList<>();
|
|
|
+ String[] equipmentIdArray = facility.getFitnessEquipmentIds().split(ID_SEPARATOR);
|
|
|
+ for (String equipmentIdStr : equipmentIdArray) {
|
|
|
+ try {
|
|
|
+ Integer equipmentId = Integer.parseInt(equipmentIdStr.trim());
|
|
|
+ equipmentIdList.add(equipmentId);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析设备ID失败,equipmentIdStr={},异常信息:{}", equipmentIdStr, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(equipmentIdList)) {
|
|
|
+ // 查询设备信息(用于后续扩展,当前vo中没有equipmentList字段)
|
|
|
+ // List<FitnessEquipmentInfo> equipmentList = new ArrayList<>(
|
|
|
+ // fitnessEquipmentInfoService.listByIds(equipmentIdList));
|
|
|
+ // 注意:如果需要返回设备信息,可以在vo中添加equipmentList字段
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
return vo;
|
|
|
}
|
|
|
|