private void ValidateEditDto(CustomerCategoryEditDto dto)
 {
     if (string.IsNullOrWhiteSpace(dto.Name))
     {
         throw new Exception($"错误,{_modelDescription}的名称不能为空!");
     }
 }
 public void Save(CustomerCategoryEditDto dto)
 {
     if (dto.UpdateId > 0)
     {
         Update(dto);
     }
     else
     {
         Add(dto);
     }
 }
        private void Add(CustomerCategoryEditDto dto)
        {
            ValidateEditDto(dto);

            if (DataDbContext.Set <CustomerCategory>().Any(cc => cc.Name != null && cc.Name == dto.Name))
            {
                throw new Exception($"错误,新增失败,名称:{dto.Name}的{_modelDescription}已经存在!");
            }

            var customerCategory = dto.MapTo <CustomerCategory>();

            customerCategory.CreatorTime    = DateTime.Now;
            customerCategory.LastModifyTime = DateTime.Now;
            DataDbContext.Set <CustomerCategory>().Add(customerCategory);
            DataDbContext.SaveChanges();
        }
        public void Update(CustomerCategoryEditDto dto)
        {
            ValidateEditDto(dto);

            var customerCategory = DataDbContext.Set <CustomerCategory>().FirstOrDefault(c => c.Id == dto.UpdateId);

            if (customerCategory == null)
            {
                throw new Exception($"错误,Id={dto.UpdateId} 的{_modelDescription}不存在!");
            }

            if (DataDbContext.Set <CustomerCategory>().Any(cc => cc.Id != customerCategory.Id && cc.Name != null && cc.Name == dto.Name))
            {
                throw new Exception($"错误,修改失败,名称:{dto.Name}的{_modelDescription}已经存在!");
            }

            dto.MapTo <CustomerCategory>(customerCategory);
            customerCategory.LastModifyTime = DateTime.Now;
            DataDbContext.SaveChanges();
        }