using System; using System.Collections.Generic; using System.Linq; using System.Text; using ICSSoft.Frame.Data.Entity; using ICSSoft.Base.Config.DBHelper; using System.Data; using System.Data.SqlClient; using ICSSoft.Base.Config.AppConfig; namespace ICSSoft.Frame.Data.DAL { public class ICSEquipmentDAL { #region 增加修改 public static void AddAndEdit(List equipmentInfoList, string dsconn) { FramDataContext db = new FramDataContext(dsconn); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { foreach (FormICSEquipmentUIModel equipmentInfo in equipmentInfoList) { bool isNew = false; var line = db.ICSEquipment.SingleOrDefault(a => a.EQPID == equipmentInfo.EQPID); if (line == null) { isNew = true; line = new ICSEquipment(); line.EQPID = AppConfig.GetGuid(); } var codes = db.ICSEquipment.Where(a => a.EQPCode == equipmentInfo.EQPCode && a.EQPID != line.EQPID); if (codes.ToList().Count > 0) { throw new Exception("设备编号已存在"); } line.EQPCode = equipmentInfo.EQPCode; line.EQPName = equipmentInfo.EQPName; line.Model = equipmentInfo.Model; line.Type = equipmentInfo.Type; line.EQPStatus = equipmentInfo.EQPStatus; line.EQPDESC = equipmentInfo.EQPDESC; line.EType = equipmentInfo.EType; line.MTStatus = equipmentInfo.MTStatus; line.Company = equipmentInfo.Company; line.Address = equipmentInfo.Address; line.TelPhone = equipmentInfo.TelPhone; line.MUSER = equipmentInfo.MUSER; line.MUSERName = equipmentInfo.MUSERName; line.MTIME = Convert.ToDateTime(equipmentInfo.MTIME); line.WorkPoint = equipmentInfo.WorkPoint; line.EATTRIBUTE1 = equipmentInfo.EATTRIBUTE1; if (isNew) db.ICSEquipment.InsertOnSubmit(line); db.SubmitChanges(); } db.SubmitChanges(); db.Transaction.Commit(); } catch (Exception ex) { db.Transaction.Rollback(); throw new Exception(ex.Message); } } #endregion #region 班次代码是否存在 public static bool IsIncludingShiftCode(string shiftcode, string dsconn) { FramDataContext db = new FramDataContext(dsconn); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { var line = db.ICSSHIFT.SingleOrDefault(a => a.SHIFTCODE == shiftcode); if (line == null) return true; else return false; } catch (Exception ex) { db.Transaction.Rollback(); throw ex; } } #endregion //#region 班次次序是否存在 //public static bool IsIncludingInShiftSeq(int shiftsqe, string shifttypeid) //{ // FramDataContext db = new FramDataContext(AppConfig.AppConnectString); // db.Connection.Open(); // db.Transaction = db.Connection.BeginTransaction(); // try // { // var line = db.ICSSHIFT.SingleOrDefault(a => a.SHIFTSEQ == shiftsqe && a.SHIFTTYPEID == shifttypeid); // if (line == null) // return true; // else // return false; // } // catch (Exception ex) // { // db.Transaction.Rollback(); // throw ex; // } //} //#endregion public static List SearchEquipmentInfoByCode(string id, string dsconn) { List returnshift = new List(); string sql = @"select a.EQPCode,a.EQPName,a.Model,a.Type,a.EQPStatus,a.EQPDESC,a.EType,a.Company,a.Address,a.TelPhone,b.TypeCODE,a.MUSERName,a.MTIME,a.EATTRIBUTE1,a.mtstatus from ICSEquipment a left join ICSEquipmentType b on a.EType=b.TypeCODE where EQPID='{0}'"; sql = string.Format(sql, id); DataTable dt = DBHelper.ExecuteDataset(dsconn, CommandType.Text, sql).Tables[0]; foreach (DataRow dr in dt.Rows) { FormICSEquipmentUIModel equipmentInfo = new FormICSEquipmentUIModel(); equipmentInfo.EQPID = id; equipmentInfo.EQPCode = dr["EQPCode"].ToString(); equipmentInfo.EQPName = dr["EQPName"].ToString(); equipmentInfo.Model = dr["Model"].ToString(); equipmentInfo.Type = dr["Type"].ToString(); equipmentInfo.EQPStatus = dr["EQPStatus"].ToString(); equipmentInfo.EQPDESC = dr["EQPDESC"].ToString(); equipmentInfo.EType = dr["EType"].ToString(); equipmentInfo.Company = dr["Company"].ToString(); equipmentInfo.Address = dr["Address"].ToString(); equipmentInfo.TelPhone = dr["TelPhone"].ToString(); //equipmentInfo.SS = new FormICSSSUIModel(); //equipmentInfo.SS.SSCODE = dr["SSCODE"].ToString(); //equipmentInfo.SS.SSDESC = dr["SSDESC"].ToString(); equipmentInfo.MUSERName = dr["MUSERName"].ToString(); equipmentInfo.MTIME = System.DateTime.Parse(dr["MTIME"].ToString()); equipmentInfo.MTStatus = dr["mtstatus"].ToString(); equipmentInfo.EATTRIBUTE1 = dr["EATTRIBUTE1"].ToString();//设备属性 if (!returnshift.Contains(equipmentInfo)) { returnshift.Add(equipmentInfo); } } return returnshift; } public static DataTable SelectShiftTypeCode() { string sql = @"select SHIFTTYPECODE as [班制代码] from dbo.ICSSHIFTTYPE where 1=1"; sql = string.Format(sql); DataTable dt = DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql).Tables[0]; return dt; } public static DataTable SelectShiftTypeId(string str) { string sql = @"select ID from dbo.ICSSHIFTTYPE where SHIFTTYPECODE='" + str + "'"; sql = string.Format(sql); DataTable dt = DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql).Tables[0]; return dt; } #region delete public static void delete(List guidList) { FramDataContext db = new FramDataContext(AppConfig.AppConnectString); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { var lines = db.ICSEquipment.Where(a => guidList.Contains(a.EQPID)); db.ICSEquipment.DeleteAllOnSubmit(lines); db.SubmitChanges(); db.Transaction.Commit(); } catch (Exception ex) { db.Transaction.Rollback(); throw ex; } } #endregion public static DataTable GetShiftCode() { try { string sql = @"select TOP 1 [SHIFTCODE] FROM [dbo].[ICSSHIFT] order by SHIFTCODE desc"; return DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql).Tables[0]; } catch (Exception ex) { throw ex; } } // public static DataTable GetShiftSeqCode() // { // try // { // string sql = @"select TOP 1 [SHIFTSEQ] // FROM [dbo].[ICSSHIFT] order by [SHIFTTYPEID] desc"; // return DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql).Tables[0]; // } // catch (Exception ex) // { // throw ex; // } // } public static ICSEquipment GetEQPByCode(string code, string workpoint, string dsconn) { FramDataContext db = new FramDataContext(dsconn); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { var line = db.ICSEquipment.SingleOrDefault(a => a.EQPCode == code && a.WorkPoint == workpoint); if (line != null) { return line; } else { return null; } } catch (Exception ex) { db.Transaction.Rollback(); throw new Exception(ex.Message); } } } }