增加 修改,裝載狀態(父品類)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DrugSaleSystem.DAL;
using DrugSaleSystem.Model;
using DrugSaleSystem.Components.Control;
using DrugSaleSystem.Components;

namespace DrugSaleSystem.ManagementCenter
{
    public partial class frmDrugCategoryEdit : frmBaseForm
    {
        #region 私有參數
        private List<GroupPermissionsInfo> gpInfos;
        #endregion
        #region 公共參數
        public int iDrugCategoryCode = 0;
        #endregion
        private static frmDrugCategoryEdit _Instance = null;


        public frmDrugCategoryEdit()
        {
            InitializeComponent();
            _Instance = this;
        }

        public static frmDrugCategoryEdit Instance
        {
            get
            {
                //第一次使用,如果沒有實例,創建一個 
                if (_Instance == null)
                {
                    _Instance = new frmDrugCategoryEdit();
                }
                return _Instance;
            }
        }
        /// <summary>
        /// 初始化

        /// </summary>
        public void InitData()
        {
            frmMain.Instance.StyleType = ConstInfo.const_Stype;
            ParentDrugCategoryInit();
            DrugCategoryInit();

        }
        private void ControlClesrInit()
        {
            txbDrugCategoryCode.Text = "";
            cbParentDrugCategory.Text = ""; 
            txbCategoryName.Text = "";
            txbDescription.Text = "";
            txbOperator.Text = "";
            txbDrugCategoryCode.Enabled = true;
            
        }
        /// <summary>
        /// 裝載父類別名稱
        /// </summary>
        private void ParentDrugCategoryInit()
        {
            cbParentDrugCategory.Items.Clear();
            List<DrugCategoryInfo> _DrugCategoryInfos = DrugCategory.Get(0, 0, "", 100, 1).DrugCategoryInfo;
            ComboBoxItem pItem = new ComboBoxItem();
            pItem.Text = "請選擇父類別";
            pItem.Value = "";
            cbParentDrugCategory.Items.Add(pItem);
            ComboBoxItem TopItem = new ComboBoxItem();
            TopItem.Text = "頂級類別";
            TopItem.Value = "0";
            cbParentDrugCategory.Items.Add(TopItem);
            foreach (DrugCategoryInfo info in _DrugCategoryInfos)
            {
                ComboBoxItem Item = new ComboBoxItem();
                Item.Text = info.CategoryName;
                Item.Value = info.DrugCategoryCode;
                cbParentDrugCategory.Items.Add(Item);
                List<DrugCategoryInfo> _ChildDrugCategoryInfos = DrugCategory.Get(0, info.DrugCategoryCode, "", 100, 1).DrugCategoryInfo;
                foreach (DrugCategoryInfo cinfo in _ChildDrugCategoryInfos)
                {
                    ComboBoxItem cItem = new ComboBoxItem();
                    cItem.Text = string.Format("┗{0}", cinfo.CategoryName);
                    cItem.Value = cinfo.DrugCategoryCode;
                    cbParentDrugCategory.Items.Add(cItem); 
                }
            }
            cbParentDrugCategory.SelectedIndex = 0;
        }

        private void DrugCategoryInit()
        {

            if (iDrugCategoryCode > 0)
            {
                DrugCategoryInfo info = DrugCategory.GetByDrugCategoryCode(iDrugCategoryCode);
                this.lbTitle.Text = string.Format("編輯品類[{0}]", info.CategoryName);
                txbDrugCategoryCode.Text = info.DrugCategoryCode.ToString();
                //cbParentDrugCategory.Text = info.ParentDrugCategory.ToString();
                txbCategoryName.Text = info.CategoryName;
                txbDescription.Text = info.Description;
                for (int i = 0; i < cbParentDrugCategory.Items.Count; i++)
                {
                    if (((ComboBoxItem)cbParentDrugCategory.Items[i]).Value.ToString() == info.ParentDrugCategory.ToString())
                    {
                        cbParentDrugCategory.SelectedIndex = i;
                        break;
                    }
                }
                txbOperator.Text = info.Operator;
                txbDrugCategoryCode.Enabled = false;
            }
            else
            {
                this.lbTitle.Text = "新增品類";
                ControlClesrInit();

            }
        }
        ///// <summary>
        ///// 裝載

        ///// </summary>
        //private void DrugCategoryStatusInit()
        //{
        //    cbParentDrugCategory.Items.Clear();
        //    DrugSaleSystem.Components.Control.ComboBoxItem pitem = new DrugSaleSystem.Components.Control.ComboBoxItem();
        //    pitem.Text = "請選擇狀態";
        //    pitem.Value = "-1";
        //    cbParentDrugCategory.Items.Add(pitem);
        //    Dictionary<string, string> list = CommStatusAttributeType.LoadCommStatusAttribute();
        //    foreach (var pair in list)
        //    {
        //        DrugSaleSystem.Components.Control.ComboBoxItem item = new DrugSaleSystem.Components.Control.ComboBoxItem();
        //        item.Text = pair.Value;
        //        item.Value = pair.Key;
        //        cbParentDrugCategory.Items.Add(item);
        //    }
        //    cbParentDrugCategory.SelectedIndex = 0;
        //}
        /// <summary>
        /// 關閉窗體
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 保存數據
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            #region 驗證
            if (string.IsNullOrEmpty(txbCategoryName.Text))
            {
                MessageBox.Show("請輸入品類名稱!");
                return;
            }
            #endregion
            DrugCategoryInfo editinfo = new DrugCategoryInfo();
            if (iDrugCategoryCode > 0)
            {
                #region 修改
                
                editinfo = DrugCategory.GetByDrugCategoryCode(iDrugCategoryCode);
                editinfo.DrugCategoryCode = Convert.ToInt32(txbDrugCategoryCode.Text);
                editinfo.ParentDrugCategory = int.Parse(((ComboBoxItem)cbParentDrugCategory.SelectedItem).Value.ToString());
                editinfo.CategoryName = txbCategoryName.Text;
                editinfo.Operator = txbOperator.Text;
                editinfo.Description = txbDescription.Text;
                if (DrugCategory.Update(editinfo) > 0)
                {
                    #region 記錄日誌
                    OperationLogInfo loginfo = new OperationLogInfo();
                    loginfo.OperatorNO = ConstInfo.const_UserCode.ToString();
                    loginfo.OperatorName = ConstInfo.const_UserTrueName;
                    loginfo.Body = string.Format("修改品類成功!類別編碼:{0}", editinfo.DrugCategoryCode);
                    loginfo.OperResult = "成功";
                    loginfo.Source = Clients.GetLocalIP();
                    loginfo.OperateDate = DateTime.Now;
                    OperationLog.Create(loginfo);
                    #endregion
                    frmDrugCategory.Instance.DrugCategoryInit();
                    MessageBox.Show("修改品類成功!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改品類失敗!!");
                }
                #endregion
            }
            else
            {
                #region 新增
                int DrugCategoryCode = 0;
                if(string.IsNullOrEmpty(txbDrugCategoryCode.Text))
                {
                    MessageBox.Show("請輸入品類編號!");
                    return;
                }
                if (!int.TryParse(txbDrugCategoryCode.Text, out DrugCategoryCode))
                {
                    MessageBox.Show("請輸入品類名稱!");
                    return;
                }
                editinfo.DrugCategoryCode = DrugCategoryCode;

                int ParentDrugCategory = 0;
                editinfo.ParentDrugCategory = int.Parse(((ComboBoxItem)cbParentDrugCategory.SelectedItem).Value.ToString());
                //if (cbParentDrugCategory.Text == "")
                //{
                //    ParentDrugCategory = 0;
                //}
                //else
                //{
                //    editinfo.ParentDrugCategory = Convert.ToInt32(cbParentDrugCategory.Text);
                //}
                editinfo.CategoryName = txbCategoryName.Text;
                editinfo.Description = txbDescription.Text;
                editinfo.Operator = ConstInfo.const_UserTrueName;
                editinfo.DrugCategoryID = DrugCategory.Create(editinfo);    //新增一條數據
                if (editinfo.DrugCategoryID > 0)
                {
                    #region 記錄日誌
                    OperationLogInfo loginfo = new OperationLogInfo();
                    loginfo.OperatorNO = ConstInfo.const_UserCode.ToString();
                    loginfo.OperatorName = ConstInfo.const_UserTrueName;
                    loginfo.Body = string.Format("添加品類成功!類別編碼:{0}", editinfo.DrugCategoryCode);
                    loginfo.OperResult = "成功";
                    loginfo.Source = Clients.GetLocalIP();
                    loginfo.OperateDate = DateTime.Now;
                    OperationLog.Create(loginfo);
                    #endregion
                    frmDrugCategory.Instance.DrugCategoryInit();
                    MessageBox.Show("添加品類成功!");
                    this.Close();
                }
                else if (editinfo.DrugCategoryID == -2)
                {
                    MessageBox.Show("添加品類失敗!該分類編碼已存在!");
                }
                else
                {
                    MessageBox.Show("添加品類失敗!!");
                }
                #endregion
            }

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章