using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.DAL; using QMAPP.MD.Entity; using QMFrameWork.Data; using QMFrameWork.Log; using System.Data; using System.Net.Sockets; using QMAPP.MD.Entity.View; namespace QMAPP.MD.DAL { /// /// 模块名称:Bom信息 /// 作 者:郭兆福 /// 编写日期:2017年05月10日 /// public class PbomDAL : BaseDAL { #region 获取信息 /// /// 获取信息 /// /// 条件 /// 信息 public Pbom Get(Pbom model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.Append(" SELECT * "); sqlBuilder.Append(" FROM T_MD_PBOM "); sqlBuilder.Append(" WHERE FLGDEL = '0'"); if (string.IsNullOrEmpty(model.PBOM_CODE) == false) { sqlBuilder.Append(" AND PBOM_CODE= @PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = model.PBOM_CODE }); } if (string.IsNullOrEmpty(model.MATERIAL_CODE) == false) { sqlBuilder.Append(" AND MATERIAL_CODE= @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = model.MATERIAL_CODE }); } if (string.IsNullOrEmpty(model.PID) == false) { sqlBuilder.Append(" AND PID= @PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); } try { if (null != BaseSession) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); model = BaseSession.Get(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 model = session.Get(sqlChange, parameters.ToArray()); } } return model; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取信息"); throw ex; } } /// /// 获取信息 /// /// 条件 /// 信息 public Pbom TJGet(Pbom model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.Append(" SELECT * "); sqlBuilder.Append(" FROM T_MD_PBOM "); sqlBuilder.Append(" WHERE FLGDEL = '0'"); if (string.IsNullOrEmpty(model.PBOM_CODE) == false) { sqlBuilder.Append(" AND PBOM_CODE = @PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = model.PBOM_CODE }); } if (string.IsNullOrEmpty(model.MATERIAL_CODE) == false) { sqlBuilder.Append(" AND MATERIAL_CODE LIKE @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = "%" + model.MATERIAL_CODE + "%" }); } if (string.IsNullOrEmpty(model.PID) == false) { sqlBuilder.Append(" AND PID= @PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); } try { if (null != BaseSession) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); model = BaseSession.Get(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 model = session.Get(sqlChange, parameters.ToArray()); } } return model; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取信息"); throw ex; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(Pbom condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); #region 排序 //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } #endregion using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sql,session); page = session.GetDataPage(sqlChange, parameters.ToArray(), page); } return page; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取列表"); throw ex; } } /// /// 获取列表信息 /// /// /// public List GetList(Pbom condition) { List list = new List(); StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); List paraList = new List(); //构成查询语句 sqlBuilder.Append("SELECT PID,PBOM_CODE,MATERIAL_CODE,PROPERTY_CODE,EOA_DATE,EXP_DATE,STATUS,ITEM_NUM,VER_NUM,REMARK,CREATEUSER,CREATEDATE,UPDATEUSER,UPDATEDATE "); sqlBuilder.Append("FROM T_MD_PBOM where "); if (string.IsNullOrEmpty(condition.PBOM_CODE) == false) { whereBuilder.AppendFormat("and PBOM_CODE=@PBOM_CODE "); paraList.Add(new DataParameter() { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = condition.PBOM_CODE }); } if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false) { whereBuilder.AppendFormat("and MATERIAL_CODE=@MATERIAL_CODE "); paraList.Add(new DataParameter() { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE }); } sqlBuilder.Append(whereBuilder.ToString().Substring(3)); using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sqlBuilder.ToString(), paraList.ToArray()).ToList(); } return list; } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(Pbom condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" SELECT B.PID "); sqlBuilder.AppendLine(" ,B.PBOM_CODE "); sqlBuilder.AppendLine(" ,B.MATERIAL_CODE "); sqlBuilder.AppendLine(" ,M.MATERIAL_NAME "); sqlBuilder.AppendLine(" ,B.PROPERTY_CODE "); sqlBuilder.AppendLine(" ,R.ROUTE_CODE "); sqlBuilder.AppendLine(" ,R.ROUTE_NAME "); sqlBuilder.AppendLine(" ,B.EOA_DATE "); sqlBuilder.AppendLine(" ,B.EXP_DATE "); sqlBuilder.AppendLine(" ,B.STATUS "); sqlBuilder.AppendLine(" ,B.ITEM_NUM "); sqlBuilder.AppendLine(" ,B.VER_NUM "); sqlBuilder.AppendLine(" ,B.REMARK "); sqlBuilder.AppendLine(" ,B.CREATEUSER "); sqlBuilder.AppendLine(" ,B.CREATEDATE "); sqlBuilder.AppendLine(" ,B.UPDATEUSER "); sqlBuilder.AppendLine(" ,B.UPDATEDATE "); sqlBuilder.AppendLine(" ,B.FLGDEL "); sqlBuilder.AppendLine(" ,B.FACTORY_CODE "); sqlBuilder.AppendLine(" ,F.FACTORY_NAME "); sqlBuilder.AppendLine(" FROM T_MD_PBOM B "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL M ON M.MATERIAL_CODE=B.MATERIAL_CODE AND M.FLGDEL ='0' "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL_ROUTE MR ON B.MATERIAL_CODE=MR.MATERIAL_CODE AND MR.FLGDEL ='0' "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_PROCESS_ROUTE R ON MR.ROUTE_CODE=R.ROUTE_CODE AND R.FLGDEL ='0' "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=B.FACTORY_CODE "); whereBuilder.Append(" AND B.FLGDEL='0'"); //查询条件 //工厂 if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false) { whereBuilder.Append(" AND B.FACTORY_CODE = @FACTORY_CODE"); parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE }); } //PBOM号 if (string.IsNullOrEmpty(condition.PBOM_CODE) == false) { whereBuilder.Append(" AND B.PBOM_CODE = @PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = condition.PBOM_CODE }); } //物料号 if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false) { whereBuilder.Append(" AND B.MATERIAL_CODE = @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE }); } //BOM版本 if (string.IsNullOrEmpty(condition.VER_NUM) == false) { whereBuilder.Append(" AND B.VER_NUM= @VER_NUM "); parameters.Add(new DataParameter { ParameterName = "VER_NUM", DataType = DbType.String, Value = condition.VER_NUM }); } //BOM特征码 if (string.IsNullOrEmpty(condition.PROPERTY_CODE) == false) { whereBuilder.Append(" AND B.PROPERTY_CODE= @PROPERTY_CODE "); parameters.Add(new DataParameter { ParameterName = "PROPERTY_CODE", DataType = DbType.String, Value = condition.PROPERTY_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region BOM号,物料号信息是否重复 /// /// 判断是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsBomHdr(Pbom model) { string PID = ""; int count = 0; List parameters = new List(); StringBuilder sqlBuilder = new StringBuilder(); try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sqlBuilder.Append(" SELECT COUNT(*) FROM T_MD_PBOM WHERE PID <> @PID AND FACTORY_CODE <> @FACTORY_CODE AND (PBOM_CODE =@PBOM_CODE OR MATERIAL_CODE =@MATERIAL_CODE ) "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = PID }); parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = model.FACTORY_CODE }); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = model.PBOM_CODE }); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = model.MATERIAL_CODE }); if (this.BaseSession != null) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); count = Convert.ToInt32(BaseSession.ExecuteSqlScalar(sqlChange, parameters.ToArray())); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, parameters.ToArray())); } } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取列表"); throw ex; } } #endregion #region 判断物料是否存在 /// /// 判断是否存在 /// /// 物料号 /// 是否PBOM项目 /// true:已存在;fasel:不存在。 public bool IsExists(string materialCode,bool isItem) { int count = 0; List parameters = new List(); StringBuilder sqlBuilder = new StringBuilder(); try { sqlBuilder.Append(" SELECT COUNT(*) FROM "); if (isItem == true) { sqlBuilder.Append(" T_MD_PBOM_ITEM "); } else { sqlBuilder.Append(" T_MD_PBOM "); } sqlBuilder.Append(" WHERE MATERIAL_CODE =@MATERIAL_CODE "); sqlBuilder.Append(" AND FLGDEL = '0' "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = materialCode }); if (this.BaseSession != null) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); count = Convert.ToInt32(BaseSession.ExecuteSqlScalar(sqlChange, parameters.ToArray())); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, parameters.ToArray())); } } return count == 0 ? false : true; } catch (Exception ex) { RecordExceptionLog(ex, "判断物料号是否在BOM存在"); throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int Insert(Pbom model) { int count = 0; try { if (BaseSession != null) { //插入基本信息 count = BaseSession.Insert(model); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-插入信息"); throw ex; } } #endregion #region 删除Bom信息 /// /// 删除信息 /// /// Bom信息(ID) /// 删除个数 public int DeleteBomHdr(Pbom model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); StringBuilder whereBuilder = new StringBuilder(); int count = 0; try { //构成查询语句 sqlBuilder.AppendLine(" UPDATE T_MD_PBOM SET FLGDEL = '1' "); if (string.IsNullOrEmpty(model.PID) == false) { whereBuilder.Append(" AND PID=@PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } if (this.BaseSession != null) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); //删除基本信息 count = BaseSession.ExecuteSql(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), session); //删除基本信息 count = session.ExecuteSql(sqlChange, parameters.ToArray()); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-删除Bom信息"); throw ex; } } #endregion #region Bom信息下拉 /// /// /// /// /// public List GetBomHdrList(Pbom condition) { List list = new List(); string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sql, session); list = session.GetList(sqlChange, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { RecordExceptionLog(ex, "BOM信息-下拉列表"); throw ex; } } #endregion #region 根据BOM明细查找BOM头 /// /// 根据BOM明细查找BOM头 /// /// /// BOM头列表 public List GetPbomByItem(string materialCode) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.Append(" SELECT H.* "); sqlBuilder.Append(" FROM T_MD_PBOM H WHERE H.FLGDEL = '0' AND EXISTS "); sqlBuilder.Append(" ( "); sqlBuilder.Append(" SELECT 1 FROM T_MD_PBOM_ITEM I "); sqlBuilder.Append(" WHERE H.PBOM_CODE = I.PBOM_CODE AND I.FLGDEL = '0' AND I.MATERIAL_CODE = @MATERIAL_CODE "); sqlBuilder.Append(" ) "); parameters.Add(new DataParameter("MATERIAL_CODE", DbType.String, materialCode)); try { if (null != BaseSession) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); return BaseSession.GetList(sqlChange, parameters.ToArray()).ToList(); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 return session.GetList(sqlChange, parameters.ToArray()).ToList(); } } } catch (Exception ex) { RecordExceptionLog(ex, "根据BOM明细查找BOM头 -出错!"); throw ex; } } /// /// 根据物料号查找BOM头 /// /// /// BOM头列表 public List GetPbomByMaterialCode(string materialCode) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.AppendLine("SELECT H.* "); sqlBuilder.AppendLine(" FROM T_MD_PBOM AS H "); sqlBuilder.AppendLine(" WHERE H.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND (EXISTS (SELECT 1 "); sqlBuilder.AppendLine(" FROM T_MD_PBOM_ITEM AS I "); sqlBuilder.AppendLine(" WHERE H.PBOM_CODE = I.PBOM_CODE "); sqlBuilder.AppendLine(" AND I.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND I.MATERIAL_CODE = @materialcode) "); sqlBuilder.AppendLine(" OR H.[MATERIAL_CODE]=@materialcode) "); parameters.Add(new DataParameter("materialcode", DbType.String, materialCode)); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 return session.GetList(sqlChange, parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } /// /// 根据物料号查找BOM头 /// /// /// BOM头列表 public List GetPbomByMaterialCodeWithPlan(string materialCode,string BomCode) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.AppendLine("SELECT H.* "); sqlBuilder.AppendLine(" FROM T_MD_PBOM AS H "); sqlBuilder.AppendLine(" WHERE H.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND (EXISTS (SELECT 1 "); sqlBuilder.AppendLine(" FROM T_MD_PBOM_ITEM AS I "); sqlBuilder.AppendLine(" WHERE "); sqlBuilder.AppendLine(" I.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND I.MATERIAL_CODE = @materialcode) "); sqlBuilder.AppendLine(" ) and H.PBOM_CODE = @BomCode "); parameters.Add(new DataParameter("materialcode", DbType.String, materialCode)); parameters.Add(new DataParameter("BomCode", DbType.String, BomCode)); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 return session.GetList(sqlChange, parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } /// /// 根据物料号查找最终的总成BOM头 /// /// /// BOM头列表 public List GetFinalPbom(string materialCode) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.AppendLine("WITH CTE AS ( "); sqlBuilder.AppendLine(" SELECT H.*,0 as N "); sqlBuilder.AppendLine(" FROM T_MD_PBOM AS H "); sqlBuilder.AppendLine(" WHERE H.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND (EXISTS (SELECT 1 "); sqlBuilder.AppendLine(" FROM T_MD_PBOM_ITEM AS I "); sqlBuilder.AppendLine(" WHERE H.PBOM_CODE = I.PBOM_CODE "); sqlBuilder.AppendLine(" AND I.FLGDEL = '0' "); sqlBuilder.AppendLine(" AND I.MATERIAL_CODE = @materialcode) "); sqlBuilder.AppendLine(" OR H.[MATERIAL_CODE]=@materialcode) "); sqlBuilder.AppendLine(" UNION ALL "); sqlBuilder.AppendLine(" SELECT B.*,C.N+1 AS N "); sqlBuilder.AppendLine(" FROM CTE C "); sqlBuilder.AppendLine(" JOIN T_MD_PBOM_ITEM AS BI "); sqlBuilder.AppendLine(" ON BI.MATERIAL_CODE=c.MATERIAL_CODE "); sqlBuilder.AppendLine(" JOIN T_MD_PBOM AS B "); sqlBuilder.AppendLine(" ON B.PBOM_CODE=BI.PBOM_CODE "); sqlBuilder.AppendLine(" WHERE B.FLGDEL='0' "); sqlBuilder.AppendLine(") "); sqlBuilder.AppendLine("SELECT * FROM CTE "); sqlBuilder.AppendLine(" WHERE N=(SELECT MAX(N) FROM CTE ) "); parameters.Add(new DataParameter("materialcode", DbType.String, materialCode)); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 return session.GetList(sqlChange, parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } #endregion #region 配置BOM明细 #region 获取BOM明细列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage BomHdrConfigList(PbomItem condition, DataPage page) { string sql = ""; List parameters = new List(); try { sql = this.GetBomDetailListSql(condition, ref parameters); //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sql, session); page = session.GetDataPage(sqlChange, parameters.ToArray(), page); } return page; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取明细列表"); throw ex; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetBomDetailListSql(PbomItem condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" SELECT B.PID "); sqlBuilder.AppendLine(" ,B.PBOM_CODE "); sqlBuilder.AppendLine(" ,B.PROPERTY_CODE "); sqlBuilder.AppendLine(" ,B.MATERIAL_CODE "); sqlBuilder.AppendLine(" ,M.MATERIAL_NAME "); sqlBuilder.AppendLine(" ,B.SEQ_NUM "); sqlBuilder.AppendLine(" ,B.UP_SQ_NUM "); sqlBuilder.AppendLine(" ,B.UNIT_SUM "); sqlBuilder.AppendLine(" ,B.GMP "); sqlBuilder.AppendLine(" ,C.WORKCELL_NAME AS GMP_NAME "); sqlBuilder.AppendLine(" ,B.FLAG_CSP "); sqlBuilder.AppendLine(" ,B.MFRS "); sqlBuilder.AppendLine(" ,B.REMARK "); sqlBuilder.AppendLine(" ,B.FLGDEL "); sqlBuilder.AppendLine(" ,B.CREATEUSER "); sqlBuilder.AppendLine(" ,B.CREATEDATE "); sqlBuilder.AppendLine(" ,B.UPDATEUSER "); sqlBuilder.AppendLine(" ,B.UPDATEDATE "); sqlBuilder.AppendLine(" FROM T_MD_PBOM_ITEM B "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCELL C ON B.GMP = C.WORKCELL_CODE AND C.FLGDEL = '0' "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL M ON B.MATERIAL_CODE = M.MATERIAL_CODE AND M.FLGDEL = '0' "); whereBuilder.Append(" AND B.FLGDEL ='0' "); //查询条件 if (string.IsNullOrEmpty(condition.PBOM_CODE) == false) { whereBuilder.Append(" AND B.PBOM_CODE= @PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = condition.PBOM_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 获取Bom明细列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public List BomDetailList(PbomItem condition) { string sql = ""; List parameters = new List(); List list = new List(); try { sql = this.GetBomDetailListSql(condition, ref parameters); if (null != BaseSession) { string sqlChange = base.ChangeSqlByDB(sql, BaseSession); list = BaseSession.GetList(sqlChange, parameters.ToArray()).ToList(); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sql, session); list = session.GetList(sqlChange, parameters.ToArray()).ToList(); } } return list; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取Bom明细列表"); throw ex; } } #endregion #region 插入Bom明细信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int InsertBomDetail(PbomItem model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-插入Bom明细信息"); throw ex; } } #endregion #region 获取Bom明细 /// /// 获取Bom明细 /// /// 条件 /// *信息 public PbomItem GetBomDetail(PbomItem model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); sqlBuilder.Append(" SELECT * "); sqlBuilder.Append(" FROM T_MD_PBOM_ITEM "); sqlBuilder.Append(" WHERE FLGDEL = '0'"); if (string.IsNullOrEmpty(model.PBOM_CODE) == false && string.IsNullOrEmpty(model.MATERIAL_CODE) == false) { sqlBuilder.Append(" AND PBOM_CODE= @PBOM_CODE "); sqlBuilder.Append(" AND MATERIAL_CODE= @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = model.PBOM_CODE }); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = model.MATERIAL_CODE }); } if (string.IsNullOrEmpty(model.PID) == false) { sqlBuilder.Append(" AND PID= @PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); } if (string.IsNullOrEmpty(model.GMP) == false) { sqlBuilder.Append(" AND GMP= @GMP "); parameters.Add(new DataParameter { ParameterName = "GMP", DataType = DbType.String, Value = model.GMP }); } try { if (null != BaseSession) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); model = BaseSession.Get(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session); //获取信息 model = session.Get(sqlChange, parameters.ToArray()); } } return model; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-获取信息"); throw ex; } } #endregion #region 删除Bom明细 /// /// 删除信息 /// /// Bom信息(ID) /// 删除个数 public int DeleteBomDetail(PbomItem model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); StringBuilder whereBuilder = new StringBuilder(); int count = 0; try { //构成查询语句 sqlBuilder.AppendLine(" UPDATE T_MD_PBOM_ITEM SET FLGDEL = '1' "); if (string.IsNullOrEmpty(model.PID) == false) { whereBuilder.Append(" AND PID=@PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); } if (string.IsNullOrEmpty(model.PBOM_CODE) == false) { whereBuilder.Append(" AND PBOM_CODE=@PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = model.PBOM_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } if (this.BaseSession != null) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); //删除基本信息 count = BaseSession.ExecuteSql(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), session); //删除基本信息 count = session.ExecuteSql(sqlChange, parameters.ToArray()); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-删除BOM详细信息"); throw ex; } } /// /// 删除信息 /// /// Bom信息(ID) /// 删除个数 public int DeleteBomDetailByPbomId(string pid) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); StringBuilder whereBuilder = new StringBuilder(); int count = 0; try { //构成查询语句 sqlBuilder.AppendLine(" UPDATE T_MD_PBOM_ITEM SET FLGDEL = '1' WHERE PBOM_CODE IN (SELECT T.PBOM_CODE FROM T_MD_PBOM T WHERE T.PID = @PID)"); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = pid }); if (this.BaseSession != null) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), BaseSession); //删除基本信息 count = BaseSession.ExecuteSql(sqlChange, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = base.ChangeSqlByDB(sqlBuilder.ToString(), session); //删除基本信息 count = session.ExecuteSql(sqlChange, parameters.ToArray()); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-删除BOM详细信息"); throw ex; } } #endregion #endregion /// /// 获取Bom信息 /// /// Bom编码 /// public Pbom GetByCode(string BomCode) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT * FROM [T_MD_PBOM] WHERE [PBOM_CODE]=@bomcode"); parameters.Add(new DataParameter("bomcode", BomCode)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { throw ex; } } /// /// 根据BOM定位查找BOM明细项 /// /// BOM定位信息 /// public PbomItem GetByLocate(BomLocation locate) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT * FROM [T_MD_PBOM_ITEM] WHERE [PBOM_CODE]=@bomcode AND [SEQ_NUM]=@itemno"); parameters.Add(new DataParameter("bomcode", locate.BOMCode)); parameters.Add(new DataParameter("itemno", locate.ItemNo)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { throw ex; } } /// /// 根据BOM定位查找BOM明细项列表 /// /// BOM定位信息 /// public List GetBomItemsByLocate(BomLocation locate) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT * FROM [T_MD_PBOM_ITEM] WHERE [PBOM_CODE]=@bomcode AND [UP_SQ_NUM]=@itemno"); parameters.Add(new DataParameter("bomcode", locate.BOMCode)); parameters.Add(new DataParameter("itemno", locate.ItemNo)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } public List GetBomItemsByLocateWithType(BomLocation locate) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("SELECT B.* "); sql.AppendLine(" ,M.MATERIAL_TYPE_CODE "); sql.AppendLine(" ,C.UP_MATERIAL_TYPE_CODE "); sql.AppendLine("FROM [T_MD_PBOM_ITEM] AS B "); sql.AppendLine("LEFT JOIN T_MD_MATERIAL AS M "); sql.AppendLine("ON B.MATERIAL_CODE=M.MATERIAL_CODE "); sql.AppendLine("LEFT JOIN T_MD_MATERIAL_CLASS AS C "); sql.AppendLine("ON C.MATERIAL_TYPE_CODE=m.MATERIAL_TYPE_CODE "); sql.AppendLine("WHERE [PBOM_CODE]=@bomcode AND [UP_SQ_NUM]=@itemno"); parameters.Add(new DataParameter("bomcode", locate.BOMCode)); parameters.Add(new DataParameter("itemno", locate.ItemNo)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } /// /// 定位BOM /// /// /// public List LocateBom(params string[] materials) { if (materials.Length == 0) { throw new Exception("至少需要一种物料号才能确定BOM!"); } StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("SELECT [PBOM_CODE] AS [BOMCode]"); sql.AppendLine(" ,[UP_SQ_NUM] AS [ItemNo] "); sql.AppendLine(" ,(SELECT COUNT(*) "); sql.AppendLine(" FROM [T_MD_PBOM_ITEM] AS A "); sql.AppendLine(" LEFT JOIN T_MD_MATERIAL M ON M.MATERIAL_CODE=A.MATERIAL_CODE "); sql.AppendLine(" LEFT JOIN T_MD_MATERIAL_CLASS MC ON MC.MATERIAL_TYPE_CODE=M.MATERIAL_TYPE_CODE "); sql.AppendLine(" WHERE A.[PBOM_CODE]=W.[PBOM_CODE] "); sql.AppendLine(" AND A.[UP_SQ_NUM]=W.[UP_SQ_NUM] "); sql.AppendLine(" AND MC.MATERIAL_ATTRIBUTE NOT IN ('3') "); sql.AppendLine(" )AS [SubCount] "); sql.AppendLine(" ,(SELECT TOP 1 MATERIAL_CODE "); sql.AppendLine(" FROM [T_MD_PBOM_ITEM] AS B "); sql.AppendLine(" WHERE B.[PBOM_CODE]=W.[PBOM_CODE] "); sql.AppendLine(" AND B.[SEQ_NUM]=W.[UP_SQ_NUM] "); sql.AppendLine(" )AS [UP_MATERIAL_CODE] "); sql.AppendLine(" FROM [T_MD_PBOM_ITEM] AS W"); sql.Append(" WHERE W.[MATERIAL_CODE] IN ("); for (int i = 0; i < materials.Length; i++) { if (i > 0) { sql.Append(","); } sql.Append(string.Format("@material{0}", i)); parameters.Add(new DataParameter(string.Format("@material{0}", i), materials[i])); } sql.AppendLine(") "); sql.AppendLine(" GROUP BY W.[PBOM_CODE],W.[UP_SQ_NUM]"); sql.AppendLine("HAVING COUNT(*)>=@materialcount"); parameters.Add(new DataParameter("materialcount", materials.Length)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } /// /// 定位BOM /// /// /// public List LocateBomByUpMaterial(string upmaterial) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("SELECT [PBOM_CODE] AS [BOMCode] "); sql.AppendLine(" ,[SEQ_NUM] AS [ItemNo] "); sql.AppendLine(" ,(SELECT COUNT(*) "); sql.AppendLine(" FROM [T_MD_PBOM_ITEM] AS A "); sql.AppendLine(" WHERE A.[PBOM_CODE]=B.[PBOM_CODE] "); sql.AppendLine(" AND A.[UP_SQ_NUM]=B.[SEQ_NUM])AS [SubCount] "); sql.AppendLine(" ,[MATERIAL_CODE] AS [UP_MATERIAL_CODE] "); sql.AppendLine(" FROM T_MD_PBOM_ITEM B "); sql.AppendLine(" WHERE MATERIAL_CODE=@upmaterial "); sql.AppendLine("UNION "); sql.AppendLine("SELECT M.PBOM_CODE AS [BOMCode] "); sql.AppendLine(" ,0 AS [ItemNo] "); sql.AppendLine(" ,(SELECT COUNT(*) "); sql.AppendLine(" FROM [T_MD_PBOM_ITEM] AS A "); sql.AppendLine(" WHERE A.[PBOM_CODE]=M.[PBOM_CODE] "); sql.AppendLine(" AND A.[UP_SQ_NUM]=0)AS [SubCount] "); sql.AppendLine(" ,M.[MATERIAL_CODE] AS [UP_MATERIAL_CODE] "); sql.AppendLine(" FROM T_MD_PBOM AS M "); sql.AppendLine(" WHERE M.MATERIAL_CODE=@upmaterial "); parameters.Add(new DataParameter("upmaterial", upmaterial)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } public List GetSubItem(string upmaterial) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("WITH CTE AS ("); sql.AppendLine(" SELECT TOP 1 *"); sql.AppendLine(" FROM T_MD_PBOM_ITEM "); sql.AppendLine(" WHERE MATERIAL_CODE=@upmaterial"); sql.AppendLine(" UNION ALL"); sql.AppendLine(" SELECT TOP 1 NULL AS [PID]"); sql.AppendLine(" ,[PBOM_CODE]"); sql.AppendLine(" ,[MATERIAL_CODE]"); sql.AppendLine(" ,0 AS [SEQ_NUM]"); sql.AppendLine(" ,0 AS [UP_SQ_NUM]"); sql.AppendLine(" ,1 AS [UNIT_SUM]"); sql.AppendLine(" ,'' AS [GMP]"); sql.AppendLine(" ,NULL AS [FLAG_CSP]"); sql.AppendLine(" ,'' AS [MFRS]"); sql.AppendLine(" ,[REMARK]"); sql.AppendLine(" ,[CREATEUSER]"); sql.AppendLine(" ,[CREATEDATE]"); sql.AppendLine(" ,[UPDATEUSER]"); sql.AppendLine(" ,[UPDATEDATE]"); sql.AppendLine(" ,[PROPERTY_CODE]"); sql.AppendLine(" ,[FLGDEL]"); sql.AppendLine(" ,[FACTORY_CODE]"); sql.AppendLine(" FROM T_MD_PBOM"); sql.AppendLine(" WHERE MATERIAL_CODE=@upmaterial"); sql.AppendLine(" UNION ALL"); sql.AppendLine(" SELECT B.* "); sql.AppendLine(" FROM CTE AS C "); sql.AppendLine(" JOIN T_MD_PBOM_ITEM AS B "); sql.AppendLine(" ON B.PBOM_CODE=C.PBOM_CODE"); sql.AppendLine(" AND B.UP_SQ_NUM=C.SEQ_NUM"); sql.AppendLine(")"); sql.AppendLine("SELECT * FROM CTE"); parameters.Add(new DataParameter("upmaterial", upmaterial)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } public List GetSubMaterial(string upmaterial) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("SELECT C.* "); sql.AppendLine(" FROM T_MD_PBOM_ITEM AS C "); sql.AppendLine(" JOIN (SELECT PBOM_CODE "); sql.AppendLine(" ,SEQ_NUM "); sql.AppendLine(" FROM T_MD_PBOM_ITEM "); sql.AppendLine(" WHERE MATERIAL_CODE=@upmaterial "); sql.AppendLine(" UNION ALL "); sql.AppendLine(" SELECT [PBOM_CODE] "); sql.AppendLine(" ,0 AS [SEQ_NUM] "); sql.AppendLine(" FROM T_MD_PBOM "); sql.AppendLine(" WHERE MATERIAL_CODE=@upmaterial) AS B "); sql.AppendLine(" ON C.PBOM_CODE=B.PBOM_CODE "); sql.AppendLine(" AND C.UP_SQ_NUM=B.SEQ_NUM"); parameters.Add(new DataParameter("upmaterial", upmaterial)); using (IDataSession session = AppDataFactory.CreateMainSession()) { var list= session.GetList(sql.ToString(), parameters.ToArray()).ToList(); return list.Select(p => p.MATERIAL_CODE).Distinct().ToList(); } } catch (Exception ex) { throw ex; } } /// /// 根据物料组查询BOM /// /// 物料组 /// public Pbom GetBomWithMaterials(params string[] materials) { if (materials.Length == 0) { throw new Exception("至少需要一种物料号才能确定BOM!"); } StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.AppendLine("SELECT * "); sql.AppendLine(" FROM T_MD_PBOM AS B "); sql.AppendLine(" WHERE EXISTS (SELECT PBOM_CODE,UP_SQ_NUM FROM T_MD_PBOM_ITEM AS W "); sql.AppendLine(" WHERE W.[MATERIAL_CODE] IN ("); for (int i = 0; i < materials.Length; i++) { if (i > 0) { sql.Append(","); } sql.Append(string.Format("@material{0}", i)); parameters.Add(new DataParameter(string.Format("@material{0}", i), materials[i])); } sql.AppendLine(") "); sql.AppendLine(" AND (SELECT COUNT(1) "); sql.AppendLine(" FROM T_MD_PBOM_ITEM AS I "); sql.AppendLine(" WHERE I.PBOM_CODE=W.PBOM_CODE "); sql.AppendLine(" AND I.UP_SQ_NUM=W.UP_SQ_NUM "); sql.AppendLine(" AND I.UNIT_SUM>0)=@materialcount "); sql.AppendLine(" AND W.PBOM_CODE=B.PBOM_CODE "); sql.AppendLine(" GROUP BY PBOM_CODE,UP_SQ_NUM HAVING COUNT(*)>=@materialcount)"); parameters.Add(new DataParameter("materialcount", materials.Length)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { throw ex; } } /// /// 根据物料类型和BOM编号查找BOM明细项 /// /// /// /// public PbomItem GetBomItemByMaterilType(string bomcode, string materialtype) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT TOP 1 P.* "); sql.Append(" FROM T_MD_PBOM_ITEM AS P "); sql.Append(" LEFT JOIN T_MD_MATERIAL AS M "); sql.Append(" ON P.MATERIAL_CODE=M.MATERIAL_CODE "); sql.Append(" WHERE P.PBOM_CODE=@bomcode "); sql.Append(" AND M.MATERIAL_TYPE_CODE=@materialtype "); parameters.Add(new DataParameter("bomcode", bomcode)); parameters.Add(new DataParameter("materialtype", materialtype)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { throw ex; } } /// /// 根据物料类型和BOM编号查找BOM明细项 /// /// /// /// public PbomItem GetBomItemByMaterilType(string bomcode, string materialtype,bool parenttype=false) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT TOP 1 P.* "); sql.Append(" FROM T_MD_PBOM_ITEM AS P "); sql.Append(" LEFT JOIN T_MD_MATERIAL AS M "); sql.Append(" ON P.MATERIAL_CODE=M.MATERIAL_CODE "); sql.Append(" WHERE P.PBOM_CODE=@bomcode "); if (parenttype) { sql.Append(" AND M.MATERIAL_TYPE_CODE LIKE @materialtype + '%' "); } else { sql.Append(" AND M.MATERIAL_TYPE_CODE=@materialtype "); } parameters.Add(new DataParameter("bomcode", bomcode)); parameters.Add(new DataParameter("materialtype", materialtype)); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { throw ex; } } #region 获取全部列表 /// /// 获取全部列表信息 /// /// /// public List GetPbomItemList(PbomItem model) { List parameters = new List(); var sql = this.GetQuerySqlPbom(model, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetList(sql, parameters.ToArray()).ToList(); } } #endregion #region 获取pbom查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySqlPbom(PbomItem condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" select t1.*,t2.MATERIAL_NAME from T_MD_PBOM_ITEM t1,T_MD_MATERIAL t2 "); sqlBuilder.AppendLine(" where t1.MATERIAL_CODE=t2.MATERIAL_CODE "); //查询条件 if (string.IsNullOrEmpty(condition.PBOM_CODE) == false) { whereBuilder.Append(" AND PBOM_CODE=@PBOM_CODE "); parameters.Add(new DataParameter { ParameterName = "PBOM_CODE", DataType = DbType.String, Value = condition.PBOM_CODE }); } //UP_SQ_NUM号 if (condition.UP_SQ_NUM >=0) { whereBuilder.Append(" AND UP_SQ_NUM = @UP_SQ_NUM "); parameters.Add(new DataParameter { ParameterName = "UP_SQ_NUM", DataType = DbType.String, Value = condition.UP_SQ_NUM }); } //查询条件 if (string.IsNullOrEmpty(condition.GMP) == false) { whereBuilder.Append(" AND GMP=@GMP "); parameters.Add(new DataParameter { ParameterName = "GMP", DataType = DbType.String, Value = condition.GMP }); } if (whereBuilder.Length > 0) { //sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); sqlBuilder.Append(whereBuilder); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 获取信息 /// /// 获取信息 /// /// 条件 /// *信息 public PbomItem GetPbomItemOne(PbomItem model) { try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取信息 model = session.Get(model); } return model; } catch (Exception ex) { throw ex; } } #endregion #region 获取SEQ_NUM最大号 /// /// 获取信息 /// /// 条件 /// *信息 public PbomItem GetMaxSeqNum(PbomItem model) { try { List parameters = new List(); var sql = " SELECT max(seq_num) as seq_num FROM T_MD_PBOM_ITEM where PBOM_CODE ='" + model .PBOM_CODE+ "' "; using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql, parameters.ToArray()); } } catch (Exception ex) { throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int InsertPbomItem(PbomItem model) { int count = 0; try { if (BaseSession != null) { //插入基本信息 count = BaseSession.Insert(model); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-插入信息"); throw ex; } } #endregion #region 更新信息 /// /// 更新信息 /// /// 信息 /// 插入行数 public int UpdatePbomItem(PbomItem model) { int count = 0; try { if (BaseSession != null) { //插入基本信息 count = BaseSession.Update(model); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Update(model); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-更新信息"); throw ex; } } #endregion #region 删除信息 /// /// 删除信息 /// /// 信息 /// 插入行数 public int DelPbomItem(string pid) { List parameters = new List(); int count = 0; try { var sql = "delete from T_MD_PBOM_ITEM WHERE PID = '" + pid + "'"; if (this.BaseSession != null) { //删除基本信息 count = BaseSession.ExecuteSql(sql, parameters.ToArray()); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { //删除基本信息 count = session.ExecuteSql(sql, parameters.ToArray()); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "Bom信息维护数据层-删除BOM详细信息"); throw ex; } } #endregion public PbomItem GetUPLevelItem(string bomcode, string submaterial) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT * "); sql.Append(" FROM T_MD_PBOM_ITEM "); sql.Append(" WHERE PBOM_CODE=@bomcode "); sql.Append(" AND SEQ_NUM=(SELECT UP_SQ_NUM "); sql.Append(" FROM T_MD_PBOM_ITEM AS I "); sql.Append(" WHERE I.PBOM_CODE=@bomcode "); sql.Append(" AND I.MATERIAL_CODE=@submaterial)"); parameters.Add(new DataParameter("bomcode", bomcode)); parameters.Add(new DataParameter("submaterial", submaterial)); using (IDataSession session = AppDataFactory.CreateMainSession()) { var bomitem= session.Get(sql.ToString(), parameters.ToArray()); return bomitem; } } catch (Exception ex) { throw ex; } } public List GetWorkingBom(string WorklocCode, string OrderID) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT B.*"); sql.Append(" ,M.MATERIAL_NAME"); sql.Append(" ,M.MATERIAL_TYPE_CODE"); sql.Append(" FROM T_MD_PBOM_ITEM AS B"); sql.Append(" LEFT JOIN T_MD_MATERIAL AS M"); sql.Append(" ON M.MATERIAL_CODE=B.MATERIAL_CODE"); sql.Append(" WHERE PBOM_CODE=(SELECT PBOM_CODE "); sql.Append(" FROM T_PP_WORKORDER "); sql.Append(" WHERE PID=@OrderID ) "); //sql.Append(" AND STATE='2')"); sql.Append(" AND GMP=@WorklocCode"); sql.Append(" ORDER BY B.MATERIAL_CODE"); parameters.Add(new DataParameter("WorklocCode", WorklocCode)); parameters.Add(new DataParameter("OrderID", OrderID)); using (IDataSession session = AppDataFactory.CreateMainSession()) { var bomitem = session.GetList(sql.ToString(), parameters.ToArray()); return bomitem.ToList(); } } catch (Exception ex) { throw ex; } } public List GetRelatedItems(string itemmaterial) { StringBuilder sql = new StringBuilder(); List parameters = new List(); try { sql.Append("SELECT * "); sql.Append(" FROM T_MD_PBOM_ITEM "); sql.Append(" WHERE PBOM_CODE IN (SELECT PBOM_CODE "); sql.Append(" FROM T_MD_PBOM_ITEM "); sql.Append(" WHERE MATERIAL_CODE=@itemmaterial) "); parameters.Add(new DataParameter("itemmaterial", itemmaterial)); using (IDataSession session = AppDataFactory.CreateMainSession()) { var bomitem = session.GetList(sql.ToString(), parameters.ToArray()); return bomitem.ToList(); } } catch (Exception ex) { throw ex; } } public WorkCell GetGMP(string materialcode) { WorkCell cell = new WorkCell(); StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT * "); sql.AppendLine(" FROM T_MD_WORKCELL "); sql.AppendLine(" WHERE WORKCELL_CODE = (SELECT TOP 1 GMP "); sql.AppendLine(" FROM T_MD_PBOM_ITEM "); sql.AppendLine(" WHERE MATERIAL_CODE=@materialcode)"); using (IDataSession session = AppDataFactory.CreateMainSession()) { cell = session.Get(sql.ToString(), new DataParameter("materialcode", materialcode)); } return cell; } } }