public async Task <int> Surrender(SurrenderDto input)
        {
            CheckUpdatePermission();

            int surrenderNum = 0;

            input.InsurancePolicyIds.ForEach(x =>
            {
                var insurancePolicy = Repository.FirstOrDefault(p => p.Id == x);

                CheckInsurancePolicy(insurancePolicy);

                var insuranceDetails = _insuranceDetailRepository.GetAll().Where(d => d.InsurancePolicyId == x).ToList();
                if (insurancePolicy == null || insuranceDetails == null || insuranceDetails.Count <= 0)
                {
                    throw new UserFriendlyException("保单资料异常");
                }
                var percent = Convert.ToDecimal(Math.Round((input.EndTime.ToLocalTime().Date - insurancePolicy.StartTime.ToLocalTime().Date).TotalDays) / Math.Round((insurancePolicy.EndTime.ToLocalTime().Date - insurancePolicy.StartTime.ToLocalTime().Date).TotalDays));
                for (int i = 0; i < insuranceDetails.Count; i++)
                {
                    var OldInsuranceJson = JsonConvert.SerializeObject(insuranceDetails[i]);
                    insuranceDetails[i].OriginalAmount                = insuranceDetails[i].OriginalAmount * percent;
                    insuranceDetails[i].TransactionAmount             = insuranceDetails[i].TransactionAmount * percent;
                    insuranceDetails[i].NoDeductibleOriginalAmount    = insuranceDetails[i].NoDeductibleOriginalAmount * percent;
                    insuranceDetails[i].NoDeductibleTransactionAmount = insuranceDetails[i].NoDeductibleTransactionAmount * percent;
                    insuranceDetails[i] = UpdateInsuranceCalculation(insuranceDetails[i]);
                    _insuranceLogRepository.Insert(new InsuranceLog
                    {
                        InsuranceOperationType = InsuranceOperationType.Surrender,
                        InsuranceDetailId      = insuranceDetails[i].Id,
                        SerialNumber           = input.SerialNumber,
                        OldInsuranceJson       = OldInsuranceJson,
                        NewInsuranceJson       = JsonConvert.SerializeObject(insuranceDetails[i])
                    });
                }
                insurancePolicy.EndTime = input.EndTime.ToLocalTime();
                surrenderNum++;
            });

            await CurrentUnitOfWork.SaveChangesAsync();

            return(surrenderNum);
        }
Example #2
0
        public async Task <IActionResult> Surrender(
            [FromServices] IMediator mediator,
            [FromBody] SurrenderDto surrender)
        {
            GetCoinsResponse resultQuery = await mediator.Send(new GetCoinsQuery());

            var coins = new List <Coins>(resultQuery.Coins).OrderByDescending(x => x.Par).ToList();

            int index = 0;

            while (surrender.Surrender != 0)
            {
                if (index > coins.Count - 1)
                {
                    break;
                }

                if (coins[index].Count > 0 && surrender.Surrender >= coins[index].Par)
                {
                    surrender.Surrender -= coins[index].Par;
                    coins[index].Count  -= 1;
                }
                else
                {
                    index++;
                }
            }

            foreach (var coin in coins)
            {
                var command = new UpdateCoinCommand(coin);
                await mediator.Send(command);
            }

            return(Ok(surrender));
        }