using CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Lskj.PubPower
{
public static class ReplaceHelper
{
///
/// 带参数控件替换规则
///
private static readonly string _replace_param = "#[^##]+#";
private static readonly string _replace_parent_left_key = "{#";
private static readonly string _replace_param_first_key = "{";
private static readonly string _replace_param_end_key = "}";
private static readonly string _proc_first_key = "@";
///
/// 树节点替换key
///
private static readonly string _replace_parent_key = "{parent.key}";
///
/// 替换sql语句中的对应字段
///
private static readonly string _replace_field_key = "{([^{])+}";
///
/// 存储过程标识
///
private static readonly string _procedureFlag = "exec";
///
/// 查询语句标记
///
private static readonly string _selectFlag = "select";
///
/// 查询条件替换值
///
private static readonly string _search_control_value = "{value}";
///
/// loginid 固定字段
///
private static readonly string _loginid_key = "{loginid}";
///
/// loginname 固定字段
///
private static readonly string _loginname_key = "{loginname}";
///
/// groupid 固定字段
///
private static readonly string _groupid_key = "{groupid}";
///
/// password 固定字段
///
private static readonly string _password_key = "{password}";
///
/// 车间固定字段
///
private static readonly string _cj_key = "{logincj}";
///
/// 机台固定字段
///
private static readonly string _jt_key = "{loginjt}";
///
/// 班次固定字段
///
private static readonly string _bz_key = "{loginbz}";
///
/// 班组固定字段
///
private static readonly string _className_key = "{className}";
///
/// 替换表格列标识
///
private static readonly string _column_key = "{COLUMN_NAME}";
///
/// 替换表格列标题标识
///
private static readonly string _column_title_key = "{COLUMN_TITLE}";
///
/// 取单元格值
///
private static readonly string _column_value_key = "{COLUMN_VALUE}";
///
/// 系统id固定字段
///
private static readonly string _seriesId_id = "{SeriesId}";
///
/// 系统种类固定字段
///
private static readonly string _logintype = "{logintype}";
///
/// UserCode 固定字段
///
private static readonly string _userCode_key = "{lserpUserCode}";
///
/// 参数替换父级字符前缀
///
/// The replace parent left key.
public static string ReplaceParentLeftKey { get { return _replace_parent_left_key; } }
///
/// 参数替换首字符
///
/// The replace parameter first key.
public static string ReplaceParamFirstKey { get { return _replace_param_first_key; } }
///
/// 参数替换尾字符
///
/// The replace parameter end key.
public static string ReplaceParamEndKey { get { return _replace_param_end_key; } }
///
/// 存储过程参数执行方式首字符
///
/// The proc first key.
public static string ProcFirstKey { get { return _proc_first_key; } }
///
/// 带参数控件替换规则
///
/// The replace parameter.
public static string ReplaceParamKey { get { return _replace_param; } }
///
/// 说明:替换userid、username、groupid
/// 创建人:龚宇超
/// 创建日期:2017-10-25
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The default value.
/// System.String.
public static string ReplaceUserInfo(string defaultValue)
{
return ReplaceUserInfo(defaultValue, ERPInfo.Instance.EmployeeId+"", ERPInfo.Instance.EmployeeName, ERPInfo.Instance.GroupId, ERPInfo.Instance.Password);
}
///
/// 说明:替换userid、username、groupid
/// 创建人:龚宇超
/// 创建日期:2017-08-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The default value.
/// The user identifier.
/// Name of the user.
/// The group identifier.
/// System.String.
public static string ReplaceUserInfo(string defaultValue, string userId, string userName, string groupId, string password)
{
return defaultValue = defaultValue.Replace(_loginid_key, userId, true)
.Replace(_loginname_key, userName, true)
.Replace(_groupid_key, groupId, true)
.Replace(_password_key, password, true)
//.Replace(_cj_key, ERPInfo.Instance.WorkCJ, true)
//.Replace(_jt_key, ERPInfo.Instance.WorkJT, true)
//.Replace(_bz_key, ERPInfo.Instance.WorkBZ, true)
//.Replace(_seriesId_id, ERPInfo.Instance.SeriesId, true)
//.Replace(_className_key, ERPInfo.Instance.ClassName, true)
.Replace(_logintype, "0", true);
//.Replace(_userCode_key, ERPInfo.Instance.LoginAccount, true);
}
///
/// 说明:带参数替换为1=1
/// 创建人:龚宇超
/// 创建日期:2017-08-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// System.String.
public static string ReplaceParam(string defaultValue)
{
try
{
if (SubstringCount(defaultValue, "#") > 1)
{
return defaultValue.Replace(defaultValue.Substring(defaultValue.IndexOf('#'), defaultValue.LastIndexOf('#') + 1 - defaultValue.IndexOf('#')), " 1=1 ");
}
else
{
return defaultValue;
}
}
catch (Exception)
{
return Regex.Replace(defaultValue, _replace_param, " 1=1 ");
}
}
///
/// 说明:判断字符串出现次数
/// 创建人:龚宇超
/// 创建日期:
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The string.
/// The substring.
/// System.Int32.
private static int SubstringCount(string str, string substring)
{
if (str.Contains(substring))
{
string strReplaced = str.Replace(substring, "");
return (str.Length - strReplaced.Length) / substring.Length;
}
return 0;
}
/// 说明:替换树节点parentkey
/// 创建人:龚宇超
/// 创建日期:2017-09-04
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// System.String.
public static string ReplaceTreeViewParentKeyCond(string defaultValue, string value)
{
if (string.IsNullOrWhiteSpace(defaultValue)) return defaultValue;
return defaultValue.Replace(_replace_parent_key, value);
}
///
/// 说明:获取sql语句中替换
/// 创建人:龚宇超
/// 创建日期:2017-09-04
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// List<System.String>.
public static List GetParamFields(string defaultValue)
{
List list = new List();
Regex regex = new Regex(_replace_field_key);
MatchCollection mcs = regex.Matches(defaultValue);
foreach (Match item in mcs)
{
if (list.IndexOf(item.Value) == -1)
list.Add(item.Value);
}
return list;
}
///
/// 说明:{key}字符串替换
/// 创建人:龚宇超
/// 创建日期:2018-05-28
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The key.
/// The value.
/// 是否忽略大小写
/// System.String.
public static string Replace(this string str, string key, string value, bool ignoreCase)
{
if (string.IsNullOrEmpty(str)) return str;
return Regex.IsMatch(str, key, RegexOptions.IgnoreCase) ?
System.Text.RegularExpressions.Regex.Replace(str, key, value, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)
: str;
}
///
/// 说明:将{xxx}格式全部替换为某个值
/// 创建人:龚宇超
/// 创建日期:2017-12-01
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
/// l
/// The default value.
/// The value.
/// System.String.
public static string ReplaceParamToValue(string defaultValue, string value)
{
if (string.IsNullOrEmpty(defaultValue)) return defaultValue;
var fields = GetParamFields(defaultValue);
foreach (string item in fields)
{
defaultValue = defaultValue.Replace(item, value);
}
return defaultValue;
}
}
}