using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.Areas.Import.Models.Cargoinfo; using DSWeb.Areas.Import.Models.Cargociq; using HcUtility.Comm; using Microsoft.Practices.EnterpriseLibrary.Data; namespace DSWeb.Areas.Import.DAL.Cargoinfo { public partial class CargoinfoDAL { #region 主表 static public List GetDataList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("ID,Code,Name,tax_zhg,tax_pt,tax_zz,tax_cl,JG"); strSql.Append(" from Import_cargoinfo "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } return SetData(strSql); } static public Decimal GetCargoid(string CodeID) { Decimal cargoid=0; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("Max(ID) as ID"); strSql.Append(" from Import_cargoinfo "); strSql.Append(" where Code='"+CodeID+"'"); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { cargoid=Convert.ToDecimal(reader["ID"]); } reader.Close(); } return cargoid; } 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()) { Import_Cargoinfo data = new Import_Cargoinfo(); #region Set DB data to Object data.ID = Convert.ToDecimal(reader["ID"]); data.Code = Convert.ToString(reader["Code"]); data.Name = Convert.ToString(reader["Name"]); data.Tax_Zhg = Convert.ToDecimal(reader["tax_zhg"]); data.Tax_Pt = Convert.ToDecimal(reader["tax_pt"]); data.Tax_Zz = Convert.ToDecimal(reader["tax_zz"]); data.Tax_Cl = Convert.ToDecimal(reader["tax_cl"]); data.JG = Convert.ToString(reader["JG"]); #endregion headList.Add(data); } reader.Close(); } return headList; } static public Import_Cargoinfo GetHeadDataByBillNo(string billno) { Import_Cargoinfo data = null; var list = GetDataList(" ID=" + billno); if (list.Count > 0) data = list[0]; if (data == null) { data = new Import_Cargoinfo(); } return data; } static public Import_Cargoinfo GetHeadData ( string Condition ) { Import_Cargoinfo data = null; var list = GetDataList(Condition); if (list.Count > 0) data = list[0]; if (data == null) { data = new Import_Cargoinfo(); } return data; } #endregion #region 明细表 static public List GetBodyList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("ID,CargoinfoID,CargoinfoCode,CiqName,CiqCode"); strSql.Append(" from Import_Cargociq "); if (strCondition.Trim() != String.Empty) { strSql.Append(" where " + strCondition); } return SetBodyData(strSql); } private static List SetBodyData(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { Import_Cargociq data = new Import_Cargociq(); #region Set DB data to Object data.ID = Convert.ToString(reader["ID"]); data.CargoinfoID = Convert.ToString(reader["CargoinfoID"]); data.CargoinfoCode = Convert.ToString(reader["CargoinfoCode"]); data.CiqCode = Convert.ToString(reader["CiqCode"]); data.CiqName = Convert.ToString(reader["CiqName"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } #endregion #region 参照部分 #endregion public static DBResult Save(Import_Cargoinfo headData, List bodyList,string cargoID) { var result=new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var CodeId = headData.Code; var cmdDelete = db.GetSqlStringCommand("delete from Import_Cargociq where CargoinfoID=" + cargoID); db.ExecuteNonQuery(cmdDelete, tran); var cmdInsert = db.GetSqlStringCommand( @"insert into Import_Cargociq (CargoinfoID, CargoinfoCODE, CIQCODE,CIQNAME) values (@ID, @CODE, @CIQCODE, @CIQNAME) "); foreach (var enumValue in bodyList) { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@ID", DbType.Int32, cargoID); db.AddInParameter(cmdInsert, "@CODE", DbType.String, CodeId); db.AddInParameter(cmdInsert, "@CIQCODE", DbType.String, enumValue.CiqCode); db.AddInParameter(cmdInsert, "@CIQNAME", DbType.String, enumValue.CiqName); db.ExecuteNonQuery(cmdInsert, tran); } tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "保存成功"; return result; } } }