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 GetDisportList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" PORTID,PORT,CHAU,PORTTYPE,EDICODE,cname,country,LANEGID"); strSql.Append(" ,(select LANE from code_lanes where gid=code_disport.lanegid) LANE "); strSql.Append(" from code_disport "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } strSql.Append(" order by LANE desc"); return SetData(strSql); } #region 根据ID进行查询 static public List 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 SetData(StringBuilder strSql) { var headList = new List(); 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 SetDataByID(string strSql) { var headList = new List(); 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.LANEGID = 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.EXPLAIN = 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 GetCountrytList() { string sql = "select Gid,countryid+' '+country as countryid,country from code_country"; return SetCountryData(sql); } private static List SetCountryData(string strSql) { var headList = new List(); 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 GetLaneList() { string sql = "select gid,laneid,lane from code_lanes"; return SetLaneData(sql); } private static List SetLaneData(string strSql) { var headList = new List(); 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 } }