public CompanyDTO Add(CompanyDTO companyDTO)
        {
            var company = companyDTO.ToModel();

            company.Id      = IdentityGenerator.NewSequentialGuid();
            company.Created = DateTime.UtcNow;
            if (companyDTO.Category != null)
            {
                company.Category = _CategoryRepository.Get(companyDTO.Category.Id);
            }
            else
            {
                company.Category = null;
            }

            if (company.Name.IsNullOrBlank())
            {
                throw new DefinedException(CommonMessageResources.Name_Empty);
            }

            if (_Repository.Exists(company))
            {
                throw new DataExistsException(string.Format(BaseMessagesResources.Company_Exists_WithValue, company.Name));
            }

            var ruleName = CommonMessageResources.SerialNumberRule_Company;
            var snRule   = SerialNumberRuleQuerier.FindBy(ruleName);

            if (snRule == null)
            {
                throw new DataNotFoundException(string.Format(BaseMessagesResources.SerialNumberRule_NotExists_WithValue, ruleName));
            }
            company.Sn = SerialNumberGenerator.GetSerialNumber(snRule.Prefix, snRule.UseDateNumber, snRule.NumberLength);

            _Repository.Add(company);

            #region 操作日志

            var companyDto = company.ToDto();

            OperateRecorder.RecordOperation(companyDto.Sn,
                                            BaseMessagesResources.Add_Company,
                                            companyDto.GetOperationLog());

            #endregion

            //commit the unit of work
            _Repository.UnitOfWork.Commit();

            return(company.ToDto());
        }
示例#2
0
        public ShipDTO Add(ShipDTO shipDTO)
        {
            var ship = shipDTO.ToModel();

            ship.Id      = IdentityGenerator.NewSequentialGuid();
            ship.Created = DateTime.UtcNow;

            if (ship.Name.IsNullOrBlank())
            {
                throw new DefinedException(CommonMessageResources.Name_Empty);
            }

            if (_Repository.Exists(ship))
            {
                throw new DataExistsException(string.Format(BaseMessagesResources.Ship_Exists_WithValue, ship.Name));
            }

            var ruleName = CommonMessageResources.SerialNumberRule_Ship;
            var snRule   = SerialNumberRuleQuerier.FindBy(ruleName);

            if (snRule == null)
            {
                throw new DataNotFoundException(string.Format(BaseMessagesResources.SerialNumberRule_NotExists_WithValue, ruleName));
            }
            ship.Sn = SerialNumberGenerator.GetSerialNumber(snRule.Prefix, snRule.UseDateNumber, snRule.NumberLength);

            _Repository.Add(ship);

            #region 操作日志

            var shipDto = ship.ToDto();

            OperateRecorder.RecordOperation(shipDto.Sn,
                                            BaseMessagesResources.Add_Ship,
                                            shipDto.GetOperationLog());

            #endregion

            //commit the unit of work
            _Repository.UnitOfWork.Commit();

            return(ship.ToDto());
        }
        public CategoryDTO Add(CategoryDTO categoryDTO)
        {
            var category = categoryDTO.ToModel();

            category.Id      = IdentityGenerator.NewSequentialGuid();
            category.Created = DateTime.UtcNow;
            if (category.Parent == null || category.Parent.Id == Guid.Empty)
            {
                category.Depth = 1;
            }
            else
            {
                category.Parent = _Repository.Get(categoryDTO.Parent.Id);
                category.Depth  = category.Parent.Depth + 1;
            }

            if (category.Name.IsNullOrBlank())
            {
                throw new DefinedException(CommonMessageResources.Name_Empty);
            }

            if (_Repository.Exists(category))
            {
                throw new DataExistsException(string.Format(BaseMessagesResources.Category_Exists_WithValue, category.Name));
            }

            if (category.Parent != null)
            {
                var rulePrefix = category.Parent.ChildSnRulePrefix;
                if (rulePrefix.IsNullOrBlank())
                {
                    throw new DefinedException(BaseMessagesResources.Category_Parent_SerialNumberRule_Prefix_Empty);
                }
                var ruleNumberLength = category.Parent.ChildSnRuleNumberLength;
                if (ruleNumberLength <= 0)
                {
                    throw new DefinedException(BaseMessagesResources.Category_Parent_SerialNumberRule_NumberLength_NotGreat_Than_Zero);
                }
                category.Sn = SerialNumberGenerator.GetSerialNumber(rulePrefix, false, ruleNumberLength);
            }
            else
            {
                var ruleName = CommonMessageResources.SerialNumberRule_Category;
                var snRule   = SerialNumberRuleQuerier.FindBy(ruleName);
                if (snRule == null)
                {
                    throw new DataNotFoundException(string.Format(BaseMessagesResources.SerialNumberRule_NotExists_WithValue, ruleName));
                }
                category.Sn = SerialNumberGenerator.GetSerialNumber(snRule.Prefix, snRule.UseDateNumber, snRule.NumberLength);
            }
            _Repository.Add(category);

            #region 操作日志

            var categoryDto = category.ToDto();

            OperateRecorder.RecordOperation(categoryDto.Sn,
                                            BaseMessagesResources.Add_Category,
                                            categoryDto.GetOperationLog());

            #endregion

            //commit the unit of work
            _Repository.UnitOfWork.Commit();

            return(category.ToDto());
        }