mahjong_group/server/CoreCms.Net.Services/SQ/SQRoomPricingServices.cs
2026-01-01 14:35:52 +08:00

306 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.ViewModels.SQ;
using CoreCms.Net.Services;
using CoreCms.Net.Utility.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreCms.Net.Services
{
/// <summary>
/// 房间时段价格服务实现
/// </summary>
public class SQRoomPricingServices : BaseServices<SQRoomPricing>, ISQRoomPricingServices
{
private readonly ISQRoomPricingRepository _pricingRepository;
private readonly IUnitOfWork _unitOfWork;
public SQRoomPricingServices(IUnitOfWork unitOfWork, ISQRoomPricingRepository pricingRepository)
{
_unitOfWork = unitOfWork;
_pricingRepository = pricingRepository;
BaseDal = pricingRepository;
}
/// <summary>
/// 获取房间指定时段的价格配置
/// </summary>
public async Task<SQRoomPricing> GetRoomPricingAsync(int roomId, int timeSlotType, DateTime? date = null)
{
// 构建查询条件
var where = PredicateBuilder.True<SQRoomPricing>();
where = where.And(p => p.room_id == roomId);
where = where.And(p => p.time_slot_type == timeSlotType);
where = where.And(p => p.is_active == true);
if (date.HasValue)
{
var queryDate = date.Value.Date;
// 优先查询有日期范围的价格配置
var dateRangePricing = await _pricingRepository.QueryByClauseAsync(
p => p.room_id == roomId &&
p.time_slot_type == timeSlotType &&
p.is_active == true &&
p.effective_date_start <= queryDate &&
p.effective_date_end >= queryDate,
p => p.id, OrderByType.Desc);
if (dateRangePricing != null)
{
return dateRangePricing;
}
}
// 查询默认价格配置日期范围为NULL
where = where.And(p => p.effective_date_start == null && p.effective_date_end == null);
return await _pricingRepository.QueryByClauseAsync(where, p => p.id, OrderByType.Desc);
}
/// <summary>
/// 获取房间所有时段的价格配置
/// </summary>
public async Task<List<SQRoomPricing>> GetRoomAllPricingAsync(int roomId, DateTime? date = null)
{
var result = new List<SQRoomPricing>();
// 获取4个时段的价格配置
for (int i = 0; i <= 3; i++)
{
var pricing = await GetRoomPricingAsync(roomId, i, date);
if (pricing != null)
{
result.Add(pricing);
}
}
return result;
}
/// <summary>
/// 设置房间时段价格
/// </summary>
public async Task<bool> SetRoomPricingAsync(SetRoomPricingDto dto)
{
// 检查是否已存在相同配置
var existing = await _pricingRepository.QueryByClauseAsync(
p => p.room_id == dto.room_id &&
p.time_slot_type == dto.time_slot_type &&
p.effective_date_start == dto.effective_date_start &&
p.effective_date_end == dto.effective_date_end,
p => p.id, OrderByType.Desc);
if (existing != null)
{
// 更新现有配置
existing.standard_price = dto.standard_price;
existing.member_price = dto.member_price;
existing.price_desc_standard = dto.price_desc_standard ?? $"{dto.standard_price}元/时段";
existing.price_desc_member = dto.price_desc_member ?? $"{dto.member_price}元/时段";
existing.updated_at = DateTime.Now;
return await _pricingRepository.UpdateAsync(existing);
}
else
{
// 创建新配置
var pricing = new SQRoomPricing
{
room_id = dto.room_id,
time_slot_type = dto.time_slot_type,
standard_price = dto.standard_price,
member_price = dto.member_price,
price_desc_standard = dto.price_desc_standard ?? $"{dto.standard_price}元/时段",
price_desc_member = dto.price_desc_member ?? $"{dto.member_price}元/时段",
effective_date_start = dto.effective_date_start,
effective_date_end = dto.effective_date_end,
is_active = true,
created_at = DateTime.Now,
updated_at = DateTime.Now
};
var id = await _pricingRepository.InsertAsync(pricing);
return id > 0;
}
}
/// <summary>
/// 批量设置房间价格
/// </summary>
public async Task<bool> BatchSetRoomPricingAsync(BatchSetRoomPricingDto dto)
{
if (dto.room_ids == null || dto.room_ids.Count == 0)
{
return false;
}
try
{
foreach (var roomId in dto.room_ids)
{
var singleDto = new SetRoomPricingDto
{
room_id = roomId,
time_slot_type = dto.time_slot_type,
standard_price = dto.standard_price,
member_price = dto.member_price,
price_desc_standard = dto.price_desc_standard,
price_desc_member = dto.price_desc_member,
effective_date_start = dto.effective_date_start,
effective_date_end = dto.effective_date_end
};
await SetRoomPricingAsync(singleDto);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 获取房间价格列表(含时段名称)
/// </summary>
public async Task<List<SQRoomPricingDto>> GetRoomPricingListAsync(int roomId)
{
var pricingList = await GetRoomAllPricingAsync(roomId);
var result = pricingList.Select(p => new SQRoomPricingDto
{
id = p.id,
room_id = p.room_id,
time_slot_type = p.time_slot_type,
time_slot_name = TimeSlotHelper.GetTimeSlotName(p.time_slot_type),
standard_price = p.standard_price,
member_price = p.member_price,
price_desc_standard = p.price_desc_standard,
price_desc_member = p.price_desc_member,
effective_date_start = p.effective_date_start,
effective_date_end = p.effective_date_end,
is_active = p.is_active,
created_at = p.created_at,
updated_at = p.updated_at
}).ToList();
return result;
}
/// <summary>
/// 设置节假日价格
/// </summary>
public async Task<bool> SetHolidayPricingAsync(SetHolidayPricingDto dto)
{
if (dto.slot_prices == null || dto.slot_prices.Count == 0)
{
return false;
}
try
{
// 如果没有指定房间,则获取所有房间
var roomIds = dto.room_ids;
if (roomIds == null || roomIds.Count == 0)
{
// 这里需要注入房间服务来获取所有房间,暂时简化处理
return false;
}
foreach (var roomId in roomIds)
{
foreach (var slotPrice in dto.slot_prices)
{
var setDto = new SetRoomPricingDto
{
room_id = roomId,
time_slot_type = slotPrice.time_slot_type,
standard_price = slotPrice.standard_price,
member_price = slotPrice.member_price,
price_desc_standard = $"{slotPrice.standard_price}元/时段({dto.holiday_name}",
price_desc_member = $"{slotPrice.member_price}元/时段({dto.holiday_name}",
effective_date_start = dto.start_date,
effective_date_end = dto.end_date
};
await SetRoomPricingAsync(setDto);
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 查询某房间某日期的节假日价格列表
/// </summary>
public async Task<List<SQRoomPricing>> GetHolidayPricingByDateAsync(int roomId, DateTime date)
{
var queryDate = date.Date;
// 查询该日期的节假日价格(有日期范围的价格配置)
var holidayPricing = await _pricingRepository.QueryListByClauseAsync(
p => p.room_id == roomId &&
p.effective_date_start != null &&
p.effective_date_end != null &&
p.effective_date_start <= queryDate &&
p.effective_date_end >= queryDate &&
p.is_active == true,
p => p.time_slot_type,
OrderByType.Asc
);
return holidayPricing ?? new List<SQRoomPricing>();
}
/// <summary>
/// 删除某房间某日期的节假日价格
/// </summary>
public async Task<bool> DeleteHolidayPricingAsync(int roomId, DateTime date)
{
try
{
var queryDate = date.Date;
// 查询该日期的节假日价格
var holidayPricing = await GetHolidayPricingByDateAsync(roomId, queryDate);
if (holidayPricing == null || holidayPricing.Count == 0)
{
return true; // 没有数据,直接返回成功
}
// 删除所有节假日价格记录
var ids = holidayPricing.Select(p => p.id).ToArray();
return await _pricingRepository.DeleteByIdsAsync(ids);
}
catch
{
return false;
}
}
/// <summary>
/// 检查某房间某日期是否设置了节假日价格
/// </summary>
public async Task<bool> HasHolidayPricingAsync(int roomId, DateTime date)
{
var holidayPricing = await GetHolidayPricingByDateAsync(roomId, date);
return holidayPricing != null && holidayPricing.Count > 0;
}
}
}