using System; using System.Data; using System.Collections.Generic; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; using DSWeb.Areas.CommMng.DAL; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Models.MsStevedores; using System.Transactions; using System.Data.SqlClient; using DSWeb.DataAccess; using DSWeb.Areas.MvcShipping.Models.WMSFeeModel; namespace DSWeb.Areas.MvcShipping.DAL.WMSFeeModel { public class WMSFeeModelDAL { public static List GetDataList (string condition ) { string sql = "select GID,ModelName,CustName,isCustOnly,Oper,Optime,convert(char(10), VALIDITYFROM, 20) AS VALIDITYFROM,convert(char(10), VALIDITYTO, 20) AS VALIDITYTO,STORAGENAME from WMS_FEEMODEL"; if (condition!="") { sql += " where " + condition; } var list = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql)) { while (reader.Read()) { WMSFeeModelModel fm = new WMSFeeModelModel(); fm.GID = reader["GID"].ToString(); fm.ModelName = reader["ModelName"].ToString(); fm.CustName = reader["CustName"].ToString(); fm.IsCustOnly = reader["isCustOnly"].ToString(); fm.Oper = reader["Oper"].ToString(); fm.Optime = reader["Optime"].ToString(); fm.VALIDITYFROM = reader["VALIDITYFROM"].ToString(); fm.VALIDITYTO = reader["VALIDITYTO"].ToString(); fm.STORAGENAME = reader["STORAGENAME"].ToString(); list.Add(fm); } } return list; } public static List GetDataDetailsList ( string condition ) { string sql = "select * from WMS_FEEMODEL_DETAILS"; if (condition!=""&&condition!=null) { sql += " where " + condition; } var list = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql)) { while (reader.Read()) { WMSFeeModelDetails fmd = new WMSFeeModelDetails(); fmd.GID = reader["GID"].ToString(); fmd.PGID = reader["PGID"].ToString(); fmd.GGID = reader["GGID"].ToString(); fmd.JiFeiDengJi = reader["JiFeiDengJi"].ToString(); fmd.ShouFuFangXiang = reader["ShouFuFangXiang"].ToString(); fmd.Optime = reader["Optime"].ToString(); fmd.JiFeiQuJian = reader["JiFeiQuJian"].ToString(); fmd.JiFeiDanJia = reader["JiFeiDanJia"].ToString(); fmd.ZengZhiJia = reader["ZengZhiJia"].ToString(); fmd.FengDingJia = reader["FengDingJia"].ToString(); fmd.Optime = reader["Optime"].ToString(); list.Add(fmd); } } return list; } public static List GetDataLoaderList(string condition) { string sql = "select * from WMS_FEEMODEL_LOADER"; if (condition != "" && condition != null) { sql += " where " + condition; } var list = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql)) { while (reader.Read()) { LOADERFEEMODEL fmd = new LOADERFEEMODEL(); fmd.GID = reader["GID"].ToString(); fmd.PGID = reader["PGID"].ToString(); fmd.GGID = reader["GGID"].ToString(); fmd.FEETYPE = reader["FEETYPE"].ToString(); fmd.FEEDIRECTION = reader["FEEDIRECTION"].ToString(); fmd.FEEUNIT = reader["FEEUNIT"].ToString(); fmd.FEEPRICE = reader["FEEPRICE"].ToString(); list.Add(fmd); } } return list; } public static List GetPinMing(string pgid){ StringBuilder sb = new StringBuilder(); sb.Append("select g.gid,g.goodcode,g.goodname,"); sb.Append(" (select count(*) from WMS_FEEMODEL_DETAILS d where d.GGID = g.gid and d.PGID = '" + pgid + "') as detailNum,"); sb.Append("(select count(*) from WMS_FEEMODEL_LOADER l where l.GGID = g.gid and l.PGID = '" + pgid + "') as loaderNum"); sb.Append(" from code_goods g "); var list = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, sb.ToString())) { while (reader.Read()) { FEEPinMing pm = new FEEPinMing(); pm.GID = reader["GID"].ToString(); pm.CODE = reader["goodcode"].ToString(); pm.NAME = reader["goodname"].ToString(); pm.detailNum = reader["detailNum"].ToString(); pm.loaderNum = reader["loaderNum"].ToString(); list.Add(pm); } } return list; } public static DBResult DeleteDetail(WMSFeeModelModel 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 WMS_FEEMODEL where GID='" + headData.GID + "'"); db.ExecuteNonQuery(cmd, tran); var cmdDelete = db.GetSqlStringCommand("delete from WMS_FEEMODEL_DETAILS where PGID='" + headData.GID + "'"); db.ExecuteNonQuery(cmdDelete, tran); var cmdLoader = db.GetSqlStringCommand("delete from WMS_FEEMODEL_LOADER where PGID='" + headData.GID + "'"); db.ExecuteNonQuery(cmdLoader, tran); tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "删除出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "删除成功"; return result; } public static bool Save (WMSFeeModelModel fm, List fmdList, List loaderList, out string msg,out string MGID) { //保存主表 int Exmnum = 0; int Exdnum = 0; string GID = fm.GID; string PGID = fm.GID; bool mainRst = false; Database db = DatabaseFactory.CreateDatabase(); if (GID==""||GID=="*") { //新增 GID = Guid.NewGuid().ToString(); PGID = GID; StringBuilder sb = new StringBuilder(); sb.Append("insert into WMS_FEEMODEL (GID,ModelName,CustName,isCustOnly,Oper,VALIDITYFROM,VALIDITYTO,STORAGENAME) "); sb.Append("values('"+GID+"','"+fm.ModelName+"','"+fm.CustName+"','"+fm.IsCustOnly+"','"+fm.Oper+ "','" + fm.VALIDITYFROM + "','" + fm.VALIDITYTO + "','"+fm.STORAGENAME+"')"); Exmnum = db.ExecuteNonQuery(CommandType.Text, sb.ToString()); mainRst = Exmnum >0; } else { //修改 StringBuilder sb = new StringBuilder(); sb.Append("update WMS_FEEMODEL set "); sb.Append("ModelName = '"+fm.ModelName+"',"); sb.Append("CustName = '" + fm.CustName + "',"); sb.Append("isCustOnly = '" + fm.IsCustOnly + "',"); sb.Append("Oper = '" + fm.Oper + "',"); sb.Append("VALIDITYFROM = '" + fm.VALIDITYFROM + "',"); sb.Append("VALIDITYTO = '" + fm.VALIDITYTO + "',"); sb.Append("STORAGENAME = '" + fm.STORAGENAME + "'"); sb.Append(" where GID = '"+fm.GID+"'"); mainRst = db.ExecuteNonQuery(CommandType.Text,sb.ToString()) > 0; } if (mainRst) { try { using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); //保存明细表 if (fmdList==null) { fmdList = new List(); } foreach (var item in fmdList) { item.PGID = PGID; if (item.GID == "" || item.GID == "*") { //新增 string newGID = Guid.NewGuid().ToString(); item.GID = newGID; StringBuilder sbd = new StringBuilder(); sbd.Append("insert into WMS_FEEMODEL_DETAILS (GID,PGID,GGID,JiFeiDengJi,ShouFuFangXiang,JiFeiQuJian,JiFeiDanJia,ZengZhiJia,FengDingJia) "); sbd.Append(" values ("); sbd.Append("'" + item.GID + "',"); sbd.Append("'" + item.PGID + "',"); sbd.Append("'" + item.GGID + "',"); sbd.Append("'" + item.JiFeiDengJi + "',"); sbd.Append("'" + item.ShouFuFangXiang + "',"); sbd.Append("'" + item.JiFeiQuJian + "',"); sbd.Append("'" + item.JiFeiDanJia + "',"); sbd.Append("'" + item.ZengZhiJia + "',"); sbd.Append("'" + item.FengDingJia + "')"); var cmdInsertwork = db.GetSqlStringCommand(sbd.ToString()); Exdnum += db.ExecuteNonQuery(cmdInsertwork, tran); } else { //修改 StringBuilder sbd = new StringBuilder(); sbd.Append("update WMS_FEEMODEL_DETAILS set "); sbd.Append("JiFeiDengJi='" + item.JiFeiDengJi + "',"); sbd.Append("ShouFuFangXiang='" + item.ShouFuFangXiang + "',"); sbd.Append("JiFeiQuJian='" + item.JiFeiQuJian + "',"); sbd.Append("JiFeiDanJia='" + item.JiFeiDanJia + "',"); sbd.Append("ZengZhiJia='" + item.ZengZhiJia + "',"); sbd.Append("FengDingJia='" + item.FengDingJia + "' "); sbd.Append("where GID = '" + item.GID + "'"); var cmdUpdatework = db.GetSqlStringCommand(sbd.ToString()); Exdnum+= db.ExecuteNonQuery(cmdUpdatework, tran); } } if (loaderList == null) { loaderList = new List(); } foreach (var it in loaderList) { it.PGID = PGID; if (it.GID == "" || it.GID == "-") { //新增 string newGID = Guid.NewGuid().ToString(); it.GID = newGID; StringBuilder strLoader = new StringBuilder(); strLoader.Append("insert into WMS_FEEMODEL_LOADER (GID,PGID,GGID,FEETYPE,FEEDIRECTION,FEEUNIT,FEEPRICE) "); strLoader.Append(" values ("); strLoader.Append("'" + it.GID + "',"); strLoader.Append("'" + it.PGID + "',"); strLoader.Append("'" + it.GGID + "',"); strLoader.Append("'" + it.FEETYPE + "',"); strLoader.Append("'" + it.FEEDIRECTION + "',"); strLoader.Append("'" + it.FEEUNIT + "',"); strLoader.Append("'" + it.FEEPRICE + "')"); var cmdInsertloader = db.GetSqlStringCommand(strLoader.ToString()); Exdnum += db.ExecuteNonQuery(cmdInsertloader, tran); } else { //修改 StringBuilder sbdloader = new StringBuilder(); sbdloader.Append("update WMS_FEEMODEL_LOADER set "); sbdloader.Append("FEETYPE='" + it.FEETYPE + "',"); sbdloader.Append("FEEDIRECTION='" + it.FEEDIRECTION + "',"); sbdloader.Append("FEEUNIT='" + it.FEEUNIT + "',"); sbdloader.Append("FEEPRICE='" + it.FEEPRICE + "' "); sbdloader.Append("where GID = '" + it.GID + "'"); var cmdUpdateloader = db.GetSqlStringCommand(sbdloader.ToString()); Exdnum += db.ExecuteNonQuery(cmdUpdateloader, tran); } } tran.Commit(); } } catch (Exception ex) { msg = "操作异常!"+ex.Message; mainRst = false; MGID = GID; return mainRst; } MGID = GID; msg ="操作完成!"; return mainRst; } else { MGID = GID; msg ="主表数据保存失败!"; return false; } } public static bool CopyNew(WMSFeeModelModel fm, out string msg, out string MGID) { //保存主表 int Exmnum = 0; int Exdnum = 0; string oldGID = fm.GID; string GID = Guid.NewGuid().ToString(); string PGID = GID; bool mainRst = false; Database db = DatabaseFactory.CreateDatabase(); //if (GID == "" || GID == "*") { fm.ModelName = ""; fm.CustName = ""; StringBuilder sb = new StringBuilder(); sb.Append("insert into WMS_FEEMODEL (GID,ModelName,CustName,isCustOnly,Oper,VALIDITYFROM,VALIDITYTO,STORAGENAME ) "); sb.Append("values('" + GID + "','" + fm.ModelName + "','" + fm.CustName + "','" + fm.IsCustOnly + "','" + fm.Oper + "','" + fm.VALIDITYFROM + "','" + fm.VALIDITYTO + "','"+fm.STORAGENAME+"')"); Exmnum = db.ExecuteNonQuery(CommandType.Text, sb.ToString()); mainRst = Exmnum > 0; } if (mainRst) { try { using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); //保存明细表 //新增 StringBuilder sbd = new StringBuilder(); sbd.Append("insert into WMS_FEEMODEL_DETAILS (GID,PGID,GGID,JiFeiDengJi,ShouFuFangXiang,JiFeiQuJian,JiFeiDanJia,ZengZhiJia,FengDingJia) "); sbd.Append(" select "); sbd.Append(" newid() as GID,"); sbd.Append(" '" + PGID + "' as PGID,"); sbd.Append(" GGID,JiFeiDengJi,ShouFuFangXiang,JiFeiQuJian,JiFeiDanJia,ZengZhiJia,FengDingJia "); sbd.Append(" from WMS_FEEMODEL_DETAILS where PGID = '" + oldGID + "'"); var cmdInsertwork = db.GetSqlStringCommand(sbd.ToString()); Exdnum += db.ExecuteNonQuery(cmdInsertwork, tran); StringBuilder strLoader = new StringBuilder(); strLoader.Append("insert into WMS_FEEMODEL_LOADER (GID,PGID,GGID,FEETYPE,FEEDIRECTION,FEEUNIT,FEEPRICE) "); strLoader.Append(" select "); strLoader.Append(" newid() as GID,"); strLoader.Append(" '" + PGID + "' as PGID,"); strLoader.Append(" GGID,FEETYPE,FEEDIRECTION,FEEUNIT,FEEPRICE "); strLoader.Append(" from WMS_FEEMODEL_LOADER where PGID = '" + oldGID + "'"); var cmdInsertloader = db.GetSqlStringCommand(strLoader.ToString()); Exdnum += db.ExecuteNonQuery(cmdInsertloader, tran); tran.Commit(); } } catch (Exception ex) { msg = "操作异常!" + ex.Message; mainRst = false; MGID = GID; return mainRst; } MGID = GID; msg = "操作完成!"; return mainRst; } else { MGID = GID; msg = "主表数据保存失败!"; return false; } } } }