xinjiang/SGGL/FineUIPro.Web/SysManage/UserIn.aspx.cs

520 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web.UI;
using BLL;
using FineUIPro.Web.HSSE.PostTraining;
using Model;
namespace FineUIPro.Web.SysManage
{
public partial class UserIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 人员集合
/// </summary>
public static List<Model.View_Sys_User> userViews = new List<Model.View_Sys_User>();
///// <summary>
///// 错误集合
///// </summary>
//public static string errorInfos = string.Empty;
/// <summary>
/// 错误集合
/// </summary>
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.hdFileName.Text = string.Empty;
this.hdCheckResult.Text = string.Empty;
if (userViews != null)
{
userViews.Clear();
}
if (errorInfos != null)
{
errorInfos.Clear();
}
//errorInfos = string.Empty;
}
}
#endregion
#region
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
if (this.fuAttachUrl.HasFile == false)
{
ShowNotify("请您选择Excel文件", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xls")
{
ShowNotify("只可以选择Excel文件", MessageBoxIcon.Warning);
return;
}
if (userViews != null)
{
userViews.Clear();
}
if (errorInfos != null)
{
errorInfos.Clear();
}
//if (!string.IsNullOrWhiteSpace(errorInfos))
//{
// errorInfos = string.Empty;
//}
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
if (!Directory.Exists(initFullPath))
{
Directory.CreateDirectory(initFullPath);
}
this.hdFileName.Text = BLL.Funs.GetNewFileName() + IsXls;
string filePath = initFullPath + this.hdFileName.Text;
this.fuAttachUrl.PostedFile.SaveAs(filePath);
//PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("PersonDataAudit.aspx?FileName={0}&ProjectId={1}", this.hdFileName.Text, Request.Params["ProjectId"], "审核 - ")));
ImportXlsToData(rootPath + initPath + this.hdFileName.Text);
}
catch (Exception ex)
{
ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData(string fileName)
{
try
{
userViews.Clear();
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += fileName;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
}
string sqlMaster;
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
AddDatasetToSQL(ds.Tables[0]);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Dataset的数据导入数据库
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集行数</param>
/// <returns></returns>
private bool AddDatasetToSQL(DataTable pds)
{
string result = string.Empty;
int ic, ir;
ic = pds.Columns.Count;
ir = pds.Rows.Count;
if (pds != null && ir > 0)
{
var users = from x in Funs.DB.View_Sys_User select x;
var units = from x in Funs.DB.Base_Unit select x;
var roles = from x in Funs.DB.Sys_Role select x;
var departs = from x in Funs.DB.Base_Depart select x;
var cNProfessionals = from x in Funs.DB.Base_CNProfessional select x;
for (int i = 0; i < ir; i++)
{
Model.View_Sys_User newSysUser = new Model.View_Sys_User
{
RCount = i + 2,
UserCode = pds.Rows[i][0].ToString().Trim(),
UserName = pds.Rows[i][1].ToString().Trim(),
Account = pds.Rows[i][2].ToString().Trim(),
UnitName = pds.Rows[i][3].ToString().Trim(),
DepartName = pds.Rows[i][4].ToString().Trim(),
RoleName = pds.Rows[i][5].ToString().Trim(),
IdentityCard = pds.Rows[i][6].ToString().Trim(),
Telephone = pds.Rows[i][7].ToString().Trim(),
IsPostName = pds.Rows[i][8].ToString().Trim(),
IsPost = pds.Rows[i][8].ToString().Trim() == "是" ? true : false,
IsOffice = pds.Rows[i][9].ToString().Trim() == "是" ? true : false,
MainCNProfessionalName = pds.Rows[i][10].ToString().Trim(),
ViceCNProfessionalName = pds.Rows[i][11].ToString().Trim()
};
if (string.IsNullOrWhiteSpace(newSysUser.UserCode))
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "员工编号", Reason = "此项为必填项!" });
}
else
{
//判断人员编号是否存在
bool isexist = users.Where(x => x.UserCode == newSysUser.UserCode).Any();
if (isexist)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "员工编号", Reason = $"已存在编号为[{newSysUser.UserCode}]的人员!" });
}
}
if (!string.IsNullOrWhiteSpace(newSysUser.IdentityCard))
{
//判断人员编号是否存在
bool isexist = users.Where(x => x.IdentityCard == newSysUser.IdentityCard).Any();
if (isexist)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "身份证号", Reason = $"已存在身份证号为[{newSysUser.IdentityCard}]的人员!" });
}
}
if (string.IsNullOrWhiteSpace(newSysUser.UserName))
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "人员姓名", Reason = "此项为必填项!" });
}
if (string.IsNullOrWhiteSpace(newSysUser.Account))
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "登录账号", Reason = "此项为必填项!" });
}
string unitName = newSysUser.UnitName;
if (!string.IsNullOrWhiteSpace(unitName))
{
var unit = units.FirstOrDefault(e => e.UnitName == unitName);
if (unit == null)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "单位", Reason = $"[{unitName}]不在单位表中!" });
}
else
{
newSysUser.UnitId = unit.UnitId;
}
}
else
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "单位", Reason = "此项为必填项!" });
}
string departName = newSysUser.DepartName;
if (!string.IsNullOrWhiteSpace(departName))
{
var depart = departs.FirstOrDefault(e => e.DepartName == departName);
if (depart == null)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "部门", Reason = $"[{departName}]不在部门表中!" });
}
else
{
newSysUser.DepartId = depart.DepartId;
}
}
else
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "部门", Reason = "此项为必填项!" });
}
string roleName = newSysUser.RoleName;
string roleIds = string.Empty;
string roleNames = string.Empty;
if (!string.IsNullOrWhiteSpace(roleName))
{
var roleNameList = roleName.Split(',');
string errRoleName = string.Empty;
foreach (var rol in roleNameList)
{
var role = roles.FirstOrDefault(e => e.RoleName == rol);
if (role == null)
{
errRoleName = !string.IsNullOrWhiteSpace(errRoleName) ? $"{errRoleName},{rol}" : rol;
}
else
{
roleIds = !string.IsNullOrWhiteSpace(roleIds) ? $"{roleIds},{role.RoleId}" : role.RoleId;
roleNames = !string.IsNullOrWhiteSpace(roleNames) ? $"{roleNames},{role.RoleName}" : role.RoleName;
}
}
if (!string.IsNullOrWhiteSpace(errRoleName))
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "角色", Reason = $"[{errRoleName}]不在角色表中!" });
}
}
newSysUser.RoleId = roleIds;
newSysUser.RoleName = roleNames;
//var role = roles.FirstOrDefault(e => e.RoleName == roleName);
//if (role == null)
//{
// result += "第" + (i + 2).ToString() + "行," + "角色" + "," + "[" + roleName + "]错误!" + "|";
//}
//else
//{
// newSysUser.RoleId = role.RoleId;
// newSysUser.RoleName = role.RoleName;
//}
if (!newSysUser.IsPost.HasValue)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "在岗", Reason = "此项为必填项!" });
}
string mainCNProfessionalName = newSysUser.MainCNProfessionalName;
if (!string.IsNullOrWhiteSpace(mainCNProfessionalName))
{
var cNProfessional = cNProfessionals.FirstOrDefault(e => e.ProfessionalName == mainCNProfessionalName);
if (cNProfessional == null)
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "主专业", Reason = $"[{mainCNProfessionalName}]不在专业表中!" });
}
else
{
newSysUser.MainCNProfessionalId = cNProfessional.CNProfessionalId;
}
}
string viceCNProfessionalName = newSysUser.ViceCNProfessionalName;
if (!string.IsNullOrWhiteSpace(viceCNProfessionalName))
{
string viceCNProfessionalId = string.Empty;
bool hasErr = false;
string[] strs = viceCNProfessionalName.Split('');
string errViceCNProfessionalName = string.Empty;
foreach (var item in strs)
{
if (!string.IsNullOrWhiteSpace(item))
{
var cNProfessional = cNProfessionals.FirstOrDefault(e => e.ProfessionalName == item);
if (cNProfessional == null)
{
hasErr = true;
//result += "第" + (i + 2).ToString() + "行," + "副专业" + "," + "[" + item + "]不在专业表中!" + "|";
errViceCNProfessionalName = !string.IsNullOrWhiteSpace(errViceCNProfessionalName) ? $"{errViceCNProfessionalName},{item}" : item;
}
else
{
viceCNProfessionalId += cNProfessional.CNProfessionalId + ",";
}
}
}
if (!string.IsNullOrWhiteSpace(errViceCNProfessionalName))
{
errorInfos.Add(new ErrorInfo { Row = (i + 2).ToString(), Column = "副专业", Reason = $"[{errViceCNProfessionalName}]不在专业表中!" });
}
if (!string.IsNullOrWhiteSpace(viceCNProfessionalId))
{
viceCNProfessionalId = viceCNProfessionalId.Substring(0, viceCNProfessionalId.Length - 1);
}
if (!hasErr)
{
newSysUser.ViceCNProfessionalId = viceCNProfessionalId;
}
}
///加入用户视图
userViews.Add(newSysUser);
}
if (errorInfos.Any())
{
userViews.Clear();
this.gvErrorInfo.DataSource = errorInfos;
this.gvErrorInfo.DataBind();
}
else
{
errorInfos.Clear();
if (!userViews.Any())
{
Alert.ShowInTop("导入数据为空!", MessageBoxIcon.Warning);
}
else
{
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
}
}
else
{
Alert.ShowInTop("导入数据为空!", MessageBoxIcon.Warning);
}
return true;
}
#endregion
#endregion
#region
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (!errorInfos.Any())
{
int a = userViews.Count();
int insertCount = 0;
int updateCount = 0;
for (int i = 0; i < a; i++)
{
Model.Sys_User newUser = new Model.Sys_User
{
UserCode = userViews[i].UserCode,
UserName = userViews[i].UserName,
Account = userViews[i].Account,
UnitId = userViews[i].UnitId,
DepartId = userViews[i].DepartId,
RoleId = userViews[i].RoleId,
IdentityCard = userViews[i].IdentityCard,
Telephone = userViews[i].Telephone,
IsPost = userViews[i].IsPost,
IsOffice = userViews[i].IsOffice,
MainCNProfessionalId = userViews[i].MainCNProfessionalId,
ViceCNProfessionalId = userViews[i].ViceCNProfessionalId,
};
var getUser = Funs.DB.Sys_User.FirstOrDefault(x => x.Account == userViews[i].Account);
if (getUser == null)
{
newUser.Password = Funs.EncryptionPassword(Const.Password);
newUser.RawPassword = Const.Password;
BLL.UserService.AddUser(newUser);
insertCount++;
}
else
{
newUser.UserId = getUser.UserId;
BLL.UserService.UpdateUser(newUser);
updateCount++;
}
}
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
string filePath = initFullPath + this.hdFileName.Text;
if (filePath != string.Empty && File.Exists(filePath))
{
File.Delete(filePath);//删除上传的XLS文件
}
ShowNotify("导入完成!插入" + insertCount.ToString() + "条,更新" + updateCount.ToString() + "条记录。", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning);
}
}
#endregion
#region
///// <summary>
///// 关闭导入弹出窗口
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//protected void Window2_Close(object sender, WindowCloseEventArgs e)
//{
// if (Session["sysUsers"] != null)
// {
// userViews = Session["sysUsers"] as List<Model.View_Sys_User>;
// }
// if (userViews.Count > 0)
// {
// this.Grid1.Hidden = false;
// this.Grid1.DataSource = userViews;
// this.Grid1.DataBind();
// }
//}
#endregion
#region
/// <summary>
/// 下载模板按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载导入模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
}
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
{
if (e.EventArgument == "Confirm_OK")
{
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.UserTemplateUrl;
string filePath = Const.UserTemplateUrl;
string fileName = Path.GetFileName(filePath);
FileInfo info = new FileInfo(uploadfilepath);
long fileSize = info.Length;
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.ContentType = "excel/plain";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Length", fileSize.ToString().Trim());
Response.TransmitFile(uploadfilepath, 0, fileSize);
Response.End();
}
}
#endregion
}
}