using MESClassLibrary.BLL.Log; using MESClassLibrary.EFModel; using MESClassLibrary.Model; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; using MESClassLibrary.DAL; namespace MESClassLibrary.BLL.Stock { public class StockInBLL { BBMPTEntities dbe = new BBMPTEntities(); BasicBLL db = new BasicBLL(); BasicBLL Resultdb = new BasicBLL(); /// /// 查询信息 /// /// public string SearchInfo(string page, string pagesize, string StartTime, string EndTime, string workClass, string color, string productInfo) { try { string jsonStr = "[]"; int total = 0;//总行数 //DateTime start = Convert.ToDateTime(StartTime); //DateTime end = Convert.ToDateTime(EndTime); //List list = new List(); //string sql = @"IF object_id(N'tempdb..#c',N'U') is not null // BEGIN // DROP Table #c // END // CREATE TABLE #c (barcode NVARCHAR(50),workClass NVARCHAR(100),productInfo NVARCHAR(200)) // INSERT INTO #c // ( barcode,workClass, productInfo ) // SELECT d.barcode,d.workClass, d.productInfo FROM ( // SELECT *,cre_date_idx = ROW_NUMBER() OVER (PARTITION BY barcode ORDER BY createTime DESC) // FROM dbo.tb_InspectResult // WHERE barcode IN (SELECT barcode FROM dbo.tb_StockIn WHERE createTime >='"+ start+ @"' AND createTime <='"+ end+ @"') // ) d // WHERE d.cre_date_idx =1 // CREATE INDEX IX_TempTable ON #c(workClass,productInfo) // SELECT a.ID,a.barcode,a.pass,a.createTime,#c.productInfo,#c.workClass FROM dbo.tb_StockIn a // LEFT OUTER JOIN // #c ON a.barcode=#c.barcode // WHERE createTime >='" + start+ @"' AND createTime <='"+ end+ @"'"; //if (!string.IsNullOrEmpty(workClass)) //{ // sql += " and #c.workClass like '%" + workClass + @"%' "; //} //if (!string.IsNullOrEmpty(color)) //{ // sql += " and #c.productInfo like '%" + color + @"%' "; //} //if (!string.IsNullOrEmpty(productInfo)) //{ // sql += " and #c.productInfo like '%" + productInfo + @"%' "; //} //sql += " order by a.createTime"; //DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0]; //if (dt != null && dt.Rows.Count > 0) //{ // list = Tool.ConvertTo(dt).ToList(); //} List list = GetStockData(StartTime, EndTime, workClass, color, productInfo); if (list.Count > 0) { total = list.Count; int Skipcount = (Convert.ToInt32(page) - 1) * Convert.ToInt32(pagesize); list = list.Skip(Skipcount).Take(Convert.ToInt32(pagesize)).ToList();; JsonDataModel md = new JsonDataModel(); md.total = total.ToString(); md.rows = list; jsonStr = JSONTools.ScriptSerialize>(md); } return jsonStr; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return ""; } } List GetStockData(string StartTime, string EndTime, string workClass, string color, string productInfo) { DateTime start = Convert.ToDateTime(StartTime); DateTime end = Convert.ToDateTime(EndTime); List list = new List(); string sql = @"IF object_id(N'tempdb..#c',N'U') is not null BEGIN DROP Table #c END CREATE TABLE #c (barcode NVARCHAR(50),workClass NVARCHAR(100),productInfo NVARCHAR(200)) INSERT INTO #c ( barcode,workClass, productInfo ) SELECT d.barcode,d.workClass, d.productInfo FROM ( SELECT *,cre_date_idx = ROW_NUMBER() OVER (PARTITION BY barcode ORDER BY createTime DESC) FROM dbo.tb_InspectResult WHERE barcode IN (SELECT barcode FROM dbo.tb_StockIn WHERE createTime >='" + start + @"' AND createTime <='" + end + @"') ) d WHERE d.cre_date_idx =1 CREATE INDEX IX_TempTable ON #c(workClass,productInfo) SELECT a.ID,a.barcode,a.pass,a.createTime,#c.productInfo,#c.workClass FROM dbo.tb_StockIn a LEFT OUTER JOIN #c ON a.barcode=#c.barcode WHERE createTime >='" + start + @"' AND createTime <='" + end + @"'"; if (!string.IsNullOrEmpty(workClass)) { sql += " and #c.workClass like '%" + workClass + @"%' "; } if (!string.IsNullOrEmpty(color)) { sql += " and #c.productInfo like '%" + color + @"%' "; } if (!string.IsNullOrEmpty(productInfo)) { sql += " and #c.productInfo like '%" + productInfo + @"%' "; } sql += " order by a.createTime"; DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0]; if (dt != null && dt.Rows.Count > 0) { list = Tool.ConvertTo(dt).ToList(); } return list; } /// /// 查询信息 /// /// public List SearchInfoAll(string StartTime, string EndTime, string workClass, string color, string productInfo) { try { //List list = db.SearchAllInfo(); //DateTime start = Convert.ToDateTime(StartTime); //DateTime end = Convert.ToDateTime(EndTime); //list = list.Where(p => p.createTime >= start && p.createTime <= end && "1".Equals(p.pass)).ToList(); //List barcodeList = list.Select(m => m.barcode).ToList(); //var resultList = dbe.tb_InspectResult.Where(p => barcodeList.Contains(p.barcode) && p.inspectResult.Contains("合格")).ToList(); //if (!String.IsNullOrEmpty(workClass)) //{ // resultList = resultList.Where(p => p.workClass.Contains(workClass)).ToList(); //} //if (!String.IsNullOrEmpty(color)) //{ // resultList = resultList.Where(p => p.productInfo.Contains(color)).ToList(); //} //if (!String.IsNullOrEmpty(productInfo)) //{ // resultList = resultList.Where(p => p.productInfo.Contains(productInfo)).ToList(); //} //list = list.Where(p => resultList.Select(m => m.barcode).ToList().Contains(p.barcode)).ToList(); //List StockInList = new List(); //if (list.Count > 0) //{ // foreach (var item in list) // { // StockInModel dm = Tool.Mapper(item); // var info = resultList.FirstOrDefault(p => p.barcode == item.barcode); // if (info != null) // { // dm.workClass = info.workClass; // dm.productInfo = info.productInfo; // } // StockInList.Add(dm); // } //} //return StockInList; List StockInList = GetStockData(StartTime, EndTime, workClass, color, productInfo); return StockInList; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } } }