爱摄叽歪

爱生活爱叽歪,一个不务正业的人!

基于SQLite+EF6实现一套自己的Key-Value存储管理工具包(4)

前面讲的是基础实现结构,这里就贴几段实际应用的代码。在几微助手系统里面有一个产品模块。下面这个就是产品模块的通用扩展设置的使用代码。

 /// <summary>
    /// 站点产品设置
    /// </summary>
    public class SiteProductSettings : BaseSiteSettingExtensions
    {
        /// <summary>
        /// 获取或者设置站点热卖商品的id列表
        /// </summary>
        public Guid[] HotSaleProducts
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["hotsaleproducts"]))
                {
                    string[] ids = this.Items["hotsaleproducts"].Split(';');
                    Guid[] pids = new Guid[ids.Length];
                    for (int i = 0; i < ids.Length; i++)
                    {
                        pids[i] = new Guid(ids[i]);
                    }
                    return pids;
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (value != null && value.Length > 0)
                {
                    string ids = "";
                    foreach (Guid id in value)
                    {
                        ids += id.ToString("N") + ";";
                    }
                    ids=ids.Trim(';');
                    this.Items["hotsaleproducts"] = ids;
                }
                else
                {
                    this.Items["hotsaleproducts"] = "";
                }
            }
        }

        /// <summary>
        /// 是否支持配送
        /// </summary>
        public bool CanTakeOut
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["CanTakeOut"]))
                    return bool.Parse(this.Items["CanTakeOut"]);
                else
                    return false;
            }
            set
            {
                if (value)
                    this.Items["CanTakeOut"] = "true";
                else
                    this.Items["CanTakeOut"] = "false";

            }
        }

        /// <summary>
        /// 站内通用的产品服务承诺
        /// </summary>
        public List<string> DefaultProductPromise
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.Items["DefaultProductPromise"]))
                    return null;
                else
                {
                    List<string> images = null;
                    //兼容处理
                    images = this.GetItem<List<string>>("DefaultProductPromise");
                    return images;
                }
            }
            set
            {
                this.Items["DefaultProductPromise"] = this.ToJson(value); //JsonConvert.SerializeObject(value);
            }
        }

        /// <summary>
        /// 全局默认产品服务说明
        /// </summary>
        public string DefaultServiceNote
        {
            get
            {
                return this.Items["DefaultServiceNote"];
            }
            set
            {
                this.Items["DefaultServiceNote"] = value;
            }
        }

        /// <summary>
        /// 构造函数,根据siteunid创建站点产品设置实例
        /// </summary>
        /// <param name="siteunid">站点id</param>
        public SiteProductSettings(Guid siteunid):base(siteunid,SiteSettingExtensionTypes.Product.ToString())
        {
            this.Items = new SettingDictionary
            {
                { "hotsaleproducts",""},//热门推荐产品
                { "CanTakeOut","true"},//默认是否支持配送
                { "DefaultProductPromise",""},//通用的产品服务承诺
                { "canuseoffer","true"},//默认是否可以用优惠券
                { "DefaultServiceNote",""}//全局的产品服务说明

            };


        }

        /// <summary>
        /// 获取指定站点的产品设置实例
        /// </summary>
        /// <param name="siteunid">站点id</param>
        /// <returns>获取初始化的站点产品设置</returns>
        public static SiteProductSettings Get(Guid siteunid)
        {
            SiteProductSettings result = new SiteProductSettings(siteunid);
            result.GetItems();
            return result;
        }

        /// <summary>
        /// 设置产品的热卖清单状态
        /// </summary>
        /// <param name="pid">热卖产品编号</param>
        /// <returns>返回完成操作的当前状态是否在热卖列表</returns>
        public bool SetHotSale(string pid)
        {
            if (this.Items["hotsaleproducts"].Contains(pid))
            {
                DeleteFromHotSaleProducts(pid);
                return false;
            }
            else
            {
                AddToHotSaleProducts(pid);
                return true;
            }
        }

        /// <summary>
        /// 添加指定id产品到热卖产品
        /// </summary>
        /// <param name="pid">添加到热卖的产品id</param>
        public void AddToHotSaleProducts(string pid)
        {
            Product product=Product.GetProductByUnid(new Guid(pid));
            SiteMsg site = SiteMsg.GetSiteMsgBySiteUnid(product.SiteUnid);
            SiteProductSettings siteset = site.GetProductSettings();
            if (siteset.GetItem("hotsaleproducts").Contains(pid))
                throw new WeixinException("热卖商品已经存在");
            string hotProduct = siteset.GetItem("hotsaleproducts");
            hotProduct += ";" + pid;
            hotProduct = hotProduct.Trim(';');
            siteset.SaveItem("hotsaleproducts",hotProduct);
        }

        /// <summary>
        /// 从热卖产品列表删除指定产品
        /// </summary>
        /// <param name="pid">移除热卖的产品id</param>
        public void DeleteFromHotSaleProducts(string pid)
        {
            Product product = Product.GetProductByUnid(new Guid(pid));
            if (product != null)
            {
                SiteMsg site = SiteMsg.GetSiteMsgBySiteUnid(product.SiteUnid);
                SiteProductSettings siteset = site.GetProductSettings();
                if (!siteset.GetItem("hotsaleproducts").Replace("-","").Contains(pid))
                    throw new WeixinException("热卖商品不存在!");
                string hotProduct = siteset.GetItem("hotsaleproducts").Replace("-", "");

                hotProduct = hotProduct.Replace(";" + pid, "");
                hotProduct = hotProduct.Replace(pid + ";", "");
                hotProduct = hotProduct.Replace(pid, "");
                hotProduct = hotProduct.Trim(';');
                siteset.SaveItem("hotsaleproducts", hotProduct);
            }
        }

        /// <summary>
        /// 获取指定站点的产品库存预警
        /// </summary>
        /// <param name="siteunid">站点id</param>
        /// <returns>返回站点下所有库存达到预警的产品列表</returns>
        public List<Product> GetStocksForewarnProducts(Guid siteunid)
        {
            string StocksForewarn = this.Items["StocksForewarn"];
            int StocksForewarnCount = 0;
            int.TryParse(StocksForewarn, out StocksForewarnCount);
            List<Product> listProductGuid = new List<Product>();
            if(StocksForewarn.Trim().Length>0)
            {
                List<Product> ListProduct= Product.GetAllProductListBySiteUnid(siteunid);
                listProductGuid = ListProduct.Where(s => s.Num < StocksForewarnCount).ToList();
            }
            return listProductGuid;
        }
    }

下面这个是产品模块单项的扩展

  /// <summary>
    /// 产品项扩展配置
    /// </summary>
    public class ProductItemSettings: BaseSiteModuleItemExtensions
    {
        /// <summary>
        /// 可以重写原始价格的临时定价
        /// </summary>
        public double Price
        {
            get {
                if (!String.IsNullOrWhiteSpace(this.Items["price"]))
                {
                    double price = double.Parse(this.Items["price"]);
                    return price;
                }
                else
                {
                    return 0.00;
                }
            }

            set
            {
                if (value > 0.00)
                {
                    this.Items["price"] = value.ToString();
                }
                else
                {
                    this.Items["price"] = "0.00";
                }
            }
        }

        /// <summary>
        /// 产品的规格扩展属性,是一个字典对象,用于多维度记录产品的规格
        /// 字典的键名Key是规格的名称,Value部分是只一个字符串数组,用于保存规格的全部选项
        /// </summary>
        public Dictionary<string, string[]> SUK
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["SUK"]))
                {
                    Dictionary<string, string[]> suk = this.GetItem<Dictionary<string, string[]>>("SUK");
                    // Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string[]>>(this.Items["SUK"]);
                    return suk;
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (value!=null)
                {
                    
                    this.Items["SUK"] = this.ToJson(value); 
                    //Newtonsoft.Json.JsonConvert.SerializeObject(value);
                }
                else
                {
                    this.Items["SUK"] = "";
                }
            }
        }

        /// <summary>
        /// 这是一个特殊的关联价格的规格选项,是一个定义的强类型ProductPriceSUK
        /// </summary>
        public ProductPriceSUK PriceSUK
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["PriceSUK"]))
                {
                    ProductPriceSUK suk = ProductPriceSUK.Get(this.Items["PriceSUK"]);
                    return suk;
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (value != null)
                {
                    this.Items["PriceSUK"] = ProductPriceSUK.ToString(value);
                }
                else
                {
                    this.Items["PriceSUK"] = "";
                }
            }
        }

        /// <summary>
        /// 保存产品详情内的图片在云存储的ID列表
        /// </summary>
        public List<string> ProductDescriptionImgs
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.Items["productdescriptionimgs"]))
                    return null;
                else
                {
                    List<string> images=null;
                    //兼容处理
                    if (!this.Items["productdescriptionimgs"].Contains('['))
                    {
                        string[] arr = this.Items["productdescriptionimgs"].Split(';');
                        images = new List<string>(arr);
                        return images;
                    }
                    //兼容处理
                    images = this.GetItem<List<string>>("productdescriptionimgs");
                    return images;
                }
            }
            set
            {
                this.Items["productdescriptionimgs"] = this.ToJson(value); //JsonConvert.SerializeObject(value);
            }
        }

        /// <summary>
        /// 产品承诺
        /// 默认将继承SiteProductSettings.DefaultProductPromise的属性设置
        /// </summary>
        public List<string> ProductPromise
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.Items["ProductPromise"]))
                    return null;
                else
                {
                    List<string> images=null;
                    //兼容处理
                    images = this.GetItem<List<string>>("ProductPromise");
                    return images;
                }
            }
            set
            {
                this.Items["ProductPromise"] = this.ToJson(value); //JsonConvert.SerializeObject(value);
            }
        }



        /// <summary>
        /// 产品服务说明
        /// </summary>
        public string ServiceNote
        {
            get
            {
                return this.Items["ServiceNote"];
            }
            set
            {
                this.Items["ServiceNote"] = value;
            }
        }

        /// <summary>
        /// 获取或者设置一个值,表示该产品是否支持使用优惠券
        /// </summary>
        public bool CanUseOffer
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["canuseoffer"]))
                    return bool.Parse(this.Items["canuseoffer"]);
                else
                    return false;
            }
            set
            {
                if (value)
                    this.Items["canuseoffer"] = "true";
                else
                    this.Items["canuseoffer"] = "false";

            }
        }

        /// <summary>
        /// 获取或者设置一个值得,表示该产品是否支持配送外卖
        /// </summary>
        public bool CanTakeOut
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(this.Items["CanTakeOut"]))
                    return bool.Parse(this.Items["CanTakeOut"]);
                else
                    return false;
            }
            set
            {
                if (value)
                    this.Items["CanTakeOut"] = "true";
                else
                    this.Items["CanTakeOut"] = "false";

            }
        }

        /// <summary>
        /// 产品的销售量
        /// </summary>
        public int SalesVolume
        {
            get { return int.Parse(this.Items["SalesVolume"]); }
            set { this.Items["SalesVolume"] = value.ToString(); }
        }


        /// <summary>
        /// 初始化一个产品项配置实例
        /// </summary>
        /// <param name="siteunid">站点编号</param>
        /// <param name="productUnid">产品编号</param>
        public ProductItemSettings(Guid siteunid,Guid productUnid):base(siteunid, productUnid, SiteModuleItemExtensionTypes.ProductItem.ToString())
        {
            SiteProductSettings productSetting = SiteProductSettings.Get(siteunid);//获取全局的产品设置
            this.Items = new SettingDictionary
            {
                { "price",""},//可以重写原始价格的临时定价
                { "canuseoffer",productSetting.Items["canuseoffer"]},//是否支持优惠券使用
                { "CanTakeOut",productSetting.Items["CanTakeOut"]},//是否支持配送
                { "SUK",""}, //产品规格,可以多纬度添加,不影响价格
                { "PriceSUK",""},//价格关联的规格,一般用于套餐分量之类
                { "SalesVolume","0" },//销售量
                { "ProductPromise",productSetting.Items["DefaultProductPromise"]},//产品承诺
                { "ServiceNote",productSetting.Items["DefaultServiceNote"]},//产品服务说明
                { "productdescriptionimgs",""}//产品详情 的图片
            };
        }

        /// <summary>
        /// 获取一个指定站点ID和产品ID的配置实例
        /// </summary>
        /// <param name="siteunid">站点ID</param>
        /// <param name="productUnid">产品ID</param>
        /// <returns>返回ProductItemSettings类型已经初始化的实例</returns>
        public static ProductItemSettings Get(Guid siteunid, Guid productUnid)
        {
            ProductItemSettings result = new ProductItemSettings(siteunid, productUnid);
            result.GetItems();
            return result;
        }

     

    }

 

不允许评论