2025-12-22 21:14:34 +08:00
using BLL ;
using BLL.Common ;
using FineUIPro.Web.DataShow ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.SqlClient ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using System.Web.Services.Description ;
using WIA ;
using AspNet = System . Web . UI . WebControls ;
namespace FineUIPro.Web.ProjectData
{
public partial class ProjectDevices : PageBase
{
#region 加 载
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load ( object sender , EventArgs e )
{
if ( ! IsPostBack )
{
Funs . DropDownPageSize ( this . ddlPageSize ) ;
this . ddlPageSize . SelectedValue = Grid1 . PageSize . ToString ( ) ;
2025-12-25 20:17:07 +08:00
2025-12-22 21:14:34 +08:00
// 绑定表格
this . BindGrid ( ) ;
////权限按钮方法
this . GetButtonPower ( ) ;
}
}
#endregion
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid ( )
{
string strSql = @" Select * ,case when [isInOut] =1 then '进' else '出' end [IsInOutValue] ,case when [YunMouDeviceId] is not null and [YunMouDeviceId] <> '' then '是' else '' end [DeviceToYunMou] from Project_Devices WHERE " ;
List < SqlParameter > listStr = new List < SqlParameter > ( ) ;
strSql + = " ProjectId = @ProjectId" ;
listStr . Add ( new SqlParameter ( "@ProjectId" , this . CurrUser . LoginProjectId ) ) ;
if ( ! string . IsNullOrEmpty ( this . txtDeviceName . Text . Trim ( ) ) )
{
strSql + = " AND DeviceName LIKE @DeviceName" ;
listStr . Add ( new SqlParameter ( "@DeviceName" , "%" + this . txtDeviceName . Text . Trim ( ) + "%" ) ) ;
}
SqlParameter [ ] parameter = listStr . ToArray ( ) ;
DataTable tb = SQLHelper . GetDataTableRunText ( strSql , parameter ) ;
Grid1 . RecordCount = tb . Rows . Count ;
tb = GetFilteredTable ( Grid1 . FilteredData , tb ) ;
var table = this . GetPagedDataTable ( Grid1 , tb ) ;
Grid1 . DataSource = table ;
2025-12-25 20:17:07 +08:00
Grid1 . DataBind ( ) ;
2025-12-22 21:14:34 +08:00
}
#region 操 作 Events
/// <summary>
/// 右键删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuDelete_Click ( object sender , EventArgs e )
{
if ( Grid1 . SelectedRowIndexArray . Length > 0 )
{
string unitName = string . Empty ;
foreach ( int rowIndex in Grid1 . SelectedRowIndexArray )
{
string deviceId = Grid1 . DataKeys [ rowIndex ] [ 0 ] . ToString ( ) ;
Model . Project_Devices devices = Funs . DB . Project_Devices . FirstOrDefault ( x = > x . DeviceId = = deviceId ) ;
var token = YunMouHelper . getToken ( ) ;
YunMouHelper . deleteDevices ( devices . DeviceSerial , token ) ;
Funs . DB . Project_Devices . DeleteOnSubmit ( devices ) ;
Funs . DB . SubmitChanges ( ) ;
}
BindGrid ( ) ;
if ( string . IsNullOrEmpty ( unitName ) )
{
ShowNotify ( "删除数据成功!" , MessageBoxIcon . Success ) ;
}
}
}
#endregion
#region 排 序 分 页
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange ( object sender , GridPageEventArgs e )
{
BindGrid ( ) ;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged ( object sender , EventArgs e )
{
Grid1 . PageSize = Convert . ToInt32 ( ddlPageSize . SelectedValue ) ;
BindGrid ( ) ;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort ( object sender , FineUIPro . GridSortEventArgs e )
{
BindGrid ( ) ;
}
#endregion
/// <summary>
/// 增加按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click ( object sender , EventArgs e )
{
PageContext . RegisterStartupScript ( Window1 . GetShowReference ( "ProjectDevicesEdit.aspx" , "添加设备" , 800 , 300 ) ) ;
}
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_RowDoubleClick ( object sender , GridRowClickEventArgs e )
{
this . EditData ( ) ;
}
/// <summary>
/// 右键编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuEdit_Click ( object sender , EventArgs e )
{
this . EditData ( ) ;
}
protected void btnMenuDeviceToYunMou_Click ( object sender , EventArgs e )
{
if ( Grid1 . SelectedRowIndexArray . Length = = 0 )
{
Alert . ShowInTop ( "请至少选择一条记录!" , MessageBoxIcon . Warning ) ;
return ;
}
2025-12-25 20:17:07 +08:00
var device = Funs . DB . Project_Devices . FirstOrDefault ( x = > x . DeviceId = = Grid1 . SelectedRowID ) ;
2025-12-22 21:14:34 +08:00
if ( device ! = null )
{
var project = Funs . DB . Base_Project . FirstOrDefault ( x = > x . ProjectId = = CurrUser . LoginProjectId ) ;
var token = YunMouHelper . getToken ( ) ;
2026-01-04 15:03:51 +08:00
string addMessage = "" ;
2025-12-22 21:14:34 +08:00
string data ;
data = Regex . Replace ( project . ProjectCode , "[^0-9A-Fa-f]" , "" , RegexOptions . IgnoreCase ) ;
2026-01-04 15:03:51 +08:00
var YunMouDeviceId = YunMouHelper . addDevices ( device . DeviceSerial , data , device . ValidateCode , token , out addMessage ) ; //添加设备
2025-12-22 21:14:34 +08:00
if ( ! string . IsNullOrEmpty ( YunMouDeviceId ) )
{
device . YunMouDeviceId = YunMouDeviceId ;
Funs . DB . SubmitChanges ( ) ;
ShowNotify ( "同步成功" , MessageBoxIcon . Success ) ;
BindGrid ( ) ;
}
else
{
2026-01-04 15:03:51 +08:00
Alert . ShowInTop ( "同步设备出错:" + addMessage , MessageBoxIcon . Warning ) ;
2025-12-22 21:14:34 +08:00
}
}
}
protected void btnMenuSyncPermission_Click ( object sender , EventArgs e )
{
if ( Grid1 . SelectedRowIndexArray . Length = = 0 )
{
Alert . ShowInTop ( "请至少选择一条记录!" , MessageBoxIcon . Warning ) ;
return ;
}
var device = Funs . DB . Project_Devices . FirstOrDefault ( x = > x . DeviceId = = Grid1 . SelectedRowID ) ;
if ( device ! = null )
{
var project = Funs . DB . Base_Project . FirstOrDefault ( x = > x . ProjectId = = CurrUser . LoginProjectId ) ;
var token = YunMouHelper . getToken ( ) ;
var res = YunMouHelper . addDevicesToGroups ( project . YunMouGroupId , new string [ ] { device . DeviceSerial } , token ) ; //添加到权限组
2025-12-25 20:17:07 +08:00
YunMouHelper . setDefence ( device . DeviceSerial , "1" , token ) ;
2025-12-22 21:14:34 +08:00
if ( string . IsNullOrEmpty ( res ) )
{
Alert . ShowInTop ( "关联权限出错!" , MessageBoxIcon . Warning ) ;
}
2025-12-25 20:17:07 +08:00
else
{
2025-12-22 21:14:34 +08:00
device . YunMouPermission = "是" ;
Funs . DB . SubmitChanges ( ) ;
ShowNotify ( "关联成功" , MessageBoxIcon . Success ) ;
BindGrid ( ) ;
}
}
}
2025-12-25 20:17:07 +08:00
2025-12-22 21:14:34 +08:00
protected void btnMenuDeletePermission_Click ( object sender , EventArgs e )
{
if ( Grid1 . SelectedRowIndexArray . Length = = 0 )
{
Alert . ShowInTop ( "请至少选择一条记录!" , MessageBoxIcon . Warning ) ;
return ;
}
var device = Funs . DB . Project_Devices . FirstOrDefault ( x = > x . DeviceId = = Grid1 . SelectedRowID ) ;
if ( device ! = null )
{
var project = Funs . DB . Base_Project . FirstOrDefault ( x = > x . ProjectId = = CurrUser . LoginProjectId ) ;
var token = YunMouHelper . getToken ( ) ;
var res = YunMouHelper . deleteDevicesFromGroups ( project . YunMouGroupId , new string [ ] { device . DeviceSerial } , token ) ; //从权限组移除设备
if ( string . IsNullOrEmpty ( res ) )
{
Alert . ShowInTop ( "关联权限出错!" , MessageBoxIcon . Warning ) ;
}
else
{
device . YunMouPermission = null ;
Funs . DB . SubmitChanges ( ) ;
BindGrid ( ) ;
}
}
}
/// <summary>
/// 编辑数据方法
/// </summary>
private void EditData ( )
{
if ( Grid1 . SelectedRowIndexArray . Length = = 0 )
{
Alert . ShowInTop ( "请至少选择一条记录!" , MessageBoxIcon . Warning ) ;
return ;
}
2025-12-25 20:17:07 +08:00
PageContext . RegisterStartupScript ( Window1 . GetShowReference ( String . Format ( "ProjectDevicesEdit.aspx?DeviceId={0}" , Grid1 . SelectedRowID ) , "编辑项目单位" , 800 , 300 ) ) ;
2025-12-22 21:14:34 +08:00
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Window1_Close ( object sender , EventArgs e )
{
BindGrid ( ) ;
}
#region 获 取 按 钮 权 限
/// <summary>
/// 获取按钮权限
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
private void GetButtonPower ( )
{
if ( Request . Params [ "value" ] = = "0" )
{
return ;
}
string menuId = BLL . Const . ProjectDevicesMenuId ;
2025-12-25 20:17:07 +08:00
2025-12-22 21:14:34 +08:00
var buttonList = BLL . CommonService . GetAllButtonList ( this . CurrUser . LoginProjectId , this . CurrUser . PersonId , menuId ) ;
if ( buttonList . Count ( ) > 0 )
{
if ( buttonList . Contains ( BLL . Const . BtnAdd ) )
{
this . btnAdd . Hidden = false ;
2025-12-25 20:17:07 +08:00
2025-12-22 21:14:34 +08:00
}
if ( buttonList . Contains ( BLL . Const . BtnModify ) )
{
2025-12-25 20:17:07 +08:00
this . btnMenuDeletePermission . Hidden = false ;
2025-12-22 21:14:34 +08:00
this . btnAdd . Hidden = false ;
this . btnMenuEdit . Hidden = false ;
this . btnMenuDeviceToYunMou . Hidden = false ;
this . btnMenuSyncPermission . Hidden = false ;
}
if ( buttonList . Contains ( BLL . Const . BtnDelete ) )
{
this . btnMenuDelete . Hidden = false ;
}
}
2025-12-25 20:17:07 +08:00
2025-12-22 21:14:34 +08:00
}
#endregion
#region 查 询
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TextBox_TextChanged ( object sender , EventArgs e )
{
this . BindGrid ( ) ;
this . GetButtonPower ( ) ;
}
2025-12-25 20:17:07 +08:00
#endregion
2025-12-22 21:14:34 +08:00
}
}