using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using Telerik.Web.UI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
namespace VRS
{
///
/// 接口状态码
///
public enum APICode
{
///
/// 成功
///
Success,
///
/// 失败
///
Fail
}
public abstract class BasePage : System.Web.UI.Page
{
///
/// md5密码加密
///
///
///
public static string GetMD5(string passWord)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bt = Encoding.Default.GetBytes(passWord);//将待加密字符转为 字节型数组
byte[] resualt = md5.ComputeHash(bt);//将字节数组转为加密的字节数组
string pwds = BitConverter.ToString(resualt).Replace("-", "");
passWord = pwds;
return passWord;
}
#region 根据属性名称获取值
///
/// 根据属性名称获取值
///
/// 对象实例
/// 属性名
/// object
private static object GetValueByStrAttribute(object obj, string strAttribute)
{
System.Reflection.PropertyInfo propertyInfoName = (obj.GetType()).GetProperty(strAttribute);
if (null == propertyInfoName)
{
throw new Exception("属性名:" + strAttribute + " 不存在!");
}
return propertyInfoName.GetValue(obj, null);
}
#endregion
#region 根据属性名称设置值
///
/// 根据属性名称设置值
///
/// 对象实例
/// 属性名
/// 值
public static void SetValueByStrAttribute(object obj, string strAttribute, object value)
{
System.Reflection.PropertyInfo propertyInfoName = (obj.GetType()).GetProperty(strAttribute);
if (null != propertyInfoName)
{
if (propertyInfoName.PropertyType == typeof(Nullable) || propertyInfoName.PropertyType == typeof(Int32))
{
int int_value = 0;
if (int.TryParse(value.ToString(), out int_value))
{
propertyInfoName.SetValue(obj, int_value, null);
}
}
else if (propertyInfoName.PropertyType == typeof(Nullable) || propertyInfoName.PropertyType == typeof(decimal))
{
decimal decimal_value = 0;
if (decimal.TryParse(value.ToString(), out decimal_value))
{
propertyInfoName.SetValue(obj, decimal_value, null);
}
propertyInfoName.SetValue(obj, decimal_value, null);
}
else
{
propertyInfoName.SetValue(obj, value, null);
}
}
}
#endregion
///
/// 验证手机号码是否符合标准
///
///
///
public static bool IsMobile(string mobile)
{
return Regex.IsMatch(mobile, @"^(13|14|15|16|17|18|19)\d{9}$");
}
///
/// 验证是否身份证号码
///
/// 身份证号码
///
public static bool IsIdcard(string idcard)
{
return Regex.IsMatch(idcard, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase);
}
///
/// 判断是否是合格的18位身份证号码
///
///
///
public static bool Is_valid_idcard18(string str)
{
//从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ;
if (str.Length == 18)
{
var sum = int.Parse(str.Substring(0, 1)) * 7 + int.Parse(str.Substring(1, 1)) * 9 + int.Parse(str.Substring(2, 1)) * 10 + int.Parse(str.Substring(3, 1)) * 5
+ int.Parse(str.Substring(4, 1)) * 8 + int.Parse(str.Substring(5, 1)) * 4 + int.Parse(str.Substring(6, 1)) * 2 + int.Parse(str.Substring(7, 1)) * 1
+ int.Parse(str.Substring(8, 1)) * 6 + int.Parse(str.Substring(9, 1)) * 3 + int.Parse(str.Substring(10, 1)) * 7 + int.Parse(str.Substring(11, 1)) * 9
+ int.Parse(str.Substring(12, 1)) * 10 + int.Parse(str.Substring(13, 1)) * 5 + int.Parse(str.Substring(14, 1)) * 8 + int.Parse(str.Substring(15, 1)) * 4
+ int.Parse(str.Substring(16, 1)) * 2;
var mod = sum % 11;
//余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2;
var dic = new Dictionary();
dic.Add(0, "1");
dic.Add(1, "0");
dic.Add(2, "X");
dic.Add(3, "9");
dic.Add(4, "8");
dic.Add(5, "7");
dic.Add(6, "6");
dic.Add(7, "5");
dic.Add(8, "4");
dic.Add(9, "3");
dic.Add(10, "2");
var compute = dic[mod];
var last_str = str.Substring(17, 1);
if (compute == last_str.ToUpper())
{
return true;
}
else
{
return false;
}
}
return false;
}
public static int GetPosition()
{
return (int)long.Parse(DateTime.Now.ToString("yMdHms"));
}
public static string GetLongId(string flag)
{
string NewId = string.Format("{0}{1}", flag.ToUpper(), GetNewId());
return NewId;
}
///
/// 获取房间号
///
///
public static string GetId()
{
lock (Lockobj)
{
Thread.Sleep(1);
var val = ConvertDateTimeInt(DateTime.Now);
return string.Format("{0}{1}", val, DateTime.Now.ToString("fff"));
}
}
///
/// 获取房间号
///
///
public static string GetTimeId()
{
lock (Lockobj)
{
Thread.Sleep(1000);
return string.Format("{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
}
}
public static string GetNextId(string Id, int Position)
{
return string.Format("{0}{1}", Id, Position.ToString().PadLeft(3, '0'));
}
public static string GetLongId()
{
return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0));
}
///
/// 新ID
///
///
///
public static string GetNewId(string flag)
{
string NewId = string.Format("{0}{1}", flag.ToUpper(), GetNewId());
return NewId;
}
private static object lockObject = new object();
public static string GetNewId(bool sync = true)
{
var d = new Random(BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0));
var array = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var NewId = "";
if (!sync)
{
NewId = string.Format("{0}{1}{2}{3}{4}", DateTime.Now.ToString("yyyyMMddHHmmssffff"), d.Next(0, 1000).ToString().PadLeft(3, '0'), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First());
return NewId;
}
lock (lockObject)
{
System.Threading.Thread.Sleep(1);
NewId = string.Format("{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff"));
return NewId;
}
}
///
/// 绑定系统角色
///
/// 控件
/// 默认值
protected void BindSystemRoles(Telerik.Web.UI.RadDropDownList control, string defaultItem = null)
{
BindRoles("sys", control, defaultItem);
}
///
/// 绑定用户角色
///
/// 控件
/// 默认值
protected void BindUserRoles(Telerik.Web.UI.RadDropDownList control, string defaultItem = null)
{
BindRoles("usr", control, defaultItem);
}
protected void BindRoleFour(RadDropDownList dpp, DataService.Model.admin_user user)
{
DataService.BLL.admin_user bll = new DataService.BLL.admin_user();
dpp.Items.Clear();
dpp.Items.Add("");
var schoolAdminRoleId = bll.getSchoolAdminRoleId();
var teacherRoleId = bll.getTeacherRoleId();
var studentRoleId = bll.getStudentRoleId();
if (bll.IsAdministrator(user))
{
dpp.Items.Add(new DropDownListItem("学生", studentRoleId));
dpp.Items.Add(new DropDownListItem("老师", teacherRoleId));
dpp.Items.Add(new DropDownListItem("学校管理员", schoolAdminRoleId));
}
else if (bll.IsSchoolAdministrator(user))
{
dpp.Items.Add(new DropDownListItem("学生", studentRoleId));
dpp.Items.Add(new DropDownListItem("老师", teacherRoleId));
}
else if (bll.IsTeacher(user))
{
dpp.Items.Add(new DropDownListItem("学生", studentRoleId));
dpp.Items.Add(new DropDownListItem("老师", teacherRoleId));
}
}
///
/// 根据用户ID获取用户角色列表
///
/// 用户ID(非用户账号/工号)
///
protected List GetUserRoles(string Id)
{
DataService.BLL.admin_user_role bllRole = new DataService.BLL.admin_user_role();
return bllRole.GetModelList(string.Format(" user_id = '{0}' ", Id));
}
///
/// 绑定角色
///
/// 条件(r1做为查询条件)
/// 控件
/// 默认值
public void BindRoles(string query, RadDropDownList control, string defaultItem = null)
{
DataService.BLL.admin_config bll_config = new DataService.BLL.admin_config();
DataService.BLL.admin_role bllRole = new DataService.BLL.admin_role();
var config = bll_config.GetModelList(" is_use =1 ").FirstOrDefault();
var app_key = config.app_key;
var where = " 1=1 and app_key='" + config.app_key + "' ";
if (!string.IsNullOrEmpty(query))
where = where + string.Format(" and r1 = '{0}' ", query);
var roles = bllRole.GetModelList(where);
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
roles.ForEach(role =>
{
control.Items.Add(new DropDownListItem(role.role_name, role.role_id));
});
}
///
/// 绑定专业
///
///
///
protected void BindMajors(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.base_major bllMajor = new DataService.BLL.base_major();
var majors = bllMajor.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(major =>
{
control.Items.Add(new DropDownListItem(major.major, major.major));
});
/*
if (majors.Count > 0)
{
control.SelectedIndex = 1;
}
*/
}
///
/// 绑定考试批次
///
///
///
protected void BindExamBatch(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_exam_batch bll = new DataService.BLL.pro_exam_batch();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(major =>
{
//var text = major.grade +"-"+ major.major+"-" + major.batch_name;
var text = major.batch_name;
var item = new DropDownListItem(text, major.batch_id);
if (major.state == 1)
{
item.Selected = true;
}
control.Items.Add(item);
});
}
///
/// 绑定部门名称
///
///
protected void BindDepName(RadComboBox control)
{
BindField(control, "dep_name");
}
protected void BindField(RadComboBox control, string fieldName)
{
DataService.BLL.admin_user bll_sysuser = new DataService.BLL.admin_user();
var array = bll_sysuser.GetFieldValues(fieldName);
control.Items.Clear();
control.Items.Add("");
for (var i = 0; i < array.Count; i++)
{
var text = array[i];
var value = array[i];
control.Items.Add(new RadComboBoxItem(text, value));
}
}
/*
*/
///
/// 绑定级别
///
///
///
protected void BindGrade(RadDropDownList control, string defaultItem = null)
{
var majors = new List();
var year = DateTime.Now.Year;
for (var i = year; i > year - 20; i--)
{
majors.Add(i.ToString() + "级");
}
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(major =>
{
control.Items.Add(new DropDownListItem(major, major));
});
control.SelectedIndex = 2;
}
///
/// 绑定省份
///
///
///
protected void BindProvince(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.admin_province bllMajor = new DataService.BLL.admin_province();
var list = bllMajor.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.province_name, item.province_id));
});
}
///
/// 绑定城市
///
///
///
protected void BindCity(RadDropDownList control, string province_id, string defaultItem = null)
{
DataService.BLL.admin_city bllMajor = new DataService.BLL.admin_city();
var list = bllMajor.GetModelList(" province_id='" + province_id + "' ");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.city, item.city_id));
});
}
///
/// 绑定单位
///
///
///
protected void BindUnit(RadDropDownList control, string city_id, string defaultItem = null)
{
DataService.BLL.admin_unit bllMajor = new DataService.BLL.admin_unit();
var list = bllMajor.GetModelList(" city_id='" + city_id + "' ");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.unit_name, item.unit_id));
});
}
///
/// 绑定考试科目名称
///
///
///
protected void BindResultSubject(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_result bllMajor = new DataService.BLL.pro_result();
var list = bllMajor.GetSubjectNames();
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.subject_name, item.subject_name));
});
}
///
/// 绑定考试科目名称
///
///
///
protected void BindExamSubject(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.exam_subject_match bll = new DataService.BLL.exam_subject_match();
var list = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.subject, item.subject));
});
}
///
/// 绑定竞赛科目名称
///
///
///
protected void BindExamSubjectMatch(RadDropDownList control, string defaultItem = null)
{
//DataService.BLL.exam_subject bll = new DataService.BLL.exam_subject();
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
//var list = bll.GetModelList("");
var list = bll.GetModelList(string.Format(" parent_id = 'SubjectList' "));
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.type_name, item.type_name));
});
}
///
/// 绑定竞赛科目名称ComboBox
///
///
///
protected void BindExamSubjectMatch(RadComboBox control, string defaultItem = null)
{
//DataService.BLL.exam_subject bll = new DataService.BLL.exam_subject();
//var list = bll.GetModelList("");
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
var list = bll.GetModelList(string.Format(" parent_id = 'SubjectList' "));
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new RadComboBoxItem(defaultItem, defaultItem));
list.ForEach(item =>
{
//control.Items.Add(new RadComboBoxItem(item.subject, item.subject));
control.Items.Add(new RadComboBoxItem(item.type_name, item.type_name));
});
}
///
/// 绑定考试科目名称
///
///
///
protected void BindDepartNames(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_result bllMajor = new DataService.BLL.pro_result();
var list = bllMajor.GetGradeNames();
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.grade, item.grade));
});
}
///
/// 绑定设备类型
///
/// 父类型id
///
///
protected void BindDeviceType(string parent_id, RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
var majors = bll.GetModelList(string.Format(" parent_id = '{0}' ", parent_id));
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.type_name, item.type_name));
});
}
///
/// 绑定试卷
///
///
///
protected void BindExam(RadDropDownList control, string defaultItem = null,string exam_type="考试")
{
DataService.BLL.pro_examination bll = new DataService.BLL.pro_examination();
var majors = bll.GetModelList(string.Format(" exam_status=2 and exam_type='{0}' ", exam_type));//查询已经发布试卷
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.exam_name, item.id));
});
}
///
/// 绑定场景
///
///
///
protected void BindSceneBase(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_scene_base bll = new DataService.BLL.pro_scene_base();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.scene_name,item.scene_id));
});
}
///
/// 绑定学校
///
///
///
protected void BindSchool(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.admin_school bll = new DataService.BLL.admin_school();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.school, item.id));
});
}
///
/// 绑定学校班级
///
/// 父类型id
///
///
protected void BindSchoolGrade(string school_id, RadComboBox control, string defaultItem = null)
{
DataService.BLL.admin_user bll = new DataService.BLL.admin_user();
var majors = bll.GetGradeValues(school_id);
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new RadComboBoxItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new RadComboBoxItem(item, item));
});
}
///
/// 从成绩绑定考试批次
///
///
///
protected void BindSchoolExamBatchFromResult(string school_id, RadComboBox control, string defaultItem = null,string lianxi="1")
{
DataService.BLL.pro_exam_batch_result bll_batch_result = new DataService.BLL.pro_exam_batch_result();
var dic = bll_batch_result.GetDicBatch(school_id, lianxi);
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new RadComboBoxItem(defaultItem, defaultItem));
foreach(var kvp in dic)
{
control.Items.Add(new RadComboBoxItem(kvp.Value, kvp.Key));
}
}
///
/// 从批次字典绑定考试批次
///
///
///
protected void BindSchoolExamBatch(string school_id, RadComboBox control, string defaultItem = null)
{
DataService.BLL.pro_exam_batch bll = new DataService.BLL.pro_exam_batch();
var majors = bll.GetModelList("school_id='"+ school_id + "'");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new RadComboBoxItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new RadComboBoxItem(item.batch_name,item.batch_id));
});
}
///
/// 绑定学校班级
///
///
///
protected void BindSchoolGrade(string school_id, RadDropDownList control, string defaultItem = null)
{
DataService.BLL.admin_user bll = new DataService.BLL.admin_user();
var majors = bll.GetGradeValues(school_id);
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item, item));
});
}
///
/// 绑定科目类别
///
///
///
protected void BindSubjectType( RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
var majors = bll.GetModelList(" parent_id='SubjectType' ");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.type_name, item.type_name));
});
}
///
/// 绑定船类型
///
/// 父类型id
///
///
protected void BindBoatType(RadDropDownList control, string defaultItem = null)
{
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list_sign_boat.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.type_name, item.type_id));
});
}
private static List _list_sign_boat = new List();
///
/// 船型标识列表
///
public static List list_sign_boat
{
get
{
if (_list_sign_boat.Count <= 0)
{
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
var majors = bll.GetModelList(string.Format(" parent_id = 'sign_boat' "));
if (majors.Count > 0)
{
_list_sign_boat.AddRange(majors);
}
}
return _list_sign_boat;
}
}
private static List _list_sign_subject = new List();
///
/// 科目标识列表
///
public static List list_sign_subject
{
get
{
if (_list_sign_subject.Count <= 0)
{
DataService.BLL.pro_type_manage bll = new DataService.BLL.pro_type_manage();
var majors = bll.GetModelList(string.Format(" parent_id = 'sign_subject' "));
if (majors.Count > 0)
{
_list_sign_subject.AddRange(majors);
}
}
return _list_sign_subject;
}
}
///
/// 绑定故障类型
///
///
///
protected void BindFaultType(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_fault bll = new DataService.BLL.pro_fault();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.fault_name, item.fault_id));
});
}
///
/// 绑定场景
///
///
///
protected void BindSceneType(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.pro_scene bll = new DataService.BLL.pro_scene();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.scene_name, item.scene_id));
});
}
///
/// 绑定状态类型
///
///
///
protected void BindStateType(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.digital_state_type bll = new DataService.BLL.digital_state_type();
var majors = bll.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
majors.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.StateType, item.StateId.ToString()));
});
}
///
/// 根据名称获取类型
///
///
protected DataService.Model.pro_type_manage GetTypeByName(string typeName)
{
DataService.BLL.pro_type_manage bllType = new DataService.BLL.pro_type_manage();
var models = bllType.GetModelList(string.Format(" type_name = '{0}' ", typeName));
if (models.Any())
return models.First();
else
{
var model = new DataService.Model.pro_type_manage { type_id = Guid.NewGuid().ToString(), type_name = typeName };
if (bllType.Add(model))
return model;
}
return null;
}
///
/// 根据系统名称获取系统配置
///
///
///
protected DataService.Model.admin_config GetSystemConfig(string systemName)
{
DataService.BLL.admin_config bllConfig = new DataService.BLL.admin_config();
var models = bllConfig.GetModelList(string.Format(" system_name = '{0}' ", systemName));
if (models.Any())
return models.First();
else
{
var model = new DataService.Model.admin_config { config_id = Guid.NewGuid().ToString(), system_name = systemName, is_close = 0, is_use = 1, is_examine = 0, examine_type = 0, create_time = DateTime.Now };
if (bllConfig.Add(model))
return model;
}
return null;
}
///
/// 获取将对象转换为的JSON字符串
///
///
///
protected string GetJson(object dataObj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(dataObj);
}
///
/// 获取带状态码的JSON字符串
///
///
///
///
protected string GetJsonWithCode(APICode code, object dataObj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { code = (int)code, state = code.ToString(), data = dataObj });
}
public static bool SaveFile(UploadedFile fileUpload, HttpContext httpContext, out string filePath)
{
string dirPath = httpContext.Server.MapPath("~/Upload/");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
string fileName = GetId() + fileUpload.GetExtension();
string savePath = dirPath + fileName;
fileUpload.SaveAs(savePath);
filePath = "/Upload/" + fileName;
return true;
}
public static bool SaveFileWithDir(UploadedFile fileUpload, HttpContext httpContext,string save_dir, out string filePath)
{
string dirPath = httpContext.Server.MapPath("~/"+ save_dir + "/");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
string fileName = GetId() + fileUpload.GetExtension();
string savePath = dirPath + fileName;
fileUpload.SaveAs(savePath);
filePath = "/"+ save_dir + "/" + fileName;
return true;
}
public static bool SaveFile(UploadedFile fileUpload, HttpContext httpContext, out string filePath, out string originalName)
{
string dirPath = httpContext.Server.MapPath("~/Upload/");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
originalName = fileUpload.FileName;
var index = originalName.LastIndexOf(".");
var prefix = originalName.Substring(0, index);
string fileName = prefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileUpload.GetExtension();
string savePath = dirPath + fileName;
fileUpload.SaveAs(savePath);
filePath = "/Upload/" + fileName;
return true;
}
///
/// 页面初始化
///
///
///
protected virtual void Page_Load(object sender, EventArgs e)
{
DataService.BLL.admin_user.load_login();
}
public void VerifyPermissions(string PermissionCode)
{
DataService.BLL.admin_role_auth bll_role_auth = new DataService.BLL.admin_role_auth();
var user = DataService.BLL.admin_user.load_login();
if (user.login_name != "admin")
{
var list = bll_role_auth.GetModelListByUserid(user.user_id);
var value = list.FirstOrDefault(a => a.auth_id == PermissionCode);
if (value == null)
{
Response.Redirect("~/Util/Error.aspx?msg=" + "没有权限");
}
}
}
protected override void OnError(EventArgs e)
{
base.OnError(e);
var error = Server.GetLastError();
if (null != error)
{
write_log(error.ToString());
Response.Redirect("~/Util/Error.aspx?msg=" + error.Message.Replace("\r", " ").Replace("\n", " "));
}
}
///
/// 绑定故障列表
///
///
///
protected void BindFaultList(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.zhc_fault_list bllMajor = new DataService.BLL.zhc_fault_list();
var list = bllMajor.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.name, item.list_id));
});
}
///
/// 绑定任务列表
///
///
///
protected void BindTaskList(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.zhc_task bllMajor = new DataService.BLL.zhc_task();
var list = bllMajor.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.taskName, item.taskId));
});
}
///
/// 绑定指挥车设备字典
///
///
///
protected void BindZHCDevice(RadDropDownList control, string defaultItem = null)
{
DataService.BLL.zhc_dev bllMajor = new DataService.BLL.zhc_dev();
var list = bllMajor.GetModelList("");
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
list.ForEach(item =>
{
control.Items.Add(new DropDownListItem(item.name, item.name));
});
}
///
/// 根据Web.Config中SystemName系统名称判断菜单是否显示
///
///
///
public static string menu_show_by_SystemName(string system_name)
{
var SystemName = System.Configuration.ConfigurationManager.AppSettings["SystemName"];
if (SystemName.ToUpper() == "ALL")
return "";
if (system_name == SystemName)
{
return "";
}
return "hidden";
}
///
/// 写日志
///
///
public static void write_log(string msg)
{
lock (lockObject)
{
DateTime now = DateTime.Now;
HttpContext context = HttpContext.Current;
if (!Directory.Exists(context.Server.MapPath("/bin/logs/")))
{
Directory.CreateDirectory(context.Server.MapPath("/bin/logs/"));
}
StreamWriter sw = new StreamWriter(context.Server.MapPath("/bin/logs/" + now.ToString("yyyy-MM-dd") + ".txt"), true);
sw.Write("=============================================================\r\n" + now + "\r\n" + msg + "\r\n");
sw.Close();
}
}
///
/// DateTime时间格式转换为10位不带毫秒的Unix时间戳
///
/// Unix时间戳格式
public static long ConvertDateTimeInt(DateTime dt)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
return (long)(dt - startTime).TotalSeconds;
}
private static Object Lockobj = new object();
///
/// 获取房间号
///
///
public static string GetRoomNo()
{
lock (Lockobj)
{
Thread.Sleep(1000);
var val = ConvertDateTimeInt(DateTime.Now);
return val.ToString();
}
}
///
/// 绑定考试类型 模式 0 考试考核、 1 实训考核
///
///
///
public void BindKsMode(RadDropDownList control, string defaultItem = null)
{
var DicSysType = ConfigurationManager.AppSettings["KsMode"];
var array1 = DicSysType.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
control.Items.Clear();
if (null != defaultItem)
control.Items.Add(new DropDownListItem(defaultItem, defaultItem));
for (var i = 0; i < array1.Length; i++)
{
var sub = array1[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var value = sub[0];
var text = sub[1];
control.Items.Add(new DropDownListItem(text, value));
}
}
///
/// 语言是否英文
///
///
public static bool is_english()
{
if (null != HttpContext.Current.Request.Cookies["lang"] && HttpContext.Current.Request.Cookies["lang"].Value == "1")
{
return true;
}
return false;
}
public static Dictionary DicKsMode()
{
var DicSysType = ConfigurationManager.AppSettings["KsMode"];
var array1 = DicSysType.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var dic = new Dictionary();
for (var i = 0; i < array1.Length; i++)
{
var sub = array1[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var value = sub[0];
var text = sub[1];
dic.Add(value, text);
}
return dic;
}
public static DataTable ExcelToDataTable(string filePath, string sheetName, int startRow, bool isFirstRowColumn, bool cshz)
{
DataTable dataTable = null;
FileStream fs = null;
DataColumn column = null;
DataRow dataRow = null;
IWorkbook workbook = null;
ISheet sheet = null;
IRow row = null;
ICell cell = null;
int cellCount = 0;
// int startRow = 0;
try
{
using (fs = File.OpenRead(filePath))
{
// 2007版本
if (filePath.IndexOf(".xlsx") > 0)
workbook = new XSSFWorkbook(fs);
// 2003版本
else if (filePath.IndexOf(".xls") > 0)
workbook = new HSSFWorkbook(fs);
if (workbook != null)
{
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
if (cshz)
{
return null;
}
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
dataTable = new DataTable();
if (sheet != null)
{
int rowCount = sheet.LastRowNum;//总行数
if (rowCount > 0)
{
IRow firstRow = sheet.GetRow(startRow);//第一行
int rowindex = 0; //搜索空行,并跳过
while (firstRow == null && rowindex < rowCount)
{
rowindex++;
firstRow = sheet.GetRow(rowindex);
} //如果全为空行则返回null值
if (rowindex == rowCount) return null;
cellCount = firstRow.LastCellNum;//列数
startRow = firstRow.RowNum;
// 构建datatable的列
if (isFirstRowColumn)
{
//如果第一行是列名,则从第二行开始读取
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
cell = firstRow.GetCell(i);
if (cell != null)
{
if (cell.ToString() != null)
{
column = new DataColumn(cell.ToString());
dataTable.Columns.Add(column);
}
}
}
startRow++;
}
else
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
column = new DataColumn("column" + (i + 1));
dataTable.Columns.Add(column);
}
}
// 填充行
for (int i = startRow; i <= rowCount; ++i)
{
row = sheet.GetRow(i);
if (row == null) continue;
cellCount = row.LastCellNum; //全文行之间的列数不一样的话,继续添加列
if (cellCount > dataTable.Columns.Count)
{
for (int c = dataTable.Columns.Count; c < cellCount; c++)
{
column = new DataColumn("column" + (c + 1));
dataTable.Columns.Add(column);
}
}
dataRow = dataTable.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
cell = row.GetCell(j);
if (cell == null)
{
dataRow[j] = "";
}
else
{
// CellType(Unknown = -1, Numeric = 0, String = 1, Formula = 2, Blank = 3, Boolean = 4, Error = 5,)
switch (cell.CellType)
{
case CellType.Blank:
dataRow[j] = "";
break;
case CellType.Numeric:
short format = cell.CellStyle.DataFormat;
// 对时间格式(2015.12.5、2015 / 12 / 5、2015 - 12 - 5等)的处理
if (format == 14 || format == 31 || format == 57 || format == 58)
dataRow[j] = cell.DateCellValue;
else
dataRow[j] = cell.NumericCellValue;
break;
case CellType.String:
dataRow[j] = cell.StringCellValue;
break;
}
}
}
dataTable.Rows.Add(dataRow);
}
}
}
}
}
return dataTable;
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
return null;
}
}
}
}