using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Extend.Entitys.Dto.LqXhHyhk;
using NCC.Extend.Entitys.Dto.LqXhJksyj;
using NCC.Extend.Entitys.Dto.LqXhPxmx;
using NCC.Extend.Entitys.Enum;
using NCC.Extend.Entitys.lq_hytk_mx;
using NCC.Extend.Entitys.lq_kd_deductinfo;
using NCC.Extend.Entitys.lq_kd_kdjlb;
using NCC.Extend.Entitys.lq_kd_pxmx;
using NCC.Extend.Entitys.lq_khxx;
using NCC.Extend.Entitys.lq_person_times_record;
using NCC.Extend.Entitys.lq_xh_hyhk;
using NCC.Extend.Entitys.lq_xh_jksyj;
using NCC.Extend.Entitys.lq_xh_kjbsyj;
using NCC.Extend.Entitys.lq_xh_pxmx;
using NCC.Extend.Entitys.lq_xmzl;
using NCC.Extend.Entitys.lq_yyjl;
using NCC.Extend.Entitys.lq_yyjl_px;
using NCC.Extend.Entitys.Config;
using NCC.Extend.Helpers;
using NCC.FriendlyException;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend.LqXhHyhk
{
///
/// 耗卡与预约联动(拆分 partial)
///
public partial class LqXhHyhkService
{
///
/// 校验预约并准备新建耗卡(可选校验「同一预约是否已有有效耗卡」)
///
private async Task LoadAndValidateAppointmentForNewConsumeAsync(
LqXhHyhkCrInput input,
LqXhHyhkEntity entity,
bool checkDuplicateEffective)
{
var yyjl = await _db.Queryable().FirstAsync(x => x.Id == input.appointmentId);
if (yyjl == null)
throw NCCException.Oh("预约记录不存在");
if (yyjl.Gk != entity.Hy)
throw NCCException.Oh("预约与耗卡会员不一致");
if (yyjl.Djmd != entity.Md)
throw NCCException.Oh("预约与耗卡门店不一致");
if (!yyjl.Yysj.HasValue)
throw NCCException.Oh("预约时间为空,无法转消耗");
if (checkDuplicateEffective)
{
var dup = await _db.Queryable()
.AnyAsync(x =>
x.AppointmentId == input.appointmentId
&& x.IsEffective == StatusEnum.有效.GetHashCode());
if (dup)
throw NCCException.Oh("该预约已存在有效耗卡");
}
if (!LqYyjlStatusHelper.AllowsTransferToConsume(yyjl.F_Status))
throw NCCException.Oh("当前预约状态不允许转耗卡");
return yyjl;
}
///
/// 重开单:校验预约与当前有效耗卡一致,且预约处于「已转耗卡」或「已完成」。重开单不会变更预约状态(不回退、不前推),
/// 仅将 指向新开耗卡(见 中 applyAppointmentTransferredUpdate:false 分支)。
///
private static void ValidateYyjlForReopen(LqYyjlEntity yyjl, LqXhHyhkEntity oldCard)
{
if (yyjl.Gk != oldCard.Hy)
throw NCCException.Oh("预约与耗卡会员不一致");
if (yyjl.Djmd != oldCard.Md)
throw NCCException.Oh("预约与耗卡门店不一致");
var normalized = LqYyjlStatusHelper.NormalizeStored(yyjl.F_Status);
if (normalized != LqYyjlStatusTexts.TransferredToConsume && normalized != LqYyjlStatusTexts.Completed)
throw NCCException.Oh("当前预约状态不允许重开单");
if (!yyjl.Yysj.HasValue)
throw NCCException.Oh("预约时间为空,无法转消耗");
}
///
/// 在同一事务内执行耗卡主表与子表插入,并按约定回写预约
///
private async Task PerformConsumeInsertCoreAsync(
LqXhHyhkEntity entity,
LqXhHyhkCrInput input,
DateTime lineHksj,
LqYyjlEntity appointmentYyjl,
bool applyAppointmentTransferredUpdate)
{
// 预约转消耗:客户端未带 billingItemId 时,从预约品项表补全权益行绑定
await EnrichConsumeBillingItemsFromAppointmentAsync(input.appointmentId ?? entity.AppointmentId, input.lqXhPxmxList);
// 入库前校验:耗卡品项必须归属当前会员,且剩余次数充足(防止跨会员耗卡与超次数耗卡)
await ValidateConsumeBillingItemsAsync(entity.Hy, input.lqXhPxmxList);
var memberInfo = await _db.Queryable().Where(w => w.Id == entity.Hy).FirstAsync();
var newEntity = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnEntityAsync();
var allPxmxEntities = new List();
var allJksyjEntities = new List();
var allKjbsyjEntities = new List();
var allPersonTimesRecordEntities = new List();
if (input.lqXhPxmxList != null && input.lqXhPxmxList.Any())
{
foreach (var item in input.lqXhPxmxList)
{
var lqXhPxmxEntity = new LqXhPxmxEntity
{
Id = YitIdHelper.NextId().ToString(),
ConsumeInfoId = newEntity.Id,
BillingItemId = item.billingItemId,
CreateTIme = DateTime.Now,
Yjsj = lineHksj,
MemberId = entity.Hy,
OriginalProjectNumber = item.projectNumber ?? 0,
OvertimeProjectNumber = (decimal)(entity.OvertimeCoefficient * (item.projectNumber ?? 0)),
ProjectNumber =
(decimal)(
(item.projectNumber ?? 0)
+ (entity.OvertimeCoefficient * (item.projectNumber ?? 0))),
TotalPrice = (decimal)(item.pxjg * (item.projectNumber ?? 1)),
Px = item.px,
Pxmc = item.pxmc,
Pxjg = item.pxjg,
SourceType = item.sourceType,
IsEffective = StatusEnum.有效.GetHashCode(),
ItemCategory = await _db.Queryable().Where(x => x.Id == item.px).Select(x => x.Qt2).FirstAsync(),
PerformanceType = await _db.Queryable().Where(x => x.Id == item.px).Select(x => x.Fl3).FirstAsync() ?? "",
BeautyType = await _db.Queryable().Where(x => x.Id == item.px).Select(x => x.BeautyType).FirstAsync(),
};
allPxmxEntities.Add(lqXhPxmxEntity);
if (item.lqXhJksyjList != null && item.lqXhJksyjList.Any())
{
foreach (var ijks_tem in item.lqXhJksyjList)
{
var (normJks, normJkszh) = HealthCoachIdHelper.NormalizeForWrite(ijks_tem.jks, ijks_tem.jkszh);
allJksyjEntities.Add(
new LqXhJksyjEntity
{
Id = YitIdHelper.NextId().ToString(),
Glkdbh = newEntity.Id,
Jks = normJks,
Jksxm = ijks_tem.jksxm,
Jkszh = normJkszh,
Jksyj = ijks_tem.jksyj,
Yjsj = lineHksj,
CreateTime = DateTime.Now,
JsjId = ijks_tem.jsjId,
Kdpxid = lqXhPxmxEntity.Id,
OriginalLaborCost = ijks_tem.laborCost,
OvertimeLaborCost = (decimal)(entity.OvertimeCoefficient * (ijks_tem.laborCost ?? 0)),
LaborCost =
(decimal)(
(ijks_tem.laborCost ?? 0)
+ (entity.OvertimeCoefficient * (ijks_tem.laborCost ?? 0))),
OriginalKdpxNumber = ijks_tem.kdpxNumber,
OvertimeKdpxNumber = (decimal)(entity.OvertimeCoefficient * (ijks_tem.kdpxNumber ?? 0)),
KdpxNumber =
(decimal)(
(ijks_tem.kdpxNumber ?? 0)
+ (entity.OvertimeCoefficient * (ijks_tem.kdpxNumber ?? 0)))
+ (ijks_tem.accompaniedProjectNumber ?? 0),
IsAccompanied = ijks_tem.isAccompanied,
AccompaniedProjectNumber = ijks_tem.accompaniedProjectNumber,
IsEffective = StatusEnum.有效.GetHashCode(),
ItemCategory = lqXhPxmxEntity.ItemCategory,
ItemId = lqXhPxmxEntity.Px,
StoreId = entity.Md,
ItemName = lqXhPxmxEntity.Pxmc,
PerformanceType = lqXhPxmxEntity.PerformanceType,
BeautyType = lqXhPxmxEntity.BeautyType,
}
);
}
}
if (item.lqXhKjbsyjList != null && item.lqXhKjbsyjList.Any())
{
foreach (var ikjbs_tem in item.lqXhKjbsyjList)
{
allKjbsyjEntities.Add(
new LqXhKjbsyjEntity
{
Id = YitIdHelper.NextId().ToString(),
Glkdbh = newEntity.Id,
Kjbls = TechTeacherIdHelper.NormalizeKjbls(ikjbs_tem.kjbls, ikjbs_tem.kjblszh),
Kjblsxm = ikjbs_tem.kjblsxm,
Kjblszh = TechTeacherIdHelper.NormalizeKjblszh(ikjbs_tem.kjbls, ikjbs_tem.kjblszh),
Kjblsyj = ikjbs_tem.kjblsyj,
Yjsj = lineHksj,
CreateTime = DateTime.Now,
Hkpxid = lqXhPxmxEntity.Id,
OriginalHdpxNumber = ikjbs_tem.hdpxNumber,
OvertimeHdpxNumber = 0,
HdpxNumber = ikjbs_tem.hdpxNumber,
OriginalLaborCost = ikjbs_tem.laborCost,
OvertimeLaborCost = 0,
LaborCost = ikjbs_tem.laborCost,
IsEffective = StatusEnum.有效.GetHashCode(),
ItemCategory = lqXhPxmxEntity.ItemCategory,
ItemId = lqXhPxmxEntity.Px,
StoreId = entity.Md,
ItemName = lqXhPxmxEntity.Pxmc,
PerformanceType = lqXhPxmxEntity.PerformanceType,
BeautyType = lqXhPxmxEntity.BeautyType,
}
);
}
}
}
}
var billingRecord = await _db.Queryable()
.Where(x => x.Kdhy == entity.Hy && x.IsEffective == StatusEnum.有效.GetHashCode() && x.Sfyj > 0)
.AnyAsync();
var NoTallJksyjEntities = allJksyjEntities
.Where(x => x.Jks != null && x.Jks != "" && (x.Jksxm == null || !x.Jksxm.Contains("T区")))
.ToList();
var jksCount = NoTallJksyjEntities.GroupBy(x => x.Jks).Count();
foreach (var item in NoTallJksyjEntities.Select(x => x.Jks).Distinct())
{
allPersonTimesRecordEntities.Add(
new LqPersonTimesRecordEntity
{
Id = YitIdHelper.NextId().ToString(),
BusinessId = newEntity.Id,
BusinessType = "耗卡",
PersonType = "健康师",
PersonId = item,
PersonName = NoTallJksyjEntities.Where(x => x.Jks == item).Select(x => x.Jksxm).First(),
MemberId = entity.Hy,
MemberName = memberInfo.Khmc,
WorkDate = lineHksj,
WorkMonth = lineHksj.ToDate().ToString("yyyyMM"),
Quantity = 1 / (decimal)jksCount,
CreateTime = DateTime.Now,
IsEffective = StatusEnum.有效.GetHashCode(),
HasBilling = billingRecord ? 1 : 0,
}
);
}
var NoTallKjbsyjEntities = allKjbsyjEntities
.Where(x => x.Kjbls != null && x.Kjbls != "" && (x.Kjblsxm == null || !x.Kjblsxm.Contains("T区")))
.ToList();
var kjbCount = NoTallKjbsyjEntities.GroupBy(x => x.Kjbls).Count();
foreach (var item in NoTallKjbsyjEntities.Select(x => x.Kjbls).Distinct())
{
allPersonTimesRecordEntities.Add(
new LqPersonTimesRecordEntity
{
Id = YitIdHelper.NextId().ToString(),
BusinessId = newEntity.Id,
BusinessType = "耗卡",
PersonType = "科技部老师",
PersonId = item,
PersonName = NoTallKjbsyjEntities.Where(x => x.Kjbls == item).Select(x => x.Kjblsxm).First(),
MemberId = entity.Hy,
MemberName = memberInfo.Khmc,
WorkDate = lineHksj,
WorkMonth = lineHksj.ToDate().ToString("yyyyMM"),
Quantity = 1 / (decimal)kjbCount,
CreateTime = DateTime.Now,
IsEffective = StatusEnum.有效.GetHashCode(),
HasBilling = billingRecord ? 1 : 0,
}
);
}
if (allPersonTimesRecordEntities.Any())
await _db.Insertable(allPersonTimesRecordEntities).ExecuteCommandAsync();
if (allPxmxEntities.Any())
await _db.Insertable(allPxmxEntities).ExecuteCommandAsync();
if (allJksyjEntities.Any())
await _db.Insertable(allJksyjEntities).ExecuteCommandAsync();
if (allKjbsyjEntities.Any())
await _db.Insertable(allKjbsyjEntities).ExecuteCommandAsync();
if (appointmentYyjl != null)
{
if (applyAppointmentTransferredUpdate)
{
await _db.Updateable()
.SetColumns(x => new LqYyjlEntity
{
ConsumeId = newEntity.Id,
ServiceEndTime = DateTime.Now,
F_Status = LqYyjlStatusTexts.TransferredToConsume,
})
.Where(x => x.Id == appointmentYyjl.Id)
.ExecuteCommandAsync();
}
else
{
await _db.Updateable()
.SetColumns(x => new LqYyjlEntity { ConsumeId = newEntity.Id })
.Where(x => x.Id == appointmentYyjl.Id)
.ExecuteCommandAsync();
}
}
if (memberInfo.Khlx == MemberTypeEnum.线索.GetHashCode().ToString())
{
memberInfo.Khlx = MemberTypeEnum.新客.GetHashCode().ToString();
await _db.Updateable(memberInfo).UpdateColumns(x => x.Khlx).ExecuteCommandAsync();
}
// 入库完成后按全局有效耗卡 MAX(hksj) 重算,支持补录历史耗卡
await RefreshMemberLastConsumeAndSleepDaysAsync(entity.Hy);
}
///
/// 预约转消耗入库前,将 补写到耗卡明细,
/// 避免客户端字段大小写或预填丢失导致权益行未绑定。
///
private async Task EnrichConsumeBillingItemsFromAppointmentAsync(string appointmentId, List pxmxList)
{
if (string.IsNullOrWhiteSpace(appointmentId) || pxmxList == null || pxmxList.Count == 0)
return;
var apptPxList = await _db.Queryable()
.Where(x => x.YyjlId == appointmentId && x.IsEffective == StatusEnum.有效.GetHashCode())
.ToListAsync();
if (apptPxList.Count == 0)
return;
foreach (var item in pxmxList)
{
if (!string.IsNullOrWhiteSpace(item.billingItemId))
continue;
var pn = item.projectNumber ?? 1;
// 品项改名不影响匹配:一律以品项ID(Px)为准,名称不参与判定
var match = apptPxList.FirstOrDefault(a =>
!string.IsNullOrWhiteSpace(a.BillingItemId)
&& a.Px == item.px
&& (a.ProjectNumber ?? 1) == pn)
?? apptPxList.FirstOrDefault(a =>
!string.IsNullOrWhiteSpace(a.BillingItemId) && a.Px == item.px);
if (match != null)
item.billingItemId = match.BillingItemId;
}
}
///
/// 更新耗卡(含异常纠错)入库前:从原有效明细补写缺失的 ,
/// 避免「先删后插」时前端未回传权益行导致绑定丢失。
///
private static void EnrichConsumeBillingItemsFromExistingPxmx(
List pxmxList,
List existingPxmxList)
{
if (pxmxList == null || pxmxList.Count == 0 || existingPxmxList == null || existingPxmxList.Count == 0)
return;
for (var i = 0; i < pxmxList.Count; i++)
{
var item = pxmxList[i];
if (!string.IsNullOrWhiteSpace(item.billingItemId))
continue;
if (!string.Equals(item.sourceType, "购买", StringComparison.OrdinalIgnoreCase))
continue;
// 品项改名不影响匹配:一律以品项ID(Px)为准,名称不参与判定
var match = (i < existingPxmxList.Count
&& existingPxmxList[i].Px == item.px
&& !string.IsNullOrWhiteSpace(existingPxmxList[i].BillingItemId)
? existingPxmxList[i]
: null)
?? existingPxmxList.FirstOrDefault(e =>
e.Px == item.px
&& !string.IsNullOrWhiteSpace(e.BillingItemId)
&& string.Equals(e.SourceType, item.sourceType, StringComparison.OrdinalIgnoreCase))
?? existingPxmxList.FirstOrDefault(e =>
e.Px == item.px
&& !string.IsNullOrWhiteSpace(e.BillingItemId));
if (match != null)
item.billingItemId = match.BillingItemId;
}
}
///
/// 耗卡入库前的开单品项校验:
/// 1)耗卡品项必须归属当前耗卡会员,禁止消耗他人开单的品项(防止数据绑错人);
/// 2)本次耗卡次数不得超过该开单品项的剩余次数(剩余 = 购买 - 已消耗 - 已退卡 - 已储扣)。
/// 性能:无论品项多少,均以「品项ID 批量 IN + 数据库分组聚合」方式查询,固定 5 条 SQL 完成校验,避免逐项 N 次查库。
///
/// 当前耗卡会员ID
/// 本次耗卡品项明细
/// 更新场景排除当前耗卡单已占用次数(先删后插前校验)
private async Task ValidateConsumeBillingItemsAsync(
string memberId,
List pxmxList,
string excludeConsumeId = null)
{
if (pxmxList == null || pxmxList.Count == 0)
return;
foreach (var item in pxmxList)
{
if (string.IsNullOrWhiteSpace(item.billingItemId)
&& string.Equals(item.sourceType, "购买", StringComparison.OrdinalIgnoreCase))
{
throw NCCException.Oh($"品项【{item.pxmc ?? "未知"}】缺少开单权益绑定,无法耗卡");
}
}
// 仅校验绑定了开单来源(billingItemId)的行;体验/赠送等非购买行无开单权益来源,跳过
var requestedMap = pxmxList
.Where(x => !string.IsNullOrWhiteSpace(x.billingItemId))
.GroupBy(x => x.billingItemId)
.ToDictionary(g => g.Key, g => g.Sum(x => x.projectNumber ?? 0));
if (requestedMap.Count == 0)
return;
var billingIds = requestedMap.Keys.ToList();
var effective = StatusEnum.有效.GetHashCode();
// 1) 开单品项归属校验(1 条查询):耗卡会员必须与开单品项会员一致
var billingItems = await _db.Queryable()
.Where(x => billingIds.Contains(x.Id) && x.IsEffective == effective)
.Select(x => new { x.Id, x.MemberId, x.Pxmc })
.ToListAsync();
var billingMap = billingItems.ToDictionary(x => x.Id, x => x);
foreach (var billingId in billingIds)
{
if (!billingMap.TryGetValue(billingId, out var bi))
throw NCCException.Oh("开单品项不存在或已作废,无法耗卡");
if (bi.MemberId != memberId)
throw NCCException.Oh($"品项【{bi.Pxmc}】不属于当前会员,禁止跨会员耗卡");
}
// 2) 剩余次数校验(4 条分组聚合查询,与品项数量无关)
var purchasedMap = (await _db.Queryable()
.Where(x => billingIds.Contains(x.Id) && x.IsEffective == effective)
.GroupBy(x => x.Id)
.Select(x => new { Key = x.Id, Total = SqlFunc.AggregateSum(x.ProjectNumber) })
.ToListAsync()).ToDictionary(x => x.Key, x => x.Total);
var consumedMap = (await _db.Queryable()
.Where(x => billingIds.Contains(x.BillingItemId) && x.IsEffective == effective)
.WhereIF(!string.IsNullOrWhiteSpace(excludeConsumeId), x => x.ConsumeInfoId != excludeConsumeId)
.GroupBy(x => x.BillingItemId)
.Select(x => new { Key = x.BillingItemId, Total = SqlFunc.AggregateSum(x.OriginalProjectNumber) })
.ToListAsync()).ToDictionary(x => x.Key, x => x.Total ?? 0);
var refundedMap = (await _db.Queryable()
.Where(x => billingIds.Contains(x.BillingItemId) && x.IsEffective == effective)
.GroupBy(x => x.BillingItemId)
.Select(x => new { Key = x.BillingItemId, Total = SqlFunc.AggregateSum(x.ProjectNumber) })
.ToListAsync()).ToDictionary(x => x.Key, x => x.Total);
var deductMap = (await _db.Queryable()
.Where(x => billingIds.Contains(x.DeductId) && x.IsEffective == effective)
.GroupBy(x => x.DeductId)
.Select(x => new { Key = x.DeductId, Total = SqlFunc.AggregateSum(x.ProjectNumber) })
.ToListAsync()).ToDictionary(x => x.Key, x => x.Total ?? 0);
foreach (var kv in requestedMap)
{
var billingId = kv.Key;
var requested = kv.Value;
var purchased = purchasedMap.TryGetValue(billingId, out var p) ? p : 0m;
var consumed = consumedMap.TryGetValue(billingId, out var c) ? c : 0m;
var refunded = refundedMap.TryGetValue(billingId, out var r) ? r : 0m;
var deduct = deductMap.TryGetValue(billingId, out var d) ? d : 0m;
var remaining = purchased - consumed - refunded - deduct;
if (requested > remaining)
{
var name = billingMap.TryGetValue(billingId, out var bi) ? bi.Pxmc : "该品项";
throw NCCException.Oh($"品项【{name}】剩余次数不足,当前剩余 {remaining} 次,本次需消耗 {requested} 次");
}
}
}
///
/// 作废或变更耗卡后,按剩余有效耗卡重算最近消耗时间与沉睡天数
///
private async Task RefreshMemberLastConsumeAndSleepDaysAsync(string memberId)
{
var member = await _db.Queryable().Where(w => w.Id == memberId).FirstAsync();
if (member == null)
return;
var lastConsumeTime = await _db.Queryable()
.Where(x => x.Hy == memberId && x.IsEffective == StatusEnum.有效.GetHashCode())
.MaxAsync(x => x.Hksj);
MemberInfoUpdateConfig.ApplySleepDaysFromLastConsume(member, lastConsumeTime);
await _db.Updateable(member)
.UpdateColumns(x => new { x.LastConsumeTime, x.SleepDays, x.SleepStartTime })
.ExecuteCommandAsync();
}
///
/// 作废耗卡主档及子表、人次(不改变预约状态)
///
private async Task InvalidateConsumeCardCascadeAsync(LqXhHyhkEntity card, string remarks)
{
card.IsEffective = StatusEnum.无效.GetHashCode();
card.UpdateTime = DateTime.Now;
card.CancelRemark = remarks;
await _db.Updateable(card).ExecuteCommandAsync();
await _db.Updateable()
.SetColumns(it => new LqXhPxmxEntity { IsEffective = StatusEnum.无效.GetHashCode() })
.Where(w => w.ConsumeInfoId == card.Id)
.ExecuteCommandAsync();
await _db.Updateable()
.SetColumns(it => new LqXhJksyjEntity { IsEffective = StatusEnum.无效.GetHashCode() })
.Where(w => w.Glkdbh == card.Id)
.ExecuteCommandAsync();
await _db.Updateable()
.SetColumns(it => new LqXhKjbsyjEntity { IsEffective = StatusEnum.无效.GetHashCode() })
.Where(w => w.Glkdbh == card.Id)
.ExecuteCommandAsync();
await _db.Updateable()
.SetColumns(it => new LqPersonTimesRecordEntity { IsEffective = StatusEnum.无效.GetHashCode() })
.Where(w => w.BusinessId == card.Id)
.ExecuteCommandAsync();
}
///
/// 按预约重开耗卡:预约为「已转耗卡」或「已完成」时可调用;作废旧有效耗卡后按预约时间新开一单,
/// 仅更新预约 ,不改变 。
///
/// 预约主键
/// 与新建耗卡一致(服务端忽略客户端 hksj,以预约时间为准)
/// 新开耗卡主档
[HttpPost("ReopenForAppointment/{appointmentId}")]
public async Task ReopenForAppointment(string appointmentId, [FromBody] LqXhHyhkCrInput input)
{
input ??= new LqXhHyhkCrInput();
input.appointmentId = appointmentId;
var yyjl = await _db.Queryable().FirstAsync(x => x.Id == appointmentId);
if (yyjl == null)
throw NCCException.Oh("预约记录不存在");
if (LqYyjlStatusHelper.NormalizeStored(yyjl.F_Status) == LqYyjlStatusTexts.Cancelled)
throw NCCException.Oh("当前预约状态不允许重开单");
var oldCard = await _db.Queryable()
.Where(x =>
x.AppointmentId == appointmentId && x.IsEffective == StatusEnum.有效.GetHashCode())
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.FirstAsync();
if (oldCard == null)
throw NCCException.Oh("不存在可重开的耗卡");
await ConsumeEditableDaysHelper.EnsureConsumeWithinEditableWindowAsync(_db, oldCard);
ValidateYyjlForReopen(yyjl, oldCard);
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.Hy = string.IsNullOrWhiteSpace(entity.Hy) ? oldCard.Hy : entity.Hy;
entity.Md = string.IsNullOrWhiteSpace(entity.Md) ? oldCard.Md : entity.Md;
entity.Czry = _userManager.UserId;
var memberRow = await _db.Queryable().Where(w => w.Id == entity.Hy).FirstAsync();
if (memberRow == null)
throw NCCException.Oh(ErrorCode.COM1005, "会员不存在");
entity.MemberPhone = memberRow.Sjh;
entity.CreateTime = DateTime.Now;
entity.IsEffective = StatusEnum.有效.GetHashCode();
entity.UpdateTime = DateTime.Now;
entity.OvertimeCoefficient = input.overtimeCoefficient ?? 0;
entity.OriginalSgfy = input.sgfy;
entity.AppointmentId = appointmentId;
var jksOriginalLaborCostSum = input.lqXhPxmxList?
.SelectMany(p => p.lqXhJksyjList ?? Enumerable.Empty())
.Sum(j => j.laborCost ?? 0) ?? 0;
entity.OvertimeSgfy = (decimal)(jksOriginalLaborCostSum * entity.OvertimeCoefficient);
entity.Sgfy = entity.OriginalSgfy + entity.OvertimeSgfy;
var lineHksj = yyjl.Yysj!.Value;
entity.Hksj = lineHksj;
entity.RoomId = yyjl.RoomId;
entity.RoomName = yyjl.RoomName;
try
{
_db.BeginTran();
await InvalidateConsumeCardCascadeAsync(oldCard, "重开单作废");
await PerformConsumeInsertCoreAsync(entity, input, lineHksj, yyjl, applyAppointmentTransferredUpdate: false);
_db.CommitTran();
}
catch (AppFriendlyException)
{
_db.RollbackTran();
throw;
}
catch (Exception ex)
{
_db.RollbackTran();
throw NCCException.Oh(ErrorCode.COM1000, $"重开单失败: {ex.Message}");
}
return await _db.Queryable().FirstAsync(x => x.Id == entity.Id);
}
}
}