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 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Funs.DropDownPageSize(this.ddlPageSize); this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 this.BindGrid(); ////权限按钮方法 this.GetButtonPower(); } } #endregion /// /// 绑定数据 /// 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 listStr = new List(); 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; Grid1.DataBind(); } #region 操作 Events /// /// 右键删除事件 /// /// /// 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 排序 分页 /// /// /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } /// /// /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { BindGrid(); } #endregion /// /// 增加按钮事件 /// /// /// protected void btnAdd_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window1.GetShowReference("ProjectDevicesEdit.aspx", "添加设备", 800, 300)); } /// /// 双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { this.EditData(); } /// /// 右键编辑事件 /// /// /// 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; } 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(); string addMessage = ""; string data; data = Regex.Replace(project.ProjectCode, "[^0-9A-Fa-f]", "", RegexOptions.IgnoreCase); var YunMouDeviceId = YunMouHelper.addDevices(device.DeviceSerial, data, device.ValidateCode, token,out addMessage);//添加设备 if (!string.IsNullOrEmpty(YunMouDeviceId)) { device.YunMouDeviceId = YunMouDeviceId; Funs.DB.SubmitChanges(); ShowNotify("同步成功", MessageBoxIcon.Success); BindGrid(); } else { Alert.ShowInTop("同步设备出错:"+ addMessage, MessageBoxIcon.Warning); } } } 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);//添加到权限组 YunMouHelper.setDefence(device.DeviceSerial, "1", token); if (string.IsNullOrEmpty(res)) { Alert.ShowInTop("关联权限出错!", MessageBoxIcon.Warning); } else { device.YunMouPermission = "是"; Funs.DB.SubmitChanges(); ShowNotify("关联成功", MessageBoxIcon.Success); BindGrid(); } } } 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(); } } } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ProjectDevicesEdit.aspx?DeviceId={0}", Grid1.SelectedRowID), "编辑项目单位", 800, 300)); } /// /// /// /// /// protected void Window1_Close(object sender, EventArgs e) { BindGrid(); } #region 获取按钮权限 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { if (Request.Params["value"] == "0") { return; } string menuId = BLL.Const.ProjectDevicesMenuId; var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, menuId); ; if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnAdd.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuDeletePermission.Hidden = false; 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; } } } #endregion #region 查询 /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); this.GetButtonPower(); } #endregion } }