public async Task <IActionResult> AddAppointment([FromBody] AddAppointmentRequestDto requestDto) { #region 校验请求参数 DateTime.TryParse(requestDto.StartTime, out var startTime); if (startTime == DateTime.MinValue) { return(Failed(ErrorCode.Empty, "预约时间格式不正确,请修改!")); } if (!string.IsNullOrEmpty(requestDto.Remark)) { if (requestDto.Remark.Length > 500) { return(Failed(ErrorCode.Empty, "备注超过最大长度限制,请修改!")); } } #endregion #region 校验用户账号是否存在 var userModel = new UserBiz().GetUserByPhone(requestDto.Phone); if (userModel == null) { return(Failed(ErrorCode.Empty, $"用户“{requestDto.Phone}”不存在或已禁用!")); } #endregion #region 若服务对象存在则校验 if (!string.IsNullOrEmpty(requestDto.ServiceMemberGuid)) { var members = await new ServiceMemberBiz().GetServiceMemberListAsync(userModel.UserGuid); if (members != null && members.Count > 0) { if (!members.Any(d => d.ServiceMemberGuid == requestDto.ServiceMemberGuid)) { return(Failed(ErrorCode.Empty, "服务对象不存在!")); } } } #endregion #region 校验用户指定卡下服务项目是否可用 var goodsItemBiz = new GoodsItemBiz(); var goodsItemModel = await goodsItemBiz.GetModelAsync(requestDto.FromItemGuid); if (goodsItemModel is null) { return(Failed(ErrorCode.Empty, "抱歉,尚未有可用的卡项")); } #endregion #region 校验商户下项目是否存在 var projectBiz = new ProjectBiz(); var projectModel = await projectBiz.GetMerchantPorjectModelById(UserID, goodsItemModel.ProjectGuid); if (projectModel is null) { return(Failed(ErrorCode.Empty, "服务项目不存在!")); } #endregion var startDate = startTime.AddMinutes(projectModel.OperationTime); requestDto.EndTime = startDate.ToString("HH:mm"); #region 校验用户指定卡是否可用并获取旧卡 var goodBiz = new GoodsBiz(); //按照卡创建时间倒序,优先扣除旧卡使用次数 var goodsModel = await goodBiz.GetAvailableGoods(userModel.UserGuid); if (goodsModel is null || goodsModel.Count <= 0) { return(Failed(ErrorCode.Empty, "抱歉,尚未有可用的卡项")); } //首先获取有过期时间限制的卡,优先扣除使用次数 var goodModel = goodsModel.FirstOrDefault(d => d.EffectiveStartDate.HasValue && d.EffectiveEndDate.HasValue && DateTime.Now < d.EffectiveEndDate.Value); if (goodModel is null) { //若没有过期时间限制则获取第一张卡 goodModel = goodsModel.FirstOrDefault(d => !d.EffectiveStartDate.HasValue && !d.EffectiveEndDate.HasValue); if (goodModel is null) { return(Failed(ErrorCode.UserData, "抱歉,尚未有可用的卡项")); } } #endregion #region 查询排班是否存在或已约满 //预约项目需间隔15分钟,即预约项目成功后随即锁定后续的15分钟时间 int lockTime = 15; var merchantScheduleBiz = new MerchantScheduleBiz(); var scheduleModel = await merchantScheduleBiz.GetModelAsync(requestDto.ScheduleGuid); if (scheduleModel == null) { return(Failed(ErrorCode.Empty, $"服务项目“{projectModel.ProjectName}”无排班信息")); } if (scheduleModel.FullStatus) { return(Failed(ErrorCode.Empty, "排班已约满")); } #endregion #region 查询预约时间段是否已被预约 var merchantScheduleDetaiBiz = new MerchantScheduleDetailBiz(); var occupied = await merchantScheduleDetaiBiz.CheckScheduleDetailOccupied(requestDto.ScheduleGuid, requestDto.StartTime, startDate.AddMinutes(lockTime).ToString("HH:mm")); if (occupied) { return(Failed(ErrorCode.UserData, $"服务时间“{requestDto.StartTime}”不可预约")); } #endregion goodsItemModel.Remain--; goodsItemModel.Used++; goodsItemModel.Available = goodsItemModel.Remain > 0; var consumptionGuid = Guid.NewGuid(); var consumptionModel = new ConsumptionModel { Remark = requestDto.Remark, ConsumptionGuid = consumptionGuid.ToString("N"), ConsumptionNo = BitConverter.ToInt64(consumptionGuid.ToByteArray(), 0).ToString(), UserGuid = userModel.UserGuid, FromItemGuid = goodsItemModel.GoodsItemGuid, ProjectGuid = goodsItemModel.ProjectGuid, AppointmentDate = Convert.ToDateTime(scheduleModel.ScheduleDate.ToString("yyyy-MM-dd") + " " + requestDto.StartTime), MerchantGuid = scheduleModel.MerchantGuid, OperatorGuid = scheduleModel.TargetGuid, PlatformType = scheduleModel.PlatformType, ConsumptionStatus = ConsumptionStatusEnum.Booked.ToString(), CreatedBy = UserID, LastUpdatedBy = UserID, OrgGuid = "GuoDan" }; if (!string.IsNullOrEmpty(requestDto.ServiceMemberGuid)) { consumptionModel.ServiceMemberGuid = requestDto.ServiceMemberGuid; } var merchantScheduleDetailModel = new MerchantScheduleDetailModel { ScheduleDetailGuid = Guid.NewGuid().ToString("N"), ScheduleGuid = requestDto.ScheduleGuid, StartTime = requestDto.StartTime, EndTime = requestDto.EndTime, ConsumptionGuid = string.Empty, CreatedBy = UserID, LastUpdatedBy = UserID, OrgGuid = "GuoDan" }; var lockScheduleDetailModel = (MerchantScheduleDetailModel)null; if (lockTime > 0) { lockScheduleDetailModel = new MerchantScheduleDetailModel { ScheduleDetailGuid = Guid.NewGuid().ToString("N"), ScheduleGuid = requestDto.ScheduleGuid, StartTime = requestDto.EndTime, EndTime = startDate.AddMinutes(lockTime).ToString("HH:mm"), ConsumptionGuid = string.Empty, CreatedBy = UserID, LastUpdatedBy = UserID, OrgGuid = "GuoDan" }; } var result = await new ConsumptionBiz().MakeAnAppointmentWithConsumption(consumptionModel, merchantScheduleDetailModel, goodsItemModel, lockScheduleDetailModel); return(!result?Failed(ErrorCode.DataBaseError, "预约失败") : Success()); }
/// <summary> /// 更新ConsumptionModel /// </summary> /// <param name="model">Model</param> /// <returns></returns> public async Task <bool> CancelOppointment(ConsumptionModel model, GoodsItemModel goodsItemModel, MerchantScheduleDetailModel msdModel) { var result = await MySqlHelper.TransactionAsync(async (conn, tran) => { //消费预约信息 if (await conn.UpdateAsync(model) < 1) { return(false); } //撤回已扣除数量 if (await conn.UpdateAsync(goodsItemModel) < 1) { return(false); } //取消预约后,若个人商品处于不可用状态时,更新个人商品为可用状态 await conn.ExecuteAsync("update t_consumer_goods set available=1 where goods_guid=@goodsGuid and available=0", new { goodsGuid = goodsItemModel.GoodsGuid }); //去除预约所占的时间 await conn.DeleteAsync <MerchantScheduleDetailModel>(msdModel.LockDetailGuid); await conn.DeleteAsync(msdModel); return(true); }); return(result); }
/// <summary> /// 变更预约时间 /// </summary> /// <param name="consumptionModel"></param> /// <param name="deleteTimes"></param> /// <param name="merchantScheduleDetailModel"></param> /// <param name="lockScheduleDetailModel"></param> /// <returns></returns> public async Task <bool> AmendAppointmentAsync(ConsumptionModel consumptionModel, List <string> deleteTimes, MerchantScheduleDetailModel merchantScheduleDetailModel, MerchantScheduleDetailModel lockScheduleDetailModel = null) { return(await MySqlHelper.TransactionAsync(async (conn, tran) => { var sql = @"UPDATE t_merchant_schedule_detail a LEFT JOIN t_merchant_schedule_detail b ON a.schedule_guid = b.schedule_guid AND b.`enable` = 1 AND a.schedule_detail_guid <> b.schedule_detail_guid AND b.last_updated_date < a.last_updated_date AND ( ( a.start_time >= b.start_time AND a.start_time<b.end_time ) OR ( a.end_time > b.start_time AND a.end_time<=b.end_time ) ) SET a.consumption_guid = @consumptionGuid $lockUpdate$ WHERE a.schedule_detail_guid = @scheduleDetailGuid AND a.`enable` = 1 AND b.schedule_detail_guid IS NULL;" ; if (lockScheduleDetailModel != null) { //插入预约排班明细(项目间隔锁定时间) if (string.IsNullOrEmpty(await conn.InsertAsync <string, MerchantScheduleDetailModel>(lockScheduleDetailModel))) { return false; } //检查预约时间是否可行(项目间隔锁定时间,有无时间交叉?) if ((await conn.ExecuteAsync(sql.Replace("$lockUpdate$", ""), new { scheduleDetailGuid = lockScheduleDetailModel.ScheduleDetailGuid, consumptionGuid = "Lock" })) == 0) { return false; } } //插入预约排班明细 if (string.IsNullOrEmpty(await conn.InsertAsync <string, MerchantScheduleDetailModel>(merchantScheduleDetailModel))) { return false; } //检查预约时间是否可行(有无时间交叉?) if ((await conn.ExecuteAsync(sql.Replace("$lockUpdate$", $",a.lock_detail_guid='{lockScheduleDetailModel?.ScheduleDetailGuid}'"), new { scheduleDetailGuid = merchantScheduleDetailModel.ScheduleDetailGuid, consumptionGuid = consumptionModel.ConsumptionGuid })) == 0) { return false; } //更新消费记录 if ((await conn.UpdateAsync(consumptionModel) != 1)) { return false; } //删除旧的时间占用记录 var delRes = await conn.DeleteListAsync <MerchantScheduleDetailModel>("where schedule_detail_guid in @deleteTimes", new { deleteTimes }); return true; })); }
/// <summary> /// 消费预约 /// </summary> /// <param name="consumptionModel">消费预约model</param> /// <param name="merchantScheduleDetailModel">预约时间明细model</param> /// <param name="item">个人卡项model</param> /// <param name="lockScheduleDetailModel">预约锁定时间明细model</param> /// <returns></returns> public async Task <bool> MakeAnAppointmentWithConsumption(ConsumptionModel consumptionModel, MerchantScheduleDetailModel merchantScheduleDetailModel, GoodsItemModel item, MerchantScheduleDetailModel lockScheduleDetailModel = null) { return(await MySqlHelper.TransactionAsync(async (conn, tran) => { var lockUpdate = lockScheduleDetailModel; var sql = @"UPDATE t_merchant_schedule_detail a LEFT JOIN t_merchant_schedule_detail b ON a.schedule_guid = b.schedule_guid AND b.`enable` = 1 AND a.schedule_detail_guid <> b.schedule_detail_guid AND b.last_updated_date < a.last_updated_date AND ( ( a.start_time >= b.start_time AND a.start_time<b.end_time ) OR ( a.end_time > b.start_time AND a.end_time<=b.end_time ) ) SET a.consumption_guid = @consumptionGuid $lockUpdate$ WHERE a.schedule_detail_guid = @scheduleDetailGuid AND a.`enable` = 1 AND b.schedule_detail_guid IS NULL;" ; if (lockScheduleDetailModel != null) { //插入预约排班明细(项目间隔锁定时间) if (string.IsNullOrEmpty(await conn.InsertAsync <string, MerchantScheduleDetailModel>(lockScheduleDetailModel))) { return false; } //检查预约时间是否可行(项目间隔锁定时间,有无时间交叉?) if ((await conn.ExecuteAsync(sql.Replace("$lockUpdate$", ""), new { scheduleDetailGuid = lockScheduleDetailModel.ScheduleDetailGuid, consumptionGuid = "Lock" })) == 0) { return false; } } //插入预约排班明细 if (string.IsNullOrEmpty(await conn.InsertAsync <string, MerchantScheduleDetailModel>(merchantScheduleDetailModel))) { return false; } //检查预约时间是否可行(有无时间交叉?) if ((await conn.ExecuteAsync(sql.Replace("$lockUpdate$", $",a.lock_detail_guid='{lockScheduleDetailModel?.ScheduleDetailGuid}'"), new { scheduleDetailGuid = merchantScheduleDetailModel.ScheduleDetailGuid, consumptionGuid = consumptionModel.ConsumptionGuid })) == 0) { return false; } //插入消费记录 if (string.IsNullOrEmpty(await conn.InsertAsync <string, ConsumptionModel>(consumptionModel))) { return false; } if ((await conn.UpdateAsync(item) != 1)) { return false; } return true; })); }