DS7/DSWeb/Areas/MvcShipping/DAL/MsCodeCountry/MsCodeCountryDAL.cs

225 lines
9.3 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsCodeCountry;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
namespace DSWeb.MvcShipping.DAL.MsCodeCountry
{
public class MsCodeCountryDAL
{
#region Inquery DataList
static public List<CodeCountry> GetDataList(string strCondition, string companyid, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("[GID],[COUNTRYID],[COUNTRY],[COUNTRYENAME],[CAPITAL],[CHAU],[EXPLAIN],[COUNTRYID_3]");
strSql.Append(" from code_country ");
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 COUNTRY,COUNTRYENAME");
}
return SetData(strSql);
}
/// <summary>
/// 分页获取数据
/// </summary>
/// <param name="start"></param>
/// <param name="limit"></param>
/// <param name="strCondition"></param>
/// <param name="companyid"></param>
/// <param name="sort"></param>
/// <returns></returns>
static public List<CodeCountry> GetDataList(int start, int limit, string strCondition, string companyid, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT * FROM (SELECT row_number() over (");
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by COUNTRY,COUNTRYENAME");
}
strSql.Append(@") as num ,");
strSql.Append(" [GID],[COUNTRYID],[COUNTRY],[COUNTRYENAME],[CAPITAL],[CHAU],[EXPLAIN],[COUNTRYID_3],TARIFF,(select EnumValueName from tSysEnumValue where enumtypeid=9000 and enumvalueid=Tariff)TARIFFREF");
strSql.Append(" from code_country");
//条件
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" WHERE " + strCondition);
}
strSql.Append(@")as t");
strSql.Append(string.Format(" where t.num>{0} and t.num<={1} ORDER BY NUM ", start, start + limit));
return SetData(strSql);
}
static public CodeCountry GetData(string condition, string companyid)
{
CodeCountry data = null;
var list = GetDataList(condition,companyid);
if (list.Count > 0)
data = list[0];
if (data == null)
{
data = new CodeCountry();
}
return data;
}
public static int getTotalCount(string strCondition)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT COUNT(1) ");
strSql.Append(" from code_country");
if (!string.IsNullOrEmpty(strCondition))
strSql.Append(" where " + strCondition);
int cnt = 0;
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
cnt = Convert.ToInt32(reader[0]);
}
}
return cnt;
}
private static List<CodeCountry> SetData(StringBuilder strSql)
{
var headList = new List<CodeCountry>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
CodeCountry data = new CodeCountry();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.COUNTRYID = Convert.ToString(reader["COUNTRYID"]);
data.COUNTRY = Convert.ToString(reader["COUNTRY"]);
data.COUNTRYENAME = Convert.ToString(reader["COUNTRYENAME"]);
data.CAPITAL = Convert.ToString(reader["CAPITAL"]);
data.CHAU = Convert.ToString(reader["CHAU"]);
data.EXPLAIN = Convert.ToString(reader["EXPLAIN"]);
data.COUNTRYID_3 = Convert.ToString(reader["COUNTRYID_3"]);
data.TARIFF = Convert.ToString(reader["TARIFF"]);
data.TARIFFREF = Convert.ToString(reader["TARIFFREF"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
public static DBResult SaveDetail(List<CodeCountry> 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_country ([GID],[COUNTRYID],[COUNTRY],[COUNTRYENAME],[CAPITAL],[CHAU],[EXPLAIN],[COUNTRYID_3],[TARIFF])
values (@GID,@COUNTRYID,@COUNTRY,@COUNTRYENAME,@CAPITAL,@CHAU,@EXPLAIN,@COUNTRYID_3,@TARIFF) ");
var cmdUpdate =
db.GetSqlStringCommand(
@"update code_country set COUNTRYID=@COUNTRYID,COUNTRY=@COUNTRY,COUNTRYENAME=@COUNTRYENAME,CAPITAL=@CAPITAL,CHAU=@CHAU,EXPLAIN=@EXPLAIN,COUNTRYID_3=@COUNTRYID_3,TARIFF=@TARIFF where GID=@GID ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
if (enumValue.GID == "*" || enumValue.GID == "")
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@COUNTRYID", DbType.String, enumValue.COUNTRYID);
db.AddInParameter(cmdInsert, "@COUNTRY", DbType.String, enumValue.COUNTRY);
db.AddInParameter(cmdInsert, "@COUNTRYENAME", DbType.String, enumValue.COUNTRYENAME);
db.AddInParameter(cmdInsert, "@CAPITAL", DbType.String, enumValue.CAPITAL);
db.AddInParameter(cmdInsert, "@CHAU", DbType.String, enumValue.CHAU);
db.AddInParameter(cmdInsert, "@EXPLAIN", DbType.String, enumValue.EXPLAIN);
db.AddInParameter(cmdInsert, "@COUNTRYID_3", DbType.String, enumValue.COUNTRYID_3);
db.AddInParameter(cmdInsert, "@TARIFF", DbType.String, enumValue.TARIFF);
db.ExecuteNonQuery(cmdInsert, tran);
}
else
{
cmdUpdate.Parameters.Clear();
db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID);
db.AddInParameter(cmdUpdate, "@COUNTRYID", DbType.String, enumValue.COUNTRYID);
db.AddInParameter(cmdUpdate, "@COUNTRY", DbType.String, enumValue.COUNTRY);
db.AddInParameter(cmdUpdate, "@COUNTRYENAME", DbType.String, enumValue.COUNTRYENAME);
db.AddInParameter(cmdUpdate, "@CAPITAL", DbType.String, enumValue.CAPITAL);
db.AddInParameter(cmdUpdate, "@CHAU", DbType.String, enumValue.CHAU);
db.AddInParameter(cmdUpdate, "@EXPLAIN", DbType.String, enumValue.EXPLAIN);
db.AddInParameter(cmdUpdate, "@COUNTRYID_3", DbType.String, enumValue.COUNTRYID_3);
db.AddInParameter(cmdUpdate, "@TARIFF", DbType.String, enumValue.TARIFF);
db.ExecuteNonQuery(cmdUpdate, tran);
}
}
}
tran.Commit();
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误<"+e.Message+">,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功" + result.Message;
return result;
}
#region 参照部分
#endregion
}
}