📄 enguardroleunit.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "EnguardRoleUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "BaseInputUnit"
#pragma resource "*.dfm"
TEnguardRoleFrm *EnguardRoleFrm;
//---------------------------------------------------------------------------
__fastcall TEnguardRoleFrm::TEnguardRoleFrm(TComponent* Owner)
: TBaseDataInputFrm(Owner)
{
}
//---------------------------------------------------------------------------
bool TEnguardRoleFrm::GetRoleInfo(EnguardRole_T& iRole)
{
iRole = EnguardRole;
return !IsCanceled();
}
//---------------------------------------------------------------------------
bool TEnguardRoleFrm::SetRoleInfo(EnguardRole_T iRole)
{
EnguardRole = iRole;
return true;
}
//---------------------------------------------------------------------------
bool TEnguardRoleFrm::GetRoleInfoFromUI()
{
DateTimeToSystemTime(dtpStartTime1->DateTime,EnguardRole.BeginTime1);
DateTimeToSystemTime(dtpEndTime1->DateTime,EnguardRole.EndTime1);
DateTimeToSystemTime(dtpStartTime2->DateTime,EnguardRole.BeginTime2);
DateTimeToSystemTime(dtpEndTime2->DateTime,EnguardRole.EndTime2);
for(int i=0;i<7;i++)
{
if (clbWeek->Checked[i])
EnguardRole.Weekend[i] = 0x01;
else
EnguardRole.Weekend[i] = 0x00;
}
return true;
}
//---------------------------------------------------------------------------
bool TEnguardRoleFrm::SetRoleInfoToUI()
{
TDateTime lBaseTime = Now();
unsigned short lYear,lMonth,lDay;
lBaseTime.DecodeDate(&lYear,&lMonth,&lDay);
EnguardRole.BeginTime1.wYear = lYear;
EnguardRole.BeginTime1.wMonth = lMonth;
EnguardRole.BeginTime1.wDay = lDay;
EnguardRole.BeginTime1.wSecond = 0;
EnguardRole.BeginTime1.wDayOfWeek = GetWeekDays(lYear,lMonth,lDay);
EnguardRole.BeginTime1.wMilliseconds = 0;
try
{
dtpStartTime1->DateTime = SystemTimeToDateTime(EnguardRole.BeginTime1);
}
catch(...)
{
return false;
}
EnguardRole.EndTime1.wYear = lYear;
EnguardRole.EndTime1.wMonth = lMonth;
EnguardRole.EndTime1.wDay = lDay;
EnguardRole.EndTime1.wSecond = 0;
EnguardRole.EndTime1.wDayOfWeek = GetWeekDays(lYear,lMonth,lDay);
EnguardRole.EndTime1.wMilliseconds = 0;
try
{
dtpEndTime1->DateTime = SystemTimeToDateTime(EnguardRole.EndTime1);
}
catch(...)
{
return false;
}
EnguardRole.BeginTime2.wYear = lYear;
EnguardRole.BeginTime2.wMonth = lMonth;
EnguardRole.BeginTime2.wDay = lDay;
EnguardRole.BeginTime2.wSecond = 0;
EnguardRole.BeginTime2.wMilliseconds = 0;
EnguardRole.BeginTime2.wDayOfWeek = GetWeekDays(lYear,lMonth,lDay);
try
{
dtpStartTime2->DateTime = SystemTimeToDateTime(EnguardRole.BeginTime2);
}
catch(...)
{
return false;
}
EnguardRole.EndTime2.wYear = lYear;
EnguardRole.EndTime2.wMonth = lMonth;
EnguardRole.EndTime2.wDay = lDay;
EnguardRole.EndTime2.wSecond = 0;
EnguardRole.EndTime2.wMilliseconds = 0;
EnguardRole.EndTime2.wDayOfWeek = GetWeekDays(lYear,lMonth,lDay);
try
{
dtpEndTime2->DateTime = SystemTimeToDateTime(EnguardRole.EndTime2);
}
catch(...)
{
return false;
}
for(int i=0;i<7;i++)
{
if (EnguardRole.Weekend[i] == 0x01)
clbWeek->Checked[i] = true;
else
clbWeek->Checked[i] = false;
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::FormCreate(TObject *Sender)
{
pDBAccess = new TDBAccess();
int lRV = pDBAccess->ReadRoleData(&EnguardRole);
if (lRV != OK || EnguardRole.RoleType != rtNoLimit)
{
NewYearRoleData();
}
TabSheetList[0] = tsMonth_1;
TabSheetList[1] = tsMonth_2;
TabSheetList[2] = tsMonth_3;
TabSheetList[3] = tsMonth_4;
TabSheetList[4] = tsMonth_5;
TabSheetList[5] = tsMonth_6;
TabSheetList[6] = tsMonth_7;
TabSheetList[7] = tsMonth_8;
TabSheetList[8] = tsMonth_9;
TabSheetList[9] = tsMonth_10;
TabSheetList[10] = tsMonth_11;
TabSheetList[11] = tsMonth_12;
for(int i=0;i<12;i++)
{
TabSheetList[i]->Tag = i;
TabSheetList[i]->PageIndex = i%6;
TabSheetList[i]->OnShow = OnTabSheetShow;
}
clbWeek->Checked[5] = true;
clbWeek->Checked[6] = true;
for(int i = 0;i < this->ComponentCount;i++)
{
//批命名日期按键
if(this->Components[i]->ClassNameIs("TSpeedButton"))
{
TSpeedButton* pBtn = (TSpeedButton*)this->Components[i];
//日期按键是"spbnDay_"开头+ID命名
int lMyNameStrIndex = pBtn->Name.Pos("spbnDay_");
if (lMyNameStrIndex != 0)
{
pBtn->OnClick = OnSpeedButtonClick;
pBtn->Cursor = crHandPoint;
pBtn->ShowHint = true;
pBtn->Hint = "";
int lIndex = pBtn->Tag;
ButtonDayList[lIndex-1] = pBtn;
pBtn->Font->Color = clBlue;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::FormDestroy(TObject *Sender)
{
delete pDBAccess;
}
//---------------------------------------------------------------------------
bool TEnguardRoleFrm::ShowRole()
{
for(int i=0;i<12;i++)
{
TabSheetList[i]->Caption = AnsiString(EnguardRole.Holiday[i].Year)+"年" + EnguardRole.Holiday[i].Month+"月";
}
pcHalfYear1->TabIndex = 0;
pcHalfYear2->TabIndex = 0;
return true;
}
//---------------------------------------------------------------------------
void TEnguardRoleFrm::SetVisibleTab(int iTabIndex)
{
for(int i=0;i<pcRole->PageCount;i++)
{
pcRole->Pages[i]->TabVisible = iTabIndex == i;
}
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::OnTabSheetShow(TObject *Sender)
{
//SheetShow日历Show事件
TTabSheet* pTabSheet = (TTabSheet*)Sender;
int lIndexMonth = pTabSheet->Tag; //lIndexMonth[0-11]
int lCurYear = EnguardRole.Holiday[lIndexMonth].Year;
int lCurMonth = EnguardRole.Holiday[lIndexMonth].Month;
int lDayCount = GetMonthDays(lCurYear,lCurMonth);
int lPanelIndex = 0;
int lWeekDay;
if (lIndexMonth<6)
{
plMonth1->Parent = pTabSheet;
lPanelIndex = 0;
}
else
{
plMonth2->Parent = pTabSheet;
lPanelIndex = 1;
}
ClearHalfYearCapton(lPanelIndex);
for(int i=0;i<lDayCount;i++)
{
if (EnguardRole.Holiday[lIndexMonth].Days[i] == 0x00)
{
ButtonDayList[lPanelIndex*31+i]->Font->Color = clBlue;
ButtonDayList[lPanelIndex*31+i]->Font->Style = TFontStyles()>> fsBold>>fsUnderline;
}
else
{
ButtonDayList[lPanelIndex*31+i]->Font->Color = clRed;
ButtonDayList[lPanelIndex*31+i]->Font->Style = TFontStyles()<< fsBold<<fsUnderline;
}
lWeekDay = GetWeekDays(lCurYear,lCurMonth,i+1);
ButtonDayList[lPanelIndex*31+i]->ShowHint = true;
ButtonDayList[lPanelIndex*31+i]->Caption = AnsiString(i+1)+"号";
ButtonDayList[lPanelIndex*31+i]->Hint = "星期" + lWeekDay;
}
}
//---------------------------------------------------------------------------
void TEnguardRoleFrm::ClearHalfYearCapton(int iIndex)
{
for(int i=0;i<31;i++)
{
ButtonDayList[iIndex*31+i]->Caption = "";
}
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::OnSpeedButtonClick(TObject *Sender)
{
TSpeedButton* pBtn = (TSpeedButton*)Sender;
int lMontIndex = 0;
for(int i=0;i<12;i++)
{
if (TabSheetList[i] == (TTabSheet*)pBtn->Parent->Parent->Parent)
{
lMontIndex = i;
break;
}
}
int lDayIndex;
if (lMontIndex >=6)
lDayIndex = pBtn->Tag - 32;
else
lDayIndex = pBtn->Tag-1;
if (pBtn->Font->Color == clBlue)
{
pBtn->Font->Color = clRed;
pBtn->Font->Style = TFontStyles()<< fsBold<<fsUnderline;
EnguardRole.Holiday[lMontIndex].Days[lDayIndex] = 0x01;
}
else if(pBtn->Font->Color == clRed)
{
pBtn->Font->Color = clBlue;
pBtn->Font->Style = TFontStyles()>> fsBold>>fsUnderline;
pBtn->Flat = true;
EnguardRole.Holiday[lMontIndex].Days[lDayIndex] = 0x00;
}
}
//---------------------------------------------------------------------------
int TEnguardRoleFrm::GetMonthDays(int iYear,int iMonth)
{
int lDays;
switch(iMonth)
{
case 4:
case 6:
case 9:
case 11: lDays = 30;break;
case 2:
if((iYear%4 == 0 && iYear%100!=0)||iYear%400 == 0)
lDays = 29;
else
lDays = 28;
break;
default:
lDays = 31;
}
return lDays;
}
//---------------------------------------------------------------------------
int TEnguardRoleFrm::GetWeekDays(int iYear,int iMonth,int iDay)
{
//iMonth[0-11]
int lDayIndex = 0;
for(int i=0;i<iMonth-1;i++)
lDayIndex = lDayIndex + GetMonthDays(iYear,iMonth);
lDayIndex = lDayIndex + (iYear)*365 + (iYear)/4 + (iYear)/100 + (iYear)/400 + iDay;
return lDayIndex%7;
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::FormShow(TObject *Sender)
{
rgRoleType->ItemIndex = 0;
SetVisibleTab(0);
ShowRole();
plMonth1->Parent = TabSheetList[0];
plMonth2->Parent = TabSheetList[6];
pcHalfYear1->TabIndex = 0;
pcHalfYear2->TabIndex = 0;
SetRoleInfoToUI();
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::rgRoleTypeClick(TObject *Sender)
{
SetVisibleTab(rgRoleType->ItemIndex);
}
//---------------------------------------------------------------------------
int TEnguardRoleFrm::SaveRoleData()
{
int lRV;
if (pDBAccess->IsExistRoleData())
{
lRV = pDBAccess->UpdateRoleData(EnguardRole);
if (lRV != OK)
{
Application->MessageBoxA("保存门禁规则数据失败!","提示信息",MB_OK + MB_ICONERROR);
}
}
else
{
lRV = pDBAccess->InsertRoleData(EnguardRole);
if (lRV != OK)
{
Application->MessageBoxA("保存门禁规则数据失败!","提示信息",MB_OK + MB_ICONERROR);
}
}
return lRV;
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::bbtnOKClick(TObject *Sender)
{
TBaseDataInputFrm::bbtnOKClick(Sender);
GetRoleInfoFromUI();
if (SaveRoleData() == OK)
Close();
}
//---------------------------------------------------------------------------
void TEnguardRoleFrm::NewYearRoleData()
{
unsigned short lYear,lMonth,lDay;
TDateTime lCurTime = Now();
lCurTime.DecodeDate(&lYear,&lMonth,&lDay);
EnguardRole.RoleType = rtNoLimit;
DateTimeToSystemTime(lCurTime,EnguardRole.BeginTime1);
EnguardRole.BeginTime1.wHour = 8;
EnguardRole.BeginTime1.wMinute = 0;
EnguardRole.BeginTime1.wSecond = 0;
DateTimeToSystemTime(lCurTime,EnguardRole.EndTime1);
EnguardRole.EndTime1.wHour = 17;
EnguardRole.EndTime1.wMinute = 0;
EnguardRole.EndTime1.wSecond = 0;
DateTimeToSystemTime(lCurTime,EnguardRole.BeginTime2);
EnguardRole.BeginTime2.wHour = 8;
EnguardRole.BeginTime2.wMinute = 0;
EnguardRole.BeginTime2.wSecond = 0;
DateTimeToSystemTime(lCurTime,EnguardRole.EndTime2);
EnguardRole.EndTime2.wHour = 17;
EnguardRole.EndTime2.wMinute = 0;
EnguardRole.EndTime2.wSecond = 0;
for(int i=0;i<7;i++)
EnguardRole.Weekend[i] = 0x00;
EnguardRole.Weekend[5] = 0x01;
EnguardRole.Weekend[6] = 0x01;
NewHoliday();
}
//---------------------------------------------------------------------------
void TEnguardRoleFrm::NewHoliday()
{
unsigned short lYear,lMonth,lDay;
TDateTime lCurTime = Now();
lCurTime.DecodeDate(&lYear,&lMonth,&lDay);
for(int i=0;i<12;i++)
{
if ((lMonth+i)>12)
{
EnguardRole.Holiday[i].Year = lYear+1;
EnguardRole.Holiday[i].Month = lMonth+i-12;
}
else
{
EnguardRole.Holiday[i].Year = lYear;
EnguardRole.Holiday[i].Month = lMonth+i;
}
for(int j=0;j<31;j++)
EnguardRole.Holiday[i].Days[j] = 0x00;
}
}
//---------------------------------------------------------------------------
void __fastcall TEnguardRoleFrm::bbtnNewYearClick(TObject *Sender)
{
if (Application->MessageBoxA("您确定要生成新的年度节假日表吗?新年度的节假日是从当月开始的今后一年...","提示信息",MB_YESNO + MB_ICONQUESTION) == ID_YES)
{
NewHoliday();
for(int i=0;i<62;i++)
ButtonDayList[i]->Font->Color = clBlue;
ShowRole();
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -