コード例 #1
0
        /// <summary>
        /// 추가금을 합한 총액을 원하는 비중에 따른 분배
        /// </summary>
        /// <param name="before"></param>
        /// <param name="additional"></param>
        /// <returns></returns>
        private static List <ReblancingItemReq> SetByWeight(List <ReblancingItemReq> before, decimal additional)
        {
            List <ReblancingItemReq> result = new List <ReblancingItemReq>();
            decimal originalSum             = 0;
            decimal weightSum = 0;

            // 총 합 구함 + additionalMoney
            if (before?.Any() ?? false)
            {
                originalSum = SumPriceQty(before) + additional;
                weightSum   = SumWeight(before);

                // 나머지 내림 분배
                foreach (ReblancingItemReq entity in before)
                {
                    if (entity != null)
                    {
                        ReblancingItemReq one = new ReblancingItemReq();
                        one.Name         = entity.Name;
                        one.CurrentPrice = entity.CurrentPrice;
                        one.TargetWeight = entity.TargetWeight;
                        if (entity.TargetWeight != 0 && entity.CurrentPrice != 0)
                        {
                            one.CurrentQty = (int)Math.Floor((originalSum * entity.TargetWeight / weightSum) / one.CurrentPrice);
                            result.Add(one);
                        }
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 나머지 금액을 내림차순으로 최대한 분배
        /// </summary>
        /// <param name="before"></param>
        /// <param name="rest"></param>
        /// <returns></returns>
        private static List <ReblancingItemReq> SetByRest(List <ReblancingItemReq> before, decimal rest, RestOptionTypes restOptionType)
        {
            List <ReblancingItemReq> result = new List <ReblancingItemReq>();

            switch (restOptionType)
            {
            case RestOptionTypes.Unknown:
            case RestOptionTypes.MinRest:
            {
                // 큰 가격순 정렬
                if (before?.Any() ?? false)
                {
                    before = before.OrderByDescending(x => x.CurrentPrice).ToList();

                    foreach (ReblancingItemReq entity in before)
                    {
                        ReblancingItemReq one = new ReblancingItemReq();
                        one.Name         = entity.Name;
                        one.CurrentPrice = entity.CurrentPrice;
                        one.TargetWeight = entity.TargetWeight;

                        if (entity.CurrentPrice < rest)
                        {
                            int plusQty = (int)(rest / entity.CurrentPrice);
                            one.CurrentQty = entity.CurrentQty + plusQty;
                            rest           = rest - (entity.CurrentPrice * plusQty);
                        }
                        else
                        {
                            one.CurrentQty = entity.CurrentQty;
                        }

                        result.Add(one);
                    }
                }
            }
            break;
            }

            return(result);
        }