using Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace BLL { public static class CommonService { #region 获取当前人系统集合 /// /// 获取当前人系统集合 /// /// 用户id /// 是否具有权限 public static List GetSystemPowerList(string userId) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { var getUser = db.Sys_User.FirstOrDefault(x => x.UserId == userId); if (getUser != null) { if (userId == Const.sysglyId || userId == Const.hfnbdId) ////|| getUser.DepartId == Const.Depart_constructionId { return new List() { Const.Menu_Server, Const.Menu_HSSE, Const.Menu_CQMS, Const.Menu_HJGL }; } else if (userId == Const.sedinId) { return new List() { Const.Menu_CQMS }; } else { List returnList = new List(); string rolesStr = string.Empty; if (!string.IsNullOrEmpty(getUser.RoleId)) { rolesStr = getUser.RoleId; var getOffice = db.Sys_RolePower.FirstOrDefault(x => x.RoleId == getUser.RoleId && x.IsOffice == true); if (getOffice != null) { returnList.Add(Const.Menu_Server); } } ////获取项目角色的集合 var getPRoles = (from x in db.Project_ProjectUser join y in db.Base_Project on x.ProjectId equals y.ProjectId where (y.ProjectState == Const.ProjectState_1 || y.ProjectState == null) && x.UserId == userId && x.RoleId != null && (y.IsDelete == null || y.IsDelete == false) select x.RoleId).ToList(); foreach (var item in getPRoles) { if (string.IsNullOrEmpty(rolesStr)) { rolesStr = item; } else { if (!rolesStr.Contains(item)) { rolesStr += "," + item; } } } ////项目角色集合list List roleIdList = Funs.GetStrListByStr(rolesStr, ',').Distinct().ToList(); var getProjectRolePowers = (from x in db.Sys_RolePower where roleIdList.Contains(x.RoleId) select x).ToList(); if (getProjectRolePowers.FirstOrDefault(x => x.MenuType == Const.Menu_HSSE) != null) { returnList.Add(Const.Menu_HSSE); } if (getProjectRolePowers.FirstOrDefault(x => x.MenuType == Const.Menu_CQMS) != null) { returnList.Add(Const.Menu_CQMS); } if (getProjectRolePowers.FirstOrDefault(x => x.MenuType == Const.Menu_HJGL) != null) { returnList.Add(Const.Menu_HJGL); } return returnList.Distinct().ToList(); } } else { return null; } } } #endregion #region 获取当前人菜单集合 /// /// 获取当前人菜单集合 /// /// 项目ID /// 用户id /// 是否具有权限 public static List GetAllMenuList(string projectId, string userId) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { /// 启用且末级菜单 var getMenus = from x in db.Sys_Menu where x.IsUsed == true && (x.MenuType == Const.Menu_Server || x.MenuType == Const.Menu_HSSE || x.MenuType == Const.Menu_CQMS || x.MenuType == Const.Menu_HJGL) select x; List menus = new List(); if (userId == Const.sysglyId || userId == Const.hfnbdId || userId == Const.sedinId) { menus = getMenus.ToList(); } else { var getUser = UserService.GetUserByUserId(userId); ////用户 if (getUser != null) { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); menus = (from x in db.Sys_RolePower join y in getMenus on x.MenuId equals y.MenuId where roleIdList.Contains(x.RoleId) select y).ToList(); } } return menus.Select(x => x.MenuId).Distinct().ToList(); } } public static List GetZJAllMenuList(string projectId, string userId) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { /// 启用且末级菜单 var getMenus = from x in db.Sys_Menu where x.IsUsed == true && (x.MenuType == Const.Menu_Server || x.MenuType == Const.ZJ_Menu_P_HSSE || x.MenuType == Const.ZJ_Menu_P_CQMS || x.MenuType == Const.ZJ_Menu_P_Person || x.MenuType == Const.ZJ_Menu_P_Device) select x; List menus = new List(); if (userId == Const.sysglyId || userId == Const.hfnbdId || userId == Const.sedinId) { menus = getMenus.ToList(); } else { var getUser = UserService.GetUserByUserId(userId); ////用户 if (getUser != null) { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); menus = (from x in db.Sys_RolePower join y in getMenus on x.MenuId equals y.MenuId where roleIdList.Contains(x.RoleId) select y).ToList(); } } return menus.Select(x => x.MenuId).Distinct().ToList(); } } #endregion #region 得到本单位信息 /// /// 得到本单位信息 /// /// public static Model.Base_Unit GetIsThisUnit() { return (Funs.DB.Base_Unit.FirstOrDefault(e => e.IsThisUnit == true)); //本单位 } /// /// 得到本单位Id /// /// public static string GetThisUnitId() { string unitId = string.Empty; var thisUnit = Funs.DB.Base_Unit.FirstOrDefault(e => e.IsThisUnit == true); //本单位 if (thisUnit != null) { unitId = thisUnit.UnitId; } return unitId; } /// /// 获取本单位名称 /// /// public static string GetThisUnitName() { string unitName = string.Empty; var thisUnit = Funs.DB.Base_Unit.FirstOrDefault(e => e.IsThisUnit == true); //本单位 if (thisUnit != null) { unitName = thisUnit.UnitName; } return unitName; } #endregion #region 根据项目和单位id获取单位类型 /// /// 得到本单位Id /// /// public static string GetProjectUnitType(string projectId, string unitId) { string unitType = string.Empty; var projectUnit = Funs.DB.Project_ProjectUnit.FirstOrDefault(e => e.ProjectId == projectId && e.UnitId == unitId); if (projectUnit != null) { unitType = projectUnit.UnitType; } return unitType; } #endregion #region 根据项目id获取项目总包单位Id /// /// 得到本单位Id /// /// public static string GetMainProjectUnitId(string projectId) { string unitId = string.Empty; var projectUnit = Funs.DB.Project_ProjectUnit.FirstOrDefault(e => e.ProjectId == projectId && e.UnitType == BLL.Const.ProjectUnitType_1); if (projectUnit != null) { unitId = projectUnit.UnitId; } return unitId; } #endregion #region 根据登陆id菜单id判断是否有权限 /// /// 根据登陆id菜单id判断是否有权限 /// /// /// public static bool ReturnMenuByUserIdMenuId(string userId, string menuId, string projectId) { bool returnValue = false; var menu = Funs.DB.Sys_Menu.FirstOrDefault(x => x.MenuId == menuId); var getUser = Funs.DB.Sys_User.FirstOrDefault(x => x.UserId == userId); ////用户 if (menu != null && getUser != null) { ///1、当前用户是管理员 ///2、当前菜单是个人设置 if (userId == Const.sysglyId || userId == Const.hfnbdId || userId == Const.sedinId) { returnValue = true; } else if (userId == Const.sedinId) { returnValue = true; } else if (menu.MenuType == Const.Menu_Personal) { returnValue = true; } else if (string.IsNullOrEmpty(projectId)) ///本部、系统设置 { if (!string.IsNullOrEmpty(getUser.RoleId)) { var power = Funs.DB.Sys_RolePower.FirstOrDefault(x => x.MenuId == menuId && x.RoleId == getUser.RoleId); if (power != null) { returnValue = true; } } } else { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); var power = Funs.DB.Sys_RolePower.FirstOrDefault(x => x.MenuId == menuId && roleIdList.Contains(x.RoleId)); if (power != null) { returnValue = true; } } } return returnValue; } #endregion #region 获取当前人按钮集合 /// /// 获取当前人按钮集合 /// /// 用户id /// 按钮id /// 是否具有权限 public static List GetAllButtonList(string projectId, string userId, string menuId) { Model.SUBQHSEDB db = Funs.DB; List buttonList = new List(); List buttons = new List(); if (userId == Const.sedinId) { return buttonList; } var getMenu = Funs.DB.Sys_Menu.FirstOrDefault(x => x.MenuId == menuId); var user = BLL.UserService.GetUserByUserId(userId); ////用户 if (getMenu != null && user != null) { if (userId == Const.sysglyId || userId == Const.hfnbdId || getMenu.MenuType == Const.Menu_Personal) { buttons = (from x in db.Sys_ButtonToMenu where x.MenuId == menuId select x).ToList(); } else { if (string.IsNullOrEmpty(projectId)) { buttons = (from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId where y.RoleId == user.RoleId && y.MenuId == menuId && x.MenuId == menuId select x).ToList(); } else { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); buttons = (from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId where roleIdList.Contains(y.RoleId) && y.MenuId == menuId && x.MenuId == menuId select x).ToList(); } } } if (buttons.Count() > 0) { buttonList = buttons.Select(x => x.ButtonName).ToList(); } if (!String.IsNullOrEmpty(projectId) && menuId != BLL.Const.ProjectShutdownMenuId) { var porject = BLL.ProjectService.GetProjectByProjectId(projectId); if (porject != null && (porject.ProjectState == BLL.Const.ProjectState_2 || porject.ProjectState == BLL.Const.ProjectState_3)) { buttonList.Clear(); } } return buttonList; } #endregion #region 获取当前人是否具有按钮操作权限 /// /// 获取当前人是否具有按钮操作权限 /// /// 用户id /// 按钮id /// 按钮名称 /// 是否具有权限 public static bool GetAllButtonPowerList(string projectId, string userId, string menuId, string buttonName) { Model.SUBQHSEDB db = Funs.DB; bool isPower = false; ////定义是否具备按钮权限 if (userId == Const.sedinId) { return isPower; } if (!isPower && (userId == Const.sysglyId || userId == Const.hfnbdId)) { isPower = true; } // 根据角色判断是否有按钮权限 if (!isPower) { var user = UserService.GetUserByUserId(userId); ////用户 if (user != null) { if (string.IsNullOrEmpty(projectId)) { if (!string.IsNullOrEmpty(user.RoleId)) { var buttonToMenu = from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId join z in db.Sys_Menu on x.MenuId equals z.MenuId where y.RoleId == user.RoleId && y.MenuId == menuId && x.ButtonName == buttonName && x.MenuId == menuId select x; if (buttonToMenu.Count() > 0) { isPower = true; } } } else { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); var buttonToMenu = from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId join z in db.Sys_Menu on x.MenuId equals z.MenuId where roleIdList.Contains(y.RoleId) && y.MenuId == menuId && x.ButtonName == buttonName && x.MenuId == menuId select x; if (buttonToMenu.Count() > 0) { isPower = true; } } } } if (isPower && !String.IsNullOrEmpty(projectId) && menuId != BLL.Const.ProjectShutdownMenuId) { var porject = BLL.ProjectService.GetProjectByProjectId(projectId); if (porject != null && (porject.ProjectState == BLL.Const.ProjectState_2 || porject.ProjectState == BLL.Const.ProjectState_3)) { isPower = false; } } return isPower; } public static bool GetAllButtonPowerList(string projectId, string userId, string menuId, string buttonName,string type) { Model.SUBQHSEDB db = Funs.DB; bool isPower = false; ////定义是否具备按钮权限 if (userId == Const.sedinId) { return isPower; } if (!isPower && (userId == Const.sysglyId || userId == Const.hfnbdId)) { isPower = true; } // 根据角色判断是否有按钮权限 if (!isPower) { var user = UserService.GetUserByUserId(userId); ////用户 if (user != null) { if (string.IsNullOrEmpty(projectId)) { if (!string.IsNullOrEmpty(user.RoleId)) { var buttonToMenu = from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId join z in db.Sys_Menu on x.MenuId equals z.MenuId where y.RoleId == user.RoleId && y.MenuId == menuId && x.ButtonName == buttonName && x.MenuId == menuId select x; if (buttonToMenu.Count() > 0) { isPower = true; } } } else { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); var buttonToMenu = from x in db.Sys_ButtonToMenu join y in db.Sys_ButtonPower on x.ButtonToMenuId equals y.ButtonToMenuId join z in db.Sys_Menu on x.MenuId equals z.MenuId where roleIdList.Contains(y.RoleId) && y.MenuId == menuId && x.ButtonName == buttonName && x.MenuId == menuId select x; if (buttonToMenu.Count() > 0) { isPower = true; } } } } if (isPower && type !="shiye"&&!String.IsNullOrEmpty(projectId) && menuId != BLL.Const.ProjectShutdownMenuId) { var porject = BLL.ProjectService.GetProjectByProjectId(projectId); if (porject != null && (porject.ProjectState == BLL.Const.ProjectState_2 || porject.ProjectState == BLL.Const.ProjectState_3)) { isPower = false; } } return isPower; } #endregion #region 根据用户Id判断是否为本单位(分公司) 本部用户或管理员 /// /// 根据用户UnitId判断是否为本单位用户或管理员 /// /// public static bool IsMainOrSubUnitOrAdmin(string userId) { bool result = false; if (userId == Const.sysglyId || userId == Const.hfnbdId) { result = true; } else { var user = UserService.GetUserByUserId(userId); if (user != null && user.IsOffice == true) { if (user.UnitId == GetThisUnitId()) { result = true; } else { var getUnit = UnitService.GetUnitByUnitId(user.UnitId); if (getUnit != null && getUnit.IsBranch == true) { result = true; } } } } return result; } #endregion /// ///根据userid获取当前人的单位类型(0总公司,1分公司,2其他) /// /// /// public static int GetUnitTypeByUserId(string userId) { int result = 0; var user = UserService.GetUserByUserId(userId); if ((userId == Const.hfnbdId || userId == Const.sysglyId)||(!string.IsNullOrEmpty(user.UnitId) && user.UnitId == CommonService.GetThisUnitId()))//公司级别 { result = 0; } else if (!string.IsNullOrEmpty(user.UnitId) && UnitService.GetUnitByUnitId(user.UnitId).IsBranch == true && user.IsOffice == true) //子公司级数据 { result = 1; } else //外来账户 { result = 2; } return result; } #region 根据用户Id判断是否为本单位用户或管理员 /// /// 根据用户UnitId判断是否为本单位用户或管理员 /// /// public static bool IsMainUnitOrAdmin(string userId) { bool result = false; if (userId == Const.sysglyId || userId == Const.hfnbdId) { result = true; } else { var user = UserService.GetUserByUserId(userId); string thisUnitId = string.Empty; var thisUnit = CommonService.GetIsThisUnit(); if (thisUnit != null) { thisUnitId = thisUnit.UnitId; } if (user != null && user.UnitId == thisUnitId) { result = true; } var unit = BLL.UnitService.GetUnitByUnitId(user.UnitId); if (unit != null && unit.IsBranch == true) { result = true; } } return result; } #endregion #region 根据用户ID判断是否 本单位本部用户或管理员 /// /// 根据用户UnitId判断是否为本单位用户或管理员 /// /// public static bool IsThisUnitLeaderOfficeOrManage(string userId) { bool result = false; if (userId == Const.sysglyId || userId == Const.hfnbdId || userId == Const.sedinId) { result = true; } else { var user = BLL.UserService.GetUserByUserId(userId); if (user != null && user.IsOffice == true) { result = true; } } return result; } #endregion #region 根据当前人ID、系统类型判断是否有进入权限 /// /// 根据当前人ID、系统类型判断是否有进入权限 /// /// /// /// public static bool IsHaveSystemPower(string userId, string menuType, string projectId) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { bool isHave = false; var getUser = db.Sys_User.FirstOrDefault(x => x.UserId == userId); if (getUser != null) { if (userId == Const.sysglyId || userId == Const.hfnbdId || userId == Const.sedinId) { isHave = true; } else { if (string.IsNullOrEmpty(projectId)) { var getOffice = db.Sys_RolePower.FirstOrDefault(x => x.MenuType == menuType); if (getOffice != null) { isHave = true; } } else { List roleIdList = UserService.GetRoleListByProjectIdUserId(projectId, userId); var pPower = db.Sys_RolePower.FirstOrDefault(x => roleIdList.Contains(x.RoleId) && x.MenuType == menuType); if (pPower != null) { isHave = true; } } } } return isHave; } } #endregion #region 根据主键删除附件 /// ///根据主键删除附件 /// /// public static void DeleteAttachFileById(string id) { Model.AttachFile attachFile = Funs.DB.AttachFile.FirstOrDefault(e => e.ToKeyId == id); if (attachFile != null) { if (!string.IsNullOrEmpty(attachFile.AttachUrl)) { UploadFileService.DeleteFile(Funs.RootPath, attachFile.AttachUrl); } Funs.DB.AttachFile.DeleteOnSubmit(attachFile); Funs.DB.SubmitChanges(); } } /// ///根据主键删除附件 /// /// public static void DeleteAttachFileById(string menuId, string id) { Model.SUBQHSEDB db = Funs.DB; Model.AttachFile attachFile = db.AttachFile.FirstOrDefault(e => e.MenuId == menuId && e.ToKeyId == id); if (attachFile != null) { if (!string.IsNullOrEmpty(attachFile.AttachUrl)) { BLL.UploadFileService.DeleteFile(Funs.RootPath, attachFile.AttachUrl); } db.AttachFile.DeleteOnSubmit(attachFile); db.SubmitChanges(); } } #endregion #region 根据主键删除流程 /// ///根据主键删除流程 /// /// public static void DeleteFlowOperateByID(string id) { using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString)) { var flowOperateList = from x in db.Sys_FlowOperate where x.DataId == id select x; if (flowOperateList.Count() > 0) { db.Sys_FlowOperate.DeleteAllOnSubmit(flowOperateList); db.SubmitChanges(); } } } #endregion #region 保存数据 /// /// 保存数据 /// /// 菜单id /// 主键id /// 是否关闭这步流程 /// 单据内容 /// 路径 public static void btnSaveData(string projectId, string menuId, string dataId, string userId, bool isClosed, string content, string url) { Model.Sys_FlowOperate newFlowOperate = new Model.Sys_FlowOperate { MenuId = menuId, DataId = dataId, OperaterId = userId, State = Const.State_2, IsClosed = isClosed, Opinion = "系统自动关闭流程", ProjectId = projectId, Url = url }; var user = UserService.GetUserByUserId(newFlowOperate.OperaterId); if (user != null) { var roles = RoleService.GetRoleByRoleId(user.RoleId); if (roles != null && !string.IsNullOrEmpty(roles.RoleName)) { newFlowOperate.AuditFlowName = "[" + roles.RoleName + "]"; } else { newFlowOperate.AuditFlowName = "[" + user.UserName + "]"; } newFlowOperate.AuditFlowName += "系统审核完成"; } var updateFlowOperate = from x in Funs.DB.Sys_FlowOperate where x.DataId == newFlowOperate.DataId && (x.IsClosed == false || !x.IsClosed.HasValue) select x; if (updateFlowOperate.Count() > 0) { foreach (var item in updateFlowOperate) { item.OperaterId = newFlowOperate.OperaterId; item.OperaterTime = System.DateTime.Now; item.State = newFlowOperate.State; item.Opinion = newFlowOperate.Opinion; item.AuditFlowName = "系统审核完成"; item.IsClosed = newFlowOperate.IsClosed; Funs.DB.SubmitChanges(); } } else { int maxSortIndex = 1; var flowSet = Funs.DB.Sys_FlowOperate.Where(x => x.DataId == newFlowOperate.DataId); var sortIndex = flowSet.Select(x => x.SortIndex).Max(); if (sortIndex.HasValue) { maxSortIndex = sortIndex.Value + 1; } newFlowOperate.FlowOperateId = SQLHelper.GetNewID(typeof(Model.Sys_FlowOperate)); newFlowOperate.SortIndex = maxSortIndex; newFlowOperate.OperaterTime = System.DateTime.Now; newFlowOperate.AuditFlowName = "系统审核完成"; Funs.DB.Sys_FlowOperate.InsertOnSubmit(newFlowOperate); Funs.DB.SubmitChanges(); } if (newFlowOperate.IsClosed == true) { var updateNoClosedFlowOperate = from x in Funs.DB.Sys_FlowOperate where x.DataId == newFlowOperate.DataId && (x.IsClosed == false || !x.IsClosed.HasValue) select x; if (updateNoClosedFlowOperate.Count() > 0) { foreach (var itemClosed in updateNoClosedFlowOperate) { itemClosed.IsClosed = true; Funs.DB.SubmitChanges(); } } } } #endregion #region 接收http post请求 /// 接收http post请求 /// /// 地址 /// 查询参数集合 /// public static string CreateGetHttpResponse(string url) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求对象 request.Method = "Get";//请求方式 request.ContentType = "application/json;charset=utf-8";//链接类型 try { HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse; using (Stream s = webresponse.GetResponseStream()) { StreamReader reader = new StreamReader(s, Encoding.UTF8); return reader.ReadToEnd(); } } catch (Exception ex) { return ex.Message; } } #endregion /// /// Http (GET/POST) /// /// 请求URL /// 请求参数 /// 请求方法 /// 响应内容 public static string sendHttp(string url, string data, string method) { if (method.ToLower() == "post") { HttpWebRequest req = null; HttpWebResponse rsp = null; System.IO.Stream reqStream = null; try { req = (HttpWebRequest)WebRequest.Create(url); req.Method = method; req.KeepAlive = false; req.ProtocolVersion = HttpVersion.Version10; req.Timeout = 5000; req.ContentType = "application/json;charset=utf-8"; byte[] bytes = Encoding.UTF8.GetBytes(data); reqStream = req.GetRequestStream(); reqStream.Write(bytes, 0, bytes.Length); reqStream = req.GetRequestStream(); rsp = (HttpWebResponse)req.GetResponse(); Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet); return GetResponseAsString(rsp, encoding); } catch (Exception ex) { return ex.Message; } finally { if (reqStream != null) reqStream.Close(); if (rsp != null) rsp.Close(); } } else { //创建请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //GET请求 request.Method = "GET"; request.ReadWriteTimeout = 5000; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); //返回内容 string retString = myStreamReader.ReadToEnd(); return retString; } } /// /// 组装普通文本请求参数。 /// /// Key-Value形式请求参数字典 /// URL编码后的请求数据 static string BuildQuery(IDictionary parameters, string encode) { StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; // 忽略参数名或参数值为空的参数 if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value) { if (hasParam) { postData.Append("&"); } postData.Append(name); postData.Append("="); if (encode == "gb2312") { postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312"))); } else if (encode == "utf8") { postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8)); } else { postData.Append(value); } hasParam = true; } } return postData.ToString(); } /// /// 把响应流转换为文本。 /// /// 响应流对象 /// 编码方式 /// 响应文本 static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding) { System.IO.Stream stream = null; StreamReader reader = null; try { // 以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream(); reader = new StreamReader(stream, encoding); return reader.ReadToEnd(); } finally { // 释放资源 if (reader != null) reader.Close(); if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } /// /// 计算两个字符串相似度 /// /// /// /// public static decimal GetLikeRate(string oldStr, string newStr) { //decimal Kq = 2; //decimal Kr = 1; //decimal Ks = 1; //char[] ss = oldStr.ToCharArray(); //char[] st = newStr.ToCharArray(); ////获取交集数量 //int q = 0; //foreach (var s in ss) //{ // if (st.Contains(s)) // { // q++; // } //} ////int q = ss.Intersect(st).Count(); //int s1 = ss.Length - q; //int r = st.Length - q; //return Kq * q / (Kq * q + Kr * r + Ks * s1); if (!string.IsNullOrEmpty(oldStr) && !string.IsNullOrEmpty(newStr)) { char[] ss = oldStr.ToCharArray(); char[] st = newStr.ToCharArray(); //获取交集数量 decimal q = 0; foreach (var s in st) { if (ss.Contains(s)) { q++; } } return q / st.Length; } else { return 0; } } /// /// 获取年龄 /// /// /// /// public static int CalculateAgeCorrect(DateTime birthDate) { DateTime now = DateTime.Now; int age = now.Year - birthDate.Year; if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--; return age; } #region 获取比例 /// /// 获取比例 /// public static string getRate(int A, int B) { if (A > 0 && B > 0) { var a = Convert.ToDouble(A); var b = Convert.ToDouble(B); return Convert.ToDouble(decimal.Round(decimal.Parse((a / b * 100).ToString()), 1)).ToString() + "%"; } else { return "0%"; } } #endregion #region 获取比例值 /// /// 获取比例值 /// public static double getRateValue(int A, int B) { if (A > 0 && B > 0) { var a = Convert.ToDouble(A); var b = Convert.ToDouble(B); return Convert.ToDouble(decimal.Round(decimal.Parse((a / b * 100).ToString()), 1)); } else { return 0; } } #endregion #region 绑定本部 单位-项目树 /// /// 绑定本部 单位-项目树 /// /// /// public static void SetUnitProjectTree(FineUIPro.Tree tvProjectAndUnit, Model.Sys_User CurrUser) { tvProjectAndUnit.Nodes.Clear(); string unitId = CurrUser.UnitId ?? GetThisUnitId(); var unit = UnitService.GetUnitByUnitId(unitId); if (unit != null) { FineUIPro.TreeNode rootNode = new FineUIPro.TreeNode { Text = unit.UnitName, ToolTip = "分公司机关", NodeID = unitId, EnableClickEvent = true }; if (unitId == GetThisUnitId()) { rootNode.ToolTip = "公司机关"; } tvProjectAndUnit.Nodes.Add(rootNode); var getProjects = ProjectService.GetProjectWorkList(unitId); foreach (var item in getProjects) { FineUIPro.TreeNode drootNode = new FineUIPro.TreeNode { Text = item.ProjectName, NodeID = item.ProjectId, ToolTip = "分公司项目", EnableClickEvent = true }; if (unitId == GetThisUnitId()) { rootNode.ToolTip = "直属项目"; } rootNode.Nodes.Add(drootNode); } var getSubUnit = from x in Funs.DB.Base_Unit where x.IsBranch == true select x; foreach (var subitem in getSubUnit) { FineUIPro.TreeNode crootNode = new FineUIPro.TreeNode { Text = subitem.UnitName, ToolTip = "分公司机关", NodeID = subitem.UnitId, EnableClickEvent = true }; tvProjectAndUnit.Nodes.Add(crootNode); var getSProjects = ProjectService.GetProjectWorkList(subitem.UnitId); foreach (var sitem in getSProjects) { FineUIPro.TreeNode scrootNode = new FineUIPro.TreeNode { Text = sitem.ProjectName, ToolTip = "分公司项目", NodeID = sitem.ProjectId, EnableClickEvent = true }; crootNode.Nodes.Add(scrootNode); } } } } #endregion } }