| | |
| | | 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.FilmLocationVO; |
| | | import com.mzl.flower.entity.film.FilmLocation; |
| | | import com.mzl.flower.entity.film.FilmLocationWork; |
| | | import com.mzl.flower.entity.film.FilmWorks; |
| | | 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.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.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.function.Function; |
| | | 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 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(); |
| | |
| | | |
| | | @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); |
| | | 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); |
| | | |
| | | } |
| | | |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | } |