Example #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mall_id"></param>
        /// <param name="locProductId"></param>
        /// <returns></returns>
        public bool MapMallProduct(string mall_id, int locProductId)
        {
            bool result = false;
            if (string.IsNullOrEmpty(mall_id))
            {
                return result;
            }

            if (locProductId <= 0)
            {
                return result;
            }

            Product locProduct = null;
            IOProductManager productManager = null;
            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                locProduct = (from p in db.Product where p.Product_ID == locProductId && p.Parent_ID == 0 select p).FirstOrDefault<Product>();
                if (locProduct == null)
                {
                    throw new KMJXCException("本地产品不存在,无法关联到商城宝贝");
                }

                Mall_Product locMallProduct = (from mp in db.Mall_Product where mp.Mall_ID == mall_id select mp).FirstOrDefault<Mall_Product>();
                if (locMallProduct == null)
                {
                    throw new KMJXCException("宝贝信息不存在,请先同步出售中的宝贝");
                }

                Access_Token token = this.AccessToken;
                Shop desshop = this.Shop;
                //current user is main shop user
                if (this.Shop.Shop_ID == this.Main_Shop.Shop_ID)
                {
                    int[] child_shop_ids = (from c in this.ChildShops select c.ID).ToArray<int>();
                    if (!child_shop_ids.Contains(locMallProduct.Shop_ID))
                    {
                        if (locMallProduct.Shop_ID != this.Main_Shop.Shop_ID)
                        {
                            throw new KMJXCException("不能关联他人店铺的宝贝,请不要尝试此操作");
                        }
                    }
                    else
                    {
                        desshop = (from s in this.ChildShops
                                   where s.ID == locMallProduct.Shop_ID
                                   select new Shop
                                   {
                                       Shop_ID = s.ID,
                                       Name = s.Title,
                                       Mall_Type_ID = s.Type!=null?s.Type.Mall_Type_ID:0
                                   }).FirstOrDefault<Shop>();
                        User shopUser = (from user in db.User
                                         from shop in db.Shop
                                         where shop.Shop_ID == locMallProduct.Shop_ID && user.User_ID == shop.User_ID
                                         select user).FirstOrDefault<User>();

                        if (desshop == null || shopUser == null)
                        {
                            throw new KMJXCException("请退出系统,用子店铺账户从商城授权登录进销存");
                        }
                        if (desshop.Mall_Type_ID == 0)
                        {
                            throw new KMJXCException("请退出系统,重新登录");
                        }
                        token = (from t in db.Access_Token where t.User_ID==shopUser.User_ID select t).FirstOrDefault<Access_Token>();
                        if (token == null)
                        {
                            throw new KMJXCException("子账户授权已经过期,请退出系统,用子店铺账户从商城授权登录进销存");
                        }

                        if (IsTokenExpired(token))
                        {
                            throw new KMJXCException("子账户授权已经过期,请退出系统,用子店铺账户从商城授权登录进销存");
                        }
                    }
                }
                else
                {
                    if (locMallProduct.Shop_ID == this.Main_Shop.Shop_ID)
                    {
                        throw new KMJXCException("不能关联主店铺宝贝");
                    }

                    if (locMallProduct.Shop_ID != this.Shop.Shop_ID)
                    {
                        throw new KMJXCException("不能关联他人店铺的宝贝,请不要尝试此操作");
                    }
                }

                productManager = new TaobaoProductManager(token, desshop.Mall_Type_ID);

                result = productManager.MappingProduct(locProductId.ToString(), mall_id);
                if (result)
                {
                    locMallProduct.Outer_ID = locProductId;
                }

                db.SaveChanges();
            }

            return result;
        }
Example #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="skuId"></param>
        /// <param name="locProductId"></param>
        /// <returns></returns>
        public bool MapSku(string skuId,int locProductId)
        {
            bool result = false;
            if (string.IsNullOrEmpty(skuId))
            {
                return result;
            }

            if (locProductId <= 0)
            {
                return result;
            }
            Product locProduct = null;
            Access_Token token = this.AccessToken;
            Shop desshop = this.Shop;
            IOProductManager productManager = new TaobaoProductManager(this.AccessToken, this.Shop.Mall_Type_ID);
            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                locProduct = (from p in db.Product where p.Product_ID == locProductId && p.Parent_ID > 0 select p).FirstOrDefault<Product>();
                if (locProduct == null)
                {
                    throw new KMJXCException("进销存销售属性不存在,无法关联到商城宝贝的SKU");
                }

                Mall_Product_Sku msku = null;
                msku = (from sk in db.Mall_Product_Sku where sk.SKU_ID == skuId select sk).FirstOrDefault<Mall_Product_Sku>();
                if (msku == null)
                {
                    throw new KMJXCException("未找到SKU ID 对应的商城宝贝ID");
                }

                Mall_Product_Sku existed=(from esku in db.Mall_Product_Sku where esku.Mall_ID==msku.Mall_ID && esku.Outer_ID==locProductId select esku).FirstOrDefault<Mall_Product_Sku>();
                if (existed != null)
                {
                    throw new KMJXCException("所选库存属性已经被关联到此宝贝下其他的SKU,请选择其他的库存属性进行关联");
                }

                Mall_Product locMallProduct=(from p in db.Mall_Product where p.Mall_ID==msku.Mall_ID select p).FirstOrDefault<Mall_Product>();
                if (locMallProduct == null)
                {
                    throw new KMJXCException("此SKU所对应的宝贝快照不存在,请先同步在售宝贝");
                }
                //current user is main shop user
                if (this.Shop.Shop_ID == this.Main_Shop.Shop_ID)
                {
                    int[] child_shop_ids = (from c in this.ChildShops select c.ID).ToArray<int>();
                    if (!child_shop_ids.Contains(locMallProduct.Shop_ID))
                    {
                        if (locProduct.Shop_ID != this.Shop.Shop_ID)
                        {
                            throw new KMJXCException("不能关联他人店铺的宝贝,请不要尝试此操作");
                        }
                    }
                    else
                    {
                        desshop = (from s in this.ChildShops
                                   where s.ID == locMallProduct.Shop_ID
                                   select new Shop
                                   {
                                       Shop_ID = s.ID,
                                       Name = s.Title,
                                       Mall_Type_ID=s.Type.Mall_Type_ID
                                   }).FirstOrDefault<Shop>();
                        User shopUser = (from user in db.User
                                         from shop in db.Shop
                                         where shop.Shop_ID == locMallProduct.Shop_ID && user.User_ID == shop.User_ID
                                         select user).FirstOrDefault<User>();

                        if (desshop == null || shopUser == null)
                        {
                            throw new KMJXCException("请退出系统,用子店铺账户从商城授权登录进销存");
                        }

                        token = (from t in db.Access_Token where t.User_ID == shopUser.User_ID select t).FirstOrDefault<Access_Token>();
                        if (token == null)
                        {
                            throw new KMJXCException("子账户授权已经过期,请退出系统,用子店铺账户从商城授权登录进销存");
                        }

                        if (IsTokenExpired(token))
                        {
                            throw new KMJXCException("子账户授权已经过期,请退出系统,用子店铺账户从商城授权登录进销存");
                        }
                    }
                }
                else
                {
                    if (locMallProduct.Shop_ID == this.Main_Shop.Shop_ID)
                    {
                        throw new KMJXCException("不能关联主店铺宝贝");
                    }

                    if (locMallProduct.Shop_ID != this.Shop.Shop_ID)
                    {
                        throw new KMJXCException("不能关联他人店铺的宝贝,请不要尝试此操作");
                    }
                }

                productManager = new TaobaoProductManager(token, desshop.Mall_Type_ID);

                result = productManager.MappingSku(locProductId.ToString(), skuId, msku.Mall_ID, msku.Properties);
                if (result)
                {
                    msku.Outer_ID = locProductId;
                }

                db.SaveChanges();
            }
            return result;
        }
Example #3
0
        /// <summary>
        /// Sync onsale products to local database
        /// </summary>
        /// <returns></returns>
        public List<BMallProduct> SyncMallOnSaleProducts(int shop_id=0,bool create_product=false,bool mapProduct=false)
        {
            List<BMallProduct> newProducts = new List<BMallProduct>();
            List<BMallProduct> products = new List<BMallProduct>();
            IOProductManager productManager = new TaobaoProductManager(this.AccessToken,this.Shop.Mall_Type_ID);
            List<BMallProduct> newUnMappedProducts = new List<BMallProduct>();
            long total = 0;
            long page = 1;
            long pageSize = 40;
            List<BMallProduct> tmp = new List<BMallProduct>();
            Shop shop = this.Shop;
            if (shop_id > 0)
            {
                shop = new Shop() { Shop_ID=shop_id };
            }
            tmp = productManager.GetOnSaleProducts(this.CurrentUser, shop, page, pageSize, out total);

            if (tmp != null)
            {
                products = products.Concat(tmp).ToList<BMallProduct>();
            }

            long totalPage = 1;

            if (total > pageSize)
            {
                if (total % pageSize == 0)
                {
                    totalPage = total / pageSize;
                }
                else
                {
                    totalPage = total / pageSize + 1 ;
                }
            }

            if (totalPage > 1)
            {
                page++;
                while (page <= totalPage)
                {
                    tmp = productManager.GetOnSaleProducts(this.CurrentUser, this.Shop, page, pageSize, out total);
                    products = products.Concat(tmp).ToList<BMallProduct>();
                    page++;
                }
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                List<Product> locProducts=(from locPdt in db.Product where locPdt.Shop_ID==this.Shop.Shop_ID select locPdt).ToList<Product>();

                List<Mall_Product> dbMallProducts=(from pdt in db.Mall_Product
                                                   where pdt.Shop_ID==this.Shop.Shop_ID
                                                   select pdt).ToList<Mall_Product>();

                List<Mall_Product_Sku> dbSkus = (from sku in db.Mall_Product_Sku
                                                 where sku.Shop_ID == this.Shop.Shop_ID
                                                 select sku).ToList<Mall_Product_Sku>();

                foreach (BMallProduct product in products)
                {
                    Mall_Product dbProduct=(from dbPdt in dbMallProducts where dbPdt.Mall_ID==product.ID select dbPdt).FirstOrDefault<Mall_Product>();
                    bool isNew = false;
                    if (dbProduct == null)
                    {
                        isNew = true;
                        dbProduct = new Mall_Product();
                        dbProduct.CreatedProduct = false;
                        dbProduct.FirstSync = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    }

                    Product mappedLocProduct=(from p in locProducts where p.Product_ID==product.OuterID select p).FirstOrDefault<Product>();
                    if (mappedLocProduct == null)
                    {
                        product.OuterID = 0;
                    }

                    dbProduct.Created = product.Created;
                    dbProduct.Description = product.Description;
                    dbProduct.Mall_ID = product.ID;
                    dbProduct.Modified = product.Modified;
                    dbProduct.Outer_ID = product.OuterID;
                    dbProduct.PicUrl = product.PicUrl;
                    dbProduct.Price = product.Price;
                    dbProduct.Quantity = (int)product.Quantity;
                    dbProduct.Shop_ID = this.Shop.Shop_ID;

                    if (product.Shop != null)
                    {
                        dbProduct.Shop_ID = product.Shop.ID;
                    }
                    else {
                        product.Shop = new BShop { ID=this.Shop.Shop_ID };
                    }

                    dbProduct.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbProduct.Title = product.Title;

                    if (isNew)
                    {
                        db.Mall_Product.Add(dbProduct);
                        newProducts.Add(product);
                        if (mappedLocProduct == null)
                        {
                            newUnMappedProducts.Add(product);
                        }
                    }

                    if (product.Skus!=null)
                    {
                        foreach (BMallSku mSku in product.Skus)
                        {
                            bool skuNew = false;
                            Mall_Product_Sku dbSku=(from dbs in dbSkus where mSku.SkuID== dbs.SKU_ID select dbs).FirstOrDefault<Mall_Product_Sku>();
                            if (dbSku == null)
                            {
                                skuNew = true;
                                dbSku = new Mall_Product_Sku();
                            }

                            Product childLocProduct=(from p in locProducts where p.Product_ID==mSku.OuterID select p).FirstOrDefault<Product>();
                            if (childLocProduct == null)
                            {
                                mSku.OuterID = 0;
                            }

                            dbSku.Outer_ID = mSku.OuterID;
                            dbSku.Mall_ID = mSku.MallProduct_ID;
                            dbSku.Price = mSku.Price;
                            dbSku.Properties = mSku.Properities;
                            dbSku.Properties_name = mSku.PropertiesName;
                            dbSku.Quantity = (int)mSku.Quantity;
                            dbSku.Shop_ID = product.Shop.ID;
                            dbSku.SKU_ID = mSku.SkuID;
                            if (skuNew)
                            {
                                db.Mall_Product_Sku.Add(dbSku);
                            }
                        }
                    }
                }

                SyncWithMall sync = new SyncWithMall();
                sync.Shop_ID = this.Shop.Shop_ID;
                sync.User_ID = this.CurrentUser.ID;
                sync.SyncType = 0;//宝贝同步
                sync.SyncTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                db.SyncWithMall.Add(sync);
                db.SaveChanges();

                if (create_product)
                {
                    this.CreateProductsByMallProducts(newUnMappedProducts,mapProduct);
                }

                base.CreateActionLog(new BUserActionLog() {Shop=new BShop{ ID=shop.Shop_ID}, Action = new BUserAction() { Action_ID = UserLogAction.SYNC_SHOP_ONSALE_PRODUCT }, Description = "同步商城在售宝贝,共有 " + newProducts.Count+ " 个新宝贝同步到进销存" });
            }

            return newProducts;
        }