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.

243 lines
7.8 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsCodeFactry;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
namespace DSWeb.MvcShipping.DAL.MsCodeFactry
{
public class MsCodeFactryDAL
{
#region Inquery DataList
static public List<CodeFactry> GetDataList(string strCondition, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GID,CODENAME,CUSTOMERNAME,FACTRYNAME,FACTRYADDR,FACTRYATTN,FACTRYTEL,CODENAME + '-' + FACTRYNAME AS CodeAndName ");
strSql.Append(",CREATEUSER,CREATETIME,AUDITOPERATOR,AUDITDATE,BSSTATUS");
strSql.Append(",(select ShowName from [user] where GID=code_factry.CREATEUSER) as CREATEUSERREF");
strSql.Append(",(select ShowName from [user] where GID=code_factry.AUDITOPERATOR) as AUDITOPERATORREF");
strSql.Append(" from code_factry ");
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 CUSTOMERNAME,FACTRYNAME");
}
return SetData(strSql);
}
static public CodeFactry GetData(string condition)
{
CodeFactry data = null;
var list = GetDataList(condition);
if (list.Count > 0)
data = list[0];
if (data == null)
{
data = new CodeFactry();
}
return data;
}
private static List<CodeFactry> SetData(StringBuilder strSql)
{
var headList = new List<CodeFactry>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
CodeFactry data = new CodeFactry();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.CODENAME = Convert.ToString(reader["CODENAME"]);
data.CUSTOMERNAME = Convert.ToString(reader["CUSTOMERNAME"]);
data.FACTRYNAME = Convert.ToString(reader["FACTRYNAME"]);
data.FACTRYADDR = Convert.ToString(reader["FACTRYADDR"]);
data.FACTRYATTN = Convert.ToString(reader["FACTRYATTN"]);
data.FACTRYTEL = Convert.ToString(reader["FACTRYTEL"]);
data.CodeAndName = Convert.ToString(reader["CodeAndName"]);
data.FACTRYTEL = Convert.ToString(reader["FACTRYTEL"]);
data.BSSTATUS = Convert.ToString(reader["BSSTATUS"]);
if (reader["CREATETIME"] != DBNull.Value)
data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]).ToString("yyyy-MM-dd hh:mm:ss");
data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]);
data.AUDITOPERATORREF = Convert.ToString(reader["AUDITOPERATORREF"]);
if (reader["AUDITDATE"] != DBNull.Value)
data.AUDITDATE = Convert.ToDateTime(reader["AUDITDATE"]).ToString("yyyy-MM-dd hh:mm:ss");
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
public static DBResult DeleteDetail(List<CodeFactry> headData)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
if (headData != null)
{
foreach (var enumValue in headData)
{
var cmdDelete = db.GetSqlStringCommand("delete from CODE_FACTRY where GID='" + enumValue.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 LockList(String bills,string userid)
{
var result = new DBResult();
var billList = bills.Split(',');
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var bill in billList)
{
var cmdupdate = db.GetSqlStringCommand("update CODE_FACTRY set BSSTATUS='锁定',AUDITOPERATOR='"+userid+ "',AUDITDATE=GETDATE() where GID=@BILLNO");
cmdupdate.Parameters.Clear();
db.AddInParameter(cmdupdate, "@BILLNO", DbType.String, bill);
db.ExecuteNonQuery(cmdupdate, tran);
result = new DBResult();
result.Success = true;
result.Message = "锁定成功";
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "锁定错误,请重试或联系系统管理员";
return result;
}
}
return result;
}
public static DBResult UnLockList(String bills)
{
var result = new DBResult();
var billList = bills.Split(',');
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
foreach (var bill in billList)
{
var cmdupdate = db.GetSqlStringCommand("update CODE_FACTRY set BSSTATUS='未锁定',AUDITOPERATOR=null,AUDITDATE=null where GID=@BILLNO");
cmdupdate.Parameters.Clear();
db.AddInParameter(cmdupdate, "@BILLNO", DbType.String, bill);
db.ExecuteNonQuery(cmdupdate, tran);
result = new DBResult();
result.Success = true;
result.Message = "取消锁定成功";
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "取消锁定错误,请重试或联系系统管理员";
return result;
}
}
return result;
}
}
}