package com.tourguide.modules.operation.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.tourguide.common.api.PageResponse;
|
import com.tourguide.common.exception.BusinessException;
|
import com.tourguide.modules.operation.dto.CityResponse;
|
import com.tourguide.modules.operation.entity.OperationCity;
|
import com.tourguide.modules.operation.mapper.OperationCityMapper;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.StringUtils;
|
import java.time.LocalDate;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/** 运营城市查询、新建和状态管理服务。 */
|
@Service
|
public class OperationCityService {
|
private final OperationCityMapper cityMapper;
|
|
public OperationCityService(OperationCityMapper cityMapper) {
|
this.cityMapper = cityMapper;
|
}
|
|
public PageResponse<CityResponse> page(long page, long size, String keyword, Integer status,
|
LocalDate startDate, LocalDate endDate) {
|
LambdaQueryWrapper<OperationCity> query = new LambdaQueryWrapper<OperationCity>();
|
query.like(StringUtils.hasText(keyword), OperationCity::getCityName,
|
StringUtils.hasText(keyword) ? keyword.trim() : null)
|
.eq(status != null, OperationCity::getStatus, status)
|
.ge(startDate != null, OperationCity::getCreatedAt, startDate == null ? null : startDate.atStartOfDay())
|
.lt(endDate != null, OperationCity::getCreatedAt, endDate == null ? null : endDate.plusDays(1).atStartOfDay())
|
.orderByDesc(OperationCity::getId);
|
Page<OperationCity> result = cityMapper.selectPage(new Page<OperationCity>(page, size), query);
|
return new PageResponse<CityResponse>(
|
result.getRecords().stream().map(CityResponse::from).collect(Collectors.toList()),
|
result.getTotal(), result.getCurrent(), result.getSize());
|
}
|
|
public List<CityResponse> enabledOptions() {
|
return cityMapper.selectList(new LambdaQueryWrapper<OperationCity>()
|
.eq(OperationCity::getStatus, 1)
|
.orderByAsc(OperationCity::getCityName))
|
.stream().map(CityResponse::from).collect(Collectors.toList());
|
}
|
|
@Transactional
|
public CityResponse create(String cityName) {
|
String normalizedName = cityName.trim();
|
if (cityMapper.selectCount(new LambdaQueryWrapper<OperationCity>()
|
.eq(OperationCity::getCityName, normalizedName)) > 0) {
|
throw new BusinessException(409, "城市名称已存在");
|
}
|
OperationCity city = new OperationCity();
|
city.setCityName(normalizedName);
|
city.setStatus(1);
|
city.setDeleted(0);
|
cityMapper.insert(city);
|
return CityResponse.from(city);
|
}
|
|
@Transactional
|
public void updateStatus(Long id, Integer status) {
|
OperationCity city = cityMapper.selectById(id);
|
if (city == null) { throw new BusinessException(404, "城市不存在"); }
|
city.setStatus(status);
|
cityMapper.updateById(city);
|
}
|
}
|