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/TruckMng/DAL/MsCodeTruckPort/MsCodeTruckPortDAL.cs

157 lines
5.5 KiB
C#

3 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.TruckMng.Models.CodeTruckPort;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
namespace DSWeb.TruckMng.DAL.MsCodeTruckPort
{
public class MsCodeTruckPortDAL
{
#region Inquery DataList
static public List<CodeTruckPort> GetDataList(string strCondition, string companyid, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("PORTID,PORTCODE,PORT,PROVINCE,PORTCODE+'-'+PORT CODEANDNAME");
strSql.Append(" from code_truckport ");
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 PORTCODE");
}
return SetData(strSql);
}
static public CodeTruckPort GetData(string condition, string companyid)
{
CodeTruckPort data = null;
var list = GetDataList(condition,companyid);
if (list.Count > 0)
data = list[0];
if (data == null)
{
data = new CodeTruckPort();
}
return data;
}
private static List<CodeTruckPort> SetData(StringBuilder strSql)
{
var headList = new List<CodeTruckPort>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
CodeTruckPort data = new CodeTruckPort();
#region Set DB data to Object
data.PORTID = Convert.ToString(reader["PORTID"]);
data.PORTCODE = Convert.ToString(reader["PORTCODE"]);
data.PORT = Convert.ToString(reader["PORT"]);
data.PROVINCE = Convert.ToString(reader["PROVINCE"]);
data.CODEANDNAME = Convert.ToString(reader["CODEANDNAME"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
public static DBResult SaveDetail(List<CodeTruckPort> bodyList, string companyid)
{
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_truckport (PORTID,PORTCODE,PORT,PROVINCE)
values (@PORTID,@PORTCODE,@PORT,@PROVINCE) ");
var cmdUpdate =
db.GetSqlStringCommand(
@"update code_truckport set PORTCODE=@PORTCODE,PORT=@PORT,PROVINCE=@PROVINCE where PORTID=@PORTID ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
if (enumValue.PORTID == "*" || enumValue.PORTID == "")
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@PORTID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@PORTCODE", DbType.String, enumValue.PORTCODE);
db.AddInParameter(cmdInsert, "@PORT", DbType.String, enumValue.PORT);
db.AddInParameter(cmdInsert, "@PROVINCE", DbType.String, enumValue.PROVINCE);
db.ExecuteNonQuery(cmdInsert, tran);
}
else
{
cmdUpdate.Parameters.Clear();
db.AddInParameter(cmdUpdate, "@PORTID", DbType.String, enumValue.PORTID);
db.AddInParameter(cmdUpdate, "@PORT", DbType.String, enumValue.PORT);
db.AddInParameter(cmdUpdate, "@PROVINCE", DbType.String, enumValue.PROVINCE);
db.AddInParameter(cmdUpdate, "@PORTCODE", DbType.String, enumValue.PORTCODE);
db.ExecuteNonQuery(cmdUpdate, tran);
}
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功" + result.Message;
return result;
}
#region 参照部分
#endregion
}
}