Newer
Older
EMS_REFACTOR / FrmRESC.cs
nn-203 on 26 Jul 2017 7 KB first commit
using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace Cyberpipe
{
    public partial class FrmRESC : Office2007Form
    {
        int id;
        public FrmRESC(int _id)
        {
            InitializeComponent();
            id = _id;
        }
        /// <summary>
        /// 添加、保存按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (comboBoxEx1.SelectedIndex == -1)
            {
                MessageBox.Show("请选择分系统名称!");
                return;
            }
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入资源名称!");
                return;
            }
            string resourceParentName = comboxXResourceParentName.Text.Trim();
            if (resourceParentName == "")
            {
                resourceParentName = "NULL";
            }
            string sql;
            try
            {
                sql = "select \"id\" from casic_app where \"name\" = '" + comboBoxEx1.SelectedItem + "'";

                DataTable dt = OledbHelper.QueryTable(sql);
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        int aid = Convert.ToInt32(dt.Rows[0][0].ToString());
                        if (id == -1)
                        {
                            sql = "select * from casic_resc where \"name\" ='" + txtName.Text.Trim() + "' and \"pname\" ='" + comboxXResourceParentName.Text.Trim() + "' and \"aid\" =" + aid;
                            if (OledbHelper.QueryTable(sql).Rows.Count > 0)
                            {
                                MessageBox.Show("该分系统下已录入该资源信息!");
                                return;
                            }
                            sql = "insert into casic_resc(\"name\",\"desc\",\"aid\",\"pname\") values('" + txtName.Text.Trim() + "','" + txtDesc.Text.Trim() + "'," + aid + ",'" + resourceParentName + "')";
                        }
                        else
                        {
                            if (txtName.Text == table.Rows[0][1].ToString().Trim() &&
                            txtDesc.Text == table.Rows[0][2].ToString().Trim() &&
                            comboBoxEx1.SelectedItem.ToString() == table.Rows[0][0].ToString())
                            {
                                DialogResult = DialogResult.OK;
                                Close();
                                return;
                            }
                            sql = "select * from casic_resc where \"name\" ='" + txtName.Text.Trim() + "'  and \"pname\" ='" + comboxXResourceParentName.Text.Trim() + "' and \"aid\" =" + aid + " and \"id\" <>" + id;
                            if (OledbHelper.QueryTable(sql).Rows.Count > 0)
                            {
                                MessageBox.Show("该分系统下已录入该资源信息!");
                                return;
                            }
                            sql = "update casic_resc set \"name\"='" + txtName.Text.Trim() + "',\"desc\" = '" + txtDesc.Text.Trim() + "',\"aid\" = " + aid + ",\"pname\"='" + resourceParentName + "' where \"id\" = " + id;
                        }
                        if (OledbHelper.sqlExecuteNonQuery(sql) > 0)
                        {
                            if (id != -1)
                            {
                                sql = "update casic_resc set \"pname\"='" + txtName.Text.Trim() + "' where \"pname\"='" + resourceName + "'";
                                OledbHelper.sqlExecuteNonQuery(sql);
                            }
                            MessageBox.Show("保存成功!");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogError.PublishError(ex);
            }
        }
        /// <summary>
        /// 关闭按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }
        DataTable table;
        string resourceName = "";
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmRESC_Load(object sender, EventArgs e)
        {
            string sql = "select \"id\",\"name\" from casic_app";
            table = OledbHelper.QueryTable(sql);
            if (table != null)
            {
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        comboBoxEx1.Items.Add(table.Rows[i]["name"].ToString());
                    }

                    sql = "select \"name\",\"pname\" from casic_resc";
                    table = OledbHelper.QueryTable(sql);
                    if (table != null && table.Rows.Count > 0)
                    {
                        for (int j = 0; j < table.Rows.Count; j++)
                        {
                            if (!comboxXResourceParentName.Items.Contains(table.Rows[j][0].ToString().Trim()))
                            {
                                comboxXResourceParentName.Items.Add(table.Rows[j][0].ToString().Trim());
                            }
                            if (!comboxXResourceParentName.Items.Contains(table.Rows[j][1].ToString().Trim()))
                            {
                                comboxXResourceParentName.Items.Add(table.Rows[j][1].ToString().Trim());
                            }
                        }
                    }

                    if (id != -1)
                    {
                        Text = "修改资源信息";
                        buttonX1.Text = "保存";

                        sql = "select casic_app.\"name\",casic_resc.\"name\",casic_resc.\"desc\",casic_resc.\"pname\"  from casic_app inner join casic_resc on casic_app.\"id\" = casic_resc.\"aid\" where casic_resc.\"id\" = " + id;
                        table = OledbHelper.QueryTable(sql);
                        if (table != null && table.Rows.Count > 0)
                        {
                            for (int j = 0; j < comboBoxEx1.Items.Count; j++)
                            {
                                if (comboBoxEx1.Items[j].ToString() == table.Rows[0][0].ToString())
                                {
                                    comboBoxEx1.SelectedIndex = j;
                                    break;
                                }
                            }
                            for (int j = 0; j < comboxXResourceParentName.Items.Count; j++)
                            {
                                if (comboxXResourceParentName.Items[j].ToString() == table.Rows[0][3].ToString())
                                {
                                    comboxXResourceParentName.SelectedIndex = j;
                                    break;
                                }
                            }
                            txtName.Text = table.Rows[0][1].ToString().Trim();
                            resourceName = table.Rows[0][1].ToString().Trim();
                            txtDesc.Text = table.Rows[0][2].ToString().Trim();
                        }                        
                    }
                }
            }
        }
    }
}