You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/TruckMng/DAL/MsKfLy/MsKfLyDAL.cs

373 lines
16 KiB
C#

3 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.Areas.TruckMng.Models.MsKfLy;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
namespace DSWeb.Areas.TruckMng.DAL.MsKfLy
{
public partial class MsKfLyDAL
{
#region 主表
static public List<MsKfLyHead> GetDataList(string strCondition, string userid, string usercode, string orgcode, string sort = null)
{
var rangstr = GetRangDAStr("index", userid, usercode, orgcode);
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and " + rangstr;
}
else
{
strCondition = rangstr;
}
}
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("BillNo,LrDate,JzDate,UserCode,");
strSql.Append("(select CodeAndName from vMsTruckUser where UserCode=tMsKfLyHead.UserCode) as UserCode_Ref");
strSql.Append(",UserName,TimeMark,BillType,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99011 and EnumValueID=tMsKfLyHead.BillType) as BillType_Ref");
strSql.Append(",OrgCode,");
strSql.Append("(select CodeAndName from vMsTruckOrg where OrgCode=tMsKfLyHead.OrgCode) as OrgCode_Ref");
strSql.Append(",OrgName,CkCode,");
strSql.Append("(select CodeAndName from vMsTruckOrg where OrgCode=tMsKfLyHead.CkCode) as CkCode_Ref");
strSql.Append(",CkName,LyDate,LyrCode,");
strSql.Append("(select CodeAndName from vMsTruckUser where UserCode=tMsKfLyHead.LyrCode) as LyrCode_Ref");
strSql.Append(",LyrName,LyCount,TruckNo,");
strSql.Append("(select TruckSpec from tMsWlTruck where TruckNo=tMsKfLyHead.TruckNo) as TruckNo_Ref");
strSql.Append(",Remark,WxBillNo,PluType,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99003 and EnumValueID=tMsKfLyHead.PluType) as PluType_Ref");
strSql.Append(" from tMsKfLyHead ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" where " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
return SetData(strSql);
}
private static List<MsKfLyHead> SetData(StringBuilder strSql)
{
var headList = new List<MsKfLyHead>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsKfLyHead data = new MsKfLyHead();
#region Set DB data to Object
data.BillNo = Convert.ToString(reader["BillNo"]);
if (reader["LrDate"] != DBNull.Value)
data.LrDate = Convert.ToDateTime(reader["LrDate"]);
if (reader["JzDate"] != DBNull.Value)
data.JzDate = Convert.ToDateTime(reader["JzDate"]);
data.UserCode = Convert.ToString(reader["UserCode"]);
data.UserCode_Ref = Convert.ToString(reader["UserCode_Ref"]);
data.UserName = Convert.ToString(reader["UserName"]);
data.TimeMark = Convert.ToDecimal(reader["TimeMark"]);
data.BillType = Convert.ToString(reader["BillType"]);
data.BillType_Ref = Convert.ToString(reader["BillType_Ref"]);
data.OrgCode = Convert.ToString(reader["OrgCode"]);
data.OrgCode_Ref = Convert.ToString(reader["OrgCode_Ref"]);
data.OrgName = Convert.ToString(reader["OrgName"]);
data.CkCode = Convert.ToString(reader["CkCode"]);
data.CkCode_Ref = Convert.ToString(reader["CkCode_Ref"]);
data.CkName = Convert.ToString(reader["CkName"]);
data.LyDate = Convert.ToString(reader["LyDate"]);
data.LyrCode = Convert.ToString(reader["LyrCode"]);
data.LyrCode_Ref = Convert.ToString(reader["LyrCode_Ref"]);
data.LyrName = Convert.ToString(reader["LyrName"]);
data.LyCount = Convert.ToDecimal(reader["LyCount"]);
data.TruckNo = Convert.ToString(reader["TruckNo"]);
data.TruckNo_Ref = Convert.ToString(reader["TruckNo_Ref"]);
data.Remark = Convert.ToString(reader["Remark"]);
data.PluType = Convert.ToString(reader["PluType"]);
data.PluType_Ref = Convert.ToString(reader["PluType_Ref"]);
data.WxBillNo = Convert.ToString(reader["WxBillNo"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
static public MsKfLyHead GetHeadDataByBillNo(string billno, string userid, string usercode, string orgcode)
{
MsKfLyHead data = null;
var list = GetDataList(" BillNo='" + billno + "'",userid,usercode,orgcode);
if (list.Count > 0)
data = list[0];
if (data == null)
{
data = new MsKfLyHead();
}
return data;
}
#endregion
#region 明细表
static public List<MsKfLyBody> GetBodyList(string strCondition)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("BillNo,SerialNo,PluCode,");
strSql.Append("(select PluName from tMsTruckPlu where PluCode=tMsKfLyBody.PluCode) as PluCode_Ref");
strSql.Append(",PluName,Spec,Unit,Price,LyCount,LyTotal,Remark,PcNo");
strSql.Append(",TyreType,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99013 and EnumValueID=tMsKfLyBody.TyreType) as TyreType_Ref");
strSql.Append(",TyrePos,PlanMil,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99015 and EnumValueID=tMsKfLyBody.TyrePos) as TyrePos_Ref");
strSql.Append(" from tMsKfLyBody ");
if (strCondition.Trim() != String.Empty)
{
strSql.Append(" where " + strCondition);
}
return SetBodyData(strSql);
}
private static List<MsKfLyBody> SetBodyData(StringBuilder strSql)
{
var bodyList = new List<MsKfLyBody>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsKfLyBody data = new MsKfLyBody();
#region Set DB data to Object
data.BillNo = Convert.ToString(reader["BillNo"]);
data.SerialNo = Convert.ToDecimal(reader["SerialNo"]);
data.PluCode = Convert.ToString(reader["PluCode"]);
data.PluCode_Ref = Convert.ToString(reader["PluCode_Ref"]);
data.PluName = Convert.ToString(reader["PluName"]);
data.Spec = Convert.ToString(reader["Spec"]);
data.Unit = Convert.ToString(reader["Unit"]);
data.Price = Convert.ToDecimal(reader["Price"]);
data.LyCount = Convert.ToDecimal(reader["LyCount"]);
data.LyTotal = Convert.ToDecimal(reader["LyTotal"]);
data.Remark = Convert.ToString(reader["Remark"]);
data.PcNo = Convert.ToString(reader["PcNo"]);
data.PlanMil = Convert.ToDecimal(reader["PlanMil"]);
data.TyreType = Convert.ToString(reader["TyreType"]);
data.TyreType_Ref = Convert.ToString(reader["TyreType_Ref"]);
data.TyrePos = Convert.ToString(reader["TyrePos"]);
data.TyrePos_Ref = Convert.ToString(reader["TyrePos_Ref"]);
#endregion
bodyList.Add(data);
}
reader.Close();
}
return bodyList;
}
#endregion
#region 参照部分
internal static List<MsKfLyHeadRefMsWlTruck> GetTruckNoList(string userid, string usercode, string orgcode)
{
var rangstr = GetRangDAStr("", userid, usercode, orgcode);
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(" TruckNo,TruckSpec");
strSql.Append(" from tMsWlTruck");
if (!string.IsNullOrEmpty(rangstr))
{
strSql.Append(" where " + rangstr);
}
var dataList = new List<MsKfLyHeadRefMsWlTruck>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
var data = new MsKfLyHeadRefMsWlTruck();
#region Set DB data to Object
data.TruckNo = Convert.ToString(reader["TruckNo"]);
data.TruckSpec = data.TruckNo + "-" + Convert.ToString(reader["TruckSpec"]);
#endregion
dataList.Add(data);
}
reader.Close();
}
return dataList;
}
internal static List<MsKfLyPriceJxCount> GetPriceJxCountList(string strPluCode, string orgcode)
{
var strSql = new StringBuilder();
strSql.Append("select plucode,Price,sum(JhCount-LyCount) as JxCount,orgcode from ");
strSql.Append("(SELECT b.PluCode,b.Price,sum(b.JhCount) JhCount,0 as LyCount,upper(h.OrgCode) as orgcode from tMsKfJhBody b left join tMsKfJhHead h");
strSql.Append(" on (h.BillNo=b.BillNo) where h.JzDate is not null");
strSql.Append(" and b.PluCode='" + strPluCode + "' and upper(h.OrgCode)='" +orgcode + "'");
strSql.Append(" group by b.PluCode,b.Price,upper(h.OrgCode)");
strSql.Append(" union SELECT b.PluCode,b.Price,0 as JhCount,sum(b.LyCount) LyCount,upper(h.OrgCode) as orgcode from tMsKfLyBody b left join tMsKfLyHead h");
strSql.Append(" on (h.BillNo=b.BillNo) where h.JzDate is not null");
strSql.Append(" and b.PluCode='" + strPluCode + "' and upper(h.OrgCode)='" + orgcode + "' group by b.PluCode,b.Price,upper(h.OrgCode) ) t");
strSql.Append(" group by plucode,Price,orgcode");
var dataList = new List<MsKfLyPriceJxCount>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
var data = new MsKfLyPriceJxCount();
#region Set DB data to Object
data.PluCode = Convert.ToString(reader["plucode"]);
data.Price = Convert.ToDecimal(reader["Price"]);
data.JxCount = Convert.ToDecimal(reader["JxCount"]);
data.PriceAndJxCount = Convert.ToString(reader["Price"]) + "-剩余数量" + Convert.ToString(reader["JxCount"]); ;
#endregion
dataList.Add(data);
}
reader.Close();
}
return dataList;
}
internal static List<MsKfLyPriceJxCount> GetPrice(string strPluCode, string orgcode)
{
var strSql = new StringBuilder();
strSql.Append("select plucode,Price,sum(JhCount-LyCount) as JxCount,orgcode from ");
strSql.Append("(SELECT b.PluCode,b.Price,b.JhCount,0 as LyCount,upper(h.OrgCode) as orgcode from tMsKfJhBody b left join tMsKfJhHead h");
strSql.Append(" on (h.BillNo=b.BillNo) where h.JzDate is not null");
strSql.Append(" and b.PluCode='" + strPluCode + "' and upper(h.OrgCode)='" + orgcode + "'");
strSql.Append(" union SELECT b.PluCode,b.Price,0 as JhCount,b.LyCount,upper(h.OrgCode) as orgcode from tMsKfLyBody b left join tMsKfLyHead h");
strSql.Append(" on (h.BillNo=b.BillNo) where h.JzDate is not null");
strSql.Append(" and b.PluCode='" + strPluCode + "' and upper(h.OrgCode)='" + orgcode + "') t");
strSql.Append(" group by plucode,Price,orgcode");
var dataList = new List<MsKfLyPriceJxCount>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
var data = new MsKfLyPriceJxCount();
#region Set DB data to Object
data.Price = Convert.ToDecimal(reader["Price"]);
#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]='modTruckPj' 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 = " UserCode='" + usercode + "'";
}
else if (visiblerange == "3")
{
if (tb == "index")
{
str = " UserCode='" + usercode + "'";
}
else
{
str = " UPPER(OrgCode)='" + orgcode + "'";
}
}
else if (visiblerange == "2")
{
if (tb == "index")
{
var rangeDa = new RangeDA();
var companyid = rangeDa.GetCORPID(userid);
var deptname = rangeDa.GetDEPTNAME(userid);
str = " UserCode in (select codename from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "') and GID in (select userid from user_baseinfo where DEPTNAME='" + deptname + "'))";
}
else
{
str = " UPPER(OrgCode)='" + orgcode + "'";
}
}
else if (visiblerange == "1")
{
str = " UPPER(OrgCode)='" + orgcode + "'";
}
return str;
}
#endregion
}
}