using MESClassLibrary.BLL.Log; using MESClassLibrary.EFModel; using MESClassLibrary.Model; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using MESClassLibrary.DAL.BasicInfo; namespace MESClassLibrary.BLL.BasicInfo { public class InjectionPlanBLL { BBMPTEntities ef = new BBMPTEntities(); BasicBLL db = new BasicBLL(); BasicBLL db1 = new BasicBLL(); InjectionPlanDAL dal = new InjectionPlanDAL(); /// /// 新增信息 /// /// /// public bool AddInfo(tb_InjectionPlan md) { try { bool flag = false; //查询该机台是否有未完成的计划 List list = ef.tb_InjectionPlan .Where(p => p.StationID.Equals(md.StationID) && (p.IsFinish == 0 || p.IsFinish == null)).ToList(); if (list.Count == 0) { flag = true; } if (!flag) { //如果有未完成的计划则再进行时间段的校验 var info = list.Where(p => DateTime.Parse(md.BeginTime) >= DateTime.Parse(p.BeginTime) && DateTime.Parse(md.BeginTime) <= DateTime.Parse(p.EndTime)).ToList(); if (info.Count() > 0) { return flag; } var info2 = list.Where(p => DateTime.Parse(md.EndTime) >= DateTime.Parse(p.BeginTime) && DateTime.Parse(md.EndTime) <= DateTime.Parse(p.EndTime)).ToList(); if (info2.Count() > 0) { return flag; } var info3 = list.Where(p => DateTime.Parse(md.BeginTime) <= DateTime.Parse(p.BeginTime) && DateTime.Parse(md.EndTime) >= DateTime.Parse(p.EndTime)).ToList(); if (info3.Count() > 0) { return flag; } var into4 = list.Where(p => DateTime.Parse(md.BeginTime) <= DateTime.Parse(p.BeginTime) && DateTime.Parse(md.EndTime) <= DateTime.Parse(p.EndTime)).ToList(); { if (into4.Count() > 0) { return flag; } } } return db.AddInfo(md); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 修改信息 /// /// /// public bool UpdateInfo(tb_InjectionPlan md) { try { //初始化要更新的字段 string[] proNames = new string[10]; proNames[0] = "StationID"; proNames[1] = "BeginTime"; proNames[2] = "StockNo"; proNames[3] = "PlanCount"; proNames[4] = "EndTime"; proNames[5] = "PlanDate"; proNames[6] = "RealCycle"; proNames[7] = "PartNo"; proNames[8] = "ProductNature"; proNames[9] = "UserID"; //必填字段初始化,如果不需要更新必填字段则设置为空即可,时间类型无需初始化 //如果没有初始化必填字段,更新会报错 return db.UpdateInfo(md, proNames); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 删除信息 /// /// /// /// public bool DeleteInfo(tb_InjectionPlan md) { try { //判断是否已经生产,如果已经开始生产则不能删除生产计划 var list = ef.tb_InjectionPlan .Where(p => p.InjectionPlanID == md.InjectionPlanID && p.CompleteCount > 0).ToList(); if (list != null && list.Count() == 0) { return db.DelInfo(md); } return false; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 查询全部信息分页 /// /// public string SearchInfoAll(string page, string pagesize, string stationID, string stockNo) { try { string jsonStr = "[]"; int total = 0; //总行数 List list = db.SearchAllInfo().Where(p => p.IsFinish != 1).ToList(); if (!String.IsNullOrEmpty(stationID)) { list = list.Where(p => p.StationID.Equals(stationID)).ToList(); } if (!String.IsNullOrEmpty(stockNo)) { list = list.Where(p => p.StockNo != null && p.StockNo.Contains(stockNo)).ToList(); } list = list.OrderBy(p => p.BeginTime).ToList(); List modelList = new List(); 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(); #region 联查 BasicBLL product_db = new BasicBLL(); var product_list = product_db.SearchAllInfo(); BasicBLL station_db = new BasicBLL(); var station_list = station_db.SearchAllInfo(); foreach (var item in list) { InjectionPlanModel dm = Tool.Mapper(item); dm.ProductName = ""; var station_info = station_list.FirstOrDefault(p => p.StationID == item.StationID); if (station_info != null) { dm.StationNo = station_info.StationNo; } if (item.PartNo != null && item.PartNo.IndexOf(",") > 0) { string[] arr_PartNo = item.PartNo.Split(','); foreach (var w in arr_PartNo) { var product_info = product_list.FirstOrDefault(p => p.PartNo == w); if (product_info != null) { dm.ProductName = dm.ProductName + product_info.ProductName + ","; } } dm.ProductName = dm.ProductName.Substring(0, dm.ProductName.Length - 1); } else { var product_info = product_list.FirstOrDefault(p => p.PartNo == item.PartNo); if (product_info != null) { dm.ProductName = product_info.ProductName; } } modelList.Add(dm); } #endregion JsonDataModel md = new JsonDataModel(); md.total = total.ToString(); md.rows = modelList; jsonStr = JSONTools.ScriptSerialize(md); } return jsonStr; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public string SearchInfoAll2(string page, string pagesize, string stationID, string stockNo, string StartTime, string EndTime) { try { string jsonStr = "[]"; int total = 0; //总行数 total = dal.SearchByTimeCount(stationID, stockNo, StartTime, EndTime); DataTable dt = dal.SearchByTime(Convert.ToInt32(page), Convert.ToInt32(pagesize), stationID, stockNo, StartTime, EndTime); List list = Tool.ConvertTo(dt).ToList(); //List list = ef.tb_InjectionPlan.Where(p => DateTime.Parse(p.BeginTime) >= stime && DateTime.Parse(p.EndTime) <= etime).ToList(); if (!String.IsNullOrEmpty(stationID)) { list = list.Where(p => p.StationID.Equals(stationID)).ToList(); } if (!String.IsNullOrEmpty(stockNo)) { list = list.Where(p => p.StockNo != null && p.StockNo.Contains(stockNo)).ToList(); } list = list.OrderBy(p => p.BeginTime).ToList(); List modelList = new List(); 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(); #region 联查 BasicBLL product_db = new BasicBLL(); var product_list = product_db.SearchAllInfo(); BasicBLL station_db = new BasicBLL(); var station_list = station_db.SearchAllInfo(); foreach (var item in list) { InjectionPlanModel dm = Tool.Mapper(item); var product_info = product_list.FirstOrDefault(p => p.StockNo == item.StockNo); if (product_info != null) { dm.PartNo = product_info.PartNo; dm.ProductName = product_info.ProductName; } var station_info = station_list.FirstOrDefault(p => p.StationID == item.StationID); if (station_info != null) { dm.StationNo = station_info.StationNo; } modelList.Add(dm); } #endregion JsonDataModel md = new JsonDataModel(); md.total = total.ToString(); md.rows = modelList; jsonStr = JSONTools.ScriptSerialize(md); } return jsonStr; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public bool UpdateInfo2(tb_InjectionPlan md) { try { //初始化要更新的字段 string[] proNames = new string[3]; proNames[0] = "workClass"; proNames[1] = "JK_Weight"; proNames[2] = "Waste_Weight"; //必填字段初始化,如果不需要更新必填字段则设置为空即可,时间类型无需初始化 //如果没有初始化必填字段,更新会报错 return db.UpdateInfo(md, proNames); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 查询全部信息 /// /// public List SearchAll() { try { var s_list = db.SearchAllInfo().ToList(); return s_list; } catch (Exception) { return null; } } /// /// 根据ID查询信息 /// /// /// public tb_InjectionPlan SearchInfoByID(string id) { try { return db.SearchInfoByID(id); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public String GetEndTime(tb_InjectionPlan md) { try { BasicBLL Plastic_db = new BasicBLL(); var info = from c in Plastic_db.SearchAllInfo() where c.PartNo == md.PartNo && c.StationID == md.StationID select c.CycleTime; if (info.FirstOrDefault() != null) { double seconds = Convert.ToDouble(info.FirstOrDefault().Value) * Convert.ToDouble(md.PlanCount); return Convert.ToDateTime(md.BeginTime).AddSeconds(seconds).ToString("yyyy-MM-dd HH:mm:ss"); } return md.BeginTime; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchInfoByName(string StationID) { try { return dal.SearchPlanByStation(StationID); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable NextSearchInfoByName(string StationID) { try { return dal.NextSearchPlanByStation(StationID); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public bool UpdateFinish(InjectionPlanModel md) { InjectionPlanDAL dal = new InjectionPlanDAL(); try { return dal.UpdateFinish(md); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public string GetStartTime(string StationID) { var bf = db.SearchInfoByKey("StationID", StationID).OrderByDescending(p => p.EndTime).FirstOrDefault(); if (bf != null) { var info = from c in db1.SearchAllInfo() where c.StationID == StationID select c.ChangeMould; if (info.FirstOrDefault() != null && !string.IsNullOrEmpty(info.FirstOrDefault().Value.ToString())) { return Convert.ToDateTime(bf.EndTime).AddMinutes(Convert.ToDouble(info.FirstOrDefault().Value)) .ToString("yyyy-MM-dd HH:mm:ss"); } else { return bf.EndTime; } } return ""; } public DataTable searchPlanInfo(string id) { try { return dal.searchPlanInfo(id); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public bool updateFinishCount(string partNo, string stationID, string stockNo, string planID) { try { return dal.updateFinishCount(partNo, stationID, stockNo, planID); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public DataTable SearchQty(string planID) { try { return dal.SearchQty(planID); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public int GetQtyByPlan(string plan) { try { return dal.GetQtyByPlan(plan); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return 0; } } } }