public async Task <IActionResult> GetAPrescriptionDataAsync([FromQuery] GetCollectionRequest requestDto)
        {
            var biz    = new PrescriptionInformationBiz();
            var result = await biz.GetAPrescriptionDataAsync(requestDto.IsMonths, requestDto.StartDate, requestDto.EndDate, UserID, PrescriptionStatusEnum.Paied);

            return(Success(result));
        }
        public async Task <IActionResult> CreatePrescription([FromBody] ProcessPrescriptionRequestDto request)
        {
            Logger.Info($"{nameof(CreatePrescription)}: {JsonConvert.SerializeObject(request)}");

            var context = new PrescriptionContext(request);

            var appointmentBiz = new DoctorAppointmentBiz();
            var appointment    = await appointmentBiz.GetAsync(request.AppointmentGuid);

            if (appointment is null)
            {
                return(Failed(ErrorCode.Empty, "预约不存在,请检查"));
            }

            var informationBiz          = new PrescriptionInformationBiz();
            var prescriptionInformation = await informationBiz.ExistInformation(appointment.AppointmentGuid);

            if (prescriptionInformation != null)
            {
                return(Failed(ErrorCode.Empty, "该处方已提交,请勿重复提交"));
            }
            context.AppointmentModel = appointment;

            var validateResult = Validate(context);

            if (validateResult.Code != ErrorCode.Success)
            {
                return(validateResult);
            }
            context.AppointmentModel.Status = AppointmentStatusEnum.Treated.ToString();

            var prescriptionBiz = new PrescriptionBiz();
            var result          = await prescriptionBiz.CreatePrescription(context);

            if (!result)
            {
                return(Failed(ErrorCode.DataBaseError, "创建处方记录失败,请稍后重试"));
            }

            var response = new SubmitPrescriptionSuccessResponseDto
            {
                InformationGuid     = context.InformationModel.InformationGuid,
                PrescriptionSuccess = context.PrescriptionModels.Select(d => new SubmitPrescriptionSuccessItemDto()
                {
                    PrescriptionGuid = d.PrescriptionGuid,
                    PrescriptionName = d.PrescriptionName
                })
            };

            return(Success(response));
        }
        public async Task <IActionResult> GetCollectionDataync([FromQuery] GetCollectionRatioRequest requestDto)
        {
            var biz = new PrescriptionInformationBiz();
            //已收款
            var collectionTotal = await biz.GetAPrescriptionMoneyAsync(requestDto.StartDate.Date, requestDto.EndDate.Date.AddDays(1).AddSeconds(-1), UserID, PrescriptionStatusEnum.Paied);

            //未收款
            var unCollectionTotal = await biz.GetAPrescriptionMoneyAsync(requestDto.StartDate, requestDto.EndDate.Date.AddDays(1).AddSeconds(-1), UserID, PrescriptionStatusEnum.Obligation);

            GetCollectionRatioResponse result = new GetCollectionRatioResponse
            {
                Receivable  = collectionTotal,
                Uncollected = unCollectionTotal
            };

            return(Success(result));
        }
        public async Task <IActionResult> GetCollectionTodayAsync()
        {
            GetMoneyTodayResponseDto result = new GetMoneyTodayResponseDto();
            var biz = new PrescriptionInformationBiz();
            var collectionTodayTotal = await biz.GetAPrescriptionMoneyAsync(DateTime.Now.Date, DateTime.Now.Date.AddDays(1).AddSeconds(-1), UserID, PrescriptionStatusEnum.Paied);

            var yesterdayCollectionTotal = await biz.GetAPrescriptionMoneyAsync(DateTime.Now.Date.AddDays(-1), DateTime.Now.Date.AddSeconds(-1), UserID, PrescriptionStatusEnum.Paied);

            var increase = yesterdayCollectionTotal > 0 ? ((collectionTodayTotal - yesterdayCollectionTotal) / yesterdayCollectionTotal) : 1M;

            if (collectionTodayTotal == 0 && yesterdayCollectionTotal == 0)
            {
                increase = 0M;
            }
            result.Collected = new GetCollectionTodayResponseDto
            {
                AmountCollected = collectionTodayTotal,
                Increase        = Math.Round(increase, 2) * 100
            };
            var collectionUnTodayTotal = await biz.GetAPrescriptionMoneyAsync(DateTime.Now.Date, DateTime.Now.Date.AddDays(1).AddSeconds(-1), UserID, PrescriptionStatusEnum.Obligation);

            var yesterdayUnCollectionTotal = await biz.GetAPrescriptionMoneyAsync(DateTime.Now.Date.AddDays(-1), DateTime.Now.Date.AddSeconds(-1), UserID, PrescriptionStatusEnum.Obligation);

            var increaseUn = yesterdayUnCollectionTotal > 0 ? ((collectionUnTodayTotal - yesterdayUnCollectionTotal) / yesterdayUnCollectionTotal) : 1M;

            if (collectionUnTodayTotal == 0 && yesterdayUnCollectionTotal == 0)
            {
                increaseUn = 0M;
            }
            result.Uncollected = new GetUncollectedTodayResponseDto
            {
                AmountUncollected = collectionUnTodayTotal,
                Increase          = Math.Round(increaseUn, 2) * 100
            };
            if (collectionTodayTotal != 0 || collectionUnTodayTotal != 0)
            {
                result.Collected.Percentage   = Math.Round(collectionTodayTotal / (collectionTodayTotal + collectionUnTodayTotal), 2) * 100;
                result.Uncollected.Percentage = Math.Round(collectionUnTodayTotal / (collectionTodayTotal + collectionUnTodayTotal), 2) * 100;
            }
            return(Success(result));
        }
        public async Task <IActionResult> GetCollectionToMonthsAndYearAsync()
        {
            GetMoneyMonthsAndYearResponseDto result = new GetMoneyMonthsAndYearResponseDto();
            var biz = new PrescriptionInformationBiz();
            //本月
            DateTime sDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            var      collectionTodayTotal = await biz.GetAPrescriptionMoneyAsync(sDate, DateTime.Now, UserID, PrescriptionStatusEnum.Paied);

            var yesterdayCollectionTotal = await biz.GetAPrescriptionMoneyAsync(sDate.AddMonths(-1), DateTime.Now.AddMonths(-1), UserID, PrescriptionStatusEnum.Paied);

            var increase = yesterdayCollectionTotal > 0 ? ((collectionTodayTotal - yesterdayCollectionTotal) / yesterdayCollectionTotal) : 1M;

            if (collectionTodayTotal == 0 && yesterdayCollectionTotal == 0)
            {
                increase = 0M;
            }
            result.MonthsCollected = new GetCollectionMonthsResponseDto
            {
                AmountCollected = collectionTodayTotal,
                Increase        = Math.Round(increase, 2) * 100
            };
            //本年
            DateTime sDateYear = new DateTime(DateTime.Now.Year, 1, 1);
            var      collectionUnTodayTotal = await biz.GetAPrescriptionMoneyAsync(sDateYear, DateTime.Now, UserID, PrescriptionStatusEnum.Paied);

            var yesterdayUnCollectionTotal = await biz.GetAPrescriptionMoneyAsync(sDateYear.AddYears(-1), DateTime.Now.AddYears(-1), UserID, PrescriptionStatusEnum.Paied);

            var increaseUn = yesterdayUnCollectionTotal > 0 ? ((collectionUnTodayTotal - yesterdayUnCollectionTotal) / yesterdayUnCollectionTotal) : 1M;

            if (collectionUnTodayTotal == 0 && yesterdayUnCollectionTotal == 0)
            {
                increaseUn = 0M;
            }
            result.YearCollected = new GetCollectionYearResponseDto
            {
                AmountCollected = collectionUnTodayTotal,
                Increase        = Math.Round(increaseUn, 2) * 100
            };
            return(Success(result));
        }
        public async Task <IActionResult> CancellPrescription([FromBody] CancellPrescriptionRequestDto request)
        {
            var prescriptionBiz = new PrescriptionBiz();

            var prescription = await prescriptionBiz.GetAsync(request.PrescriptionGuid);

            if (prescription is null)
            {
                return(Failed(ErrorCode.Empty, "处方记录不存在,请检查"));
            }

            var name = prescription.PrescriptionName;

            if (prescription.Status == PrescriptionStatusEnum.Paied.ToString())
            {
                return(Failed(ErrorCode.Empty, $"处方【{name}】已完成付款不可作废"));
            }

            if (prescription.Status == PrescriptionStatusEnum.Cancellation.ToString())
            {
                return(Failed(ErrorCode.Empty, $"处方【{name}】已作废,请勿重复提交"));
            }

            prescription.Status          = PrescriptionStatusEnum.Cancellation.ToString();
            prescription.LastUpdatedBy   = UserID;
            prescription.LastUpdatedDate = DateTime.Now;
            prescription.Reason          = request.Reason;

            var informationBiz = new PrescriptionInformationBiz();
            var information    = await informationBiz.GetAsync(prescription.InformationGuid);

            if (information is null)
            {
                return(Failed(ErrorCode.Empty, "处方记录不存在"));
            }

            lock (locker)
            {
                information.TotalCost -= prescription.TotalCost;
            }

            var context = new PrescriptionContext(null)
            {
                InformationModel = information
            };

            var prescriptionModels = await prescriptionBiz.GetAllPrescriptionsAsync(information.InformationGuid, prescription.PrescriptionGuid);

            context.dbPrescriptionModels = prescriptionModels;

            UpdatePrescriptionInformationPaidStatus(context);

            var result = await prescriptionBiz.CancellPrescription(context.InformationModel, prescription);

            if (!result)
            {
                return(Failed(ErrorCode.DataBaseError, $"作废处方【{prescription.PrescriptionName}】失败"));
            }

            return(Success());
        }
        public async Task <IActionResult> UpdatePrescription([FromBody]
                                                             ProcessPrescriptionRequestDto request)
        {
            if (string.IsNullOrEmpty(request.InformationGuid))
            {
                return(Failed(ErrorCode.Empty, "预约不存在,请检查"));
            }

            Logger.Info($"{nameof(UpdatePrescription)}: {JsonConvert.SerializeObject(request)}");

            var context = new PrescriptionContext(request);

            var appointmentBiz = new DoctorAppointmentBiz();
            var appointment    = await appointmentBiz.GetAsync(request.AppointmentGuid);

            if (appointment is null)
            {
                return(Failed(ErrorCode.Empty, "预约不存在,请检查"));
            }
            context.AppointmentModel = appointment;

            var informationBiz = new PrescriptionInformationBiz();
            var information    = await informationBiz.GetAsync(request.InformationGuid);

            if (information is null)
            {
                return(Failed(ErrorCode.Empty, "用户信息不存在,请检查"));
            }

            if (!information.AppointmentGuid.Equals(request.AppointmentGuid))
            {
                return(Failed(ErrorCode.Empty, "预约记录和用户信息不一致,无法操作"));
            }

            context.InformationModel = information;

            var prescriptionBiz = new PrescriptionBiz();

            var prescriptionModels = await prescriptionBiz.GetAllPrescriptionsAsync(information.InformationGuid);

            prescriptionModels = prescriptionModels.Where(d =>
                                                          d.Status != PrescriptionStatusEnum.Cancellation.ToString()).ToList();

            context.dbPrescriptionModels = prescriptionModels;

            var validateResult = Validate(context);

            if (validateResult.Code != ErrorCode.Success)
            {
                return(validateResult);
            }

            context.dbPrescriptionModels = prescriptionModels
                                           .Concat(context.PrescriptionModels).ToList();

            UpdatePrescriptionInformationPaidStatus(context);

            var result = await prescriptionBiz.UpdatePrescription(context);

            if (!result)
            {
                return(Failed(ErrorCode.DataBaseError, "更新处方记录失败"));
            }

            var prescriptions = prescriptionModels.Where(d => d.Status != PrescriptionStatusEnum.Cancellation.ToString())
                                .OrderBy(d => d.CreationDate)
                                .Concat(context.PrescriptionModels)
                                .Select(d => new SubmitPrescriptionSuccessItemDto()
            {
                PrescriptionGuid = d.PrescriptionGuid,
                PrescriptionName = d.PrescriptionName
            });

            var response = new SubmitPrescriptionSuccessResponseDto()
            {
                InformationGuid     = information.InformationGuid,
                PrescriptionSuccess = prescriptions
            };

            return(Success(response));
        }