You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.9 KiB
C#

using Rs.Controls;
using Rs.DataAccess;
using Rs.Framework;
using Rs.MotionPlat.Commom;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewWindow.Model;
namespace Rs.MotionPlat.SysConfig
{
public partial class VarConfig : BaseFormHeader
{
public event Action<string> FormCloseEvent;
SqliteHelper db = new SqliteHelper();
DataTable data = new DataTable();
public VarConfig(Action<string> act)
{
InitializeComponent();
FormCloseEvent= act;
}
public VarConfig()
{
InitializeComponent();
}
private void VarConfig_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
cboxCategory.DropDownStyle= ComboBoxStyle.DropDownList;
string querySql = "select * from SysParameter where enable=1";
data=db.GetDataTable(querySql);
if(data != null && data.Rows.Count>0) {
var cate = data.AsEnumerable().GroupBy(g => g.Field<string>("category")).ToArray();
cboxCategory.Items.Add("all");
foreach( var c in cate)
{
cboxCategory.Items.Add(c.Key);
cboxCategory2.Items.Add(c.Key);
}
}
dataGridView1.DataSource = data;
cboxCategory.SelectedIndex = 0;
if(GlobalUser.IsOp() || GlobalUser.IsNologin())
{
dataGridView1.ReadOnly = true;
}
else if(GlobalUser.IsVender())
{
dataGridView1.AllowUserToAddRows = false;
}
}
void BindData()
{
Invoke(new Action(() => {
dataGridView1.DataSource = null;
string querySql = $"select * from SysParameter where enable=1";
if (cboxCategory.SelectedIndex > 0)
{
querySql += $" and category='{cboxCategory.SelectedItem.ToString()}'";
}
data = db.GetDataTable(querySql);
dataGridView1.DataSource = data;
}));
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex>=0)
{
UpdateRow(dataGridView1.Rows[e.RowIndex]);
Task.Run(() => {
BindData();
});
}
}
private void UpdateRow(DataGridViewRow row)
{
object ID = row.Cells["ID"].Value??"";
if(ID != null && !string.IsNullOrEmpty(ID.ToString()))
{
string FieldName = row.Cells["FieldName"].Value.ToString();
string FieldType = row.Cells["FieldType"].Value.ToString();
string FieldValue = row.Cells["FieldValue"].Value.ToString();
string Desc = row.Cells["Desc"].Value.ToString();
string Category = row.Cells["Category"].Value.ToString();
string Enable = row.Cells["Enable"].Value.ToString();
string updateSql = $"update SysParameter set FieldName='{FieldName}',FieldType='{FieldType}'," +
$"FieldValue='{FieldValue}',Desc='{Desc}',Category='{Category}',Enable={Enable} where id={ID}";
db.ExecuteNonQuery(updateSql);
}
else
{
string FieldName = row.Cells["FieldName"].Value.ToString();
string FieldType = row.Cells["FieldType"].Value.ToString();
string FieldValue = row.Cells["FieldValue"].Value.ToString();
string Desc = row.Cells["Desc"].Value.ToString();
string Category = row.Cells["Category"].Value.ToString();
string Enable = row.Cells["Enable"].Value.ToString() == "" ? "0" : "1";
string insertSQL = $"insert into SysParameter(FieldName,FieldType,FieldValue,Desc,Category,Enable) " +
$"values('{FieldName}','{FieldType}','{FieldValue}','{Desc}','{Category}',{1})";
db.ExecuteNonQuery(insertSQL);
}
SysConfigParam.Init();
}
private void cboxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex>=0)
{
UpdateRow(dataGridView1.Rows[e.RowIndex]);
BindData();
}
}
private void VarConfig_FormClosed(object sender, FormClosedEventArgs e)
{
FormCloseEvent?.Invoke("VarConfig");
}
private void btnSave_Click(object sender, EventArgs e)
{
string FieldName = txtFieldName.Text;
string querySql = $"select * from SysParameter where fieldname='{FieldName}'";
DataTable dt = db.GetDataTable(querySql);
if(dt!=null&&dt.Rows.Count>0)
{
Msg.ShowError($"Var name {FieldName} exist,please change anther name");
return;
}
string FieldType = cboxFieldType.SelectedItem.ToString();
string FieldValue =txtFieldValue.Text;
string Desc = txtDesc.Text;
string Category = cboxCategory2.Text.ToString();
string Enable = "1";
string insertSQL = $"insert into SysParameter(FieldName,FieldType,FieldValue,Desc,Category,Enable) " +
$"values('{FieldName}','{FieldType}','{FieldValue}','{Desc}','{Category}',{1})";
if(db.ExecuteNonQuery(insertSQL)>0)
{
SysConfigParam.Init();
BindData();
}
}
}
}