using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.Areas.TruckMng.Models; using DSWeb.Areas.TruckMng.Models.MsWlPc; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using System.Data.Common; using System.Web; using DSWeb.Areas.CommMng.DAL; using HcUtility.Core; using HcUtility.Logging; using DSWeb.TruckMng.Comm.Cookie; using WebSqlHelper; using System.Data.SqlClient; using DSWeb.Models; using HcUtility.Comm; using DSWeb.Areas.TruckMng.Controllers; namespace DSWeb.Areas.TruckMng.DAL.MsWl_Haier { public partial class MsWl_HaierDAL { public enum RowState : uint { Insert, Update, None } public static bool ImportExcelData(HttpRequestBase request, DataTable table, out string msg,string ACCDATE, out Int32 InsertCount, out Int32 UpdateCount, out string UnKnowenTruckNo, out List headList) { var isSucess = false; msg = ""; headList = new List(); if (table == null) throw new ArgumentNullException("table"); var billNoList = new List(); Database db = DatabaseFactory.CreateDatabase(); InsertCount = 0; UpdateCount = 0; UnKnowenTruckNo = ""; using (DbConnection connection = db.CreateConnection()) { connection.Open(); try { var orgCode = CookieConfig.GetCookie_OrgCode(request); var userCode = CookieConfig.GetCookie_UserCode(request); var userName = CookieConfig.GetCookie_UserName(request); var _i = -1; var ExpDate = ""; foreach (DataRow row in table.Rows) { _i++; if (_i < 2) continue; var _ExpDate = Convert.ToString(row["指令开票日期"]); if (_ExpDate == "") {} else { ExpDate = _ExpDate; } var TruckNo = Convert.ToString(row["承运车辆"]); TruckNo = TruckNo.Replace("-", ""); TruckNo = TruckNo.Trim(); if (TruckNo.Trim() == "") { continue; } var objTruckNo = db.ExecuteScalar(CommandType.Text, "select TruckNo from tMsWlTruck where TruckNo='" + TruckNo + "'"); var _TruckNo = Convert.ToString(objTruckNo); if (string.IsNullOrEmpty(_TruckNo)) { if (UnKnowenTruckNo == "") { UnKnowenTruckNo = "有如下车号在系统中不存在,请添加后重新导入:" + TruckNo; } else UnKnowenTruckNo = UnKnowenTruckNo + "/" + TruckNo; continue; } DbTransaction idbTran = connection.BeginTransaction(); var Destination = Convert.ToString(row["客户名称"]); var ContractNo = Convert.ToString(row["合同号"]); var CargoQty = Convert.ToString(row["合同数量(万支)"]); var _t = Convert.ToDouble(CargoQty) / 50; var Tons = Convert.ToString(_t); var _RowState = RowState.Insert; string strCheckRow = "select * from tMsWl_HH_ZY where ContractNo = '" + ContractNo + "'"; object statusObj = SqlHelper.ExecuteScalar(db.ConnectionString, CommandType.Text, strCheckRow, null); if (statusObj == null) { _RowState = RowState.Insert; } else { _RowState = RowState.Update; } if (_RowState == RowState.Insert) { #region 托单数据生成 //var billNo = PubSysDAL.GetBillNo("0119"); billNoList.Add(ContractNo); var gid = "JYHH_ZY" + Guid.NewGuid().ToString("N").ToUpper(); const string insertSql = @"insert into tMsWl_HH_ZY (GID,OrgCode,Destination,ExpDate,TruckNo,ContractNo,MBLNO,CargoQty ,Tons,Distance,Pay_Distance,Price_1,Price_2,Amount_1,Amount_2,In_Province,Remark) values(@GID,@OrgCode,@Destination,@ExpDate,@TruckNo,@ContractNo,@MBLNO,@CargoQty ,@Tons,(select isnull(Distance,-1) from City_Distance where name=@Destination), (select isnull(Pay_Distance,-1) from City_Distance where name=@Destination), @Price_1,@Price_2,@Amount_1,@Amount_2,@In_Province,@Remark)"; DbCommand cmd = db.GetSqlStringCommand(insertSql); db.AddInParameter(cmd, "GID", DbType.String, gid); db.AddInParameter(cmd, "OrgCode", DbType.String, orgCode); db.AddInParameter(cmd, "Destination", DbType.String, Destination); if (!string.IsNullOrEmpty(ExpDate)) { ExpDate = Convert.ToDateTime(ExpDate).ToString("yyyy-MM-dd").Trim(); } db.AddInParameter(cmd, "ExpDate", DbType.String, ExpDate); db.AddInParameter(cmd, "TruckNo", DbType.String, TruckNo); db.AddInParameter(cmd, "ContractNo", DbType.String, ContractNo); db.AddInParameter(cmd, "MBLNO", DbType.String, ""); db.AddInParameter(cmd, "CargoQty", DbType.String, CargoQty);//数量 //var Tons = Convert.ToInt32(row["合同数量(万支)"]); //Tons = Tons / 50;//50万支=1吨 db.AddInParameter(cmd, "Tons", DbType.String, Convert.ToString(Tons));//等价吨 db.AddInParameter(cmd, "Price_1", DbType.String, "0");//收费费率 db.AddInParameter(cmd, "Price_2", DbType.String, "0");//付费费率 db.AddInParameter(cmd, "Amount_1", DbType.String, "0");//备注 db.AddInParameter(cmd, "Amount_2", DbType.String, "0");//备注 db.AddInParameter(cmd, "In_Province", DbType.String, "0");//备注 db.AddInParameter(cmd, "Remark", DbType.String, "");//备注 db.ExecuteNonQuery(cmd, idbTran); idbTran.Commit(); InsertCount++; #endregion } else if (_RowState == RowState.Update) { #region 托单数据生成,插入 billNoList.Add(ContractNo); /* const string UpdSql = @" Update tSys_tMsWl_HH_ZY set CargoQty=CargoQty+@CargoQty,Tons=Tons+@Tons,Distance=@Distance where TruckNo=@TruckNo and ExpDate=@ExpDate and destination=@Destination ";*/ const string UpdSql = @" Update tMsWl_HH_ZY set Destination=@Destination,ExpDate=@ExpDate, CargoQty=@CargoQty,Tons=@Tons,Distance=(select isnull(Distance,0) from City_Distance where name=@Destination) ,Pay_Distance=(select isnull(Pay_Distance,0) from City_Distance where name=@Destination) ,Amount_1=@Tons*Price_1*(select isnull(Distance,0) from City_Distance where name=@Destination) ,Amount_2=@Tons*Price_2*(select isnull(Pay_Distance,0) from City_Distance where name=@Destination) where contractno=@ContractNo and HZSTATUS in (1,6)"; DbCommand cmd = db.GetSqlStringCommand(UpdSql); //var ExpDate = Convert.ToString(row["指令开票"]).Trim(); if (!string.IsNullOrEmpty(ExpDate)) { ExpDate = Convert.ToDateTime(ExpDate).ToString("yyyy-MM-dd").Trim(); } db.AddInParameter(cmd, "ExpDate", DbType.String, ExpDate); db.AddInParameter(cmd, "TruckNo", DbType.String, TruckNo); db.AddInParameter(cmd, "ContractNo", DbType.String, ContractNo); //db.AddInParameter(cmd, "MBLNO", DbType.String, Convert.ToString("")); db.AddInParameter(cmd, "Destination", DbType.String, Destination); db.AddInParameter(cmd, "CargoQty", DbType.String, CargoQty);//数量 //var Tons = Convert.ToInt32(row["合同数量(万支)"]); //Tons = Tons / 50;//50万支=1吨 db.AddInParameter(cmd, "Tons", DbType.String, Tons);//等价吨 db.ExecuteNonQuery(cmd, idbTran); idbTran.Commit(); UpdateCount++; //idbTran.Commit(); #endregion } } #region 获取新生成的托单费用 foreach (var billNo in billNoList) { var head =MsWl_HaierMainController. GetData(" ContractNo='" + billNo + "'"); headList.Add(head); } #endregion isSucess = true; } catch (Exception exception) { //idbTran.Rollback(); isSucess = false; msg = exception.Message; } } return isSucess; } public static bool ImportExcelData(HttpRequestBase request, DataTable table, out string msg, out Int32 InsertCount,out Int32 UpdateCount,string ACCDATE, out string UnKnowenTruckNo,out List headList) { var isSucess = false; msg = ""; headList = new List(); if (table == null) throw new ArgumentNullException("table"); var billNoList = new List(); Database db = DatabaseFactory.CreateDatabase(); InsertCount=0; UpdateCount=0; UnKnowenTruckNo = ""; var UnKnowenDrvName = ""; using (DbConnection connection = db.CreateConnection()) //using (SqlTransaction idbTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { connection.Open(); try { var orgCode = CookieConfig.GetCookie_OrgCode(request); var userCode = CookieConfig.GetCookie_UserCode(request); var userName = CookieConfig.GetCookie_UserName(request); var _i = -1; //var ExpDate = ""; foreach (DataRow row in table.Rows) { _i++; //if (_i < 1) continue; var _ACCDATE = ACCDATE; var TruckNo = Convert.ToString(row[1]); //Convert.ToString(row["车号"]); TruckNo = TruckNo.Replace("-", ""); TruckNo = TruckNo.Trim(); if (TruckNo.Trim() == "") { continue; } var objTruckNo = db.ExecuteScalar(CommandType.Text, "select TruckNo from tMsWlTruck where TruckNo='" + TruckNo +"'"); var _TruckNo = Convert.ToString(objTruckNo); if (string.IsNullOrEmpty(_TruckNo)) { if (UnKnowenTruckNo == "") { UnKnowenTruckNo = "有如下车号在系统中不存在,请添加后重新导入:"+TruckNo; } else UnKnowenTruckNo = UnKnowenTruckNo + "/" + TruckNo; continue; } var DrvName = Convert.ToString(row["驾驶员"]); var DrvCodeObj = db.ExecuteScalar(CommandType.Text, "select DrvCode from tMsWlDriver where DrvName='" + DrvName + "'"); var DrvCode = Convert.ToString(DrvCodeObj); if (string.IsNullOrEmpty(DrvCode)) { if (UnKnowenDrvName == "") { UnKnowenDrvName = "有如下驾驶员在系统中不存在,请添加后重新导入:" + DrvName; } else UnKnowenDrvName = UnKnowenDrvName + "/" + DrvName; continue; } DbTransaction idbTran = connection.BeginTransaction(); /* 序号 车号 驾驶员 "总行程(公里)" 油费(元) 小修费(元) 二保费(元) 大中修费(元) 轮胎损耗 路桥费、条码费等代垫付费用(元) * 人工成本费(元) 人工养老税金(元) 超里程激励 费用小计 税金(元) "正负激励(元)" "资产管理费(元)" "运行成本合计(元)" * "运输收入(元)" 车辆保险费 车辆折旧费 "超收入小计(元)" "超利激励(元)" "超利税金(元)" "安全事故考核(元)" "应挂金额(元)" */ var TotalMil = Convert.ToString(row[3]); var _p = 3; var FuelPrice = "0"; var FuelAmount = Convert.ToString(row[_p+1]); var XXF = Convert.ToString(row[_p+2]); var EBF = Convert.ToString(row[_p + 3]); var DZXF = Convert.ToString(row[_p + 4]); //大中修费取消 var LTSH = Convert.ToString(row[_p + 5]); //轮胎损耗 var LTKF = Convert.ToString(row[_p + 6]); //轮胎损耗 var LQF = Convert.ToString(row[_p + 7]); var RGF = Convert.ToString(row[_p + 8]); var YLS = Convert.ToString(row[_p + 9]); var LCJL = Convert.ToString(row[_p + 10]); //里程激励 var Amount_1 = Convert.ToString(row[_p + 11]); //费用小计 var Tax = Convert.ToString(row[_p + 12]); var ZFJL = Convert.ToString(row[_p + 13]);//正负激励 var ZCGL = Convert.ToString(row[_p + 14]);//资产管理费 var YXCB = Convert.ToString(row[_p + 15]); var YSSR = Convert.ToString(row[_p + 16]); var CLBX = Convert.ToString(row[_p + 17]); var CLZJ = Convert.ToString(row[_p + 18]); var Amount_2 = Convert.ToString(row[_p + 19]); var CLJL = Convert.ToString(row[_p + 20]); var CLSJ = Convert.ToString(row[_p + 21]); var AQKH = Convert.ToString(row[_p + 22]); var Amount_3 = Convert.ToString(row[_p + 23]); var _RowState=RowState.Insert; string strCheckRow = "select gid from tMsWl_HaierMain where accdate = '" + _ACCDATE + "' and truckno='" + TruckNo + "' and drvcode='" + DrvCode + "'"; object statusObj = SqlHelper.ExecuteScalar(db.ConnectionString, CommandType.Text, strCheckRow, null); if (statusObj == null) { _RowState = RowState.Insert; } else { _RowState = RowState.Update; var gid = statusObj.ToString().Trim(); billNoList.Add(gid); } if (_RowState == RowState.Insert) { #region 托单数据生成 //var billNo = PubSysDAL.GetBillNo("0119"); //billNoList.Add(ContractNo); var gid = "JY_Haier_" + Guid.NewGuid().ToString("N").ToUpper(); billNoList.Add(gid); const string insertSql = @"insert into tmswl_HaierMain (GID,TruckNo,ACCDATE,DrvCode,TotalMil,FuelPrice,FuelAmount,XXF,EBF,DZXF ,LTSH,LTKF,LQF,RGF,YLS,LCJL,Amount_1,Tax,ZFJL,ZCGL,YXCB,YSSR,CLBX,CLZJ,Amount_2,CLJL,CLSJ,AQKH,Amount_3) values(@GID,@TruckNo,@ACCDATE,@DrvCode,@TotalMil,@FuelPrice,@FuelAmount,@XXF,@EBF,@DZXF ,@LTSH,@LTKF,@LQF,@RGF,@YLS,@LCJL,@Amount_1,@Tax,@ZFJL,@ZCGL,@YXCB,@YSSR,@CLBX,@CLZJ,@Amount_2,@CLJL,@CLSJ,@AQKH,@Amount_3)"; DbCommand cmd = db.GetSqlStringCommand(insertSql); db.AddInParameter(cmd, "GID", DbType.String, gid); db.AddInParameter(cmd, "TruckNo", DbType.String, TruckNo); db.AddInParameter(cmd, "ACCDATE", DbType.String, _ACCDATE); /* if (!string.IsNullOrEmpty(ExpDate)) { ExpDate = Convert.ToDateTime(ExpDate).ToString("yyyy-MM-dd").Trim(); } db.AddInParameter(cmd, "ExpDate", DbType.String, ExpDate); */ db.AddInParameter(cmd, "DrvCode", DbType.String, DrvCode); db.AddInParameter(cmd, "TotalMil", DbType.Double, TotalMil); db.AddInParameter(cmd, "FuelPrice", DbType.Double, FuelPrice); db.AddInParameter(cmd, "FuelAmount", DbType.Double, FuelAmount); db.AddInParameter(cmd, "XXF", DbType.Double, XXF); db.AddInParameter(cmd, "EBF", DbType.Double, EBF); db.AddInParameter(cmd, "DZXF", DbType.Double, DZXF); db.AddInParameter(cmd, "LTSH", DbType.Double, LTSH); db.AddInParameter(cmd, "LTKF", DbType.Double, LTKF); db.AddInParameter(cmd, "LQF", DbType.Double, LQF); db.AddInParameter(cmd, "RGF", DbType.Double, RGF); db.AddInParameter(cmd, "YLS", DbType.Double, YLS); db.AddInParameter(cmd, "LCJL", DbType.Double, LCJL); db.AddInParameter(cmd, "Amount_1", DbType.Double, Amount_1); db.AddInParameter(cmd, "Tax", DbType.Double, Tax); db.AddInParameter(cmd, "ZFJL", DbType.Double, ZFJL); db.AddInParameter(cmd, "ZCGL", DbType.Double, ZCGL); db.AddInParameter(cmd, "YXCB", DbType.Double, YXCB); db.AddInParameter(cmd, "YSSR", DbType.Double, YSSR); db.AddInParameter(cmd, "CLBX", DbType.Double, CLBX); db.AddInParameter(cmd, "CLZJ", DbType.Double, CLZJ); db.AddInParameter(cmd, "Amount_2", DbType.Double, Amount_2); db.AddInParameter(cmd, "CLJL", DbType.Double, CLJL); db.AddInParameter(cmd, "CLSJ", DbType.Double, CLSJ); db.AddInParameter(cmd, "AQKH", DbType.Double, AQKH); db.AddInParameter(cmd, "Amount_3", DbType.Double, Amount_3); db.ExecuteNonQuery(cmd, idbTran); idbTran.Commit(); InsertCount++; #endregion } else if (_RowState == RowState.Update) { #region 托单数据生成,update //billNoList.Add(TruckNo+_ACCDATE); /* const string UpdSql = @" Update tSys_tMsWl_HH_ZY set CargoQty=CargoQty+@CargoQty,Tons=Tons+@Tons,Distance=@Distance where TruckNo=@TruckNo and ExpDate=@ExpDate and destination=@Destination ";*/ const string UpdSql = @" Update tMsWl_HaierMain set TotalMil=@TotalMil,FuelPrice=@FuelPrice, FuelAmount=@FuelAmount,XXF=@XXF,EBF=@EBF,DZXF=@DZXF ,LTSH=@LTSH,LQF=@LQF,LTKF=@LTKF,RGF=@RGF,YLS=@YLS,LCJL=@LCJL,Amount_1=@Amount_1,Tax=@Tax,ZFJL=@ZFJL,ZCGL=@ZCGL,YXCB=@YXCB,YSSR=@YSSR ,CLBX=@CLBX,CLZJ=@CLZJ,Amount_2=@Amount_2,CLJL=@CLJL,CLSJ=@CLSJ,AQKH=@AQKH,Amount_3=@Amount_3 where TruckNo=@TruckNo and ACCDATE=@ACCDATE and drvcode=@DrvCode"; DbCommand cmd = db.GetSqlStringCommand(UpdSql); //var ExpDate = Convert.ToString(row["指令开票"]).Trim(); /* if (!string.IsNullOrEmpty(ExpDate)) { ExpDate = Convert.ToDateTime(ExpDate).ToString("yyyy-MM-dd").Trim(); } db.AddInParameter(cmd, "ExpDate", DbType.String, ExpDate);*/ db.AddInParameter(cmd, "TruckNo", DbType.String, TruckNo); db.AddInParameter(cmd, "ACCDATE", DbType.String, ACCDATE); //db.AddInParameter(cmd, "MBLNO", DbType.String, Convert.ToString("")); db.AddInParameter(cmd, "DrvCode", DbType.String, DrvCode); db.AddInParameter(cmd, "TotalMil", DbType.Double, TotalMil); db.AddInParameter(cmd, "FuelPrice", DbType.Double, FuelPrice); db.AddInParameter(cmd, "FuelAmount", DbType.Double, FuelAmount); db.AddInParameter(cmd, "XXF", DbType.Double, XXF); db.AddInParameter(cmd, "EBF", DbType.Double, EBF); db.AddInParameter(cmd, "DZXF", DbType.Double, DZXF); db.AddInParameter(cmd, "LTSH", DbType.Double, LTSH); db.AddInParameter(cmd, "LQF", DbType.Double, LQF); db.AddInParameter(cmd, "LTKF", DbType.Double, LTKF); db.AddInParameter(cmd, "RGF", DbType.Double, RGF); db.AddInParameter(cmd, "YLS", DbType.Double, YLS); db.AddInParameter(cmd, "LCJL", DbType.Double, LCJL); db.AddInParameter(cmd, "Amount_1", DbType.Double, Amount_1); db.AddInParameter(cmd, "Tax", DbType.Double, Tax); db.AddInParameter(cmd, "ZFJL", DbType.Double, ZFJL); db.AddInParameter(cmd, "ZCGL", DbType.Double, ZCGL); db.AddInParameter(cmd, "YXCB", DbType.Double, YXCB); db.AddInParameter(cmd, "YSSR", DbType.Double, YSSR); db.AddInParameter(cmd, "CLBX", DbType.Double, CLBX); db.AddInParameter(cmd, "CLZJ", DbType.Double, CLZJ); db.AddInParameter(cmd, "Amount_2", DbType.Double, Amount_2); db.AddInParameter(cmd, "CLJL", DbType.Double, CLJL); db.AddInParameter(cmd, "CLSJ", DbType.Double, CLSJ); db.AddInParameter(cmd, "AQKH", DbType.Double, AQKH); db.AddInParameter(cmd, "Amount_3", DbType.Double, Amount_3); db.ExecuteNonQuery(cmd, idbTran); idbTran.Commit(); UpdateCount++; //idbTran.Commit(); #endregion } } #region 获取新生成的托单费用 foreach (var gid in billNoList) { var head = MsWl_HaierMainController.GetData(" m.gid='" + gid + "'"); headList.Add(head); } #endregion isSucess = true; } catch (Exception exception) { //idbTran.Rollback(); isSucess = false; msg = exception.Message; } } UnKnowenTruckNo = UnKnowenTruckNo + " " + UnKnowenDrvName; return isSucess; } private static RowState CheckRow(String TruckNo, String ExpDate, String Destination, String ContractNo) {//检查当前数据是否有相同 var strSql = new StringBuilder(); //strSql.Append(" select ( select count(*) from tMsWl_HH_ZY where TruckNo='" + TruckNo + "' and ExpDate='" + ExpDate + "' and Destination='" + Destination + "') Count_1, "); //strSql.Append(" ( select count(*) from tMsWl_HH_ZY where TruckNo='" + TruckNo + "' and ExpDate='" + ExpDate + "' and Destination='" + Destination + "' and ContractNo like '%" + ContractNo + "%') Count_2 "); strSql.Append(" select count(*) Count from tMsWl_HH_ZY where TruckNo='" + TruckNo + "' and ExpDate='" + ExpDate + "' and Destination='" + Destination + "' and ContractNo = '" + ContractNo + "' "); var Count_1 = 0; //var Count_2 = 0; Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { #region Set DB data to Object Count_1 = Convert.ToInt32(reader["Count"]); //Count_2 = Convert.ToInt32(reader["Count_2"]); #endregion } reader.Close(); } if (Count_1 == 0) { //if (Count_2 == 0) { return RowState.Insert; } //else { return RowState.Update; } return RowState.Insert; } else { //if (Count_2 == 0) { return RowState.Update; } //else { return RowState.Update; //} } } static public DBResult SetPrice(string Price_1, string Price_2, string GIdSql) { var result = new DBResult(); using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { var sSql=""; if (Price_1 != "") {if (Convert.ToDouble(Price_1) >= 0 ) { sSql = "update tmswl_HH_ZY set Price_1=" + Price_1; sSql = sSql + ",Amount_1=(case BillingType when 0 then Tons*distance*" + Price_1 + " when 1 then distance*" + Price_1 + " end )"; } } if (Price_2 != "") { if (Convert.ToDouble(Price_2) >= 0) { if (sSql == "") { sSql = "update tmswl_HH_ZY set Price_2=" + Price_2 + ",Amount_2=(case BillingType when 0 then Tons*distance*" + Price_2 + " when 1 then distance*" + Price_2 + " end )"; } else { sSql = sSql + ", Price_2=" + Price_2 + ",Amount_2=(case BillingType when 0 then Tons*distance*" + Price_2 + " when 1 then distance*" + Price_2 + " end )"; } } } sSql = sSql + GIdSql; SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSql, null); //事务提交 sqlTran.Commit(); result.Success = true; result.Message = "保存成功"; } catch (Exception execError) { result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; throw execError; } finally { SqlHelper.CloseConnection(); } } return result; } static public DBResult SetSettlement(string GIDList, string Amount_Settlement, string Type) { //var result = new DBResult(); var _TruckNoList = new List(); var strSql = new StringBuilder(); var S_Amount=Convert.ToDouble(Amount_Settlement); var _r = new DBResult(); if (Type == "1" || Type == "2") { strSql.Append(" select GID,isnull(Amount_" + Type + ",0),isnull(Paid_Amount_" + Type + ",0),isnull(Amount_" + Type + ",0)-isnull(Paid_Amount_" + Type + ",0) as needPay"); strSql.Append(" from tmswl_hh_zy where GID in(" + GIDList + ") order by expdate"); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { var GID = Convert.ToString(reader["GID"]); var needPay = Convert.ToDouble(reader["needPay"]); if (needPay <= S_Amount)//剩余金额足以结算 { _r = DoSettlement(GID, needPay, Type); if (_r.Success == true) { S_Amount = S_Amount - needPay; continue; } } if (needPay > S_Amount)//剩余金额不够结算 { _r = DoSettlement(GID, S_Amount, Type); if (_r.Success == true) { break; } } } reader.Close(); } } if (Type == "3" || Type == "4") { var type = ""; if (Type == "3") { type = "1"; } if (Type == "4") { type = "2"; } strSql.Append(" select GID,isnull(Amount_" + type + ",0),isnull(Paid_Amount_" + type + ",0) Paid_Amount"); strSql.Append(" from tmswl_hh_zy where GID in(" + GIDList + ") order by expdate"); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { var GID = Convert.ToString(reader["GID"]); var Paid_Amount = Convert.ToDouble(reader["Paid_Amount"]); if (Paid_Amount <= S_Amount)//剩余金额足以反结算 { _r = DoSettlement(GID, Paid_Amount, Type); if (_r.Success == true) { S_Amount = S_Amount - Paid_Amount; continue; } } if (Paid_Amount > S_Amount)//剩余金额不够结算 { _r = DoSettlement(GID, S_Amount, Type); if (_r.Success == true) { break; } } } reader.Close(); } } return _r; } static public DBResult DoSettlement(string GID, Double SettlementAmount, string type) { var result = new DBResult(); var _Mark = ""; if (type == "1" || type=="2") { _Mark = "+"; } if (type == "3") { type = "1"; _Mark = "-"; } if (type == "4") { type = "2"; _Mark = "-"; } using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { var strSql = new StringBuilder(); strSql.Append(" update tmswl_hh_ZY set Paid_Amount_" + type + "=Paid_Amount_" + type + _Mark + Convert.ToString(SettlementAmount) + " where GID='" + GID + "'"); SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, strSql.ToString(), null); //事务提交 sqlTran.Commit(); result.Success = true; result.Message = "保存成功"; } catch (Exception execError) { result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; throw execError; } finally { SqlHelper.CloseConnection(); } } return result; } static public DBResult Submit(string GIDList, string OldStatus, string NewStatus) { var result = new DBResult(); using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { var sSql = ""; sSql = "update tmswl_HH_ZY set HZSTATUS=" + NewStatus + " where GID in( " + GIDList + ") and HZSTATUS in(" + OldStatus + ")"; SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSql, null); //事务提交 sqlTran.Commit(); result.Success = true; result.Message = "设置成功"; } catch (Exception execError) { result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; throw execError; } finally { SqlHelper.CloseConnection(); } } return result; } #region 明细表 #endregion #region 参照部分 /* internal static List GetDrvCodeList(string userid, string usercode, string orgcode) { var rangstr = GetRangDAStr("", userid, usercode, orgcode); var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" DrvCode,DrvName,DrvCode + '-' + DrvName as CodeAndName"); strSql.Append(" from tMsWlDriver"); if (!string.IsNullOrEmpty(rangstr)) { strSql.Append(" where " + rangstr); } var dataList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { var data = new MsWlTruckRefMsWlDriver(); #region Set DB data to Object data.DrvCode = Convert.ToString(reader["DrvCode"]); data.DrvName = Convert.ToString(reader["DrvName"]); data.CodeAndName = Convert.ToString(reader["CodeAndName"]); #endregion dataList.Add(data); } reader.Close(); } return dataList; } public static string GetRangDAStr(string tb, string userid, string usercode, string orgcode) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" VISIBLERANGE,OPERATERANGE "); strSql.Append(" from VW_User_Authority "); strSql.Append(" where [NAME]='modTruckCar' and USERID='" + userid + "' and ISDELETE=0"); string visiblerange = "4"; string operaterange = "4"; Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { visiblerange = Convert.ToString(reader["VISIBLERANGE"]); operaterange = Convert.ToString(reader["OPERATERANGE"]); break; } reader.Close(); } if (visiblerange == "4") { str = "1=2"; } else if (visiblerange == "3") { if (tb == "index") { str = " UserCode='" + usercode + "'"; } else { str = " UPPER(OrgCode)='" + orgcode + "'"; } } else if (visiblerange == "2") { str = " UPPER(OrgCode)='" + orgcode + "'"; } else if (visiblerange == "1") { str = " UPPER(OrgCode)='" + orgcode + "'"; } return str; } */ #endregion } }