using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.MD.Entity; using QMFrameWork.Data; using System.Data; using QMAPP.DAL; using QMAPP.Entity; using QMAPP.Entity.Sys; using QMAPP.FJC.Entity.BZD; using QMAPP.FJC.DAL.BZD; namespace QMAPP.MD.DAL { /// /// 模块名称:BZD记录 /// 作 者:宁红钢 /// 编写日期:2020年04月29日 /// public class BZDRecorderDAL : BaseDAL { #region 获取信息 /// /// 获取信息 /// /// 条件 /// *信息 public BZDRecorder2 Get(BZDRecorder2 info) { try { using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //获取信息 info = session.Get(info); } return info; } catch (Exception ex) { throw ex; } } public BZDRecorder2 GetRecord(BZDRecorder2 info) { try { List parameters = new List(); var sql = "SELECT * FROM T_BD_BZDRecorder WHERE 1=1 "; if (string.IsNullOrEmpty(info.PID) == false) { sql += " AND PID = @PID"; parameters.Add(new DataParameter("PID", info.PID)); } if (string.IsNullOrEmpty(info.PRODUCTCODE) == false) { sql += " AND PRODUCTCODE = @PRODUCTCODE"; parameters.Add(new DataParameter("PRODUCTCODE", info.PRODUCTCODE)); } using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //获取信息 var list = session.GetList(sql, parameters.ToArray()).ToList(); if (list.Count > 0) return list[0]; else return null; } } catch (Exception ex) { throw ex; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(BZDRecorder2 condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql2(condition, ref parameters); //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) page.SortExpression = "CREATEDATE DESC"; using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { // 对应多种数据库 //string sqlChange = this.ChangeSqlByDB(sql, session); page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { throw ex; } } /// /// 获取全部规则 /// /// public List GetAllList() { try { string sql = "select R.pid,r.mpid,C.MATERIALCODDE,R.BZDCODE,R.ProductCode,R.Sequence,C.Company_name,C.Line_name,R.CREATEUSER,R.CREATEDATE from T_BD_BZDRecorder R,T_BD_BZDConfig c WHERE r.MPID = c.PID"; List parameters = new List(); using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { return session.GetList(sql, parameters.ToArray()).ToList(); } } catch (Exception ex) { throw ex; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(BZDRecorder condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句//[PID],[MPID],[barcodeFist],[Company_code],[Vehicle_type],[MATERIALCODDE],[configColor],[CREATEUSER],[CREATEDATE],[FLGDEL] sqlBuilder.Append("select * "); sqlBuilder.Append("FROM T_BD_BZDRecorder"); //whereBuilder.Append(" AND FLGDEL<> '1' "); //查询条件 //查询条件 //if (string.IsNullOrEmpty(condition.Company_name) == false) //{ // whereBuilder.Append(" AND C.Company_name = @Company_name "); // parameters.Add(new DataParameter { ParameterName = "Company_name", DataType = DbType.String, Value = condition.Company_name }); //} //if (string.IsNullOrEmpty(condition.Line_name) == false) //{ // whereBuilder.Append(" AND C.Line_name = @Line_name "); // parameters.Add(new DataParameter { ParameterName = "Line_name", DataType = DbType.String, Value = condition.Line_name }); //} //if (string.IsNullOrEmpty(condition.Company_code) == false) //{ // whereBuilder.Append(" AND C.Company_code = @Company_code "); // parameters.Add(new DataParameter { ParameterName = "Company_code", DataType = DbType.String, Value = condition.Company_code }); //} //if (string.IsNullOrEmpty(condition.Line_code) == false) //{ // whereBuilder.Append(" AND C.Line_code = @Line_code "); // parameters.Add(new DataParameter { ParameterName = "Line_code", DataType = DbType.String, Value = condition.Line_code }); //} if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql2(BZDRecorder2 condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句//[PID],[MPID],[barcodeFist],[Company_code],[Vehicle_type],[MATERIALCODDE],[configColor],[CREATEUSER],[CREATEDATE],[FLGDEL] sqlBuilder.Append("select R.pid as pid,r.mpid as mpid,C.MATERIALCODDE,R.BZDCODE,R.ProductCode,R.Sequence,C.Company_name,C.Line_name,R.CREATEUSER,R.CREATEDATE "); sqlBuilder.Append("FROM T_BD_BZDRecorder R,T_BD_BZDConfig c "); //whereBuilder.Append(" AND FLGDEL<> '1' "); whereBuilder.Append(" AND r.MPID = c.PID "); //查询条件 //查询条件 if (string.IsNullOrEmpty(condition.Company_name) == false) { whereBuilder.Append(" AND C.Company_name like @Company_name "); parameters.Add(new DataParameter { ParameterName = "Company_name", DataType = DbType.String, Value = "%" + condition.Company_name + "%" }); } if (string.IsNullOrEmpty(condition.Line_name) == false) { whereBuilder.Append(" AND C.Line_name like @Line_name "); parameters.Add(new DataParameter { ParameterName = "Line_name", DataType = DbType.String, Value = "%" + condition.Line_name + "%" }); } if (string.IsNullOrEmpty(condition.Company_code) == false) { whereBuilder.Append(" AND C.Company_code = @Company_code "); parameters.Add(new DataParameter { ParameterName = "Company_code", DataType = DbType.String, Value = condition.Company_code }); } if (string.IsNullOrEmpty(condition.Line_code) == false) { whereBuilder.Append(" AND C.Line_code = @Line_code "); parameters.Add(new DataParameter { ParameterName = "Line_code", DataType = DbType.String, Value = condition.Line_code }); } if (string.IsNullOrEmpty(condition.BZDCODE) == false) { whereBuilder.Append(" AND R.BZDCODE = @BZDCODE "); parameters.Add(new DataParameter { ParameterName = "BZDCODE", DataType = DbType.String, Value = condition.BZDCODE }); } if (string.IsNullOrEmpty(condition.PRODUCTCODE) == false) { whereBuilder.Append(" AND R.ProductCode = @PRODUCTCODE "); parameters.Add(new DataParameter { ParameterName = "PRODUCTCODE", DataType = DbType.String, Value = condition.PRODUCTCODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion //根据总成物料号获取BZD配置信息 public BZDConfig getBZDCofig(string planMATERIAL_CODE) { BZDConfigDAL bzdCofigdal = new BZDConfigDAL(); List bzdCofig = new List(); bzdCofig = bzdCofigdal.GetAllList(); //将当前总成号配置取出 foreach (var bzdconfig in bzdCofig) { if (bzdconfig != null) if (bzdconfig.MATERIALCODDE == planMATERIAL_CODE) { return bzdconfig; } } return null; } public List CreateBZDCode(List paramList, string userId) { try { List list = new List(); BZDConfig bzdconfig = getBZDCofig(paramList[0]); ; //将配置信息读出 var barcodeFirst = bzdconfig.barcodeFist; var cc = bzdconfig.Company_code; var vt = bzdconfig.Vehicle_type; var cfgc = bzdconfig.configColor; var y = DateTime.Today.Year.ToString().Substring(2, 2); var m = DateTime.Today.Month.ToString(); //将配置信息拼接成BZD查询条件,作为查找序列最大编码条件 string BZDCODE = barcodeFirst + ' ' + cc + vt + cfgc + DecimalToCharX.decimalToCharX(Convert.ToInt16(y), 36) + DecimalToCharX.decimalToCharX(Convert.ToInt16(m), 36); int seq = 0; //查询最大编码 seq = getMaxSeq(BZDCODE); //最大编码加1 //seq = DecimalToCharX.CharXTodecimal(BZDMaxSeq, 36) + 1; //准备打印条码数据 BZDRecorder entity = new BZDRecorder(); //---------------------------------------------------------------------------------------- BarcodeRule bc = new BarcodeRule(); bc.barcodeFirst = barcodeFirst; bc.barcodeCompnentCode = cc; bc.VehicleType = vt; bc.ConfigColor = cfgc; bc.Year = DecimalToCharX.decimalToCharX(Convert.ToInt16(y), 36); bc.Mon = DecimalToCharX.decimalToCharX(Convert.ToInt16(m), 36); bc.barcodeSequence = DecimalToCharX.decimalToCharX(seq, 36); //---------------------------------------------------------------------------------------- list.Add(bc.barcode); list.Add(bzdconfig.ColorDetail); list.Add(bzdconfig.MATERIALCODDE); //基本信息 entity.PID = Guid.NewGuid().ToString(); entity.CREATEUSER = userId; entity.CREATEDATE = DateTime.Now; entity.MPID = bzdconfig.PID; entity.BZDCODE = bc.barcode; entity.Sequence = seq.ToString(); entity.PRODUCTCODE = paramList[1]; if (!Exists(entity)) { //将打印条码记录到数据库 var rst = Insert(entity); list.Add(rst.ToString()); return list; } else { return null; } } catch (Exception e) { return null; } } #region 查询最大序列号 public int getMaxSeq(string BZDCODE) { //string PID = ""; int count = 0; string sql = null; try { //sql = "GetBZDSeq"; sql = @"DECLARE @return_value int, @Seq int EXEC @return_value = [dbo].[GetBZDSeq] @KeyValue = N'" + BZDCODE + @"', @Seq = @Seq OUTPUT SELECT @Seq as N'@Seq'"; List parameters = new List(); using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { string sqlChange = ChangeSqlByDB(sql, session); var dt = session.GetTable(sqlChange, parameters.ToArray()); return Convert.ToInt16(dt.Rows[0].ItemArray[0]); } } catch (Exception ex) { RecordExceptionLog(ex, "查询最大序列号错误!"); throw ex; } } public string getMaxSeq_old(string BZDCODE) { //string PID = ""; int count = 0; string sql = null; try { sql = "select ISNULL(MAX(substring(BZDCODE,12,3)),'000') as SEQUENCE from T_BD_BZDRecorder where substring(BZDCODE,1,11) = @BZDCODE"; List parameters = new List(); parameters.Add(new DataParameter { ParameterName = "BZDCODE", DataType = DbType.String, Value = BZDCODE }); using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { string sqlChange = ChangeSqlByDB(sql, session); var dt = session.GetTable(sqlChange, parameters.ToArray()); return dt.Rows[0]["SEQUENCE"].ToString(); } } catch (Exception ex) { RecordExceptionLog(ex, "查询最大序列号错误!"); throw ex; } return "1"; } #endregion #region 获取导出的数据 /// /// 获取导出的数据 /// /// 查询条件 /// 数据 public DataTable GetExportData(BZDRecorder2 info) { DataTable dt = null; string sql = null; List parameters = new List(); try { //构成查询语句 sql = this.GetQuerySql2(info, ref parameters); using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { dt = session.GetTable(sql, parameters.ToArray()); dt.TableName = "BZDRecorder"; } return dt; } catch (Exception ex) { throw ex; } } #endregion #region 信息是否重复 /// /// 判断名称是否存在 /// /// /// true:已存在;fasel:不存在。 public bool Exists(BZDRecorder info) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { sqlBuilder.Append("SELECT COUNT(0) FROM T_BD_BZDRecorder"); if (info.PID == null) { info.PID = ""; } whereBuilder.Append(" AND BZDCODE = @BZDCODE "); parameters.Add(new DataParameter { ParameterName = "BZDCODE", DataType = DbType.String, Value = info.BZDCODE }); //添加进行无重复字段判断代码 if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray())); } return count > 0; } catch (Exception ex) { throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int Insert(BZDRecorder info) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //插入基本信息 count = session.Insert(info); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 更新信息 /// /// 更新信息 /// /// /// 更新行数 public int Update(BZDRecorder info) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //更新基本信息 count = session.Update(info); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 逻辑删除 /// /// 逻辑删除信息 /// /// /// 删除个数 public int Delete(BZDRecorder info) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //删除基本信息 sqlBuilder.Append("UPDATE T_BD_BZDRecorder "); sqlBuilder.Append("SET FLGDEL = '1' "); sqlBuilder.Append("WHERE PID = @PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = info.PID }); count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray()); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 导入 public ImportMessage GetImportData(List list) { ImportMessage em = new ImportMessage(); List parameters = new List(); try { using (IDataSession session = AppDataFactory.CreateSession("maindbBZD")) { //设置祖先对象数据会话 session.OpenTs(); foreach (BZDRecorder info in list) { if (info.IsNewInfo) { //插入信息 int count = session.Insert(info); em.insertNum++; } else { //更新信息 int count = session.Update(info); em.updateNum++; } } session.CommitTs(); } } catch (Exception ex) { throw ex; } return em; } #endregion } }