using IBatisNet.DataMapper; using SSWMS.Common; using System; using System.Collections.Generic; using System.Data; using System.Threading; namespace SSWMS.Server { public class P_Base { protected ISqlMapper _sqlMap = null; protected Mutex _mutex = null; private string TypeName = string.Empty; public P_Base() { TypeName = typeof(T_Model).Name; } private IDbCommand GetCommand(string strSQL) { IDbCommand cmd = this._sqlMap.LocalSession.CreateCommand(CommandType.Text); cmd.CommandText = strSQL; cmd.CommandTimeout = 2000; return cmd; } public int ExecuteNonQuery(string strSQL) { int iResult = 0; bool bConnection = false; try { if (this._sqlMap.LocalSession == null) { this._sqlMap.OpenConnection(); this._sqlMap.LocalSession.OpenConnection(); bConnection = true; } iResult = this.GetCommand(strSQL).ExecuteNonQuery(); } catch (Exception ex) { MainWindow.log.Error($"{strSQL} {ex.ToString()}"); iResult = 0; } if (bConnection) { this._sqlMap.LocalSession.CloseConnection(); this._sqlMap.CloseConnection(); } return iResult; } public DataTable GetDataTable(string strSQL) { DataTable dt = null; bool bConnection = false; try { if (this._sqlMap.LocalSession == null) { this._sqlMap.OpenConnection(); this._sqlMap.LocalSession.OpenConnection(); bConnection = true; } DataSet ds = new DataSet(); this._sqlMap.LocalSession.CreateDataAdapter(this.GetCommand(strSQL)).Fill(ds); dt = ds.Tables[0]; } catch (Exception ex) { MainWindow.log.Error($"{strSQL} {ex.ToString()}"); dt = new DataTable(); } if (bConnection) { this._sqlMap.LocalSession.CloseConnection(); this._sqlMap.CloseConnection(); } return dt; } public int UpdateDataTable(DataTable dt, string sTableName, out string sResult) { sResult = string.Empty; int iResult = 0; bool bConnection = false; try { if (this._sqlMap.LocalSession == null) { this._sqlMap.OpenConnection(); this._sqlMap.LocalSession.OpenConnection(); bConnection = true; } DataSet ds = new DataSet(); if (AppSettings.IsOracleDatabase) { foreach (DataRow dr in dt.Rows) { if (dr.RowState == DataRowState.Added) { dr[0] = Convert.ToInt32(this.GetDataTable(string.Format( "select {0}_SEQ.nextval from DUAL", sTableName)).Rows[0][0]); } } } ds.Tables.Add(dt); IDbDataAdapter adapter = this._sqlMap.LocalSession.CreateDataAdapter( this.GetCommand(string.Format("select * from {0}", sTableName))); this._sqlMap.DataSource.DbProvider.CommandBuilderType.GetConstructor( new Type[] { adapter.GetType() }).Invoke(new object[] { adapter }); iResult = adapter.Update(ds); } catch (Exception ex) { MainWindow.log.Error($"{sTableName} {ex.ToString()}"); if (ex.Message.Contains("IX_GOODS_MAIN_GOODS_CODE")) { sResult = $"物料编码 {StringUtils.GetParenthesesValue(ex.Message)} 已存在"; } else if (ex.Message.Contains("IX_SYS_USER_USER_CODE")) { sResult = $"用户编码 {StringUtils.GetParenthesesValue(ex.Message)} 已存在"; } else if (ex.Message.Contains("IX_WH_CELL_CELL_CODE")) { sResult = $"货位编码 {StringUtils.GetParenthesesValue(ex.Message)} 已存在"; } else if (ex.Message.Contains("IX_STORAGE_MAIN_STOCK_BARCODE")) { sResult = $"托盘条码 {StringUtils.GetParenthesesValue(ex.Message)} 存在库存"; } else if (ex.Message.Contains("IX_MANAGE_MAIN_STOCK_BARCODE")) { sResult = $"托盘条码 {StringUtils.GetParenthesesValue(ex.Message)} 存在任务"; } else { sResult = ex.Message; } iResult = 0; } if (bConnection) { this._sqlMap.LocalSession.CloseConnection(); this._sqlMap.CloseConnection(); } return iResult; } public void BeginTransaction() { this._mutex.WaitOne(); this._sqlMap.BeginTransaction(); } public void CommitTransaction() { this._sqlMap.CommitTransaction(); this._mutex.ReleaseMutex(); } public void RollBackTransaction() { this._sqlMap.RollBackTransaction(); this._mutex.ReleaseMutex(); } public T_Model GetModel(int iID) { return this._sqlMap.QueryForObject(string.Format("{0}_SELECT_BY_ID", this.TypeName), iID); } public T_Model GetModel(string sql) { return this._sqlMap.QueryForObject(string.Format("{0}_SELECT_BY_SQL", this.TypeName), sql); } public IList GetList() { return this._sqlMap.QueryForList(string.Format("{0}_SELECT", this.TypeName), null); } public int Add(T_Model model) { return this._sqlMap.Insert(string.Format("{0}_INSERT_{1}", this.TypeName, AppSettings.IsOracleDatabase ? "ORACLE" : "SQLSERVER"), model) == null ? 0 : 1; } public int Update(T_Model model) { return this._sqlMap.Update(string.Format("{0}_UPDATE", this.TypeName), model); } public int Delete(int iID) { return this._sqlMap.Delete(string.Format("{0}_DELETE", this.TypeName), iID); } //public T GetModel(int iID) //{ // return this._sqlMap.QueryForObject(string.Format("{0}_SELECT_BY_ID", typeof(T).Name), iID); //} //public IList GetList() //{ // return this._sqlMap.QueryForList(string.Format("{0}_SELECT", typeof(T).Name), null); //} //public int Add(T model) //{ // return this._sqlMap.Insert(string.Format("{0}_INSERT_{1}", typeof(T).Name, // this.IsOracle ? "ORACLE" : "SQLSERVER"), model) == null ? 0 : 1; //} //public int Update(T model) //{ // return this._sqlMap.Update(string.Format("{0}_UPDATE", typeof(T).Name), model); //} //public int Delete(int iID) //{ // return this._sqlMap.Delete(string.Format("{0}_DELETE", typeof(T).Name), iID); //} } }