巨石化纤
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.7 KiB

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<T_Model>
{
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<T_Model>(string.Format("{0}_SELECT_BY_ID", this.TypeName), iID);
}
public T_Model GetModel(string sql)
{
return this._sqlMap.QueryForObject<T_Model>(string.Format("{0}_SELECT_BY_SQL", this.TypeName), sql);
}
public IList<T_Model> GetList()
{
return this._sqlMap.QueryForList<T_Model>(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<T>(int iID)
//{
// return this._sqlMap.QueryForObject<T>(string.Format("{0}_SELECT_BY_ID", typeof(T).Name), iID);
//}
//public IList<T> GetList<T>()
//{
// return this._sqlMap.QueryForList<T>(string.Format("{0}_SELECT", typeof(T).Name), null);
//}
//public int Add<T>(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>(T model)
//{
// return this._sqlMap.Update(string.Format("{0}_UPDATE", typeof(T).Name), model);
//}
//public int Delete<T>(int iID)
//{
// return this._sqlMap.Delete(string.Format("{0}_DELETE", typeof(T).Name), iID);
//}
}
}