/// <summary> /// Create single parent or child product which can contains Properties combination /// </summary> /// <param name="product"></param> /// <returns></returns> public void CreateProduct(BProduct product) { if (this.CurrentUserPermission.ADD_PRODUCT == 0) { throw new KMJXCException("没有权限创建产品"); } Product dbProduct = new Product(); dbProduct.Code = product.Code; dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Name = product.Title; dbProduct.Parent_ID = product.ParentID; dbProduct.Quantity = 0; dbProduct.Description = product.Description; if (product.Category != null) { dbProduct.Product_Class_ID = product.Category.ID; } dbProduct.Product_ID = 0; dbProduct.Price = product.Price; if (product.Unit != null) { dbProduct.Product_Unit_ID = product.Unit.Product_Unit_ID; } dbProduct.Shop_ID = this.Shop.Shop_ID; dbProduct.User_ID = this.MainUser.ID; if (product.Parent != null && product.Parent.ID > 0) { dbProduct.Parent_ID = product.Parent.ID; } using (KuanMaiEntities db = new KuanMaiEntities()) { db.Product.Add(dbProduct); db.SaveChanges(); if (dbProduct.Product_ID <= 0) { throw new KMJXCException("产品创建失败"); } product.ID = dbProduct.Product_ID; //Update product images if (product.Images != null && product.Images.Count > 0) { List <int> img_ids = new List <int>(); foreach (Image img in product.Images) { img_ids.Add(img.ID); } List <Image> dbImages = (from img in db.Image where img_ids.Contains(img.ID) select img).ToList <Image>(); foreach (Image image in dbImages) { image.ProductID = product.ID; } db.SaveChanges(); } Stock_Batch batch = null; if (dbProduct.Parent_ID == 0) { batch = new Stock_Batch() { Name = "P0", ProductID = dbProduct.Product_ID, Price = 0, ShopID = dbProduct.Shop_ID, Desc = "", Created_By = this.CurrentUser.ID }; batch.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); batch.ParentProductID = dbProduct.Parent_ID; db.Stock_Batch.Add(batch); db.SaveChanges(); } else { batch = (from b in db.Stock_Batch where b.ProductID == dbProduct.Parent_ID select b).FirstOrDefault <Stock_Batch>(); } Store_House defaultStoreHouse = null; List <Store_House> storeHouses = (from h in db.Store_House where (h.Shop_ID == dbProduct.Shop_ID || h.Shop_ID == this.Main_Shop.Shop_ID) select h).ToList <Store_House>(); if (storeHouses.Count == 0) { defaultStoreHouse = new Store_House(); defaultStoreHouse.Shop_ID = dbProduct.Shop_ID; defaultStoreHouse.Title = "默认仓库"; defaultStoreHouse.Address = ""; defaultStoreHouse.Phone = ""; defaultStoreHouse.User_ID = this.CurrentUser.ID; defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); defaultStoreHouse.Default = true; defaultStoreHouse.Guard = 0; db.Store_House.Add(defaultStoreHouse); db.SaveChanges(); } else { defaultStoreHouse = (from h in storeHouses where h.Default == true select h).FirstOrDefault <Store_House>(); if (defaultStoreHouse == null) { defaultStoreHouse = storeHouses[0]; defaultStoreHouse.Default = true; } } Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = product.ID; stockPile.Quantity = 0; stockPile.Shop_ID = dbProduct.Shop_ID; stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; stockPile.StockPile_ID = 0; stockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(stockPile); if (product.Properties != null && product.Properties.Count > 0) { foreach (BProductProperty pro in product.Properties) { Product_Specifications ps = new Product_Specifications(); ps.Product_ID = dbProduct.Product_ID; ps.Product_Spec_ID = pro.PID; ps.Product_Spec_Value_ID = pro.PVID; ps.User_ID = this.CurrentUser.ID; ps.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Specifications.Add(ps); } //db.SaveChanges(); } if (product.Suppliers != null) { foreach (Supplier su in product.Suppliers) { Product_Supplier ps = new Product_Supplier(); ps.Product_ID = product.ID; ps.Supplier_ID = su.Supplier_ID; db.Product_Supplier.Add(ps); } //db.SaveChanges(); } if (product.Children != null) { foreach (BProduct p in product.Children) { if (p.Parent == null) { p.ParentID = dbProduct.Product_ID; p.Parent = new BProduct() { ID = product.ID }; } p.Children = null; this.CreateProduct(p); } } else { base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = dbProduct.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_PRODUCT }, Description = "" }); } db.SaveChanges(); } }
/// <summary> /// Calback from Mall Open API Authorization, it will verify if current login user has access to the system /// </summary> /// <param name="code">returns by Mall Open API Authorization</param> /// <returns></returns> public Access_Token AuthorizationCallBack(string code) { Access_Token request_token = null; BUser requester = new BUser(); //must get access token after mall authorization to identify user request_token = TokenManager.RequestAccessToken(code); if (request_token == null) { throw new KMJXCException("没有获取到Access token", ExceptionLevel.SYSTEM); } requester.Type = new BMallType() { ID = this.Mall_Type_ID }; requester.Mall_ID = request_token.Mall_User_ID; requester.Mall_Name = request_token.Mall_User_Name; requester.Parent_ID = 0; requester.Parent = null; KuanMaiEntities db = new KuanMaiEntities(); try { var db_user = from u in db.User where u.Mall_ID == requester.Mall_ID && u.Mall_Name == requester.Mall_Name && u.Mall_Type == this.Mall_Type_ID select new BUser { ID = u.User_ID, Name = u.Name, Mall_Name = u.Mall_Name, Mall_ID = u.Mall_ID, Password = u.Password, Parent_ID = (int)u.Parent_User_ID, }; List <BUser> users = db_user.ToList <BUser>(); //Create user in local db with mall owner id if (users.Count == 0) { this.InitializeMallManagers(request_token); if (this.ShopManager == null) { throw new KMJXCException("IShopManager 实例为null", ExceptionLevel.SYSTEM); } //check if current user's shop is ready in system Shop shop = this.ShopManager.GetShop(requester); if (shop == null) { BUser subUser = this.MallUserManager.GetSubUser(requester.Mall_ID, requester.Mall_Name); if (subUser == null) { throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager) this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR); } else { // if (subUser.Parent == null || string.IsNullOrEmpty(subUser.Parent.Mall_Name)) { throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager) this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR); } BUser mainUser = null; var u = from us in db.User where us.Mall_ID == subUser.Parent.Mall_ID && us.Mall_Type == requester.Type.ID && us.Mall_Name == subUser.Parent.Mall_Name select new BUser { ID = us.User_ID, Name = us.Name, Mall_Name = us.Mall_Name, Mall_ID = us.Mall_ID, Password = us.Password, Parent_ID = (int)us.Parent_User_ID, Type = new BMallType { ID = us.Mall_Type } }; if (u.ToList <BUser>().Count() == 1) { mainUser = u.ToList <BUser>()[0]; } if (mainUser == null) { throw new KMJXCException("主账户:" + subUser.Parent.Mall_Name + " 还没有初始化店铺信息,所有子账户无法登录系统", ExceptionLevel.ERROR); } requester.Parent_ID = mainUser.ID; requester.Parent = mainUser; requester.EmployeeInfo = subUser.EmployeeInfo; } } //create user in local db requester.Name = requester.Mall_Name; requester.Password = Guid.NewGuid().ToString(); User dbUser = new User(); dbUser.User_ID = requester.ID; dbUser.Mall_ID = requester.Mall_ID; dbUser.Mall_Name = requester.Mall_Name; dbUser.NickName = ""; dbUser.Name = requester.Name; dbUser.Mall_Type = requester.Type.ID; dbUser.Parent_Mall_ID = ""; dbUser.Parent_Mall_Name = ""; dbUser.Parent_User_ID = 0; dbUser.Password = ""; dbUser.Name = dbUser.Mall_Name; dbUser.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbUser.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbUser.Modified_By = 0; if (requester.Parent != null) { dbUser.Parent_Mall_ID = requester.Parent.Mall_ID; dbUser.Parent_Mall_Name = requester.Parent.Mall_Name; dbUser.Parent_User_ID = requester.Parent.ID; } db.User.Add(dbUser); db.SaveChanges(); //create access token for the new user request_token.User_ID = dbUser.User_ID; requester.ID = dbUser.User_ID; db.Access_Token.Add(request_token); //save employee if (requester.Parent_ID > 0 && requester.EmployeeInfo != null) { requester.EmployeeInfo.User_ID = requester.ID; Employee employee = new Employee(); employee.Name = requester.EmployeeInfo.Name; employee.IdentityCard = requester.EmployeeInfo.IdentityCard; employee.MatureDate = requester.EmployeeInfo.MatureDate; employee.Phone = requester.EmployeeInfo.Phone; employee.User_ID = requester.EmployeeInfo.User_ID; employee.HireDate = requester.EmployeeInfo.HireDate; employee.Gendar = requester.EmployeeInfo.Gendar; employee.Duty = requester.EmployeeInfo.Duty; employee.Email = requester.EmployeeInfo.Email; employee.Department = requester.EmployeeInfo.Department; employee.BirthDate = requester.EmployeeInfo.BirthDate; employee.Address = requester.EmployeeInfo.Address; db.Employee.Add(employee); } if (shop != null) { //create local shop information for the new main user shop.User_ID = requester.ID; shop.Parent_Shop_ID = 0; shop.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); shop.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Shop.Add(shop); db.SaveChanges(); //save shop user Shop_User shop_User = new Shop_User(); shop_User.User_ID = requester.ID; shop_User.Shop_ID = shop.Shop_ID; db.Shop_User.Add(shop_User); //update dbuser dbUser.Shop_ID = shop.Shop_ID; db.SaveChanges(); //create default stock house Store_House shouse = new Store_House(); shouse.Shop_ID = shop.Shop_ID; shouse.Title = "默认仓库"; shouse.User_ID = requester.ID; shouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Store_House.Add(shouse); db.SaveChanges(); } if (shop != null && requester.Parent_ID == 0) { //sync mall sub users to system //List<BUser> subUsers = this.MallUserManager.GetSubUsers(requester); //if (subUsers != null && subUsers.Count > 0 && shop.Shop_ID > 0) //{ // foreach (BUser user in subUsers) // { // User db1User = new User(); // db1User.Parent_Mall_ID = requester.Mall_ID; // db1User.Parent_Mall_Name = requester.Mall_Name; // db1User.Parent_User_ID = (int)requester.ID; // db1User.Mall_Name = user.Mall_Name; // db1User.Mall_ID = user.Mall_ID; // db1User.Mall_Type = user.Type.Mall_Type_ID; // db1User.Name = user.Name; // db1User.Password = ""; // db.User.Add(db1User); // db.SaveChanges(); // if (db1User.User_ID > 0) // { // //add shop user // Shop_User shop_User1 = new Shop_User(); // shop_User1.User_ID = requester.ID; // shop_User1.Shop_ID = shop.Shop_ID; // db.Shop_User.Add(shop_User1); // if (user.EmployeeInfo != null) // { // user.EmployeeInfo.User_ID = db1User.User_ID; // db.Employee.Add(user.EmployeeInfo); // //db.SaveChanges(); // } // } // } // db.SaveChanges(); //} } } else { //Verify if local db has non expried accesstoken requester = users[0]; Access_Token local_token = GetLocalToken(requester.ID, this.Mall_Type_ID); if (local_token != null) { long timeNow = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); //last access token is expried if (timeNow >= local_token.Expirse_In + local_token.Request_Time) { request_token = TokenManager.RequestAccessToken(code); request_token.User_ID = requester.ID; UpdateLocalAccessToken(request_token); } else { request_token = local_token; } } } } catch (DbEntityValidationException dbex) { throw new KMJXCException("登录失败,请联系管理员"); } catch (Exception ex) { throw new KMJXCException(ex.Message, ExceptionLevel.SYSTEM); } finally { if (db != null) { db.Dispose(); } } return(request_token); }