public async Task <IActionResult> CreateAppointmentAsync( [FromBody] AddAppointmentRequestDto addAppointmentRequestDto, CancellationToken token = default) { try { var appointmentServiceObject = Mapper.Map <AppointmentServiceObject>(addAppointmentRequestDto); var serviceResult = await _appointmentService.CreateAppointmentAsync(appointmentServiceObject, token); return(new CreatedResult(string.Empty, Mapper.Map <AddAppointmentResponseDto>(serviceResult))); } catch (BadRequestException e) { return(new BadRequestObjectResult(e.Error)); } }
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()); }
public async Task <IActionResult> AddAppointmentAsync([FromBody] AddAppointmentRequestDto requestDto) { var doctorModel = await new DoctorBiz().GetAsync(requestDto.DoctorGuid); if (doctorModel == null) { return(Failed(ErrorCode.UserData, "未查询到此医生")); } if (!doctorModel.Enable || string.Equals(doctorModel.Status, StatusEnum.Draft.ToString(), StringComparison.OrdinalIgnoreCase)) { return(Failed(ErrorCode.UserData, "未查询到此医生")); } var scheduleModel = await new DoctorScheduleBiz().GetAsync(requestDto.ScheduleGuid); if (scheduleModel == null || !scheduleModel.Enable) { return(Failed(ErrorCode.UserData, "未查询到此排班信息")); } if (scheduleModel.ScheduleDate.Date < DateTime.Now.Date || scheduleModel.ScheduleDate.Date > DateTime.Now.AddDays(6)) { return(Failed(ErrorCode.UserData, "此时间段排班目前无法预约")); } var patientMemberModel = await new PatientMemberBiz().GetAsync(requestDto.PatientGuid); if (patientMemberModel == null || !patientMemberModel.Enable || patientMemberModel.UserGuid != UserID) { return(Failed(ErrorCode.UserData, "未查询到就诊人信息")); } //判断当前就诊人是否已经预约过 var checkPatient = await new DoctorAppointmentBiz().GetDoctorAppointmentAsync(requestDto.DoctorGuid, scheduleModel.ScheduleDate, patientMemberModel.PatientGuid); if (checkPatient != null) { return(Failed(ErrorCode.UserData, "就诊人当天已经预约过该医生")); } //查询当前用户是否允许当天挂号 ConsumerModel consumerModel = await new ConsumerBiz().GetModelAsync(UserID); if (consumerModel != null) { if (consumerModel.NoAppointmentDate.HasValue && consumerModel.NoAppointmentDate > DateTime.Now.Date) { return(Failed(ErrorCode.UserData, "您在一个月内有连续爽约或取消超过3次的记录" + consumerModel.NoAppointmentDate.Value.ToString("yyyy-MM-dd") + "才可再次预约")); } } //检查累计是否超过二次连续取消或者爽约 var checkList = await new DoctorAppointmentBiz().GetThreeMonthsDoctorAppointmentListAsync(UserID); if (checkList != null) { List <DateTime> resultList = new List <DateTime>(); var group = checkList.GroupBy(s => s.CreationDate.ToString("yyyy-MM")); foreach (var items in group) { foreach (var item in items) { if (item.Status == AppointmentStatusEnum.Cancel.ToString() || item.Status == AppointmentStatusEnum.Miss.ToString() || (item.Status == AppointmentStatusEnum.Waiting.ToString() && DateTime.Now.Date > item.AppointmentTime.Date)) { resultList.Add(item.AppointmentTime.Date); } else { resultList.Clear(); } if (resultList.Count >= 3 && DateTime.Now.Date < resultList.LastOrDefault().Date.AddMonths(3)) { return(Failed(ErrorCode.UserData, "您在一个月内有连续爽约或取消超过3次的记录" + resultList.LastOrDefault().Date.AddMonths(3).ToString("yyyy-MM-dd") + "才可再次预约")); } } } } var doctorWorkshiftDetailModel = await new DoctorWorkshiftDetailBiz().GetAsync(scheduleModel.WorkshiftDetailGuid); var appointmentTime = Convert.ToDateTime($"{scheduleModel.ScheduleDate.ToString("yyyy-MM-dd")} {doctorWorkshiftDetailModel.StartTime.ToString()}"); var appointmentEndTime = Convert.ToDateTime($"{scheduleModel.ScheduleDate.ToString("yyyy-MM-dd")} {doctorWorkshiftDetailModel.EndTime.ToString()}"); if (DateTime.Now >= appointmentEndTime) { return(Failed(ErrorCode.UserData, "当前时间不可大于就诊截止时间,请重新挂号")); } var model = new DoctorAppointmentModel { AppointmentGuid = Guid.NewGuid().ToString("N"), HospitalGuid = doctorModel.HospitalGuid, UserGuid = UserID, AppointmentNo = "", ScheduleGuid = requestDto.ScheduleGuid, DoctorGuid = requestDto.DoctorGuid, OfficeGuid = doctorModel.OfficeGuid, AppointmentTime = appointmentTime, AppointmentDeadline = appointmentEndTime, PatientGuid = patientMemberModel.PatientGuid, PatientName = patientMemberModel.Name, PatientPhone = patientMemberModel.Phone, PatientGender = patientMemberModel.Gender, PatientBirthday = patientMemberModel.Birthday, PatientCardno = patientMemberModel.CardNo, PatientRelationship = patientMemberModel.Relationship, Status = AppointmentStatusEnum.Waiting.ToString(), CreatedBy = UserID, LastUpdatedBy = UserID, OrgGuid = string.Empty }; if (!string.IsNullOrWhiteSpace(requestDto.Phone)) { model.PatientPhone = requestDto.Phone; } var result = await new DoctorAppointmentBiz().CreateAppointmentAsync(model, doctorWorkshiftDetailModel.AppointmentNoPrefix, false); if (!result) { return(Failed(ErrorCode.UserData, "挂号失败")); } var doctorAppointmentModel = await new DoctorAppointmentBiz().GetAppointmentAsync(model.AppointmentGuid); if (doctorAppointmentModel == null) { return(Failed(ErrorCode.UserData, "挂号失败")); } return(Success(doctorAppointmentModel)); }