Example #1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //Verify
                if (string.IsNullOrEmpty(this.txtCode.Text.Trim()))
                {
                    throw new ApplicationException("编码不能为空");
                }
                if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
                {
                    throw new ApplicationException("名称不能为空");
                }
                if (string.IsNullOrEmpty(this.txtUnit.Text.Trim()))
                {
                    throw new ApplicationException("单位不能为空");
                }
                if (string.IsNullOrEmpty(this.txtStandard.Text.Trim()))
                {
                    throw new ApplicationException("规格不能为空");
                }
                if (this.cmbCategory.SelectedItem == null)
                {
                    throw new ApplicationException("品种不能为空");
                }
                if (this.cmbFrom.SelectedItem == null)
                {
                    throw new ApplicationException("产地不能为空");
                }

                //Save
                ModelService service = new ModelService();
                if (this._Model == null) //新建
                {
                    if (service.GetGoodsByCode(this.txtCode.Text.Trim()) != null)
                    {
                        throw new ApplicationException(string.Format("编码{0}已经被使用,请尝试其他编码。", this.txtCode.Text.Trim()));
                    }
                    Goods newModel = new Goods();
                    newModel.Code     = this.txtCode.Text.Trim();
                    newModel.Name     = this.txtName.Text.Trim();
                    newModel.Remark   = this.txtRemark.Text.Trim();
                    newModel.Unit     = this.txtUnit.Text.Trim();
                    newModel.Standard = this.txtStandard.Text.Trim();
                    newModel.Category = (GoodsCategory)this.cmbCategory.SelectedItem;
                    newModel.From     = (GoodsFrom)this.cmbFrom.SelectedItem;
                    newModel.Actived  = true;
                    service.CreateGoods(newModel, PermissionService.GetCurrentUser().Name);
                }
                else//修改
                {
                    this._Model.Code     = this.txtCode.Text.Trim();
                    this._Model.Name     = this.txtName.Text.Trim();
                    this._Model.Remark   = this.txtRemark.Text.Trim();
                    this._Model.Unit     = this.txtUnit.Text.Trim();
                    this._Model.Standard = this.txtStandard.Text.Trim();
                    this._Model.Category = (GoodsCategory)this.cmbCategory.SelectedItem;
                    this._Model.From     = (GoodsFrom)this.cmbFrom.SelectedItem;
                    service.SaveGoods(this._Model, PermissionService.GetCurrentUser().Name);
                }

                //Close dialog
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                ErrorHandler.OnError(ex);
            }
        }