457 lines
19 KiB
C#
457 lines
19 KiB
C#
using Models;
|
||
using Sunny.UI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using static System.Net.WebRequestMethods;
|
||
|
||
namespace LeatherApp.Page
|
||
{
|
||
public partial class FSysSetting : UIPage
|
||
{
|
||
Service.UserService service = new Service.UserService();
|
||
|
||
private User currEditUserInfo;
|
||
public FSysSetting()
|
||
{
|
||
InitializeComponent();
|
||
initData();
|
||
|
||
#region dataGridView设置
|
||
uiDataGridView1.AutoGenerateColumns = false;//自动创建列
|
||
uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
|
||
uiDataGridView1.AllowUserToResizeRows = true;//用户调整行大小
|
||
uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
|
||
uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格
|
||
//显示行号与列宽度自动调整
|
||
uiDataGridView1.RowHeadersVisible = true;
|
||
uiDataGridView1.RowHeadersWidth = 30;
|
||
uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
|
||
uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头
|
||
{
|
||
Utils.Util.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e);
|
||
};
|
||
uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
|
||
//for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序
|
||
// uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
|
||
|
||
//行列交叉处标题
|
||
//if (uiDataGridView1.RowHeadersVisible) uiDataGridView1.TopLeftHeaderCell.Value = "SPH/CYL";
|
||
|
||
//事件
|
||
this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容
|
||
//this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行
|
||
this.uiDataGridView1.CellClick += UiDataGridView1_CellClick;
|
||
#endregion
|
||
#region 分页及页脚合计设置
|
||
this.uiPagination1.PageChanged += new Sunny.UI.UIPagination.OnPageChangeEventHandler(this.uiPagination1_PageChanged);
|
||
//设置分页控件每页数量
|
||
uiPagination1.PageSize = 12;
|
||
//设置统计绑定的表格
|
||
//uiDataGridViewFooter1.DataGridView = uiDataGridView1;
|
||
//激活第1第,触发uiPagination1_PageChanged
|
||
uiPagination1.ActivePage = 1;
|
||
#endregion
|
||
}
|
||
|
||
private void initData(bool onlySetting = false)
|
||
{
|
||
if (!onlySetting)
|
||
{
|
||
cmbRole.ValueMember = "Id";
|
||
cmbRole.DisplayMember = "Name";
|
||
cmbRole.DataSource = service.GetRoleItems();
|
||
}
|
||
|
||
|
||
//DEV
|
||
cmbLight_PortName.Items.AddRange(Utils.SSerialPort.getPortNames());
|
||
cmbLight_PortName.Text = Config.Light_PortName;
|
||
|
||
cmbJM_PortName.Items.AddRange(Utils.SSerialPort.getPortNames());
|
||
cmbJM_PortName.Text = Config.JM_PortName;
|
||
|
||
txtIOCard_DeviceNum.Text = Config.IOCard_DeviceNum;
|
||
|
||
//LIB
|
||
numcm2px_x.Text=Config.cm2px_x.ToString();
|
||
numcm2px_y.Text = Config.cm2px_y.ToString();
|
||
numMarginHoleWidth.Text= Config.MarginHoleWidth.ToString();
|
||
numMiddleSuperposition.Text= Config.MiddleSuperposition.ToString();
|
||
txtImagePath.Text= Config.ImagePath;
|
||
ckbSaveAllImage.Checked = Config.IsSaveAllImage;
|
||
ckbSaveDefectSourceImage.Checked = Config.IsSaveDefectSourceImage;
|
||
ckbSaveDefectCutImage.Checked = Config.IsSaveDefectCutImage;
|
||
|
||
tbCHLabeel.Text = Config.ThicknessNames;
|
||
//LOG
|
||
txtLogPath.Text= Config.LogPath;
|
||
|
||
numCutDis.Text = Config.CutDis.ToString();
|
||
chIP.Text = Config.CeHouIP;
|
||
|
||
cbJinShu.Checked = Config.OpenJinShuJianCe;
|
||
cbHD.Checked = Config.OpenHouDuJiLu;
|
||
|
||
cbJM.Checked = Config.OpenJMStop;
|
||
|
||
numKanban.Text = Config.StopLookDis.ToString();
|
||
numRM.Text = Config.residueWarnningLen.ToString();
|
||
numDM.Text = Config.orderWarnningLen.ToString();
|
||
|
||
cbClear.Checked = Config.OpenClearPic;
|
||
clearDays.Text = Config.ClearDays.ToString();
|
||
|
||
uiCheckBox1.Checked = Config.OpenOverSpeed;
|
||
}
|
||
private void saveConfig()
|
||
{
|
||
IniFile ini = new IniFile(Config.appBasePath + "\\SysConfig.ini");
|
||
//[LOG]
|
||
ini.Write("LOG", "LogPath", this.txtLogPath.Text);
|
||
//[DEV]
|
||
ini.Write("DEV", "IOCard_DeviceNum", this.txtIOCard_DeviceNum.Text.Trim());
|
||
ini.Write("DEV", "Light_PortName", this.cmbLight_PortName.Text);
|
||
|
||
ini.Write("DEV", "JM_PortName", this.cmbJM_PortName.Text);
|
||
ini.Write("DEV", "ThicknessNames", this.tbCHLabeel.Text);
|
||
//[LIB]
|
||
ini.Write("LIB", "cm2px_x", Convert.ToInt32(this.numcm2px_x.Text));
|
||
ini.Write("LIB", "cm2px_y", Convert.ToInt32(this.numcm2px_y.Text));
|
||
ini.Write("LIB", "MarginHoleWidth", Convert.ToInt32(this.numMarginHoleWidth.Text));
|
||
ini.Write("LIB", "MiddleSuperposition", Convert.ToInt32(this.numMiddleSuperposition.Text));
|
||
|
||
ini.Write("LIB", "ImagePath", this.txtImagePath.Text.Trim());
|
||
ini.Write("LIB", "IsSaveAllImage", this.ckbSaveAllImage.Checked);
|
||
ini.Write("LIB", "IsSaveDefectSourceImage", this.ckbSaveDefectSourceImage.Checked);
|
||
ini.Write("LIB", "IsSaveDefectCutImage", this.ckbSaveDefectCutImage.Checked);
|
||
|
||
ini.Write("Fun", "CutDis", this.numCutDis.Text);
|
||
|
||
ini.Write("DEV", "CeHouIP", this.chIP.Text);
|
||
ini.Write("DEV", "CeHouPort", 64000);
|
||
|
||
ini.Write("Fun", "OpenJinShuJianCe", this.cbJinShu.Checked);
|
||
ini.Write("Fun", "OpenHouDuJiLu", this.cbHD.Checked);
|
||
ini.Write("Fun", "OpenJMStop", this.cbJM.Checked);
|
||
|
||
ini.Write("Fun", "StopLookDis", this.numKanban.Text);
|
||
ini.Write("DEV", "residueWarnningLen", this.numRM.Text);
|
||
ini.Write("DEV", "orderWarnningLen", this.numDM.Text);
|
||
|
||
ini.Write("Fun", "OpenClearPic", this.cbClear.Checked);
|
||
ini.Write("Fun", "ClearDays", this.clearDays.Text);
|
||
ini.Write("Fun", "OpenOverSpeed", this.uiCheckBox1.Checked);
|
||
//
|
||
ini.UpdateFile();
|
||
}
|
||
private void sortBtns()
|
||
{
|
||
List<Control> ctls = new List<Control>();
|
||
foreach (Control item in fpnUserOpBtns.Panel.Controls)
|
||
ctls.Add(item);
|
||
int w = fpnUserOpBtns.ClientSize.Width;
|
||
int marge = (w - ctls.Sum(x => x.Width)) / 2;
|
||
Padding oldM = ctls[0].Margin;
|
||
ctls.First().Margin = new Padding(marge, oldM.Top, oldM.Right, oldM.Bottom);
|
||
ctls.Last().Margin = new Padding(oldM.Left, oldM.Top, oldM.Right, marge);
|
||
}
|
||
private void resetUserInfo()
|
||
{
|
||
this.currEditUserInfo = null;
|
||
this.uiDataGridView1.CurrentCell = null;
|
||
this.txtUserCode.Text = this.txtUserName.Text =this.txtUserNote.Text= this.txtPassword.Text = this.txtPassword2.Text = "";
|
||
this.ckbUserState.Checked = true;
|
||
this.txtUserCode.Enabled = this.pnlUserPassword.Visible = true;
|
||
|
||
this.btnUserNew.Visible =this.btnUserDel.Visible= this.btnResetPassword.Visible = false;
|
||
//sortBtns();
|
||
}
|
||
/// <summary>
|
||
/// 分页控件页面切换事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="pagingSource"></param>
|
||
/// <param name="pageIndex"></param>
|
||
/// <param name="count"></param>
|
||
private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
|
||
{
|
||
//未连接数据库,通过模拟数据来实现
|
||
//一般通过ORM的分页去取数据来填充
|
||
//pageIndex:第几页,和界面对应,从1开始,取数据可能要用pageIndex - 1
|
||
//count:单页数据量,也就是PageSize值
|
||
int totalCount=0;
|
||
var list = service.GetListNav(pageIndex-1, count, ref totalCount, null);
|
||
|
||
uiDataGridView1.DataSource = list;
|
||
uiPagination1.TotalCount= totalCount;
|
||
//表脚合计
|
||
//uiDataGridViewFooter1.Clear();
|
||
//uiDataGridViewFooter1["Column1"] = "合计:";
|
||
//uiDataGridViewFooter1["Column2"] = "Column2_" + pageIndex;
|
||
//uiDataGridViewFooter1["Column3"] = "Column3_" + pageIndex;
|
||
//uiDataGridViewFooter1["Column4"] = "Column4_" + pageIndex;
|
||
|
||
//
|
||
this.uiDataGridView1.CurrentCell = null;
|
||
//this.uiDataGridView1.ClearSelection();
|
||
this.resetUserInfo();
|
||
}
|
||
private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
|
||
{
|
||
var list = uiDataGridView1.DataSource as List<User>;
|
||
for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
|
||
{
|
||
if (list[i].RoleInfo != null)
|
||
uiDataGridView1.Rows[i].Cells["colRoleName"].Value = list[i].RoleInfo.Name;
|
||
}
|
||
}
|
||
private void UiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
|
||
{
|
||
if (e.RowIndex < 0)
|
||
return;
|
||
|
||
var list = uiDataGridView1.DataSource as List<User>;
|
||
currEditUserInfo = list[e.RowIndex];
|
||
this.txtUserCode.Text = currEditUserInfo.Code;
|
||
this.txtUserName.Text = currEditUserInfo.Name;
|
||
this.cmbRole.SelectedValue = currEditUserInfo.RoleId;
|
||
this.Banzu.Text = currEditUserInfo.WorkTeam;
|
||
this.ckbUserState.Checked = currEditUserInfo.State;
|
||
this.txtUserNote.Text = currEditUserInfo.Note;
|
||
//
|
||
this.txtUserCode.Enabled = this.pnlUserPassword.Visible = false;
|
||
this.btnUserNew.Visible = this.btnUserSave.Visible= this.btnUserDel.Visible= this.btnResetPassword.Visible = true;
|
||
}
|
||
|
||
private void btnUserNew_Click(object sender, EventArgs e)
|
||
{
|
||
resetUserInfo();
|
||
}
|
||
private void btnUserSave_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
string szCode = this.txtUserCode.Text.Trim();
|
||
string szName = this.txtUserName.Text.Trim();
|
||
string szNote = this.txtUserNote.Text.Trim();
|
||
int roleId = (int)this.cmbRole.SelectedValue;
|
||
string strBanzu = this.Banzu.Text;
|
||
|
||
if (szCode == "" || szName == "")
|
||
throw new Exception("请填写帐号和名称!");
|
||
if (currEditUserInfo == null)
|
||
{
|
||
if (this.txtPassword.Text != this.txtPassword2.Text)
|
||
throw new Exception("两次密码不一致!");
|
||
|
||
currEditUserInfo=new User();
|
||
currEditUserInfo.Password = Utils.Util.GetMD5(this.txtPassword.Text);
|
||
currEditUserInfo.CreateUserCode = Config.loginUser.Code;
|
||
}
|
||
|
||
currEditUserInfo.Code = szCode;
|
||
currEditUserInfo.Name = szName;
|
||
currEditUserInfo.RoleId = roleId;
|
||
currEditUserInfo.WorkTeam = strBanzu;
|
||
currEditUserInfo.State = this.ckbUserState.Checked;
|
||
currEditUserInfo.Note = szNote;
|
||
|
||
currEditUserInfo.ModifyUserCode = Config.loginUser.Code;
|
||
bool result;
|
||
if (currEditUserInfo.Id == 0)
|
||
{
|
||
result = service.Insert(currEditUserInfo);
|
||
if (result) uiPagination1.ActivePage = uiPagination1.ActivePage;//刷新
|
||
}
|
||
else
|
||
{
|
||
result = service.Update(currEditUserInfo);
|
||
if (result) this.uiDataGridView1.Refresh();//刷新更新单元格
|
||
}
|
||
|
||
if (!result)
|
||
throw new Exception("保存失败!");
|
||
|
||
UIMessageTip.ShowOk("保存成功!", 1000);
|
||
this.resetUserInfo();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError( ex.Message, 2000);
|
||
}
|
||
}
|
||
private void btnUserDel_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (currEditUserInfo == null)
|
||
return;
|
||
|
||
if (currEditUserInfo.Code == "admin" || currEditUserInfo.Id==1)
|
||
throw new Exception("管理员不可删除!");
|
||
|
||
if (!service.Delete(currEditUserInfo))
|
||
throw new Exception("删除失败!");
|
||
|
||
uiPagination1.ActivePage = uiPagination1.ActivePage;//刷新
|
||
UIMessageTip.ShowOk("删除成功!", 1000);
|
||
this.resetUserInfo();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
}
|
||
}
|
||
private void btnResetPassword_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (currEditUserInfo == null)
|
||
return;
|
||
|
||
currEditUserInfo.Password = Utils.Util.GetMD5("");
|
||
//只更新指定列
|
||
if (!service.Update(it => new User() { Password = currEditUserInfo.Password }, it => it.Id == currEditUserInfo.Id))
|
||
throw new Exception("重置失败!");
|
||
|
||
UIMessageTip.ShowOk("重置成功,新密码为空!", 1500);
|
||
this.resetUserInfo();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
}
|
||
}
|
||
private void txtLogPath_ButtonClick(object sender, EventArgs e)
|
||
{
|
||
string dir = "";
|
||
if (DirEx.SelectDirEx("打开文件夹", ref dir))
|
||
txtLogPath.Text = dir;
|
||
}
|
||
|
||
private void txtImagePath_ButtonClick(object sender, EventArgs e)
|
||
{
|
||
string dir = "";
|
||
if (DirEx.SelectDirEx("打开文件夹", ref dir))
|
||
txtImagePath.Text = dir;
|
||
}
|
||
|
||
private void fpnUserOpBtns_ClientSizeChanged(object sender, EventArgs e)
|
||
{
|
||
sortBtns();
|
||
}
|
||
|
||
private void btnConfig_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
saveConfig();
|
||
UIMessageTip.ShowOk("保存成功,重新开启设备后生效!", 1500);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
}
|
||
}
|
||
|
||
private void btnMinSave_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
saveConfig();
|
||
UIMessageTip.ShowOk("保存成功,重新开启设备后生效!", 1500);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
}
|
||
}
|
||
private void btnMinReload_Click(object sender, EventArgs e)
|
||
{
|
||
initData(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 只触发一次,第一次切换为显示时触发
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void FSysSetting_Shown(object sender, EventArgs e)
|
||
{
|
||
this.uiDataGridView1.ClearSelection();
|
||
}
|
||
|
||
|
||
private void FSysSetting_Paint(object sender, PaintEventArgs e)
|
||
{
|
||
btnMinSave.Top = btnMinSave1.Top = btnMinSave2.Top = 0;
|
||
btnMinReload.Top = btnMinReload1.Top = btnMinReload2.Top = 0;
|
||
}
|
||
|
||
private void uiSymbolButton1_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
string lsPath = "";
|
||
FolderBrowserDialog dlg = new FolderBrowserDialog();
|
||
dlg.SelectedPath = (lsPath != "" ? lsPath : Path.GetFullPath("."));
|
||
if (dlg.ShowDialog() == DialogResult.OK)
|
||
{
|
||
lsPath = dlg.SelectedPath;
|
||
if (string.IsNullOrWhiteSpace(lsPath))
|
||
return;
|
||
|
||
//lsPath += "\\DB" + DateTime.Now.ToString("yyyyMMddHHmm") + ".bak";
|
||
lsPath += "\\backup_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".sql";
|
||
Service.InitDB.BackupDataBase(lsPath);
|
||
MessageBox.Show("备份成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void uiSymbolButton2_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
string dbPath = "";
|
||
OpenFileDialog dlg = new OpenFileDialog();
|
||
dlg.Title = "选择文件";
|
||
//fie.InitialDirectory = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
|
||
dlg.Filter = "所有文件|*.*"; //设置文件类型
|
||
if (dlg.ShowDialog() == DialogResult.OK)
|
||
{
|
||
dbPath = dlg.FileName;
|
||
if (string.IsNullOrWhiteSpace(dbPath))
|
||
return;
|
||
if (MessageBox.Show($"是否导入{dbPath}?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
|
||
{
|
||
bool rel = Service.InitDB.RestoreDB(dbPath);
|
||
if (rel)
|
||
{
|
||
MessageBox.Show("导入成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
MessageBox.Show("导入失败!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|