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/MsCodeExcelExpFormat/MsCodeExcelExpFormatDAL.cs

397 lines
14 KiB
C#

2 years ago

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsCodeExcelExpFormat;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
namespace DSWeb.MvcShipping.DAL.MsCodeExcelExpFormatDAL
{
public class MsCodeExcelExpFormatDAL
{
#region Inquery DataList
static public List<MsCodeExcelExpFormat> GetDataList(string strCondition, string userid, string usercode, string orgcode, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,FMNAME,CORPID,FILEPATH,REMARK,BSTYPE,CREATEUSER,CREATETIME");
strSql.Append(",(select ShowName from [user] where GID=code_excel_expformat.CREATEUSER) as CREATEUSERREF");
strSql.Append(" from code_excel_expformat where CORPID='"+orgcode+"' ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by FMNAME");
}
return SetData(strSql);
}
static public MsCodeExcelExpFormat GetData(string condition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,FMNAME,CORPID,FILEPATH,BSTYPE,REMARK,CREATEUSER,CREATETIME");
strSql.Append(",(select ShowName from [user] where GID=code_excel_expformat.CREATEUSER) as CREATEUSERREF");
strSql.Append(" from code_excel_expformat ");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition);
}
var list = SetData(strSql);
if (list.Count > 0)
return list[0];
return new MsCodeExcelExpFormat();
}
private static List<MsCodeExcelExpFormat> SetData(StringBuilder strSql)
{
var headList = new List<MsCodeExcelExpFormat>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsCodeExcelExpFormat data = new MsCodeExcelExpFormat();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.FMNAME = Convert.ToString(reader["FMNAME"]);
data.FILEPATH = Convert.ToString(reader["FILEPATH"]);
data.CORPID = Convert.ToString(reader["CORPID"]);
data.BSTYPE = Convert.ToString(reader["BSTYPE"]);
data.REMARK = Convert.ToString(reader["REMARK"]);
data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]);
data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]);
data.CREATETIME = Convert.ToString(reader["CREATETIME"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 明细表
static public List<MsCodeExcelExpFormatDetail> GetBodyList(string strCondition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,TEMPLATEID,FIELDNAME");
strSql.Append(",XP,YP,FIXSTR");
strSql.Append(" from code_excel_expformatdetail ");
if (strCondition.Trim() != String.Empty)
{
strSql.Append(" where " + strCondition);
}
strSql.Append(" order by XP,YP ");
return SetBodyData(strSql);
}
private static List<MsCodeExcelExpFormatDetail> SetBodyData(StringBuilder strSql)
{
var bodyList = new List<MsCodeExcelExpFormatDetail>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsCodeExcelExpFormatDetail data = new MsCodeExcelExpFormatDetail();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.TEMPLATEID = Convert.ToString(reader["TEMPLATEID"]);
data.FIELDNAME = Convert.ToString(reader["FIELDNAME"]);
if (reader["XP"] != DBNull.Value)
data.XP = Convert.ToInt16(reader["XP"]);
if (reader["YP"] != DBNull.Value)
data.YP = Convert.ToInt16(reader["YP"]);
data.FIXSTR = Convert.ToString(reader["FIXSTR"]);
#endregion
bodyList.Add(data);
}
reader.Close();
}
return bodyList;
}
#endregion
public static DBResult SaveDetail(MsCodeExcelExpFormat headData, List<MsCodeExcelExpFormatDetail> 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(
@"insert into code_excel_expformatdetail (GID, TEMPLATEID,FIELDNAME,XP,YP,FIXSTR)
values (@GID,@TEMPLATEID,@FIELDNAME,@XP,@YP,@FIXSTR) ");
var cmdUpdate =
db.GetSqlStringCommand(
@"update code_excel_expformatdetail set FIELDNAME=@FIELDNAME,XP=@XP,YP=@YP,FIXSTR=@FIXSTR
where GID=@GID ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
if (enumValue.TEMPLATEID == "" || enumValue.TEMPLATEID == "*")
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, enumValue.GID);
db.AddInParameter(cmdInsert, "@TEMPLATEID", DbType.String, headData.GID);
db.AddInParameter(cmdInsert, "@FIELDNAME", DbType.String, enumValue.FIELDNAME);
db.AddInParameter(cmdInsert, "@XP", DbType.Int16, enumValue.XP);
db.AddInParameter(cmdInsert, "@YP", DbType.Int16, enumValue.YP);
db.AddInParameter(cmdInsert, "@FIXSTR", DbType.String, enumValue.FIXSTR);
db.ExecuteNonQuery(cmdInsert, tran);
}
else {
cmdUpdate.Parameters.Clear();
db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID);
db.AddInParameter(cmdUpdate, "@FIELDNAME", DbType.String, enumValue.FIELDNAME);
db.AddInParameter(cmdUpdate, "@XP", DbType.Int16, enumValue.XP);
db.AddInParameter(cmdUpdate, "@YP", DbType.Int16, enumValue.YP);
db.AddInParameter(cmdUpdate, "@FIXSTR", DbType.String, enumValue.FIXSTR);
db.ExecuteNonQuery(cmdUpdate, 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(MsCodeExcelExpFormat headData)
{
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 code_excel_expformatdetail where TEMPLATEID='" + 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(string GID)
{
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 code_excel_expformatdetail where GID='" + 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 UpdateFileData(string filename, string name, string BSNO, string userid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdInsert =
db.GetSqlStringCommand(
@"update code_excel_expformat set FILEPATH=@FILEPATH where GID=@GID ");
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, BSNO);
db.AddInParameter(cmdInsert, "@FILEPATH", DbType.String, filename);
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;
}
#region 参照部分
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]='modOpApplyService' 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 = "S.OP='" + username + "'";
}
else if (visiblerange == "3")
{
str = "S.OP='" + username + "'";
}
else if (visiblerange == "2")
{
var rangeDa = new RangeDA();
var deptname = rangeDa.GetDEPTNAME(userid);
str = " S.OP 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 = " S.OP 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
}
}