/// <summary>
 /// 批量修改 改custom
 /// </summary>
 /// <param name="datas"></param>
 /// <returns></returns>
 public bool Edits(List <DataDictionary> datas)
 {
     using (ShopEntities db = new ShopEntities())
     {
         var args = new DataDictionaryDataChangedArgs
         {
             DictionaryChangeType = ChangeType.Edit
         };
         foreach (var item in datas)
         {
             DataDictionary dataDictionary = db.DataDictionary.FirstOrDefault(s => s.DictionaryID == item.DictionaryID);
             dataDictionary.Custom = item.Custom;
             args.Add(dataDictionary);
         }
         try
         {
             db.SaveChanges();
         }
         catch (Exception)
         {
             return(false);
         }
         try
         {
             Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
         }
         catch
         {
         }
         InitList();
         return(true);
     }
 }
        /// <summary>
        /// 编辑子级字典
        /// </summary>
        /// <param name="changedDataDictionary">子级实体对象</param>
        public void Edit(DataDictionary changedDataDictionary)
        {
            var args = new DataDictionaryDataChangedArgs
            {
                DictionaryChangeType = ChangeType.Edit
            };

            using (var db = new ShopEntities())
            {
                var entity         = db.DataDictionary.Where(p => p.DictionaryID == changedDataDictionary.DictionaryID).FirstOrDefault() ?? throw new OperateException("不存在指定的的子级");
                var dataDictionary = db.DataDictionary.Where(p => p.Value == changedDataDictionary.Value && p.FDictionaryID == entity.FDictionaryID).FirstOrDefault();
                if (entity.Value != changedDataDictionary.Value && dataDictionary != null)
                {
                    throw new OperateException("子级中存在相同值");
                }
                db.Entry(entity).State = EntityState.Detached;
                db.Entry(db.DataDictionary.Attach(changedDataDictionary)).State = EntityState.Modified;
                db.SaveChanges();
                args.Add(changedDataDictionary);
            }
            try
            {
                Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
            }
            catch
            {
            }
        }
        /// <summary>
        /// 编辑父级字典
        /// </summary>
        /// <param name="changedDataDictionary">父级实体对象</param>
        public void Edit(FDataDictionary changedDataDictionary)
        {
            var args = new DataDictionaryDataChangedArgs
            {
                DictionaryChangeType = ChangeType.Edit
            };

            using (var db = new ShopEntities())
            {
                var entity = db.FDataDictionary.Where(p => p.FDictionaryID == changedDataDictionary.FDictionaryID).FirstOrDefault() ?? throw new OperateException("指定的字典父级已删除");
                var data   = db.FDataDictionary.Where(p => p.Argument == changedDataDictionary.Argument).FirstOrDefault();
                if (entity.Argument != changedDataDictionary.Argument && data != null)
                {
                    throw new OperateException("已存在同值父级");
                }
                db.Entry(entity).State = EntityState.Detached;
                db.Entry(db.FDataDictionary.Attach(changedDataDictionary)).State = EntityState.Modified;
                db.SaveChanges();
                args.Add(changedDataDictionary);
            }
            try
            {
                Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
            }
            catch
            {
            }
        }
        /// <summary>
        /// 删除父级
        /// </summary>
        /// <param name="id">父级ID</param>
        public void DeleteF(int id)
        {
            var args = new DataDictionaryDataChangedArgs
            {
                DictionaryChangeType = ChangeType.Delete
            };

            using (var db = new ShopEntities())
            {
                var entity = db.FDataDictionary.FirstOrDefault(p => p.FDictionaryID == id) ?? throw new OperateException("数据库中不存在此父级");
                List <DataDictionary> chirenData = entity.DataDictionary.ToList();
                db.DataDictionary.RemoveRange(chirenData);
                db.FDataDictionary.Remove(entity);
                db.SaveChanges();
                args.Add(entity);
                foreach (var item in chirenData)
                {
                    args.Add(item);
                }
            }
            try
            {
                Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
            }
            catch
            {
            }
        }
        /// <summary>
        /// 删除子级
        /// </summary>
        /// <param name="fid">父级ID</param>
        /// <param name="id">子级ID</param>
        public void DeleteC(int fid, int id)
        {
            var args = new DataDictionaryDataChangedArgs
            {
                DictionaryChangeType = ChangeType.Delete
            };

            using (var db = new ShopEntities())
            {
                var entity = db.DataDictionary.FirstOrDefault(p => p.FDictionaryID == fid && p.DictionaryID == id) ?? throw new OperateException("数据库中不存在此子级");
                args.Add(entity);
                db.DataDictionary.Remove(entity);
                db.SaveChanges();
            }
            try
            {
                Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
            }
            catch
            {
            }
        }
        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="entity">参数实体对象</param>
        public void Add(DataDictionaryAddParams entity)
        {
            var args = new DataDictionaryDataChangedArgs
            {
                DictionaryChangeType = ChangeType.Add
            };

            using (var db = new ShopEntities())
            {
                //判断可空类型是否有值,如果有值返回true反之false
                if (entity.FID.HasValue)
                {
                    int value;
                    if (!int.TryParse(entity.Value, out value))
                    {
                        throw new OperateException("子级字典值应是数值");
                    }
                    if (db.DataDictionary.Where(p => p.Value == value && p.FDictionaryID == entity.FID).Count() > 0)
                    {
                        throw new OperateException("值重复");
                    }
                    var dataDictionary = new DataDictionary
                    {
                        FDictionaryID = entity.FID.Value,
                        Color         = entity.Color,
                        Icon          = entity.Icon,
                        Key           = entity.Key,
                        Custom        = entity.Custom,
                        Value         = value,
                        Name          = entity.Name
                    };
                    db.DataDictionary.Add(dataDictionary);
                    args.Add(dataDictionary);
                }
                else
                {
                    if (db.FDataDictionary.Where(p => p.Argument.Equals(entity.Value)).Count() > 0)
                    {
                        throw new OperateException("值重复");
                    }
                    var fDataDictionary = new FDataDictionary
                    {
                        Name     = entity.Key,
                        Argument = entity.Value,
                    };
                    db.FDataDictionary.Add(fDataDictionary);
                    args.Add(fDataDictionary);
                }
                if (!(db.SaveChanges() > 0))
                {
                    throw new OperateException("数据库错误,没有数据变化");
                }
            }
            try
            {
                Task.Run(() => DataDictionaryDataChanged?.Invoke(null, args));
            }
            catch
            {
            }
            InitList();
        }