using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using iBoxDB.LocalServer; using POSV.Entity; using POSV.Entity.Pormotion; using iBoxDB.LocalServer.IO; namespace POSV.HttpApi { public class DownloadCache { private static object _lock = new object(); private static DownloadCache _instance = null; private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static string root = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "data" + Path.DirectorySeparatorChar + "v2"); private DB db = null; private AutoBox auto = null; private DownloadCache() { } private void Init() { try { if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } DB.Root(root);//数据库存储路径 long address = (int)NoSql.Download; iBoxDB.DBDebug.DDebug.DeleteDBFiles(address); this.db = new DB(address);//数据库地址 var config = this.db.GetConfig(); //config.DBConfig.FileIncSize = 1024 * 1024 * 256; //config.DBConfig.SwapMemoryBuffer = 1024 * 1024 * 128; //config.DBConfig.SwapFileBuffer = 1024 * 1024 * 256; RegisterTable(config); if (this.auto == null) { this.auto = this.db.Open(); } } catch (Exception ex) { logger.Error(ex, "缓存系统初始化异常"); } } private void RegisterTable(DatabaseConfig.Config config) { //操作员缓存 config.EnsureTable(DownloadCacheName.Worker.ToString() , "Id"); //门店营业方案 config.EnsureTable(DownloadCacheName.BusinessPlan.ToString() , "Id"); //商品类别信息 config.EnsureTable(DownloadCacheName.ProductType.ToString() , "Id"); //商品信息 config.EnsureTable(DownloadCacheName.Product.ToString() , "Id"); //单位信息 config.EnsureTable(DownloadCacheName.ProductUnit.ToString() , "Id"); //图片信息 config.EnsureTable(DownloadCacheName.ProductImage.ToString() , "Id"); //门店仓库 config.EnsureTable(DownloadCacheName.StoreStorage.ToString(), "Id"); //商品库存 config.EnsureTable(DownloadCacheName.ProductStock.ToString(), "Id"); //库存系数 config.EnsureTable(DownloadCacheName.ProductRatio.ToString(), "Id"); //规格信息 config.EnsureTable(DownloadCacheName.ProductSpec.ToString() , "Id"); //做法类型 config.EnsureTable(DownloadCacheName.ProductMakeType.ToString() , "Id"); //做法信息 config.EnsureTable(DownloadCacheName.ProductMakeDetail.ToString() , "Id"); //私有做法信息 config.EnsureTable(DownloadCacheName.ProductMakeDetailPrivate.ToString(), "Id"); //套菜 config.EnsureTable(DownloadCacheName.ProductSuit.ToString() , "Id"); //套菜明细 config.EnsureTable(DownloadCacheName.ProductSuitDetail.ToString() , "Id"); //班次方案 config.EnsureTable(DownloadCacheName.BusinessPlanDetail.ToString() , "Id"); //付款类型 config.EnsureTable(DownloadCacheName.PayType.ToString() , "Id"); //收银方式 config.EnsureTable(DownloadCacheName.PayMode.ToString() , "Id"); //餐桌区域 config.EnsureTable(DownloadCacheName.TableArea.ToString() , "Id"); //餐桌类型 config.EnsureTable(DownloadCacheName.TableType.ToString() , "Id"); //餐桌信息 config.EnsureTable(DownloadCacheName.Table.ToString() , "Id"); //门店品牌 config.EnsureTable(DownloadCacheName.Brand.ToString() , "Id"); //员工角色 config.EnsureTable(DownloadCacheName.WorkerRole.ToString() , "Id"); //员工权限 config.EnsureTable(DownloadCacheName.WorkerPermissions.ToString() , "Id"); //厨打方案 config.EnsureTable(DownloadCacheName.KitPlan.ToString(), "Id"); //厨打商品 config.EnsureTable(DownloadCacheName.KitProduct.ToString(), "Id"); //厨显方案 config.EnsureTable(DownloadCacheName.KdsPlan.ToString(), "Id"); //厨显商品 config.EnsureTable(DownloadCacheName.KdsProduct.ToString(), "Id"); //打印机信息 config.EnsureTable(DownloadCacheName.Printer.ToString() , "Id"); //读卡器信息 config.EnsureTable(DownloadCacheName.Carder.ToString() , "Id"); //门店信息 config.EnsureTable(DownloadCacheName.StoreInfo.ToString(), "Id"); //门店小票图片信息 config.EnsureTable(DownloadCacheName.StorePrintImage.ToString(), "Id"); //门店双屏图片信息 config.EnsureTable(DownloadCacheName.AdvertPicture.ToString(), "Id"); //门店双屏字幕信息 config.EnsureTable(DownloadCacheName.AdvertCaption.ToString(), "Id"); //饿了么映射关系 config.EnsureTable(DownloadCacheName.MappingDishEleMe.ToString(), "Id"); //美团映射关系 config.EnsureTable(DownloadCacheName.MappingDishMeiTuan.ToString(), "Id"); //百度映射关系 config.EnsureTable(DownloadCacheName.MappingDishBaiDu.ToString(), "Id"); //门店费用项目列表 config.EnsureTable(DownloadCacheName.Feeitem.ToString(), "Id"); //门店熟客信息 config.EnsureTable(DownloadCacheName.Visitor.ToString() , "Id"); //门店熟客标签 config.EnsureTable(DownloadCacheName.VisitorTag.ToString() , "Id"); //门店熟客地址 config.EnsureTable(DownloadCacheName.VisitorAddress.ToString() , "Id"); //线上促销 config.EnsureTable(DownloadCacheName.CardProDay.ToString(), "Id"); //门店促销 config.EnsureTable(DownloadCacheName.PromotionStoreTask.ToString(), "Id"); //门店优惠券 config.EnsureTable(DownloadCacheName.PromotionStoreCoupon.ToString(), "Id"); //充值支付参数 config.EnsureTable(DownloadCacheName.RechargeParameter.ToString(), "Id"); //设置支付参数 config.EnsureTable(DownloadCacheName.SetPlan.ToString(), "Id"); //商品耗料配方 config.EnsureTable(DownloadCacheName.ProductBurden.ToString(), "Id"); //做法耗料配方 config.EnsureTable(DownloadCacheName.MakeBurden.ToString(), "Id"); //门店商品优惠券 config.EnsureTable(DownloadCacheName.StoreProductCoupon.ToString(), "Id"); //缓存待更新的Sql语句 config.EnsureTable(DownloadCacheName.SqlCache.ToString() , "Id"); } public static DownloadCache Instance { get { if (_instance == null) { lock (_lock) { _instance = new DownloadCache(); _instance.Init(); } } return _instance; } } /// /// IBox可以进行数据库的事务操作 /// public AutoBox AutoBox { get { return _instance.auto; } } /// /// IBox可以进行数据库的事务操作 /// public IBox AutoBoxTransaction { get { return AutoBox.Cube(); } } /// /// 删除数据库,IBoxDB中没有直接删除一个表的接口,所以这个方法显得格外亲切~ /// 注意:删除数据库之前要关闭该数据库 /// /// 数据库地址 public static void ClearAll() { try { if (Directory.Exists(root)) { if (DownloadCache.Instance.db != null) { DownloadCache.Instance.AutoBox.GetDatabase().Dispose(); DownloadCache.Instance.db.Close(); DownloadCache.Instance.db.Dispose(); } DownloadCache.Instance.db = null; logger.Info("清理已下载的缓存数据...."); iBoxDB.LocalServer.DB.Root(root); iBoxDB.DBDebug.DDebug.DeleteDBFiles((int)NoSql.Download); logger.Info("缓存数据清理成功...."); _instance = null; } } catch (Exception ex) { logger.Error(ex , "清理缓存异常"); } } } public class BoxDBServer : LocalDatabaseServer { protected override DatabaseConfig BuildDatabaseConfig(long address) { return new BoxMemoryStreamConfig(); } } }