using System;
|
using System.Data.OracleClient;
|
using System.Runtime.CompilerServices;
|
using NLog;
|
|
namespace CCSTrace.CCS.Function
|
{
|
public class InitialEventData
|
{
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
private string _acceptNum = "";
|
private int _caseId = 0;
|
private int _mYear = 0;
|
private bool _isChangeYear = false;
|
private string _nowYear = string.Empty;
|
private readonly string _acceptTime = "";
|
private readonly OracleConnection _connectionTpc;
|
|
//New CaseID and New AcceptNum must be seen by others,so it need not controled by transaction
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
public InitialEventData(OracleConnection conn)
|
{
|
_connectionTpc = conn;
|
|
var sqlStmt = "SELECT ACCEPTNUM,CASEID,YEAR,TO_CHAR(SYSDATE,'YYYY/MM/DD') as ACCEPTTIME FROM EOS.EVENTNUM";
|
|
OracleCommand command = new OracleCommand(sqlStmt, _connectionTpc);
|
OracleDataReader reader = command.ExecuteReader();
|
|
try
|
{
|
if (reader.Read())
|
{
|
_acceptNum = reader["ACCEPTNUM"].ToString();
|
_caseId = Convert.ToInt32(reader["CASEID"].ToString());
|
_mYear = Convert.ToInt32(reader["YEAR"].ToString());
|
_acceptTime = reader["ACCEPTTIME"].ToString();
|
}
|
else
|
{
|
Logger.Error("起始號碼資料遺失!");
|
return;
|
}
|
}
|
catch (Exception e)
|
{
|
Logger.Error(e, "Problems occur when Initializing EventData: " + e.Message);
|
return;
|
}
|
finally
|
{
|
reader.Close();
|
command.Dispose();
|
}
|
|
CheckAcceptNum();
|
|
while (!UpdateDataBase(_isChangeYear))
|
{
|
RegetNumber();
|
CheckAcceptNum();
|
}
|
}
|
|
public int GetNewCaseId()
|
{
|
return _caseId;
|
}
|
|
public string GetNewAcceptNum()
|
{
|
return _acceptNum;
|
}
|
|
private bool UpdateDataBase(bool mChangeYear)
|
{
|
string sqlStmt;
|
bool mResult = false;
|
|
if (mChangeYear)
|
sqlStmt = "UPDATE EOS.EVENTNUM SET ACCEPTNUM = '" + (Convert.ToInt32(_acceptNum) + 1) + "',CASEID = "
|
+ (_caseId + 1) + ",YEAR = " + _nowYear + " WHERE CASEID = " + _caseId;
|
else
|
sqlStmt = "UPDATE EOS.EVENTNUM SET ACCEPTNUM = '" + (Convert.ToInt32(_acceptNum) + 1) + "',CASEID = "
|
+ (_caseId + 1) + " WHERE CASEID = " + _caseId + " AND ACCEPTNUM = '" + _acceptNum + "'";
|
|
OracleCommand command = new OracleCommand(sqlStmt, _connectionTpc);
|
|
try
|
{
|
if (command.ExecuteNonQuery() > 0)
|
mResult = true;
|
}
|
catch (Exception e)
|
{
|
Logger.Error(e, "Problems occur during EventNumCreating: " + e.Message);
|
throw;
|
}
|
finally
|
{
|
command.Dispose();
|
}
|
|
return mResult;
|
}
|
|
private void CheckAcceptNum()
|
{
|
int nowYear = Convert.ToInt32(_acceptTime.Substring(0, 4)) - 1911;
|
int nowMonth = Convert.ToInt32(_acceptTime.Substring(5, 2));
|
string tmp = _acceptNum;
|
|
if (Convert.ToInt32(tmp.Substring(0, 3)) == nowYear)
|
{
|
if (Convert.ToInt32(tmp.Substring(3, 2)) != nowMonth)
|
{
|
// 新月份
|
if (nowMonth < 10)
|
_acceptNum = nowYear + "0" + nowMonth + "0001";
|
else
|
_acceptNum = nowYear + nowMonth.ToString() + "0001";
|
}
|
}
|
else
|
{
|
_acceptNum = nowYear + "010001"; // 新年度
|
_nowYear = nowYear.ToString();
|
_isChangeYear = true;
|
}
|
|
if (!_isChangeYear)
|
{
|
}
|
}
|
|
private void RegetNumber()
|
{
|
var sqlStmt = "SELECT ACCEPTNUM,CASEID,YEAR FROM EOS.EVENTNUM";
|
OracleCommand command = new OracleCommand(sqlStmt, _connectionTpc);
|
OracleDataReader reader = command.ExecuteReader();
|
|
try
|
{
|
if (reader.Read())
|
{
|
_acceptNum = reader["ACCEPTNUM"].ToString();
|
_caseId = Convert.ToInt32(reader["CASEID"].ToString());
|
_mYear = Convert.ToInt32(reader["YEAR"].ToString());
|
}
|
}
|
catch (Exception e)
|
{
|
Logger.Error(e, e.Message);
|
}
|
finally
|
{
|
reader.Close();
|
command.Dispose();
|
}
|
}
|
}
|
}
|