public NormalResult DistributeCouponToMember(DistributeCouponToMemberArgs args) { //临时方案,先把所有会员信息拉出来 using (Entities db = new Entities()) { Coupon_Info coupon = db.Coupon_Info.FirstOrDefault(c => c.id == args.CouponId); if (coupon == null) { return(new NormalResult("指定的券不存在。")); } if (coupon.is_lose_valid == 1) { return(new NormalResult("指定的券已失效。")); } if (coupon.end_time <= DateTime.Now) { return(new NormalResult("指定的券已超过最后使用时限。")); } Member member = db.Member.FirstOrDefault(c => c.id == args.MemberId); if (member == null) { return(new NormalResult("指定的会员不存在。")); } Member_Coupon_Account couponAccount = new Member_Coupon_Account(); couponAccount.member_id = member.id; couponAccount.is_use = 0; couponAccount.coupon_id = coupon.id; couponAccount.create_time = DateTime.Now; db.Member_Coupon_Account.Add(couponAccount); db.SaveChanges(); return(new NormalResult()); } }
public NormalResult CreateOrder(Product_Order_Master order) { if (order.Product_Order_Detail == null || order.Product_Order_Detail.Count == 0) { return(new NormalResult("订单明细数据无效。")); } //product.Id = Guid.NewGuid(); using (Entities db = new Entities()) { Member member = db.Member.FirstOrDefault(e => e.id == order.member_id); if (member == null) { return(new NormalResult("订单明细中的 member_id 无效。")); } if (member.status != 2) { return(new NormalResult("指定会员的状态不是审核通过状态。")); } if (String.IsNullOrEmpty(member.phone_num) || member.phone_num.Length < 11) { return(new NormalResult("指定会员的手机号码无效。")); } order.id = DateTime.Now.ToString("yyyyMMddHHmmss") + member.phone_num.Substring(7, 4) + _random.Next(100, 999).ToString(); order.order_date_time = DateTime.Now; order.real_total_price = 0; order.order_price = 0; order.product_num = 0; order.current_status = 1;//待付费 order.rebate_flag = 0; order.del_flag = 0; order.total_Rebate_Amount = 0; order.superior_Rebate_Amount = 0; order.superior_Parent_Rebate_Amount = 0; order.receiving_real_price = 0; foreach (var detailItem in order.Product_Order_Detail) { #region 验证 //App 里不会传productId的 都是传product_code detailItem.product_id = Convert.ToInt32(detailItem.product_code); Product_Info product = db.Product_Info.FirstOrDefault(e => e.id == detailItem.product_id); if (product == null) { return(new NormalResult("订单明细中的 product_id 无效。")); } if (detailItem.buy_num <= 0) { return(new NormalResult("订单明细中的商品购买数量不正确:" + product.product_name)); } if (product.status == 0) { return(new NormalResult("订单明细中的商品已下架:" + product.product_name)); } if (product.stock < detailItem.buy_num) { return(new NormalResult("订单明细中的商品库存不足:" + product.product_name)); } Member_Coupon_Account couponAccount = null; Coupon_Info coupon = null; if (detailItem.coupon_id.HasValue) { if (detailItem.buy_num > 1) { return(new NormalResult("订单明细中的商品在使用券的情况下,购买数量仅限1件:" + product.product_name)); } couponAccount = db.Member_Coupon_Account.FirstOrDefault(e => e.id == detailItem.coupon_id.Value); if (couponAccount == null) { return(new NormalResult("订单明细中的 coupon_id 无效:" + product.product_name)); } if (couponAccount.is_use == 1) { return(new NormalResult("订单明细中指定的券是已经使用过的券:" + product.product_name)); } coupon = db.Coupon_Info.FirstOrDefault(e => e.id == couponAccount.coupon_id); if (couponAccount == null) { return(new NormalResult("订单明细中指定商品所使用的券不存在:" + product.product_name)); } if (coupon.is_lose_valid == 1) { return(new NormalResult("订单明细中指定商品所使用的券已经失效:" + product.product_name)); } if (coupon.start_time.HasValue && coupon.start_time.Value > DateTime.Now) { return(new NormalResult("订单明细中指定商品所使用的券还没有到开始使用时间:" + product.product_name)); } if (coupon.end_time.HasValue && coupon.end_time.Value < DateTime.Now) { return(new NormalResult("订单明细中指定商品所使用的券已经超过了使用截止使用时间:" + product.product_name)); } } #endregion #region 给明细表赋值 detailItem.order_id = order.id; detailItem.product_code = product.product_code; detailItem.product_name = product.product_name; detailItem.thumb_pic = product.thumb_pic; detailItem.summary = product.summary; detailItem.desc_url = product.desc_url; detailItem.real_price = product.real_price; detailItem.first_distribution_price = product.first_distribution_price; detailItem.second_distribution_price = product.second_distribution_price; detailItem.member_price = product.member_price; detailItem.cost_price = product.cost_price; if (coupon != null) { detailItem.coupon_name = coupon.name; //更新为已使用状态 couponAccount.is_use = 1; couponAccount.use_time = DateTime.Now; } detailItem.is_receiving = 0; #endregion #region 计算价格 //如果使用了券,则用会员价计算 //如果没使用券,则看会员的级别 if (coupon != null) { detailItem.current_price = coupon.discount * product.member_price; detailItem.coupon_sale_price = product.member_price - detailItem.current_price; } else { switch (member.type) { case 1: //一级代理 detailItem.current_price = product.first_distribution_price; break; case 2: //二级代理 detailItem.current_price = product.second_distribution_price; break; case 3: //会员 detailItem.current_price = product.member_price; break; default: return(new NormalResult("指定会员的会员级别无效:" + member.type)); } } #endregion #region 给订单主表字段赋值 order.real_total_price += product.real_price; order.order_price += detailItem.cost_price; order.product_num += detailItem.buy_num; #endregion } db.Product_Order_Master.Add(order); db.SaveChanges(); } return(new NormalResult()); }