using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using Commom.DotNetDevices; using Common; using Model; using Model; namespace BLL.DAL { public class DAL_ConveyorInfo { public List GetList(AjaxPlcInfoList Json, ref PageInfo pageInfo) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("select a.IP,b.* from WCSPlcInfo b left join WCSIP a on b.PlcIP = a.Id where b.IsDel = 0 and a.Type = '1' "); if (!string.IsNullOrWhiteSpace(Json.StationNum)) { strSql.Append($"and b.StationNum = '{Json.StationNum}' "); } if (!string.IsNullOrWhiteSpace(Json.Text)) { strSql.Append($"and b.Text = '{Json.Text}' "); } DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), null, "CreateTime", "", ref pageInfo); if (dt != null) { DALWcsMessage wcsSql = new DALWcsMessage(); var plcIpDt = wcsSql.GetPlcIps(); dt.Columns.Add("PlcStatusVal", Type.GetType("System.String")); foreach (DataRow item in dt.Rows) { DataRow ipRows = plcIpDt.Select($" Id = {item["PlcIP"].ToString()} ")[0]; string plcStatus = "", plcValue = ""; var statusVal = ""; PlcIO plc = new PlcIO(ipRows["IP"].ToString(), 1, ref plcStatus); if (plcStatus.Length > 0) { plc.ClosePlc(); } else { plcValue = plc.GetPlcDBValue(int.Parse(item["DbNumber"].ToString()), item["PosType"].ToString(), item["PlcPos"].ToString()); } switch (plcValue) { case "0": // 申请储位 statusVal = item["DbNumber"].ToString() =="7"|| item["DbNumber"].ToString() == "11" ? "有货,不可放货" : "空闲"; break; case "10": // 申请储位 statusVal = "申请储位"; break; case "20": // 到达出库口 statusVal = "出库完成"; break; case "100": // 堆垛机可以取货 statusVal = "堆垛机可以取货"; break; case "120": // 堆垛机可以取货 statusVal = "堆垛机可以放货"; break; default: statusVal = "连接PLC失败"; break; } item["PlcStatusVal"] = statusVal; plc.ClosePlc(); } var list = ModelConvertHelper.DataTableToModel(dt); return list.ToList(); } else { List list = new List(); return list; } } catch { throw new NotImplementedException(); } #region MyRegion //try //{ // StringBuilder strSql = new StringBuilder(); // strSql.Append("select a.*,b.userName as CreateUserName,c.userName as UpdateUserName from WCSConveyorInfo a left join UserInfo b on a.CreateUser = b.Id left join UserInfo c on a.UpdateUser = c.Id where a.IsDel = '0' "); // if (!string.IsNullOrWhiteSpace(Json.SkuType)) // { // strSql.Append(" and SkuType = '" + Json.SkuType + "' "); // } // if (!string.IsNullOrWhiteSpace(Json.Conveyor)) // { // strSql.Append(" and Conveyor = '" + Json.Conveyor + "' "); // } // if (!string.IsNullOrWhiteSpace(Json.LotNo)) // { // strSql.Append(" and LotNo like '%" + Json.LotNo + "%' "); // } // if (!string.IsNullOrWhiteSpace(Json.IsEndLot)) // { // strSql.Append(" and IsEndLot = '" + Json.IsEndLot + "' "); // } // if (!string.IsNullOrWhiteSpace(Json.Status)) // { // strSql.Append(" and Status = '" + Json.Status + "' "); // } // if (Json.BeginTime != null && Json.BeginTime != DateTime.MinValue && Json.BeginTime != DateTime.MaxValue) // { // strSql.Append($"and LoginTime >= '{Convert.ToDateTime(Json.BeginTime).ToShortDateString()}' "); // } // if (Json.BeginTime != null && Json.EndTime != DateTime.MinValue && Json.EndTime != DateTime.MaxValue) // { // strSql.Append($"and LoginTime <= '{Convert.ToDateTime(Json.EndTime).ToShortDateString() + " 23:59:59.999"}' "); // } // DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), null, "CreateTime", "Desc", ref pageInfo); // return ModelConvertHelper.DataTableToModel(dt); //} //catch //{ // throw new NotImplementedException(); //} #endregion } public string AddConveyor(ConveyorModel model, string loginUser,string orgin) { try { //转换user信息 user Code变成id UserInfo us = null; StringBuilder strSql = new StringBuilder(); strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null); us = ModelConvertHelper.ReaderToModel(dt); if (us == null) { return "未查询到当前登录人信息"; } string plcStatus = ""; // PLC连接状态 PlcIO plc = new PlcIO("192.168.21.2", 1, ref plcStatus); if (plcStatus.Length > 0) { return "连接输送线失败"; } DALWcsMessage wcsSql = new DALWcsMessage(); var PlcInfoDt = wcsSql.GetPlcInfos(); DataRow[] plcRows = PlcInfoDt.Select($"Level = '2' and StationNum='{model.StartLocatNo}' "); if (plcRows.Length == 0) { return "未查询到起始工位信息"; } var posDt = wcsSql.GetPlcPos(plcRows[0]["Id"].ToString()); // 写入起始工位 var posRos101 = posDt.Select($"Name = 'StartLocatNo' ")[0]; plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos101["PosType"].ToString(), model.StartLocatNo, posRos101["PlcPos"].ToString()); // 写入目的工位 var posRos102 = posDt.Select($"Name = 'EndLocatNo' ")[0]; plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos102["PosType"].ToString(), model.EndLocatNo, posRos102["PlcPos"].ToString()); //写入托盘号 var posRos103 = posDt.Select($"Name = 'PalletNo' ")[0];// 托盘号 plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos103["PosType"].ToString(), model.PalletNo, posRos103["PlcPos"].ToString()); //写入任务号 var posRos104 = posDt.Select($"Name = 'TaskNo' ")[0]; plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos104["PosType"].ToString(), model.TaskNo, posRos104["PlcPos"].ToString()); // 写入wcs控制字流程10 bool inBl10 = plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), plcRows[0]["PosType"].ToString(),model.WcsPos, plcRows[0]["WcsPos"].ToString(), true); plc.ClosePlc(); if (orgin == "Edit") { DAL_OperationRecord oper = new DAL_OperationRecord(); var operation = new OperationModel() { MenuName = "输送线监控", FkNo = model.TaskNo + "-" + model.PalletNo, Msg = "手动编辑输送线任务信息", Type = "编辑", }; oper.AddOperation(operation, loginUser); } return ""; } catch(Exception e) { throw new Exception(e.Message); } } public ConveyorModel GetConveyor(string id) { try { string plcStatus = ""; // PLC连接状态 ConveyorModel model = new ConveyorModel(); PlcIO plc = new PlcIO("192.168.21.2", 1, ref plcStatus); if (plcStatus.Length > 0) { return model; //throw new Exception("连接输送线失败"); } DALWcsMessage wcsSql = new DALWcsMessage(); var PlcInfoDt = wcsSql.GetPlcInfos(); DataRow[] plcRows = PlcInfoDt.Select($"Level = '2' and Id={id} "); if (plcRows.Length == 0) { return model; //throw new Exception("未查询到起始工位信息"); } var posDt = wcsSql.GetPlcPos(id); // 获取起始工位 var posRos101 = posDt.Select($"Name = 'StartLocatNo' ")[0]; model.StartLocatNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos101["PosType"].ToString(), posRos101["PlcPos"].ToString()); // 获取目的工位 var posRos102 = posDt.Select($"Name = 'EndLocatNo' ")[0]; model.EndLocatNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos102["PosType"].ToString(), posRos102["PlcPos"].ToString()); // 获取托盘号 var posRos103 = posDt.Select($"Name = 'PalletNo' ")[0];// 托盘号 model.PalletNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos103["PosType"].ToString(), posRos103["PlcPos"].ToString()); // 获取任务号 var posRos104 = posDt.Select($"Name = 'TaskNo' ")[0]; model.TaskNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos104["PosType"].ToString(), posRos104["PlcPos"].ToString()); // 获取wcs控制字流程 model.WcsPos = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), plcRows[0]["PosType"].ToString(), plcRows[0]["WcsPos"].ToString()); plc.ClosePlc(); return model; } catch (Exception e) { throw new Exception(e.Message); } } /// /// 添加物料输送信息 /// /// /// /// public bool Add(WCSConveyorInfo model, string loginUser) { bool result = false; try { //转换user信息 user Code变成id UserInfo us = null; StringBuilder strSql = new StringBuilder(); strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null); us = ModelConvertHelper.ReaderToModel(dt); if (us == null) { return false; } Hashtable ht = new Hashtable(); ht["SkuType"] = "'" + model.SkuType + "'"; ht["Conveyor"] = "'" + model.Conveyor + "'"; ht["LotNo"] = "'" + model.LotNo + "'"; ht["SupplierLot"] = "'" + model.SupplierLot + "'"; ht["ManuFactureDate"] = "'" + model.ManuFactureDate + "'"; ht["IsEndLot"] = "0"; ht["Status"] = "0"; ht["IsDel"] = "0"; ht["CreateUser"] = "" + us.ID + ""; ht["CreateTime"] = "GetDate()"; var ret = DataFactory.SqlDataBase().InsertByHashtableNullParam("WCSConveyorInfo", ht); if (ret == 1) result = true; return result; } catch { return result; } } public bool Delete(string[] infoIds, string loginUser) { bool result = false; try { UserInfo us = null; StringBuilder strSql = new StringBuilder(); strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null); us = ModelConvertHelper.ReaderToModel(dt); if (us == null) { return false; } var dt2 = DataFactory.SqlDataBase().IsExist("WcsConveyorInfo", "Id", infoIds); if (dt2 >= infoIds.Length) { StringBuilder sql = new StringBuilder(); foreach (string code in infoIds) { sql.Append("update WcsConveyorInfo set IsDel=1,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';"); } int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql); if (_ret > infoIds.Length) result = true; } return result; } catch { return result; } } public bool Run(string[] infoIds, string loginUser) { bool result = false; try { UserInfo us = null; StringBuilder strSql = new StringBuilder(); strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null); us = ModelConvertHelper.ReaderToModel(dt); if (us == null) { return false; } var dt2 = DataFactory.SqlDataBase().IsExist("WCSConveyorInfo", "Id", infoIds); if (dt2 >= infoIds.Length) { StringBuilder sql = new StringBuilder(); foreach (string code in infoIds) { StringBuilder sql2 = new StringBuilder(); sql2.Append($"select * from WCSConveyorInfo where IsDel = 0 and Id = '{code}' "); IDataReader dt3 = DataFactory.SqlDataBase().GetDataReaderBySQL(sql2, null); var info = ModelConvertHelper.ReaderToModel(dt3); sql.Append("update WcsConveyorInfo set Status=1,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';"); sql.Append("update WcsConveyorInfo set Status=0,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id !='" + code + "' and IsDel = '0' and Conveyor = '"+ info.Conveyor + "';"); } int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql); if (_ret > infoIds.Length) result = true; } return result; } catch { return result; } } public bool JiePi(string[] infoIds, string loginUser) { bool result = false; try { UserInfo us = null; StringBuilder strSql = new StringBuilder(); strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null); us = ModelConvertHelper.ReaderToModel(dt); if (us == null) { return false; } var dt2 = DataFactory.SqlDataBase().IsExist("WCSConveyorInfo", "Id", infoIds); if (dt2 >= infoIds.Length) { StringBuilder sql = new StringBuilder(); foreach (string code in infoIds) { sql.Append("update WcsConveyorInfo set IsEndLot=1,Status =0,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';"); } int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql); if (_ret > infoIds.Length) result = true; } return result; } catch { return result; } } } }