Ejemplo n.º 1
0
        /// <summary>
        ///创建订单
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int  createOrder(string username)
        {
            tb_order order = new tb_order();

            order.orderdate   = System.DateTime.Now;
            order.orderStatus = "未付款";
            order.createDate  = System.DateTime.Now;
            order.userid      = (from u in musicDB.tb_user
                                 where u.UserName == username
                                 select u.UserId).Single();
            order.total = 1;
            musicDB.tb_order.Add(order);
            musicDB.SaveChanges();
            return(order.orderId);
        }
Ejemplo n.º 2
0
        public static void 测试_插入()
        {
            //单条插入、添加一个用户
            var user1 = new tb_user
            {
                自增NO = 1, //自增列 框架不会去插入
                账号   = string.Format("U{0:D4}", 1),
                密码   = "12345678",
                用户名  = string.Format("Tester{0:D3}", 1),
                性别   = true,
                年龄   = new Random().Next(20, 60),
                会员等级 = (byte)(new Random().Next(1, 255)),
                积分   = new Random().Next(1000, 10000),
                消费能力 = Math.Abs((short)new Random().Next(1, 100)),
                头像   = 获取头像(1),
                注册日期 = DateTime.Now
            };
            //var query1 = new DyQuery<tb_user>().Insert(user1).AsQuery();
            //var result = new Dy().Query(query1);
            ////result.RowCount;//影响的行数
            //Console.WriteLine(result.AsJson());
            //Thread.Sleep(50);

            //多条 插入
            var multi_user = new List <int> {
                2, 3, 4, 5, 6, 7, 8, 9, 10
            }.Select(i =>
            {
                Thread.Sleep(50);
                return(new tb_user
                {
                    自增NO = i, //自增列 框架不会去插入
                    账号 = string.Format("U{0:D4}", i),
                    密码 = "12345678",
                    用户名 = string.Format("Tester{0:D3}", i),
                    性别 = i % 2 == 0,
                    年龄 = new Random().Next(20, 60),
                    会员等级 = (byte)(new Random().Next(1, 255)),
                    积分 = new Random().Next(1000, 10000),
                    消费能力 = Math.Abs((short)new Random().Next(1, 100)),
                    头像 = 获取头像(i),
                    注册日期 = DateTime.Now
                });
            }).ToList();
            //var dyQuery = new DyQuery<tb_user>()
            //    .Insert(multi).AsQuery();
            //string json = dy.Query(dyQuery).AsJson();
            //Console.WriteLine(json);
            //Thread.Sleep(50);


            //批量插入
            var multi_user_batch = new List <int> {
                11, 12, 13, 14, 15, 16, 17, 18, 19, 20
            }.Select(i =>
            {
                Thread.Sleep(50);
                return(new tb_user
                {
                    自增NO = i, //自增列 框架不会去插入
                    账号 = string.Format("U{0:D4}", i),
                    密码 = "12345678",
                    用户名 = string.Format("Tester{0:D3}", i),
                    性别 = i % 2 == 0,
                    年龄 = new Random().Next(20, 60),
                    会员等级 = (byte)(new Random().Next(1, 255)),
                    积分 = new Random().Next(1000, 10000),
                    消费能力 = Math.Abs((short)new Random().Next(1, 100)),
                    头像 = 获取头像(i),
                    注册日期 = DateTime.Now,
                });
            }).ToList();

            localDy.BulkInsert(multi_user_batch);


            //初始化 分类
            var multi_category = new List <tb_categories> {
                new tb_categories {
                    子ID  = "100",
                    父ID  = "",
                    分类名称 = "数码",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100100",
                    父ID  = "100",
                    分类名称 = "电脑",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100101",
                    父ID  = "100",
                    分类名称 = "手机",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100100100",
                    父ID  = "100100",
                    分类名称 = "笔记本",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100100101",
                    父ID  = "100100",
                    分类名称 = "台式机",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100101100",
                    父ID  = "100101",
                    分类名称 = "苹果",
                    顺序   = 1
                }
                , new tb_categories {
                    子ID  = "100101101",
                    父ID  = "100101",
                    分类名称 = "小米",
                    顺序   = 1
                }
            };

            var multi_products = new List <int> {
                11, 12, 13, 14, 15, 16, 17, 18, 19, 20
            }.Select(i =>
            {
                Thread.Sleep(50);
                return(new tb_product
                {
                    产品ID = Guid.NewGuid(),
                    产品名称 = string.Format("Product{0:D6}", i),
                    分类编号 = multi_category[i % 7].子ID,
                    进价 = (float)new Random().NextDouble(),
                    运费 = (decimal) new Random().NextDouble(),
                    税率 = (decimal) new Random().NextDouble(),
                    零售价 = (decimal) new Random().NextDouble() * 100,
                    会员折扣 = new Random().NextDouble(),
                    产品描述_中文 = "中文",
                    产品描述_英文 = "English",
                    RSS = "<root><product></product></root>",
                    //附件1 = "", //参考[头像]
                    //附件2 = "",
                    //版本 timestamp //无须指定
                });
            }).ToList();
            var dyQuery = new DyQuery <tb_user>()
                          .Insert(user1).Insert(multi_user).Insert(multi_category)
                          .Insert(multi_products).AsQuery();
            var json = localDy.Done(dyQuery).AsJson();

            Console.WriteLine(json);
            Thread.Sleep(50);


            //添加订单
            List <tb_user> userList = new List <tb_user>();

            userList.Add(user1);
            userList.AddRange(multi_user);
            userList.AddRange(multi_user_batch);

            for (int i = 0; i < 100; i++)
            {
                var user  = userList[new Random().Next(0, 19)];
                var order = new tb_order
                {
                    订单ID = Guid.NewGuid(),
                    订单名称 = DateTime.Now.ToShortDateString() + string.Format("{0:D4}", i),
                    订单时间 = DateTime.Now.AddDays(new Random().Next(1, 180)),
                    账号   = user.账号
                };

                List <tb_order_detail> details = new List <tb_order_detail>();
                var count = new Random().Next(2, 7); Thread.Sleep(50);
                for (int j = 0; j < count; j++)
                {
                    var product = multi_products[new Random().Next(0, 9)]; Thread.Sleep(50);
                    details.Add(new tb_order_detail
                    {
                        订单明细ID = Guid.NewGuid(),
                        订单ID   = order.订单ID,
                        产品ID   = product.产品ID,
                        产品名称   = product.产品名称,
                        序号     = j,
                        零售价    = product.零售价,
                        购买数量   = 100 - i,
                        支付价    = product.零售价 * (decimal)product.会员折扣 * (decimal)(1 - user.会员等级.ToString().Length * 0.1)
                    });
                }
                order.总金额 = details.Sum(t => t.支付价);

                var dyQuery2 = new DyQuery <tb_order>().Insert(order).Insert(details).AsQuery();
                var json2    = localDy.Done(dyQuery2).AsJson();
                Console.WriteLine(json2);
                Thread.Sleep(50);
            }
        }