You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/DAL/MsOpMailLog/MsOpMailLogDAL.cs

346 lines
14 KiB
C#

3 years ago

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsOpMailLog;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using HcUtility.Comm;
using DSWeb.Areas.CommMng.Models;
using DSWeb.MvcShipping.Models.MsOpMailSend;
using System.Text.RegularExpressions;
namespace DSWeb.Areas.MvcShipping.DAL
{
public class MsOpMailLogDAL
{
#region Inquery DataList
static public List<OpMailLog> GetDataList(string strCondition, string userid, string usercode, string companyid, string sort = null)
{
var rangstr = GetRangDAStr("index", userid, usercode, companyid);
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and " + rangstr;
}
else
{
strCondition = rangstr;
}
}
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("t.GID,t.BSNO,t.BLTYPE,t.RECEIVER,t.[SUBJECT],t.[DESCRIPTION],t.[ATTACHMENT],t.[SENDTIME],t.[SENDER],t.[SENDUSER],b.MBLNO,b.CARRIER,b.ETD,b.CUSTOMERNAME");
strSql.Append(",(select top 1 showname from [user] where GID=t.SENDUSER) as SENDUSERREF ");
strSql.Append(" from op_mail_log t left join v_op_bs b on (b.BSNO=t.BSNO) ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" where " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by t.SENDTIME DESC ");
}
return SetData(strSql);
}
static public OpMailLog GetData(string condition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("t.GID,t.BSNO,t.BLTYPE,t.RECEIVER,t.[SUBJECT],t.[DESCRIPTION],t.[ATTACHMENT],t.[SENDTIME],t.[SENDER],t.[SENDUSER],b.MBLNO,b.CARRIER,b.ETD,b.CUSTOMERNAME");
strSql.Append(",(select top 1 showname from [user] where GID=t.SENDUSER) as SENDUSERREF ");
strSql.Append(" from op_mail_log t left join v_op_bs b on (b.BSNO=t.BSNO) ");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition);
}
var list = SetData(strSql);
if (list.Count > 0)
return list[0];
return new OpMailLog();
}
public static string ToText(string HTML)
{
string input = HTML;
return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(input, @"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), "(<[^>]+?>)| ", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(\s)+", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
}
/// <summary>
/// 将HTML内容转换成纯文本形式即去除HTML格式
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ConvertToText(string source)
{
string result; //remove line breaks,tabs
result = source.Replace("\r", " ");
result = result.Replace("\n", " ");
result = result.Replace("\t", " ");
//remove the header
result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase);
//remove all styles
result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase); //clearing attributes
result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase);
//insert tabs in spaces of <td> tags
result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase);
//insert line breaks in places of <br> and <li> tags
result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase);
//insert line paragraphs in places of <tr> and <p> tags
result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase);
//remove anything thats enclosed inside < >
result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
//replace special characters:
result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);
//remove extra line breaks and tabs
result = Regex.Replace(result, @" ( )+", " ");
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
result = Regex.Replace(result, @"(\r\r)+", "\r\n");
//remove blank
result = Regex.Replace(result, @"\s", "");
return result;
}
private static List<OpMailLog> SetData(StringBuilder strSql)
{
var headList = new List<OpMailLog>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
OpMailLog data = new OpMailLog();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.BSNO = Convert.ToString(reader["BSNO"]);
data.BLTYPE = Convert.ToString(reader["BLTYPE"]);
data.RECEIVER = Convert.ToString(reader["RECEIVER"]);
data.SUBJECT = Convert.ToString(reader["SUBJECT"]);
data.DESCRIPTION = ConvertToText(Convert.ToString(reader["DESCRIPTION"]));
data.ATTACHMENT = Convert.ToString(reader["ATTACHMENT"]);
if (reader["SENDTIME"] != DBNull.Value)
data.SENDTIME = Convert.ToDateTime(reader["SENDTIME"]).ToString("yyyy-MM-dd HH:mm:ss");//截港日期
data.SENDER = Convert.ToString(reader["SENDER"]);
data.MBLNO = Convert.ToString(reader["MBLNO"]);
if (reader["ETD"] != DBNull.Value)
data.ETD = Convert.ToDateTime(reader["ETD"]).ToString("yyyy-MM-dd");//到港日期
data.CUSTOMERNAME = Convert.ToString(reader["CUSTOMERNAME"]);
data.CARRIER = Convert.ToString(reader["CARRIER"]);
data.SENDUSER = Convert.ToString(reader["SENDUSER"]);
data.SENDUSERREF = Convert.ToString(reader["SENDUSERREF"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region
static public OpMailSend GetSendData(string condition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("[GID],[PROJECTID],[BLTYPE],[BSNO],[MBLNO],[RECEIVER],[SUBJECT],[DESCRIPTION],[SIGNATURE],[SENDER],[SENDSERVICE],[SENDERUSERNAME],[SENDERPASSWORD],[SENDERPORT],[ATTACHMENT]");
strSql.Append(" from op_mail_send ");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition);
}
var list = SetSendData(strSql);
if (list.Count > 0)
return list[0];
return new OpMailSend();
}
private static List<OpMailSend> SetSendData(StringBuilder strSql)
{
var headList = new List<OpMailSend>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
OpMailSend data = new OpMailSend();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.PROJECTID = Convert.ToString(reader["PROJECTID"]);
data.BSNO = Convert.ToString(reader["BSNO"]);
data.BLTYPE = Convert.ToString(reader["BLTYPE"]);
data.MBLNO = Convert.ToString(reader["MBLNO"]);
data.RECEIVER = Convert.ToString(reader["RECEIVER"]);
data.SUBJECT = Convert.ToString(reader["SUBJECT"]);
data.DESCRIPTION = Convert.ToString(reader["DESCRIPTION"]);
data.SIGNATURE = Convert.ToString(reader["SIGNATURE"]);
data.SENDER = Convert.ToString(reader["SENDER"]);
data.SENDSERVICE = Convert.ToString(reader["SENDSERVICE"]);
data.SENDERUSERNAME = Convert.ToString(reader["SENDERUSERNAME"]);
data.SENDERPASSWORD = Convert.ToString(reader["SENDERPASSWORD"]);
data.SENDERPORT = Convert.ToString(reader["SENDERPORT"]);
data.ATTACHMENT = Convert.ToString(reader["ATTACHMENT"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 保存,删除
public static DBResult DeleteMailLog(List<OpMailLog> bodyList)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdInsert =
db.GetSqlStringCommand(
@"delete from op_mail_log where GID=@GID ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.Int32, enumValue.GID);
db.ExecuteNonQuery(cmdInsert, tran);
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "删除出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "删除成功" + result.Message;
return result;
}
#endregion
#region Rang权限范围
public static string GetRangDAStr(string tb, string userid, string username, string companyid)
{
string str = "";
var strSql = new StringBuilder();
strSql.Append("SELECT");
strSql.Append(" VISIBLERANGE,OPERATERANGE");
strSql.Append(" from VW_User_Authority");
strSql.Append(" where [NAME]='modOpMailLog' and USERID='" + userid + "' and ISDELETE=0");
string visiblerange = "4";
string operaterange = "4";
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
visiblerange = Convert.ToString(reader["VISIBLERANGE"]);
operaterange = Convert.ToString(reader["OPERATERANGE"]);
break;
}
reader.Close();
}
if (visiblerange == "4")
{
str = " t.SENDER='" + username + "'";
}
else if (visiblerange == "3")
{
str = " t.SENDER='" + username + "' ";
}
else if (visiblerange == "2")
{
var rangeDa = new RangeDA();
var deptname = rangeDa.GetDEPTNAME(userid);
str = " t.SENDER in (select showname from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "') and GID in (select userid from user_baseinfo where DEPTNAME='" + deptname + "')) ";
}
else if (visiblerange == "1")
{
str = " t.SENDER in (select showname from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "')) ";
}
else if (visiblerange == "0")
{
str = " 1=1 ";
}
return str;
}
#endregion
#region 参照部分
#endregion
}
}