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.

372 lines
13 KiB
C#

10 months ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsInfoClientMailProject;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
namespace DSWeb.MvcShipping.DAL.MsInfoClientMailProject
{
public class MsInfoClientMailProjectDAL
{
#region Inquery DataList
static public List<InfoClientMailProject> GetDataList(string strCondition, string userid, string usercode, string orgcode, string sort = null)
{
var rangstr = GetRangDAStr("index", userid, usercode, orgcode);
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and (" + rangstr + ")";
}
else
{
strCondition = rangstr ;
}
}
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,CUSTNAME,PROJECTNAME,ISDEF,INPUTBY,INPUTDATE,REMARK");
strSql.Append(",(select ShowName from [user] where GID=info_client_mailproject.INPUTBY) as INPUTBYREF");
strSql.Append(",(case ISDEF when 1 then '是' else '否' end) as ISDEFREF ");
strSql.Append(" from info_client_mailproject ");
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 CUSTNAME,PROJECTNAME");
}
return SetData(strSql);
}
static public InfoClientMailProject GetData(string condition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,CUSTNAME,PROJECTNAME,ISDEF,INPUTBY,INPUTDATE,REMARK");
strSql.Append(",(select ShowName from [user] where GID=info_client_mailproject.INPUTBY) as INPUTBYREF");
strSql.Append(",(case ISDEF when 1 then '是' else '否' end) as ISDEFREF ");
strSql.Append(" from info_client_mailproject ");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition);
}
var list = SetData(strSql);
if (list.Count > 0)
return list[0];
return new InfoClientMailProject();
}
private static List<InfoClientMailProject> SetData(StringBuilder strSql)
{
var headList = new List<InfoClientMailProject>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
InfoClientMailProject data = new InfoClientMailProject();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.CUSTNAME = Convert.ToString(reader["CUSTNAME"]);
data.PROJECTNAME = Convert.ToString(reader["PROJECTNAME"]);
if (reader["ISDEF"] != DBNull.Value)
data.ISDEF = Convert.ToBoolean(reader["ISDEF"]);
data.ISDEFREF = Convert.ToString(reader["ISDEFREF"]);
data.INPUTBY = Convert.ToString(reader["INPUTBY"]);
data.REMARK = Convert.ToString(reader["REMARK"]);
data.INPUTBYREF = Convert.ToString(reader["INPUTBYREF"]);
if (reader["INPUTDATE"] != DBNull.Value)
data.INPUTDATE = Convert.ToDateTime(reader["INPUTDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 明细表
static public List<InfoClientMailProjectDetail> GetBodyList(string strCondition, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,PROJECTID,BLTYPE,RECEIVER,SUBJECT,DESCRIPTION");
strSql.Append(",ATTACHMENT");
strSql.Append(" from info_client_mailproject_detail ");
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 BLTYPE ");
}
return SetBodyData(strSql);
}
private static List<InfoClientMailProjectDetail> SetBodyData(StringBuilder strSql)
{
var bodyList = new List<InfoClientMailProjectDetail>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
InfoClientMailProjectDetail data = new InfoClientMailProjectDetail();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.PROJECTID = Convert.ToString(reader["PROJECTID"]);
data.BLTYPE = Convert.ToString(reader["BLTYPE"]);
data.RECEIVER = Convert.ToString(reader["RECEIVER"]);
data.SUBJECT = Convert.ToString(reader["SUBJECT"]);
data.DESCRIPTION = Convert.ToString(reader["DESCRIPTION"]);
data.ATTACHMENT = Convert.ToString(reader["ATTACHMENT"]);
#endregion
bodyList.Add(data);
}
reader.Close();
}
return bodyList;
}
public static DBResult SaveDetail(InfoClientMailProject headData, List<InfoClientMailProjectDetail> bodyList,string userid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdDelete = db.GetSqlStringCommand("delete from info_client_mailproject_detail where PROJECTID='" + headData.GID + "'");
db.ExecuteNonQuery(cmdDelete, tran);
var cmdInsert =
db.GetSqlStringCommand(
@"insert into info_client_mailproject_detail (GID,PROJECTID,BLTYPE,RECEIVER,SUBJECT,DESCRIPTION,ATTACHMENT)
values (@GID,@PROJECTID,@BLTYPE,@RECEIVER,@SUBJECT,@DESCRIPTION,@ATTACHMENT) ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@PROJECTID", DbType.String,headData.GID);
db.AddInParameter(cmdInsert, "@BLTYPE", DbType.String, enumValue.BLTYPE);
db.AddInParameter(cmdInsert, "@RECEIVER", DbType.String, enumValue.RECEIVER);
db.AddInParameter(cmdInsert, "@SUBJECT", DbType.String, enumValue.SUBJECT);
db.AddInParameter(cmdInsert, "@DESCRIPTION", DbType.String, enumValue.DESCRIPTION);
db.AddInParameter(cmdInsert, "@ATTACHMENT", DbType.String, enumValue.ATTACHMENT);
db.ExecuteNonQuery(cmdInsert, tran);
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功";
return result;
}
public static DBResult DeleteDetail(InfoClientMailProject headData)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmd = db.GetSqlStringCommand("delete from info_client_mailproject where GID='" + headData.GID + "'");
db.ExecuteNonQuery(cmd, tran);
var cmdDelete = db.GetSqlStringCommand("delete from info_client_mailproject_detail where PROJECTID='" + headData.GID + "'");
db.ExecuteNonQuery(cmdDelete, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功";
return result;
}
public static DBResult DeleteDetail(InfoClientMailProjectDetail headData)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmd = db.GetSqlStringCommand("delete from info_client_mailproject_detail where GID='" + headData.GID + "'");
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功";
return result;
}
#endregion
#region 参照部分
public static string GetRangDAStr(string tb, string userid, string usercode, string orgcode)
{
string str = "";
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(" VISIBLERANGE,OPERATERANGE ");
strSql.Append(" from VW_User_Authority ");
strSql.Append(" where [NAME]='modCodeFeeTemplateSet' 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 = "1=2";
}
else if (visiblerange == "3")
{
if (tb == "index")
{
str = " INPUTBY='" + userid + "'";
}
else
{
//str = " UPPER(CORPID)='" + orgcode + "'";
}
}
else if (visiblerange == "2")
{
//if (tb == "index")
//{
// str = " UPPER(CORPID)='" + orgcode + "'";
//}
//else
//{
// str = " UPPER(CORPID)='" + orgcode + "'";
//}
}
else if (visiblerange == "1")
{
// str = " UPPER(CORPID)='" + orgcode + "'";
}
return str;
}
#endregion
}
}