feat(health):新增预约设置相关功能

1. 查询某月的预约设置 2. 查询可用的预约日期 3. (批量)设置预约人数
This commit is contained in:
JIAN
2024-09-14 21:57:39 +08:00
parent c1dce10d9e
commit 6003aca88e
6 changed files with 161 additions and 9 deletions

View File

@@ -1,5 +1,9 @@
package com.jzo2o.health.controller.admin;
import com.alibaba.excel.EasyExcel;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.health.model.excel.ReservationImportData;
import com.jzo2o.health.service.IReservationSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@@ -9,9 +13,12 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
/**
* 预约设置操作
*
* @author itcast
*/
@Slf4j
@@ -19,10 +26,26 @@ import org.springframework.web.multipart.MultipartFile;
@RequestMapping("/admin/reservation-setting")
@Api(tags = "管理端 - 批量预约设置相关接口")
public class ReservationBatchSettingController {
@Resource
private IReservationSettingService reservationSettingService;
@PostMapping("/upload")
@ApiOperation("上传文件批量预约设置")
public void upload(@RequestPart("file") MultipartFile file) {
List<ReservationImportData> reservationDataList;
try (InputStream inputStream = file.getInputStream()) {
reservationDataList = EasyExcel.read(inputStream)
.head(ReservationImportData.class)
.sheet("预约设置模板")
.doReadSync();
} catch (Exception e) {
// 防止过度包装
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
if (CollUtils.isNotEmpty(reservationDataList)) {
reservationSettingService.editNumberByDateBatch(reservationDataList);
}
}
}
}

View File

@@ -2,12 +2,15 @@ package com.jzo2o.health.controller.admin;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.service.IReservationSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.YearMonth;
import java.util.List;
/**
@@ -20,16 +23,20 @@ import java.util.List;
@RequestMapping("/admin/reservation-setting")
@Api(tags = "管理端 - 预约设置相关接口")
public class ReservationSettingController {
@Resource
private IReservationSettingService reservationSettingService;
@GetMapping("/getReservationSettingByMonth")
@ApiOperation("按月查询预约设置")
@ApiImplicitParam(name = "date", value = "月份格式yyyy-MM", required = true, dataTypeClass = String.class)
public List<ReservationSettingResDTO> getReservationSettingByMonth(@RequestParam("date") String date) {
return null;
YearMonth yearMonth = YearMonth.parse(date);
return reservationSettingService.getReservationSettingByMonth(yearMonth);
}
@PutMapping("/editNumberByDate")
@ApiOperation("编辑预约设置")
public void editNumberByDate(@RequestBody ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO) {
reservationSettingService.editNumberByDate(reservationSettingUpsertReqDTO);
}
}
}

View File

@@ -2,6 +2,7 @@ package com.jzo2o.health.controller.user;
import com.jzo2o.health.annotation.IgnoreToken;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.service.IReservationSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@@ -11,9 +12,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.YearMonth;
/**
* 预约设置操作
*
* @author itcast
*/
@Slf4j
@@ -21,12 +24,15 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user/reservation-setting")
@Api(tags = "用户端 - 预约设置相关接口")
public class ReservationSettingController {
@Resource
private IReservationSettingService reservationSettingService;
@GetMapping("/getReservationDateByMonth")
@IgnoreToken
@ApiOperation("按月查询可预约日期")
@ApiImplicitParam(name = "month", value = "月份格式yyyy-MM", required = true, dataTypeClass = String.class)
public ReservationDateResDTO getReservationDateByMonth(@RequestParam("month") String month) {
return null;
YearMonth yearMonth = YearMonth.parse(month);
return reservationSettingService.getReservationDateByMonth(yearMonth);
}
}
}

View File

@@ -3,17 +3,23 @@ package com.jzo2o.health.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzo2o.health.model.domain.ReservationSetting;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDate;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author itcast
* @since 2023-11-01
*/
public interface ReservationSettingMapper extends BaseMapper<ReservationSetting> {
@Update("UPDATE reservation_setting SET reservations = reservations + 1 WHERE id = #{id} AND reservations < number")
Integer updateReservations(@Param("id") Integer id);
}
@Select("SELECT order_date FROM reservation_setting WHERE #{start} <= order_date AND order_date <= #{end}")
List<String> getReservationDate(@Param("start") LocalDate start, @Param("end") LocalDate end);
}

View File

@@ -2,6 +2,13 @@ package com.jzo2o.health.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.model.excel.ReservationImportData;
import java.time.YearMonth;
import java.util.List;
/**
* <p>
@@ -10,4 +17,23 @@ import com.jzo2o.health.model.domain.ReservationSetting;
* @author JIAN
*/
public interface IReservationSettingService extends IService<ReservationSetting> {
/**
* 获取指定月份的所有预约数据
*/
List<ReservationSettingResDTO> getReservationSettingByMonth(YearMonth yearMonth);
/**
* 更新/插入指定日期的预约数据
*/
void editNumberByDate(ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO);
/**
* 批量更新/插入指定日期的预约数据
*/
void editNumberByDateBatch(List<ReservationImportData> reservationDataList);
/**
* 获取当前月份可用预约日期
*/
ReservationDateResDTO getReservationDateByMonth(YearMonth yearMonth);
}

View File

@@ -1,9 +1,25 @@
package com.jzo2o.health.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.health.mapper.ReservationSettingMapper;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.model.excel.ReservationImportData;
import com.jzo2o.health.service.IReservationSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* <p>
@@ -11,5 +27,73 @@ import com.jzo2o.health.service.IReservationSettingService;
* </p>
* @author JIAN
*/
@Service
public class ReservationSettingServiceImpl extends ServiceImpl<ReservationSettingMapper, ReservationSetting> implements IReservationSettingService {
@Override
@SuppressWarnings("unchecked")
public List<ReservationSettingResDTO> getReservationSettingByMonth(YearMonth yearMonth) {
List<ReservationSetting> reservationSettingList = lambdaQuery()
.ge(ReservationSetting::getOrderDate, yearMonth.atDay(1))
.le(ReservationSetting::getOrderDate, yearMonth.atEndOfMonth())
.orderByDesc(ReservationSetting::getOrderDate)
.list();
if (CollUtils.isEmpty(reservationSettingList)) {
return new ArrayList<>();
} else {
return reservationSettingList.stream()
.map(item -> ReservationSettingResDTO.builder()
.date(item.getOrderDate().toString())
.number(item.getNumber())
.reservations(item.getReservations())
.build())
.collect(Collectors.toList());
}
}
@Override
public void editNumberByDate(ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO) {
LocalDate orderDate = reservationSettingUpsertReqDTO.getOrderDate();
Integer number = reservationSettingUpsertReqDTO.getNumber();
Optional.ofNullable(lambdaQuery()
.eq(ReservationSetting::getOrderDate, orderDate)
.one())
.ifPresentOrElse(reservationSetting -> {
// 存在则更新
if (!lambdaUpdate()
.eq(ReservationSetting::getOrderDate, orderDate)
.set(ReservationSetting::getNumber, number)
.update()) {
throw new DBException("更新预约设置表失败");
}
}, () -> {
// 不存在则创建
if (!SqlHelper.retBool(baseMapper.insert(ReservationSetting
.builder()
.orderDate(orderDate)
.reservations(0)
.number(number)
.build()))) {
throw new DBException("插入预约设置表失败");
}
});
}
@Override
@Transactional
public void editNumberByDateBatch(List<ReservationImportData> reservationDataList) {
for (ReservationImportData reservationData : reservationDataList) {
this.editNumberByDate(ReservationSettingUpsertReqDTO.builder()
.orderDate(LocalDate.parse(reservationData.getDate()))
.number(Math.min(reservationData.getNumber(), 999))
.build());
}
}
@Override
public ReservationDateResDTO getReservationDateByMonth(YearMonth yearMonth) {
List<String> dates = baseMapper.getReservationDate(yearMonth.atDay(1), yearMonth.atEndOfMonth());
return new ReservationDateResDTO(dates);
}
}