/// <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; }
/// <summary> /// /// </summary> /// <param name="house"></param> public void CreateStoreHouse(BStoreHouse house) { if (this.CurrentUserPermission.ADD_STORE_HOUSE == 0) { throw new KMJXCException("没有创建仓库的权限"); } using (KuanMaiEntities db = new KuanMaiEntities()) { Store_House dbHouse = new Store_House(); int existing = (from h in db.Store_House where house.Name.Contains(h.Title) select h).Count(); if (existing > 0) { throw new KMJXCException("类似的仓库名称已经存在"); } dbHouse.Phone = house.Phone; dbHouse.Title = house.Name; dbHouse.Address = house.Address; dbHouse.Guard = 0; dbHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbHouse.User_ID = this.CurrentUser.ID; dbHouse.Default = house.IsDefault; dbHouse.Shop_ID = this.Shop.Shop_ID; if ((bool)dbHouse.Default) { Store_House defaultHouse=(from hu in db.Store_House where hu.Default==true select hu).FirstOrDefault<Store_House>(); if (defaultHouse != null) { defaultHouse.Default = false; } } db.Store_House.Add(dbHouse); db.SaveChanges(); base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = dbHouse.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_STOREHOUSE }, Description = "" }); } }
/// <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> /// /// </summary> /// <param name="products"></param> /// <param name="mapProduct"></param> private void CreateProductsByMallProducts(List<BMallProduct> products,bool mapProduct=false) { if (products == null || products.Count == 0) { return; } List<BProperty> properties = new List<BProperty>(); int shop_id = products[0].Shop.ID; using (KuanMaiEntities db = new KuanMaiEntities()) { string[] dbMallPdtIds=(from p in products select p.ID).ToArray<string>(); List<Mall_Product> dbMallProcuts = (from p in db.Mall_Product where dbMallPdtIds.Contains(p.Mall_ID) select p).ToList<Mall_Product>(); List<Product> dbProducts = new List<Product>(); List<Product_Spec> existedProperties = null; var tmpProperties = from p in db.Product_Spec select p; var tmpDbProducts = from p in db.Product where p.Parent_ID == 0 select p; if (shop_id == this.Main_Shop.Shop_ID) { int[] child_shop_ids = (from c in this.ChildShops select c.ID).ToArray<int>(); tmpProperties = tmpProperties.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); tmpDbProducts = tmpDbProducts.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); } else { tmpProperties = tmpProperties.Where(p =>(p.Shop_ID == shop_id || p.Shop_ID==this.Main_Shop.Shop_ID)); tmpDbProducts = tmpDbProducts.Where(p => (p.Shop_ID == shop_id || p.Shop_ID == this.Main_Shop.Shop_ID)); } dbProducts = tmpDbProducts.ToList<Product>(); existedProperties = tmpProperties.ToList<Product_Spec>(); int[] prop_ids=(from prop in existedProperties select prop.Product_Spec_ID).ToArray<int>(); List<Product_Spec_Value> existedPropValues=(from pv in db.Product_Spec_Value where prop_ids.Contains(pv.Product_Spec_ID) select pv).ToList<Product_Spec_Value>(); Store_House defaultStoreHouse = null; List<Store_House> storeHouses=(from h in db.Store_House where h.Shop_ID==shop_id select h).ToList<Store_House>(); if (storeHouses.Count == 0) { defaultStoreHouse = new Store_House(); defaultStoreHouse.Shop_ID = shop_id; defaultStoreHouse.Title = "默认仓库"; defaultStoreHouse.User_ID = this.CurrentUser.ID; defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); defaultStoreHouse.Default = true; 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; } } foreach (BMallProduct product in products) { Mall_Product dbMallProduct=(from p in dbMallProcuts where p.Mall_ID==product.ID select p).FirstOrDefault<Mall_Product>(); Product dbProduct = (from p in dbProducts where p.MallProduct == product.ID select p).FirstOrDefault<Product>(); if (dbProduct != null) { if (!string.IsNullOrEmpty(dbMallProduct.Title) && dbMallProduct.Title!=dbProduct.Name) { dbProduct.Name = dbMallProduct.Title; } dbMallProduct.CreatedProduct = true; db.SaveChanges(); continue; } dbProduct = new Product(); dbProduct.Code = product.Code; dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Description = dbProduct.Description; dbProduct.MallProduct = product.ID; dbProduct.Name = product.Title; dbProduct.Parent_ID = 0; dbProduct.Price = product.Price; dbProduct.Product_Class_ID = 0; //dbProduct.Quantity = (int)product.Quantity; dbProduct.Shop_ID = product.Shop.ID; dbProduct.Update_Time = dbProduct.Create_Time; dbProduct.Update_User_ID = this.CurrentUser.ID; dbProduct.User_ID = this.CurrentUser.ID; dbProduct.Wastage = 0; db.Product.Add(dbProduct); db.SaveChanges(); if (dbProduct.Product_ID <= 0) { continue; } Stock_Batch 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(); Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = dbProduct.Product_ID; stockPile.Quantity = 0; stockPile.Shop_ID = product.Shop.ID; stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; stockPile.StockPile_ID = 0; stockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(stockPile); if (product.Skus != null) { foreach (BMallSku sku in product.Skus) { Product dbChildProduct = new Product(); dbChildProduct.Code = product.Code; dbChildProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbChildProduct.Description = dbProduct.Description; dbChildProduct.MallProduct = sku.SkuID; dbChildProduct.Name = product.Title; dbChildProduct.Parent_ID = dbProduct.Product_ID; dbChildProduct.Price = product.Price; dbChildProduct.Product_Class_ID = 0; //dbChildProduct.Quantity = (int)sku.Quantity; dbChildProduct.Shop_ID = product.Shop.ID; dbChildProduct.Update_Time = dbChildProduct.Create_Time; dbChildProduct.Update_User_ID = this.CurrentUser.ID; dbChildProduct.User_ID = this.CurrentUser.ID; dbChildProduct.Wastage = 0; db.Product.Add(dbChildProduct); db.SaveChanges(); if (dbChildProduct.Product_ID <= 0) { continue; } if (string.IsNullOrEmpty(sku.PropertiesName)) { continue; } Stock_Pile skustockPile = new Stock_Pile(); skustockPile.LastLeave_Time = 0; skustockPile.Price = 0; skustockPile.Product_ID = dbChildProduct.Product_ID; skustockPile.Quantity = 0; skustockPile.Shop_ID = product.Shop.ID; skustockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; skustockPile.StockPile_ID = 0; skustockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(skustockPile); string[] props = sku.PropertiesName.Split(';'); foreach (string prop in props) { string[] values = prop.Split(':'); Product_Spec property = (from p in existedProperties where (p.Mall_PID == values[0] || p.Name==values[2]) select p).FirstOrDefault<Product_Spec>(); if (property == null) { property=new Product_Spec(); property.Name = values[2]; property.Mall_PID = values[0]; property.Shop_ID = shop_id; property.User_ID = this.CurrentUser.ID; property.Product_Class_ID = 0; property.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec.Add(property); db.SaveChanges(); if (property.Product_Spec_ID > 0) { existedProperties.Add(property); } } Product_Spec_Value propValue=(from v in existedPropValues where (v.Mall_PVID==values[2] || v.Name==values[3]) select v ).FirstOrDefault<Product_Spec_Value>(); if(propValue==null) { propValue=new Product_Spec_Value(){ Product_Spec_ID=property.Product_Spec_ID, Mall_PVID=values[1], Name=values[3], Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID=this.CurrentUser.ID}; db.Product_Spec_Value.Add(propValue); db.SaveChanges(); if (propValue.Product_Spec_Value_ID > 0) { existedPropValues.Add(propValue); } } if (property.Product_Spec_ID > 0 && propValue.Product_Spec_Value_ID > 0) { Product_Specifications ps = new Product_Specifications(); ps.Product_ID = dbChildProduct.Product_ID; ps.Product_Spec_ID = property.Product_Spec_ID; ps.Product_Spec_Value_ID = propValue.Product_Spec_Value_ID; db.Product_Specifications.Add(ps); } } } } dbMallProduct.CreatedProduct = true; } db.SaveChanges(); } }