package com.next.service;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.next.beans.PageQuery;
import com.next.common.TrainSeatLevel;
import com.next.common.TrainType;
import com.next.common.TrainTypeSeatConstant;
import com.next.dao.TrainNumberDetailMapper;
import com.next.dao.TrainNumberMapper;
import com.next.exception.BusinessException;
import com.next.model.TrainNumber;
import com.next.model.TrainNumberDetail;
import com.next.model.TrainSeat;
import com.next.param.GenerateTicketParam;
import com.next.param.PublishTicketParam;
import com.next.param.TrainSeatSearchParam;
import com.next.seatDao.TrainSeatMapper;
import com.next.util.BeanValidator;
import com.next.util.StringUtil;
import javafx.util.Pair;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class TrainSeatService {
@Resource
private TrainNumberMapper trainNumberMapper;
@Resource
private TrainNumberDetailMapper trainNumberDetailMapper;
@Resource
private TrainSeatMapper trainSeatMapper;
@Resource
private TransactionService transactionService;
public List<TrainSeat> searchList(TrainSeatSearchParam param, PageQuery pageQuery) {
BeanValidator.check(param);
BeanValidator.check(pageQuery);
TrainNumber trainNumber = trainNumberMapper.findByName(param.getTrainNumber());
if (trainNumber == null) {
throw new BusinessException("待查询的车次不存在");
}
return trainSeatMapper.searchList(trainNumber.getId(), param.getTicket(),param.getStatus(),
param.getCarriageNum(), param.getRowNum(), param.getSeatNum(),
pageQuery.getOffset(), pageQuery.getPageSize());
}
public int countList(TrainSeatSearchParam param) {
BeanValidator.check(param);
TrainNumber trainNumber = trainNumberMapper.findByName(param.getTrainNumber());
if (trainNumber == null) {
throw new BusinessException("待查询的车次不存在");
}
return trainSeatMapper.countList(trainNumber.getId(), param.getTicket(),param.getStatus(),
param.getCarriageNum(), param.getRowNum(), param.getSeatNum());
}
public void generate(GenerateTicketParam param) {
BeanValidator.check(param);
// 车次
TrainNumber trainNumber = trainNumberMapper.selectByPrimaryKey(param.getTrainNumberId());
if(trainNumber == null) {
throw new BusinessException("该车次不存在");
}
// 车次详情
List<TrainNumberDetail> detailList = trainNumberDetailMapper.getByTrainNumberId(param.getTrainNumberId());
if (CollectionUtils.isEmpty(detailList)) {
throw new BusinessException("该车次无详情,需要先添加详情");
}
//Collections.sort(detailList, Comparator.comparingInt(TrainNumberDetail::getStationIndex));
// 座位类型
TrainType trainType = TrainType.valueOf(trainNumber.getTrainType());
// 座位配置
Table<Integer, Integer, Pair<Integer, Integer>> seatTable = TrainTypeSeatConstant.getTable(trainType);
// 时间
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime fromLocalDateTime = LocalDateTime.parse(param.getFromTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
List<TrainSeat> list = Lists.newArrayList();
String ticket = fromLocalDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
for(TrainNumberDetail trainNumberDetail : detailList) { // 遍历车次的每一段
// 每一段的发车时间
Date fromDate = Date.from(fromLocalDateTime.atZone(zoneId).toInstant());
// 每一段的到站时间
Date toDate = Date.from(fromLocalDateTime.plusMinutes(trainNumberDetail.getRelativeMinute()).atZone(zoneId).toInstant());
Map<Integer, Integer> seatMoneyMap = splitSeatMoney(trainNumberDetail.getMoney());
for(Table.Cell<Integer, Integer, Pair<Integer, Integer>> cell : seatTable.cellSet()) { // 遍历 每一节车厢 每一排
Integer carriage = cell.getRowKey(); //当前车厢,获取座位数及座位等级的
Integer row = cell.getColumnKey(); // 当前车厢的排,锁定座位数
TrainSeatLevel seatLevel = TrainTypeSeatConstant.getSeatLevel(trainType, carriage); // 座位的等级
Integer money = seatMoneyMap.get(seatLevel.getLevel()); // 座位的价钱
Pair<Integer, Integer> rowSeatRange = seatTable.get(carriage, row); // 获取指定车厢指定排的座位号的范围
for(int index = rowSeatRange.getKey(); index <= rowSeatRange.getValue(); index++) { // 遍历每一排的每个座位号
// 生成座位
TrainSeat trainSeat = TrainSeat.builder()
.carriageNumber(carriage)
.rowNumber(row)
.seatNumber(index)
.money(money)
.ticket(ticket)
.seatLevel(seatLevel.getLevel())
.trainStart(fromDate)
.trainEnd(toDate)
.trainNumberId(trainNumber.getId())
.showNumber(carriage + "车" + row + "排" + index)
.status(0)
.fromStationId(trainNumberDetail.getFromStationId())
.toStationId(trainNumberDetail.getToStationId())
.build();
list.add(trainSeat);
}
}
fromLocalDateTime = fromLocalDateTime.plusMinutes(trainNumberDetail.getRelativeMinute() + trainNumberDetail.getWaitMinute());
}
transactionService.batchInsertSeat(list);
}
// 这种写法是错误的,请一定重视
@Transactional(rollbackFor = Exception.class)
public void batchInsertSeat(List<TrainSeat> list) {
List<List<TrainSeat>> trainTicketPartitionList = Lists.partition(list, 1000);
trainTicketPartitionList.parallelStream().forEach(partitionList -> {
// 批量插入
trainSeatMapper.batchInsert(partitionList);
});
}
private Map<Integer, Integer> splitSeatMoney(String money) {
// money: 0:100,1:80,2:60
try {
List<String> list = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(money);
Map<Integer, Integer> map = Maps.newHashMap();
list.stream().forEach(str -> {
String[] array = str.split(":");
map.put(Integer.parseInt(array[0]), Integer.parseInt(array[1]));
});
return map;
} catch (Exception e) {
throw new BusinessException("价钱解析出错,请检查车次详情的配置");
}
}
@Transactional(rollbackFor = Exception.class)
public void publish(PublishTicketParam param) {
BeanValidator.check(param);
TrainNumber trainNumber = trainNumberMapper.findByName(param.getTrainNumber());
if (trainNumber == null) {
throw new BusinessException("车次不存在");
}
List<Long> trainSeatIdList = StringUtil.splitToListLong(param.getTrainSeatIds());
List<List<Long>> idPartitionList = Lists.partition(trainSeatIdList, 1000);
for(List<Long> partitionList : idPartitionList) {
int count = trainSea
没有合适的资源?快使用搜索试试~ 我知道了~
Java实战高性能12306售票系统源码.zip
共821个文件
js:189个
class:118个
jar:115个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 144 浏览量
2024-02-03
20:52:33
上传
评论
收藏 103.21MB ZIP 举报
温馨提示
Java实战高性能12306售票系统源码.zip # Java实战高性能12306售票系统 ## 一、SQL初始化 ### 0、重点说明 按课程给出的代码,至少需要本地创建7个database: (可参考 common/src/main/resources/application-common.yml) 1、基础数据源:test1 2、订单数据源:t_train_order 3、座位数据源5个:t_train_seat_[1-5] 同时,需要保证数据库字符集为utf8mb4 (utf8也可以) 课程里默认DB的登录用户为:root,密码为:123456,此处需要适配本地DB用户名和密码进行调整 此处可以通过修改代码来调整分库分表策略 ### 1、基础数据源 sql/基础数据源.sql 如果需要使用课程里的数据,使用:sql/基础数据源含数据.sql ### 2、订单数据源 sql/订单库.sql ### 3、座位数据源 sql/座位库-分库1.sql sql/座位库-分库2.sql sql/座位库-分库3.sql sql/座位库-分库4.sql sql/座位库-分库5.sql
资源推荐
资源详情
资源评论
收起资源包目录
Java实战高性能12306售票系统源码.zip (821个子文件)
TrainOrderDetail.class 12KB
TrainSeatService.class 12KB
TrainSeatService.class 12KB
TrainSeat.class 10KB
TrainOrder.class 9KB
TrainSeatController.class 7KB
TrainSeatController.class 7KB
SysUser.class 7KB
TrainNumberDetail.class 6KB
TrainNumber.class 6KB
SeatDataSourceConfig.class 6KB
TrainNumberDetailController.class 6KB
TrainNumberDetailController.class 6KB
TrainTraveller.class 6KB
TrainTypeSeatConstant.class 5KB
TrainNumberController.class 5KB
TrainNumberController.class 5KB
TrainOrderDetail$TrainOrderDetailBuilder.class 5KB
BasicDataSourceConfig.class 4KB
TrainStationController.class 4KB
TrainStationController.class 4KB
TrainUser.class 4KB
TrainNumberDetailService.class 4KB
TrainNumberDetailService.class 4KB
TrainSeat$TrainSeatBuilder.class 4KB
BeanValidator.class 4KB
BeanValidator.class 4KB
TrainOrder$TrainOrderBuilder.class 4KB
JsonMapper.class 3KB
JsonMapper.class 3KB
TrainStationService.class 3KB
TrainStationService.class 3KB
TrainNumberDetailParam.class 3KB
TrainNumberDetailParam.class 3KB
AdminController.class 3KB
AdminController.class 3KB
SysUser$SysUserBuilder.class 3KB
TrainUserTraveller.class 3KB
TrainNumberDetail$TrainNumberDetailBuilder.class 3KB
TrainNumberService.class 3KB
TrainNumberService.class 3KB
TrainStation.class 3KB
TrainTraveller$TrainTravellerBuilder.class 3KB
OrderDataSourceConfig.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
TrainNumber$TrainNumberBuilder.class 3KB
TrainSeatSearchParam.class 2KB
TrainSeatSearchParam.class 2KB
TrainCityService.class 2KB
TrainCityService.class 2KB
TrainSeatMapper.class 2KB
TrainCity.class 2KB
TrainNumberParam.class 2KB
TrainNumberParam.class 2KB
TrainSeatTableShardingAlgorithm.class 2KB
TrainSeatDatabaseShardingAlgorithm.class 2KB
TransactionService.class 2KB
TransactionService.class 2KB
JsonData.class 2KB
JsonData.class 2KB
TrainUser$TrainUserBuilder.class 2KB
PageResult.class 2KB
TrainStationParam.class 2KB
TrainStationParam.class 2KB
TrainSeatDto.class 2KB
TrainSeatDto.class 2KB
PageResult$PageResultBuilder.class 2KB
TrainCityController.class 2KB
TrainCityController.class 2KB
GlobalExceptionHandler.class 2KB
GlobalExceptionHandler.class 2KB
TrainSeatLevel.class 2KB
TrainStation$TrainStationBuilder.class 1KB
GenerateTicketParam.class 1KB
GenerateTicketParam.class 1KB
TrainUserTraveller$TrainUserTravellerBuilder.class 1KB
TrainCityParam.class 1KB
TrainCityParam.class 1KB
TrainNumberDetailDto.class 1KB
TrainNumberDetailDto.class 1KB
TrainCity$TrainCityBuilder.class 1KB
PublishTicketParam.class 1KB
PublishTicketParam.class 1KB
TrainType.class 1KB
PageQuery.class 1KB
TrainNumberDto.class 1KB
TrainNumberDto.class 1KB
BusinessException.class 1KB
BusinessException.class 1KB
ParamException.class 1KB
ParamException.class 1KB
TrainNumberDetailMapper.class 928B
BackendApplication.class 899B
BackendApplication.class 899B
ServletInitializer.class 894B
ServletInitializer.class 894B
TrainStationMapper.class 890B
TrainStationDto.class 849B
TrainStationDto.class 849B
共 821 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
- 2301_772044352024-04-29感谢大佬分享的资源,对我启发很大,给了我新的灵感。
武昌库里写JAVA
- 粉丝: 6654
- 资源: 3166
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功