Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="express_id"></param>
        /// <param name="shop_id"></param>
        public void SetDefaultExpress(int express_id,int shop_id=0)
        {
            if (express_id <= 0)
            {
                throw new KMJXCException("快递公司不能为空");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                int[] child_shop = (from c in this.DBChildShops select c.Shop_ID).ToArray<int>();
                var tmpNew = from express in db.Express_Shop
                             where (express.IsDefault == 0 && express.Express_ID==express_id)
                             select express;
                var tmp = from express in db.Express_Shop
                          where express.IsDefault==1
                          select express;

                if (shop_id > 0)
                {
                    tmp = tmp.Where(t => t.Shop_ID == shop_id);
                    tmpNew = tmpNew.Where(t => t.Shop_ID == shop_id);
                }
                else
                {
                    tmp = tmp.Where(t => t.Shop_ID == this.Shop.Shop_ID);
                    tmpNew = tmpNew.Where(t => t.Shop_ID == this.Shop.Shop_ID);
                }

                Express_Shop old = tmp.FirstOrDefault<Express_Shop>();
                Express_Shop newDefault = tmpNew.FirstOrDefault<Express_Shop>();
                if (old != null)
                {
                    old.IsDefault = 0;
                    old.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    old.Modified_By = this.CurrentUser.ID;
                    db.Entry(old).State = System.Data.EntityState.Modified;

                    //db.Express_Shop.SqlQuery("Update Express_Shop set IsDefault=0 where Express_ID="+old.Express_ID+" and Shop_ID="+old.Shop_ID);
                }

                if (newDefault != null)
                {
                    newDefault.IsDefault = 1;
                    newDefault.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    newDefault.Modified_By = this.CurrentUser.ID;
                    db.Entry(newDefault).State = System.Data.EntityState.Modified;

                    //db.Express_Shop.SqlQuery("Update Express_Shop set IsDefault=1 where Express_ID=" + newDefault.Express_ID + " and Shop_ID=" + newDefault.Shop_ID);
                }
                else
                {
                    throw new KMJXCException("设置默认快递公司前,请先确保快递公司已经添加到店铺");
                }

                db.SaveChanges();
            }
        }