Exemple #1
0
        public MassPriceChangeTask StartChangePriceTask(MassPriceChange change)
        {
            if (change.Increase == false && change.Value >= 100)
            {
                throw new Exception("Нельзя уменьшить стоимость более чем на 99 процентов");
            }
            if (change.Value <= 0)
            {
                throw new Exception("Процент должен быть положительным числом больше 0");
            }

            int taskId;
            var task = new MassPriceChangeTask()
            {
                Comment      = change.Comment,
                CreationDate = DateTime.UtcNow,
                Description  = change.GetDescription(),
                State        = MassPriceChangeTaskState.Processed
            };

            var rec = Mapper.Map <Data.Models.MassPriceChangeTask>(task);

            taskId = task.TaskId = _massPriceChangeTaskGateway.Insert(rec);

            Task t = new Task(() => ChangePrice(change, taskId));

            t.Start();

            return(task);
        }
Exemple #2
0
 public ResultModel ChangePrice(MassPriceChange change)
 {
     try
     {
         var task = _massChangeService.StartChangePriceTask(change);
         return(new ResultModel(true, task));
     }
     catch (Exception e)
     {
         return(new ResultModel(false, e.Message));
     }
 }
Exemple #3
0
        private void ChangePrice(MassPriceChange change, int taskId)
        {
            using (var scope = new TransactionScope())
            {
                var filter = change.Filter;
                {
                    try
                    {
                        IList <GoodsItem> goods;
                        int total;
                        FilterCollections filterCollections;
                        goods = _shopService.GetGoodsSet(filter, 1, int.MaxValue, out total, out filterCollections, false);
                        foreach (var goodsItem in goods)
                        {
                            decimal dt  = goodsItem.Price * change.Value / 100m;
                            decimal val = change.Increase ? goodsItem.Price + dt : goodsItem.Price - dt;
                            goodsItem.Price = Math.Round(val, 0, MidpointRounding.AwayFromZero);

                            foreach (var pack in goodsItem.Packs)
                            {
                                if (!pack.Price.HasValue)
                                {
                                    continue;
                                }
                                dt         = pack.Price.Value * change.Value / 100m;
                                val        = change.Increase ? pack.Price.Value + dt : pack.Price.Value - dt;
                                pack.Price = Math.Round(val, 0, MidpointRounding.AwayFromZero);
                            }
                            _shopService.UpdateGoods(goodsItem);
                        }
                        var task = _massPriceChangeTaskGateway.SelectOne(taskId);
                        task.State = MassPriceChangeTaskState.Completed.ToString();
                        _massPriceChangeTaskGateway.Update(task);
                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        var task = _massPriceChangeTaskGateway.SelectOne(taskId);
                        _logService.LogError(e);
                        task.State = MassPriceChangeTaskState.Error.ToString();
                    }
                }
            }
        }