| | |
| | | using System; |
| | | using Dapper; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Linq; |
| | |
| | | public static ToMessage mes = new ToMessage(); //定义全局返回信息对象 |
| | | |
| | | #region【Excel模板上传验证】 |
| | | #region【Excel模板上传验证,用户清单模板】 |
| | | public static string Two(string FileCode, out string code) |
| | | { |
| | | string Message = ""; |
| | | code = ""; |
| | | DataTable excelTable = new DataTable(); |
| | | excelTable = ImportExcel.ExcelToTable(FileCode); |
| | | if (excelTable.Columns.Count != 6) |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[0].ColumnName != "序号") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[1].ColumnName != "用户编号(唯一)") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[2].ColumnName != "用户姓名") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[3].ColumnName != "密码") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[4].ColumnName != "手机号") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[5].ColumnName != "邮箱") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[6].ColumnName != "所属组织") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[7].ColumnName != "在职状态") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[8].ColumnName != "工资类型") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else |
| | | { |
| | | code = "200"; |
| | | Message = "模板检验通过"; |
| | | } |
| | | return Message; |
| | | } |
| | | #endregion |
| | | |
| | | #region【Excel模板上传验证,角色权限模板】 |
| | | public static string One(string FileCode, out string code) |
| | | { |
| | |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[3].ColumnName != "角色类型") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else |
| | | { |
| | | code = "200"; |
| | | Message = "模板检验通过"; |
| | | } |
| | | return Message; |
| | | } |
| | | #endregion |
| | | |
| | | #region【Excel模板上传验证,用户角色模板】 |
| | | public static string Two(string FileCode, out string code) |
| | | { |
| | | string Message = ""; |
| | | code = ""; |
| | | DataTable excelTable = new DataTable(); |
| | | excelTable = ImportExcel.ExcelToTable(FileCode); |
| | | if (excelTable.Columns.Count != 6) |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[0].ColumnName != "序号") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[1].ColumnName != "用户编号(唯一)") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[2].ColumnName != "用户姓名") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[3].ColumnName != "所属组织") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[4].ColumnName != "在职状态") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | | } |
| | | else if (excelTable.Columns[5].ColumnName != "工资类型") |
| | | { |
| | | code = "300"; |
| | | Message = "模板不符合规范,请检查列名"; |
| | |
| | | #endregion |
| | | |
| | | #region【Excel上传数据验证】 |
| | | #region【Excel上传数据验证,角色权限】 |
| | | public static List<ExcelErro> OneData(string FileCode, out string StuCode, out string message, out int count) |
| | | { |
| | | message = ""; |
| | | StuCode = ""; |
| | | count = 0; |
| | | string sql = ""; |
| | | DataTable dt; |
| | | List<ExcelErro> list = new List<ExcelErro>(); |
| | | |
| | | return list; |
| | | } |
| | | #endregion |
| | | |
| | | #region【Excel上传数据验证,用户角色】 |
| | | public static List<ExcelErro> TwoData(string FileCode, out string StuCode, out string message, out int count) |
| | | { |
| | |
| | | StuCode = ""; |
| | | count = 0; |
| | | string sql = ""; |
| | | var dynamicParams = new DynamicParameters(); |
| | | DataTable dt; |
| | | List<ExcelErro> list = new List<ExcelErro>(); |
| | | List<DataTable> excelTable = new List<DataTable>(); |
| | | list = ImportExcel.ExcelToTableListErro(FileCode); //验证Excel数据必填字段是否为空、唯一字段是否重复 |
| | | excelTable = ImportExcel.ExcelToTableList(FileCode); //获取Excel数据 |
| | | //主表 |
| | | for (int j = 0; j < excelTable[0].Rows.Count; j++) |
| | | { |
| | | if (excelTable[0].Rows[j][1].ToString().Trim() != null && excelTable[0].Rows[j][1].ToString().Trim() != "") |
| | | { |
| | | sql = @"select * from TGroup where group_code=@group_code"; |
| | | dynamicParams.Add("@group_code", excelTable[0].Rows[j][1].ToString().Trim()); |
| | | dt = DapperHelper.selectdata(sql, dynamicParams); |
| | | if (dt.Rows.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{班组编码(唯一)}"; |
| | | erro.ErrorCont = "班组表:{班组编码(唯一)}字段" + excelTable[0].Rows[j][1].ToString().Trim() + "已存在"; |
| | | list.Add(erro); |
| | | } |
| | | } |
| | | } |
| | | //子表 |
| | | for (int k = 0; k < excelTable[1].Rows.Count; k++) |
| | | { |
| | | if (excelTable[1].Rows[k][1].ToString().Trim() != null && excelTable[1].Rows[k][1].ToString().Trim() != "") |
| | | { |
| | | sql = @"select * from TUser where usercode=@usercode"; |
| | | dynamicParams.Add("@usercode", excelTable[1].Rows[k][1].ToString().Trim()); |
| | | dt = DapperHelper.selectdata(sql, dynamicParams); |
| | | if (dt.Rows.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{用户编码(唯一)}"; |
| | | erro.ErrorCont = "用户表:{用户编码(唯一)}字段" + excelTable[1].Rows[k][2].ToString().Trim() + "已存在"; |
| | | list.Add(erro); |
| | | } |
| | | } |
| | | if (excelTable[1].Rows[k][7].ToString().Trim() != null && excelTable[1].Rows[k][7].ToString().Trim() != "") |
| | | { |
| | | sql = @"select * from TOrganization where org_code=@org_code"; |
| | | dynamicParams.Add("@org_code", excelTable[1].Rows[k][7].ToString().Trim()); |
| | | dt = DapperHelper.selectdata(sql, dynamicParams); |
| | | if (dt == null || dt.Rows.Count <= 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{组织编码}"; |
| | | erro.ErrorCont = "用户表:{组织编码}字段" + excelTable[1].Rows[k][2].ToString().Trim() + "不存在"; |
| | | list.Add(erro); |
| | | } |
| | | } |
| | | } |
| | | //判断子表外键不存在于主表主键中的数据 |
| | | var dt3 = from r in excelTable[1].AsEnumerable() |
| | | where !( |
| | | from rr in excelTable[0].AsEnumerable() |
| | | select rr.Field<string>("班组编号(唯一)") |
| | | ).Contains(r.Field<string>("用户组编码")) |
| | | select r; |
| | | List<DataRow> listRow = dt3.ToList(); |
| | | if (listRow.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{用户组编码}"; |
| | | erro.ErrorCont = "用户表:{用户组编码}字段中有值在班组表:{班组编号(唯一)}中不存在"; |
| | | list.Add(erro); |
| | | } |
| | | if (list.Count > 0) |
| | | { |
| | | int index = 0; |
| | | foreach (ExcelErro item in list) |
| | | { |
| | | index++; |
| | | item.Seq = index.ToString(); |
| | | } |
| | | StuCode = "300"; |
| | | message = "数据验证失败"; |
| | | } |
| | | else |
| | | { |
| | | StuCode = "200"; |
| | | message = "数据验证成功"; |
| | | count = excelTable[0].Rows.Count + excelTable[1].Rows.Count; |
| | | } |
| | | return list; |
| | | } |
| | | #endregion |
| | | |
| | | #region【Excel上传数据验证,角色权限】 |
| | | public static List<ExcelErro> OneData(string FileCode, out string StuCode, out string message, out int count) |
| | | { |
| | | message = ""; |
| | | StuCode = ""; |
| | | count = 0; |
| | | string sql = ""; |
| | | var dynamicParams = new DynamicParameters(); |
| | | DataTable dt; |
| | | List<ExcelErro> list = new List<ExcelErro>(); |
| | | List<DataTable> excelTable = new List<DataTable>(); |
| | | list = ImportExcel.ExcelToTableListErro(FileCode); //验证Excel数据必填字段是否为空、唯一字段是否重复 |
| | | excelTable = ImportExcel.ExcelToTableList(FileCode); //获取Excel数据 |
| | | //主表 |
| | | for (int j = 0; j < excelTable[0].Rows.Count; j++) |
| | | { |
| | | if (excelTable[0].Rows[j][1].ToString().Trim() != null && excelTable[0].Rows[j][1].ToString().Trim() != "") |
| | | { |
| | | sql = @"select * from TRoleType where roletype_code=@roletype_code"; |
| | | dynamicParams.Add("@roletype_code", excelTable[0].Rows[j][1].ToString().Trim()); |
| | | dt = DapperHelper.selectdata(sql, dynamicParams); |
| | | if (dt.Rows.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{角色类型编码(唯一)}"; |
| | | erro.ErrorCont = "班组表:{角色类型编码(唯一)}字段" + excelTable[0].Rows[j][1].ToString().Trim() + "已存在"; |
| | | list.Add(erro); |
| | | } |
| | | } |
| | | } |
| | | //子表 |
| | | for (int k = 0; k < excelTable[1].Rows.Count; k++) |
| | | { |
| | | if (excelTable[1].Rows[k][1].ToString().Trim() != null && excelTable[1].Rows[k][1].ToString().Trim() != "") |
| | | { |
| | | sql = @"select * from TRole where role_code=@role_code"; |
| | | dynamicParams.Add("@role_code", excelTable[1].Rows[k][1].ToString().Trim()); |
| | | dt = DapperHelper.selectdata(sql, dynamicParams); |
| | | if (dt.Rows.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{角色编号(唯一)}"; |
| | | erro.ErrorCont = "角色表:{角色编号(唯一)}字段" + excelTable[1].Rows[k][2].ToString().Trim() + "已存在"; |
| | | list.Add(erro); |
| | | } |
| | | } |
| | | } |
| | | //判断子表外键不存在于主表主键中的数据 |
| | | var dt3 = from r in excelTable[1].AsEnumerable() |
| | | where !( |
| | | from rr in excelTable[0].AsEnumerable() |
| | | select rr.Field<string>("角色类型编号(唯一)") |
| | | ).Contains(r.Field<string>("角色类型编码")) |
| | | select r; |
| | | List<DataRow> listRow = dt3.ToList(); |
| | | if (listRow.Count > 0) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = "/"; |
| | | erro.ErrorField = "{角色类型编码}"; |
| | | erro.ErrorCont = "角色表:{角色类型编码}字段中有值在角色类型表:{角色类型编号(唯一)}中不存在"; |
| | | list.Add(erro); |
| | | } |
| | | if (list.Count > 0) |
| | | { |
| | | int index = 0; |
| | | foreach (ExcelErro item in list) |
| | | { |
| | | index++; |
| | | item.Seq = index.ToString(); |
| | | } |
| | | StuCode = "300"; |
| | | message = "数据验证失败"; |
| | | } |
| | | else |
| | | { |
| | | StuCode = "200"; |
| | | message = "数据验证成功"; |
| | | count = excelTable[0].Rows.Count + excelTable[1].Rows.Count; |
| | | } |
| | | return list; |
| | | } |
| | | #endregion |
| | |
| | | #endregion |
| | | |
| | | #region[Excel上传数据提交] |
| | | #region[Excel数据上传,角色权限] |
| | | public static string OneSubmit(string FileCode, string User, out string StuCode) |
| | | #region【Excel数据上传,用户角色】 |
| | | public static string TwoSubmit(string FileCode, string User, out string StuCode) |
| | | { |
| | | string message = ""; |
| | | StuCode = ""; |
| | | string sql = ""; |
| | | string ZZName = "", Enable = "", WageType = ""; |
| | | DataTable dt; |
| | | List<object> list = new List<object>(); |
| | | var dynamicParams = new DynamicParameters(); |
| | | try |
| | | { |
| | | list.Clear(); |
| | | List<DataTable> excelTable = new List<DataTable>(); |
| | | excelTable = ImportExcel.ExcelToTableList(FileCode); |
| | | //导入班组 |
| | | for (int k = 0; k < excelTable[0].Rows.Count; k++) |
| | | { |
| | | sql = @"insert into TGroup(group_code,group_name,description,lm_user,lm_date) |
| | | values(@group_code,@group_name,@description,@Operator,@CreateDate)"; |
| | | list.Add(new |
| | | { |
| | | str = sql, |
| | | parm = new |
| | | { |
| | | group_code = excelTable[0].Rows[k][1].ToString().Trim(), |
| | | group_name = excelTable[0].Rows[k][2].ToString().Trim(), |
| | | description = excelTable[0].Rows[k][3].ToString().Trim(), |
| | | CreateDate = DateTime.Now.ToString(), |
| | | Operator = User |
| | | } |
| | | }); |
| | | } |
| | | //导入用户 |
| | | for (int i = 0; i < excelTable[1].Rows.Count; i++) |
| | | { |
| | | if (excelTable[1].Rows[i][3].ToString().Trim() == "在职") |
| | | { |
| | | Enable = "Y"; |
| | | } |
| | | else |
| | | { |
| | | Enable = "N"; |
| | | } |
| | | //工资类型 |
| | | switch (excelTable[1].Rows[i][8].ToString().Trim()) |
| | | { |
| | | case "计件制": |
| | | WageType = "2"; |
| | | break; |
| | | case "计时制": |
| | | WageType = "1"; |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | sql = @"insert into TUser(usercode,username,password,enable,mobile,lm_date,email,lm_user,stu_torgcode,wagetype) |
| | | values(@UserCode,@UserName,@password,@Enable,@Mobile,@CreateDate,@Email,@Operator,@StuOrg,@wagetype)"; |
| | | list.Add(new |
| | | { |
| | | str = sql, |
| | | parm = new |
| | | { |
| | | UserCode = excelTable[1].Rows[i][1].ToString().Trim(), |
| | | UserName = excelTable[1].Rows[i][2].ToString().Trim(), |
| | | password= excelTable[1].Rows[i][4].ToString().Trim(), |
| | | Enable= Enable, |
| | | Mobile= excelTable[1].Rows[i][5].ToString().Trim(), |
| | | CreateDate= DateTime.Now.ToString(), |
| | | Email= excelTable[1].Rows[i][6].ToString().Trim(), |
| | | Operator= User, |
| | | StuOrg= excelTable[1].Rows[i][7].ToString().Trim(), |
| | | wagetype= WageType |
| | | } |
| | | }); |
| | | } |
| | | bool aa = DapperHelper.DoTransaction(list); |
| | | if (aa) |
| | | { |
| | | StuCode = "300"; |
| | | message = "导入失败!"; |
| | | } |
| | | else |
| | | { |
| | | StuCode = "200"; |
| | | message = "导入成功!"; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | |
| | | } |
| | | #endregion |
| | | |
| | | #region【Excel数据上传,用户角色】 |
| | | public static string TwoSubmit(string FileCode, string User, out string StuCode) |
| | | #region[Excel数据上传,角色权限] |
| | | public static string OneSubmit(string FileCode, string User, out string StuCode) |
| | | { |
| | | string message = ""; |
| | | StuCode = ""; |
| | | string sql = ""; |
| | | string ZZName = "", Enable = "", WageType = ""; |
| | | DataTable dt; |
| | | List<object> list = new List<object>(); |
| | | var dynamicParams = new DynamicParameters(); |
| | | try |
| | | { |
| | | |
| | | list.Clear(); |
| | | List<DataTable> excelTable = new List<DataTable>(); |
| | | excelTable = ImportExcel.ExcelToTableList(FileCode); |
| | | //导入角色类型 |
| | | for (int k = 0; k < excelTable[0].Rows.Count; k++) |
| | | { |
| | | sql = @"insert into TRoleType(roletype_code,roletype_name) |
| | | values(@roletype_code,@roletype_name)"; |
| | | list.Add(new |
| | | { |
| | | str = sql, |
| | | parm = new |
| | | { |
| | | roletype_code = excelTable[0].Rows[k][1].ToString().Trim(), |
| | | roletype_name = excelTable[0].Rows[k][2].ToString().Trim() |
| | | } |
| | | }); |
| | | } |
| | | //导入角色 |
| | | for (int i = 0; i < excelTable[1].Rows.Count; i++) |
| | | { |
| | | sql = @"insert into TRole(role_code,role_name,roletype_code,description,lm_user,lm_date) |
| | | values(@role_code,@role_name,@roletype_code,@description,@Operator,@CreateDate)"; |
| | | list.Add(new |
| | | { |
| | | str = sql, |
| | | parm = new |
| | | { |
| | | role_code = excelTable[1].Rows[i][1].ToString().Trim(), |
| | | role_name = excelTable[1].Rows[i][2].ToString().Trim(), |
| | | roletype_code = excelTable[1].Rows[i][3].ToString().Trim(), |
| | | description = excelTable[1].Rows[i][4].ToString().Trim(), |
| | | Operator = User, |
| | | CreateDate = DateTime.Now.ToString() |
| | | } |
| | | }); |
| | | } |
| | | bool aa = DapperHelper.DoTransaction(list); |
| | | if (aa) |
| | | { |
| | | StuCode = "300"; |
| | | message = "导入失败!"; |
| | | } |
| | | else |
| | | { |
| | | StuCode = "200"; |
| | | message = "导入成功!"; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |