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/DisportsD/DisportsDal.cs

272 lines
11 KiB
C#

2 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using HcUtility.Comm;
using DSWeb.Areas.MvcShipping.Models.Disport;
using System.Text.RegularExpressions;
namespace DSWeb.Areas.MvcShipping.DAL.Disports
{
public class DisportsDal
{
//查询disport语句
static public List<DisportModel> GetDisportList(string ename, string cname)
{
StringBuilder sb = new StringBuilder("select cd.PORTID,cd.PORT,cd.CHAU,cd.PORTTYPE,cd.EDICODE,cd.cname,cd.country,cl.LANE,cd.LANEGID from code_disport as cd left join code_lanes as cl on(cd.lanegid=cl.gid) ");
if (!string.IsNullOrEmpty(ename)&&!string.IsNullOrEmpty(cname))
{
sb.Append(" where port like('%" + ename + "%') and cname like('%" + cname + "%')");
}
else if (!string.IsNullOrEmpty(ename))
{
sb.Append("where port like ('%"+ename+"%')");
}
else if (!string.IsNullOrEmpty(cname))
{
sb.Append("where cname like ('%" + cname + "%')");
}
sb.Append(" order by cl.LANE desc");
return SetData(sb);
}
#region 根据ID进行查询
static public List<DisportModel> GetDisportListByID(string headData)
{
string sql = "select cd.PORTID,cd.PORT,cd.CHAU,cd.PORTTYPE,cd.EDICODE,cd.cname,cd.country,cl.LANE,cd.lanegid,cd.explain,cd.LANEGID from code_disport as cd left join code_lanes as cl on(cd.lanegid=cl.gid) where portid=('" + headData + "')";
return SetDataByID(sql);
}
#endregion
private static List<DisportModel> SetData(StringBuilder strSql)
{
var headList = new List<DisportModel>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
DisportModel data = new DisportModel();
#region Set DB data to Object
data.PORTID = Convert.ToString(reader["PORTID"]);
data.PORT = Convert.ToString(reader["PORT"]);
data.CHAU = Convert.ToString(reader["CHAU"]);
data.PORTTYPE = Convert.ToString(reader["PORTTYPE"]);
data.EDICODE = Convert.ToString(reader["EDICODE"]);
data.cname = Convert.ToString(reader["CNAME"]);
data.country = Convert.ToString(reader["country"]);
data.LANE = Convert.ToString(reader["LANE"]);
data.LANEGID = Convert.ToString(reader["LANEGID"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
private static List<DisportModel> SetDataByID(string strSql)
{
var headList = new List<DisportModel>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
DisportModel data = new DisportModel();
#region Set DB data to Object
data.PORTID = Convert.ToString(reader["PORTID"]);
data.GID = Convert.ToString(reader["lanegid"]);
data.PORT = Convert.ToString(reader["PORT"]);
data.CHAU = Convert.ToString(reader["CHAU"]);
data.PORTTYPE = Convert.ToString(reader["PORTTYPE"]);
data.EDICODE = Convert.ToString(reader["EDICODE"]);
data.cname = Convert.ToString(reader["CNAME"]);
data.country = Convert.ToString(reader["country"]);
data.LANE = Convert.ToString(reader["LANE"]);
data.DESCRIP = Convert.ToString(reader["explain"]);
data.LANEGID = Convert.ToString(reader["LANEGID"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#region 删除
public static DBResult DeleteDetail(DisportModel 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 code_disport where PORTID='" + headData.PORTID + "'");
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 插入code_disport表
public static DBResult InsertDetail(DisportModel headData)
{
var result = new DBResult();
try
{
string csql = "select count(*) as c from code_disport where cname='" + headData.cname + "' or port='" + headData.PORT.Replace("'","''") + "'";
int findresult = Convert.ToInt32(findname(csql));
if (findresult>0)
{
result.Success = false;
result.Message = "添加失败,中文名或者英文名不可重复添加!";
return result;
}
}
catch (Exception)
{
result.Success = false;
result.Message = "添加出现错误,请重试或联系系统管理员";
return result;
}
result.Success = true;
result.Message = "添加成功";
return result;
}
#endregion
#region 读取数据库返回结果
private static string findname(string strSql)
{
string c = "";
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
#region Set DB data to Object
c = Convert.ToString(reader["c"]);
#endregion
}
reader.Close();
}
return c;
}
#endregion
#region 获取国家列表
static public List<CountryTypeModel> GetCountrytList()
{
string sql = "select Gid,countryid+' '+country as countryid,country from code_country";
return SetCountryData(sql);
}
private static List<CountryTypeModel> SetCountryData(string strSql)
{
var headList = new List<CountryTypeModel>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
CountryTypeModel data = new CountryTypeModel();
#region Set DB data to Object
string re = Convert.ToString(reader["country"]);
//正则取出括号内的英文
Regex reg = new Regex(@"(?is)(?<=\()[^\)]+(?=\))");
//正则取出括号外的中文
string china = Regex.Replace(re, @"\([^\(]*\)", "");
string eng = reg.Match(re).ToString();
string country = eng + "-" + china;
data.GID = Convert.ToString(reader["GID"]);
data.countryid = Convert.ToString(reader["country"]);
data.country = country;
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 获取航线列表
static public List<lanesTypeModel> GetLaneList()
{
string sql = "select gid,laneid,lane from code_lanes";
return SetLaneData(sql);
}
private static List<lanesTypeModel> SetLaneData(string strSql)
{
var headList = new List<lanesTypeModel>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
lanesTypeModel data = new lanesTypeModel();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.laneid = Convert.ToString(reader["laneid"]);
data.LANE = Convert.ToString(reader["lane"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
public static DBResult SDetail(DisportModel 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_disport where portid='" + headData.PORTID + "'");
db.ExecuteNonQuery(cmdDelete, tran);
string sql = string.Format("insert into code_disport(Portid,port,cname,CHAU,EXPLAIN,PORTTYPE,EDICODE,country,LANEGID) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", headData.PORTID, headData.PORT.Replace("'","''"), headData.cname, headData.CHAU, headData.DESCRIP, headData.PORTTYPE, headData.EDICODE, headData.country, headData.LANEGID);
var cmd = db.GetSqlStringCommand(sql);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功";
return result;
}
}
}