using System;
|
using System.Data.OracleClient;
|
using System.Runtime.CompilerServices;
|
|
namespace CCSTrace.CCS.Function
|
{
|
public class InitialEventData
|
{
|
private string _mAcceptNum = "";
|
private int _mCaseId = 0;
|
private int _mYear = 0;
|
private bool _mChangeYear = false;
|
private string _mNowYear = string.Empty;
|
private readonly string _mAcceptTime = "";
|
private readonly RecordLog _pLog;
|
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, RecordLog log)
|
{
|
_connectionTpc = conn;
|
_pLog = log;
|
|
string 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())
|
{
|
_mAcceptNum = reader["ACCEPTNUM"].ToString();
|
_mCaseId = Convert.ToInt32(reader["CASEID"].ToString());
|
_mYear = Convert.ToInt32(reader["YEAR"].ToString());
|
_mAcceptTime = reader["ACCEPTTIME"].ToString();
|
}
|
else
|
{
|
_pLog.Error("起始號碼資料遺失!");
|
return;
|
}
|
}
|
catch (Exception e)
|
{
|
_pLog.Error("Problems occur when Initializing EventData: " + e.Message);
|
Console.WriteLine(e.StackTrace);
|
return;
|
}
|
finally
|
{
|
reader.Close();
|
command.Dispose();
|
}
|
|
CheckAcceptNum();
|
|
while (!UpdateDataBase(_mChangeYear))
|
{
|
RegetNumber();
|
CheckAcceptNum();
|
}
|
}
|
|
public int GetNewCaseId()
|
{
|
return _mCaseId;
|
}
|
|
public string GetNewAcceptNum()
|
{
|
return _mAcceptNum;
|
}
|
|
private bool UpdateDataBase(bool mChangeYear)
|
{
|
string sqlStmt;
|
bool mResult = false;
|
|
if (mChangeYear)
|
sqlStmt = "UPDATE EOS.EVENTNUM SET ACCEPTNUM = '" + (Convert.ToInt32(_mAcceptNum) + 1).ToString() + "',CASEID = "
|
+ (_mCaseId + 1) + ",YEAR = " + _mNowYear + " WHERE CASEID = " + _mCaseId;
|
else
|
sqlStmt = "UPDATE EOS.EVENTNUM SET ACCEPTNUM = '" + (Convert.ToInt32(_mAcceptNum) + 1).ToString() + "',CASEID = "
|
+ (_mCaseId + 1) + " WHERE CASEID = " + _mCaseId + " AND ACCEPTNUM = '" + _mAcceptNum + "'";
|
|
OracleCommand command = new OracleCommand(sqlStmt, _connectionTpc);
|
|
try
|
{
|
if (command.ExecuteNonQuery() > 0)
|
mResult = true;
|
}
|
catch (Exception e)
|
{
|
_pLog.Error("Problems occur during EventNumCreating: " + e.Message);
|
Console.WriteLine(e.StackTrace);
|
throw e;
|
}
|
finally
|
{
|
command.Dispose();
|
}
|
|
return mResult;
|
}
|
|
private void CheckAcceptNum()
|
{
|
int nowYear = Convert.ToInt32(_mAcceptTime.Substring(0, 4)) - 1911;
|
int nowMonth = Convert.ToInt32(_mAcceptTime.Substring(5, 2));
|
string tmp = _mAcceptNum;
|
|
if (Convert.ToInt32(tmp.Substring(0, 3)) == nowYear)
|
{
|
if (Convert.ToInt32(tmp.Substring(3, 2)) != nowMonth)
|
{
|
// 新月份
|
if (nowMonth < 10)
|
_mAcceptNum = nowYear.ToString() + "0" + nowMonth.ToString() + "0001";
|
else
|
_mAcceptNum = nowYear.ToString() + nowMonth.ToString() + "0001";
|
}
|
}
|
else
|
{
|
_mAcceptNum = nowYear.ToString() + "010001"; // 新年度
|
_mNowYear = nowYear.ToString();
|
_mChangeYear = true;
|
}
|
|
if (!_mChangeYear)
|
{
|
}
|
}
|
|
private void RegetNumber()
|
{
|
string sqlStmt = "SELECT ACCEPTNUM,CASEID,YEAR FROM EOS.EVENTNUM";
|
OracleCommand command = new OracleCommand(sqlStmt, _connectionTpc);
|
OracleDataReader reader = command.ExecuteReader();
|
|
try
|
{
|
if (reader.Read())
|
{
|
_mAcceptNum = reader["ACCEPTNUM"].ToString();
|
_mCaseId = Convert.ToInt32(reader["CASEID"].ToString());
|
_mYear = Convert.ToInt32(reader["YEAR"].ToString());
|
}
|
}
|
catch (Exception e)
|
{
|
Console.WriteLine(e.Message);
|
Console.WriteLine(e.StackTrace);
|
}
|
finally
|
{
|
reader.Close();
|
command.Dispose();
|
}
|
}
|
}
|
}
|