| | |
| | | package com.mzl.flower.service.film.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.mzl.flower.config.exception.ValidationException; |
| | | import com.mzl.flower.config.security.SecurityUtils; |
| | | import com.mzl.flower.dto.BatchDTO; |
| | | import com.mzl.flower.dto.request.film.FilmLocationDTO; |
| | | import com.mzl.flower.dto.request.film.FilmLocationQueryDTO; |
| | | import com.mzl.flower.dto.response.film.FilmLocationNameDTO; |
| | | import com.mzl.flower.dto.response.film.FilmLocationVO; |
| | | import com.mzl.flower.dto.response.film.FilmWorksVO; |
| | | import com.mzl.flower.entity.film.FilmLocation; |
| | | import com.mzl.flower.entity.film.FilmLocationWork; |
| | | import com.mzl.flower.entity.system.Role; |
| | | import com.mzl.flower.mapper.film.FilmLocationMapper; |
| | | import com.mzl.flower.mapper.film.FilmLocationWorkMapper; |
| | | import com.mzl.flower.mapper.film.FilmWorksMapper; |
| | | import com.mzl.flower.service.film.FilmLocationService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mzl.flower.service.film.FilmLocationWorkService; |
| | | import com.mzl.flower.service.system.RoleService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | public class FilmLocationServiceImpl extends ServiceImpl<FilmLocationMapper, FilmLocation> implements FilmLocationService { |
| | | |
| | | private final FilmLocationMapper filmLocationMapper; |
| | | private final RoleService roleService; |
| | | private final FilmLocationWorkMapper filmLocationWorkMapper; |
| | | |
| | | private final FilmWorksMapper filmWorksMapper; |
| | | private final FilmLocationWorkService filmLocationWorkService; |
| | | |
| | | @Override |
| | | public void saveFilmLocation(FilmLocationDTO filmLocationDTO) { |
| | | // 转换 |
| | | //增加保存时判断是否有景点名称 |
| | | if (StringUtils.isEmpty(filmLocationDTO.getLocationName())) { |
| | | throw new ValidationException("景点名称不能为空"); |
| | | } |
| | | FilmLocation filmLocation1 = filmLocationMapper.selectByLocationName(filmLocationDTO.getLocationName()); |
| | | if (!ObjectUtils.isEmpty(filmLocation1)) { |
| | | throw new ValidationException("景点名称重复"); |
| | | } |
| | | FilmLocation filmLocation = new FilmLocation(); |
| | | BeanUtils.copyProperties(filmLocationDTO, filmLocation); |
| | | filmLocation.create(); |
| | | filmLocationMapper.insert(filmLocation); |
| | | //此处传入参数是否有影视作品ID,如果不等于空,则需要保存关联关系 |
| | | if (!StringUtils.isEmpty(filmLocationDTO.getFilmId())) { |
| | | FilmLocationWork filmLocationWork = new FilmLocationWork(); |
| | | filmLocationWork.setFilmId(filmLocationDTO.getFilmId()); |
| | | filmLocationWork.setLocationId(filmLocation.getId()); |
| | | filmLocationWorkMapper.insert(filmLocationWork); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateFilmLocation(FilmLocationDTO filmLocationDTO) { |
| | | if (StringUtils.isEmpty(filmLocationDTO.getLocationName())) { |
| | | throw new ValidationException("景点名称不能为空"); |
| | | } |
| | | |
| | | //增加修改时保存得景点名称是否有重复,排除当前这条 |
| | | FilmLocation filmLocation1 = filmLocationMapper.selectByLocationName(filmLocationDTO.getLocationName()); |
| | | |
| | | FilmLocation filmLocation = filmLocationMapper.selectById(filmLocationDTO.getId()); |
| | | if (!filmLocation1.getId().equals(filmLocation.getId())) { |
| | | throw new ValidationException("景点名称重复"); |
| | | } |
| | | BeanUtils.copyProperties(filmLocationDTO, filmLocation,"filmId","startDate","endDate","visitorPhotos","isEnabled","transportGuide"); |
| | | filmLocation.update(SecurityUtils.getUserId()); |
| | | filmLocationMapper.updateById(filmLocation); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteFilmLocation(String id) { |
| | | FilmLocation filmLocation = filmLocationMapper.selectById(id); |
| | | if (filmLocation == null) { |
| | | throw new ValidationException("找不到id为" + id + "的景点"); |
| | | } |
| | | //判断景点id是否有对应得影视作品,如果有则不能删除。如果没有则可以删除 |
| | | //可能需要增加判断,如果是取消生成应该也可以删除,重新生成需要重新生成新的位置信息(取消生成的话,其实也没有对应的景点信息,这点其实暂时不用考虑) |
| | | List<FilmLocationWork> filmLocationWorks = filmLocationWorkMapper.getByLocaltionId(id); |
| | | if(!CollectionUtils.isEmpty(filmLocationWorks)){ |
| | | throw new ValidationException("景点有对应得影视作品,无法删除"); |
| | | } |
| | | filmLocationMapper.deleteById(id); |
| | | } |
| | | |
| | | @Override |
| | | public Page<FilmLocationVO> queryPage(FilmLocationQueryDTO dto, Page page) { |
| | | |
| | | List<FilmLocationVO> list = filmLocationMapper.queryPage(dto, page); |
| | | page.setRecords(list); |
| | | return page; |
| | | } |
| | | |
| | | @Override |
| | | public FilmLocationVO detail(Long id) { |
| | | FilmLocationVO filmLocationVO = filmLocationMapper.selectByIdInfo(id); |
| | | if (filmLocationVO == null) { |
| | | return null; |
| | | } |
| | | return filmLocationVO; |
| | | } |
| | | |
| | | @Override |
| | | public void changeDownState(Long id) { |
| | | |
| | | //获取当前人员角色,判断是不是审核角色 |
| | | List<String> roleIds = new ArrayList<>(); |
| | | List<Role> roleList = roleService.getUserRoleList(SecurityUtils.getUserId()); |
| | | for (Role role : roleList) { |
| | | roleIds.add(role.getId()); |
| | | } |
| | | |
| | | if (!roleIds.contains("77462b362bad4c88a7a5c64cbdd25d91")) { |
| | | throw new ValidationException("非运营角色不能清除权重"); |
| | | } |
| | | |
| | | FilmLocation filmLocation = filmLocationMapper.selectById(id); |
| | | if (filmLocation == null) { |
| | | throw new ValidationException("找不到id为" + id + "的景点"); |
| | | } |
| | | |
| | | filmLocation.setLocationWeight((double) 0); |
| | | |
| | | filmLocationMapper.updateById(filmLocation); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public Page<FilmLocationVO> queryPage(FilmLocationQueryDTO filmLocationQueryDTO, Page page) { |
| | | return null; |
| | | public void isEnable(Long id) { |
| | | FilmLocation filmLocation = filmLocationMapper.selectById(id); |
| | | if (filmLocation == null) { |
| | | throw new ValidationException("找不到id为" + id + "的景点"); |
| | | } |
| | | if (filmLocation.getIsEnabled()) { |
| | | filmLocation.setIsEnabled(false); |
| | | } else { |
| | | filmLocation.setIsEnabled(true); |
| | | } |
| | | filmLocation.update(SecurityUtils.getUserId()); |
| | | filmLocationMapper.updateById(filmLocation); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void batchMerge(BatchDTO dto) { |
| | | //获取当前人员角色,判断是不是审核角色 |
| | | // List<String> roleIds = new ArrayList<>(); |
| | | // List<Role> roleList = roleService.getUserRoleList(SecurityUtils.getUserId()); |
| | | // for (Role role : roleList) { |
| | | // roleIds.add(role.getId()); |
| | | // } |
| | | // 权限校验:仅允许运营角色操作 |
| | | List<String> roleIds = roleService.getUserRoleList(SecurityUtils.getUserId()).stream().map(Role::getId).collect(Collectors.toList()); |
| | | if (!roleIds.contains("77462b362bad4c88a7a5c64cbdd25d91")) { |
| | | throw new ValidationException("非运营角色不能合并"); |
| | | } |
| | | if (dto.getIds() == null || dto.getIds().size() < 2) { |
| | | throw new ValidationException("至少选中两条数据"); |
| | | } |
| | | |
| | | List<Long> idList = dto.getIds(); |
| | | List<FilmLocation> sortedList = idList.stream().map(id -> filmLocationMapper.selectById(id)).filter(Objects::nonNull).collect(Collectors.toList()); |
| | | |
| | | // 检查有效记录数 |
| | | if (sortedList.size() < 2) { |
| | | throw new ValidationException("有效数据不足两条"); |
| | | } |
| | | // 获取目标记录(第一条)和待合并记录 |
| | | FilmLocation target = sortedList.get(0); |
| | | List<FilmLocation> toMergeList = sortedList.subList(1, sortedList.size()); |
| | | List<Long> mergeIds = toMergeList.stream().map(FilmLocation::getId).collect(Collectors.toList()); |
| | | |
| | | // 更新关联表的外键引用 |
| | | updateLocationRelations(target.getId(), mergeIds); |
| | | |
| | | // 删除待合并记录 |
| | | filmLocationMapper.deleteBatchIds(mergeIds); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void batchDelete(BatchDTO dto) { |
| | | if (CollectionUtils.isEmpty(dto.getIds())) { |
| | | throw new ValidationException("删除ID列表不能为空"); |
| | | } |
| | | |
| | | Set<Long> protectedIds = filmLocationWorkMapper.findLocationIdsWithWorks(dto.getIds()); |
| | | if (!protectedIds.isEmpty()) { |
| | | Map<Long, FilmLocationNameDTO> locationDTOs = filmLocationMapper.getLocationNamesByIds( |
| | | new ArrayList<>(protectedIds) |
| | | ); |
| | | |
| | | String errorMsg = protectedIds.stream() |
| | | .map(id -> { |
| | | FilmLocationNameDTO locationNameDTO = locationDTOs.get(id); |
| | | String name = (locationNameDTO != null && locationNameDTO.getName() != null) ? |
| | | locationNameDTO.getName() : "未知景点"; |
| | | return String.format("%s(ID:%d)", name, id); |
| | | }) |
| | | .collect(Collectors.joining("、")); |
| | | |
| | | throw new ValidationException("以下景点有对应的影视作品,无法删除: " + errorMsg); |
| | | } |
| | | |
| | | filmLocationMapper.deleteBatchIds(dto.getIds()); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public List<FilmWorksVO> related(Long locationId) { |
| | | List<Long> ids = new ArrayList<>(); |
| | | ids.add(locationId); |
| | | Set<Long> filmIds = filmLocationWorkMapper.findFilmIdsWithWorks(ids); |
| | | if (!filmIds.isEmpty()) { |
| | | List<FilmWorksVO> filmWorksVOS = filmWorksMapper.getFilmWorksByIds(new ArrayList<>(filmIds)); |
| | | return filmWorksVOS; |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<FilmLocationVO> getFilmLocationListTop3() { |
| | | return filmLocationMapper.getFilmLocationListTop3(); |
| | | } |
| | | |
| | | private void updateLocationRelations(Long targetId, List<Long> mergeIds) { |
| | | // 查询所有待更新的工作记录 |
| | | LambdaQueryWrapper<FilmLocationWork> query = new LambdaQueryWrapper<>(); |
| | | query.in(FilmLocationWork::getLocationId, mergeIds); |
| | | List<FilmLocationWork> records = filmLocationWorkService.list(query); |
| | | |
| | | // 准备更新 |
| | | List<FilmLocationWork> toUpdate = records.stream().map(work -> { |
| | | FilmLocationWork update = new FilmLocationWork(); |
| | | update.setId(work.getId()); |
| | | update.setLocationId(targetId); |
| | | return update; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | // 批量更新(MP的批量更新方法) |
| | | if (!toUpdate.isEmpty()) { |
| | | filmLocationWorkService.updateBatchById(toUpdate); |
| | | } |
| | | } |
| | | |
| | | } |