Exemplo n.º 1
0
        private string ValidNewOrderDetails(List <OrderDetailsCreateDto> details, SchoolUser user)
        {
            var errors = new List <string>();

            foreach (var detail in details)
            {
                var error = ValidNewOrderDetail(detail);
                if (!string.IsNullOrWhiteSpace(error))
                {
                    errors.Add(error);
                }
            }
            return(string.Join(";", errors));
        }
Exemplo n.º 2
0
        public Result Create(OrderCreateDto entity, SchoolUser user)
        {
            string error;

            #region 检验 订单
            error = ValidNewOrder(entity, user);
            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.Fail(error));
            }
            #endregion

            #region 检验订单明细
            error = ValidNewOrderDetails(entity.Details, user);
            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.Fail(error));
            }
            #endregion

            #region 检验订单金额
            int originTotal = entity.Details.Sum(i => i.OriginUnitPrice * i.GoodsCount);
            int closingCost = entity.Details.Sum(i => i.ClosingUnitPrice * i.GoodsCount);

            if (closingCost != entity.ClosingCost)
            {
                return(ResultUtil.Fail("订单总额与订单明细不匹配"));
            }
            #endregion

            var order = Mapper.Map <OrderEntity>(entity);
            order.SchoolId = user.SchoolId;
            order.BeforeCreate(user);

            List <OrderDetailsEntity> details = new List <OrderDetailsEntity>();
            using (var conn = db.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        // 创建订单
                        var orderId = db.Create(order);

                        // 创建明细
                        foreach (var detail in entity.Details)
                        {
                            var temp = Mapper.Map <OrderDetailsEntity>(detail);
                            temp.OrderId = orderId;
                            temp.BeforeCreate(user);
                            details.Add(temp);
                        }
                        db.Create(details);

                        if (order.ActualPayment > 0)
                        {
                            // 创建付款记录
                            var payment = new PaymentEntity
                            {
                                SchoolId      = user.SchoolId,
                                StudentId     = entity.StudentId,
                                StudentName   = entity.StudentName,
                                OrderId       = orderId,
                                PaymentMethod = entity.PaymentMethod,
                                PaymentAmount = entity.ActualPayment
                            };
                            payment.BeforeCreate(user);

                            db.Create(payment);
                        }

                        trans.Commit();
                        return(ResultUtil.Success());
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        return(ResultUtil.Exception(ex));
                    }
                }
            }
        }