using EasyBL.WebApi.Message;
using Entity.Sugar;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Base;
using System;
using System.Collections.Generic;
namespace EasyBL.WEBAPP.EIP
{
public class LeaveSetService : ServiceBase
{
#region 初始化差勤設定
///
/// 初始化差勤設定
///
///
///
public ResponseMessage InitLeaveSet(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.GetIntance();
try
{
do
{
var sCurrentYear = _fetchString(i_crm, @"CurrentYear");
var saMembers = db.Queryable()
.Where(t => t.OrgID == i_crm.ORIGID && t.Effective == @"Y" && t.ServiceCode != @"API").ToList();
var saArguments = db.Queryable()
.Where(t => t.OrgID == i_crm.ORIGID && t.Effective == @"Y" && t.ArgumentClassID == @"LeaveType").ToList();
var saLeaveSet = new List();
foreach (OTB_SYS_Members user in saMembers)
{
var iCount = db.Queryable()
.Count(t => t.OrgID == i_crm.ORIGID && t.UserID == user.MemberID && t.TYear == sCurrentYear);
if (iCount == 0)
{
var oLeaveSet = new OTB_EIP_LeaveSet
{
Guid = Guid.NewGuid().ToString(),
OrgID = i_crm.ORIGID,
UserID = user.MemberID,
TYear = sCurrentYear,
CreateUser = @"Transfer",
CreateDate = DateTime.Now
};
var dicSetInfo = new List>();
foreach (OTB_SYS_Arguments arg in saArguments)
{
var dicInfo = new Dictionary
{
{ @"Id", arg.ArgumentID },
{ @"Name", arg.ArgumentValue },
{ @"PaymentHours", arg.Correlation ?? @"" },
{ @"UsedHours", @"0" },
{ @"RemainHours", arg.Correlation ?? @"0" },
{ @"Memo", arg.Memo }
};
dicSetInfo.Add(dicInfo);
}
oLeaveSet.SetInfo = JsonConvert.SerializeObject(dicSetInfo, Formatting.Indented);
saLeaveSet.Add(oLeaveSet);
}
}
if (saLeaveSet.Count > 0)
{
var iRel = db.Insertable(saLeaveSet).ExecuteCommand();
}
rm = new SuccessResponseMessage(null, i_crm);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetImportFeeitems(初始化差勤設定)", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 初始化差勤設定
#region 修改特休假時數
///
/// 修改特休假時數
///
///
///
public ResponseMessage UpdateLeaveTX(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
rm = SugarBase.ExecTran(db =>
{
do
{
var sUserId = _fetchString(i_crm, @"UserId");
var sAddLeaveHours = _fetchString(i_crm, @"AddLeaveHours");
var sRemainHours = _fetchString(i_crm, @"RemainHours");
var sSouseId = _fetchString(i_crm, @"SouseId");
var sMmeo = _fetchString(i_crm, @"Memo");
var sCurYear = DateTime.Now.Year;
var iAddLeaveHours = -decimal.Parse(sAddLeaveHours);
var saWenZhong1 = db.Queryable()
.Where(x => x.OrgID == i_crm.ORIGID && x.UserID == sUserId).OrderBy(x => x.EnableDate)
.ToList();
var saWenZhong = db.Queryable()
.Where(x => x.OrgID == i_crm.ORIGID && x.UserID == sUserId && (x.EnableDate.Value.Year == sCurYear || x.ExpirationDate.Value.Year == sCurYear)).OrderBy(x => x.EnableDate)
.ToList();
var saWenZhong_Upd = new List();
decimal? iOffer = iAddLeaveHours;
foreach (var item in saWenZhong)
{
if (item.RemainHours < iOffer)
{
iOffer -= item.RemainHours;
item.UsedHours += item.RemainHours;
item.RemainHours = 0;
}
else
{
item.RemainHours = item.RemainHours - iOffer;
item.UsedHours += iOffer;
iOffer = 0;
}
item.ModifyUser = i_crm.USERID;
item.ModifyDate = DateTime.Now;
saWenZhong_Upd.Add(item);
if (iOffer <= 0)
{
break;
}
}
db.Updateable(saWenZhong_Upd).UpdateColumns(x => new
{
x.RemainHours,
x.UsedHours
}).ExecuteCommand();
var logInfo = new OTB_SYS_LogInfo
{
OrgID = i_crm.ORIGID,
SouseId = sSouseId,
LogType = "leavesetchange"
};
Dictionary dic = new Dictionary();
decimal? iRemainHoursr = decimal.Parse(sRemainHours);
var sChangeRecord = $"特休假可用時數:{iRemainHoursr + iAddLeaveHours}小時 => {sRemainHours}小時";
if (i_crm.LANG == @"zh")
{
sChangeRecord = ChineseStringUtility.ToSimplified(sChangeRecord);
}
dic.Add("ChangeRecord", sChangeRecord);
var member = db.Queryable().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
dic.Add("ChangeUserName", member.MemberName);
dic.Add("Memo", sMmeo);
logInfo.LogInfo = JsonToString(dic);
_setEntityBase(logInfo, i_crm);
db.Insertable(logInfo).ExecuteCommand();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, "");
} while (false);
return rm;
});
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetImportFeeitems(修改特休假時數)", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
public ResponseMessage GetChangLog(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.GetIntance();
try
{
do
{
var slookupGuid = _fetchString(i_crm, @"SouseId");
var sLogType = _fetchString(i_crm, @"LogType");
var logInfos = db.Queryable()
.Where(x => x.OrgID == i_crm.ORIGID && x.SouseId == slookupGuid && sLogType.Contains(x.LogType) ).OrderBy(x => x.CreateDate)
.ToList();
rm = new SuccessResponseMessage(null, i_crm);
var sJson = JsonConvert.SerializeObject(logInfos, Formatting.Indented); //把list轉成Json字串
rm.DATA.Add(BLWording.REL, sJson);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetChangLog(取得變動紀錄)", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 修改特休假時數
#region 獲取請假規則設定
///
/// 獲取請假規則設定
///
///
///
public ResponseMessage GetLeaveSetting(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.DB;
try
{
do
{
var saLeaveRuelsSetting = db.Queryable((a, b) =>
new object[] {
JoinType.Left,a.OrgID==b.OrgID && a.ArgumentClassID==b.ArgumentClassID && a.ArgumentID==b.ArgumentID
})
.Where((a, b) => a.OrgID == i_crm.ORIGID && a.ArgumentClassID == "LeaveType" && a.Effective == "Y")
.Select((a, b) => new OTB_SYS_Arguments
{
ArgumentID = SqlFunc.GetSelfAndAutoFill(a.ArgumentID),
ExFeild1 = b.Field1,
ExFeild2 = b.Field2,
ExFeild3 = b.Field3,
ExFeild4 = b.Field4
})
.OrderBy(a => a.OrderByValue)
.ToList();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, saLeaveRuelsSetting);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"GetLeaveSetting(獲取請假規則設定)", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 獲取請假規則設定
#region 請假規則設定(更新)
///
/// 請假規則設定(更新)
///
/// todo: describe i_crm parameter on UpdImportCustomers
///
public ResponseMessage UpdateLeaveSetting(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
rm = SugarBase.ExecTran(db =>
{
do
{
var oEntity = _fetchEntity(i_crm);
_setEntityBase(oEntity, i_crm);
var oArgumentsRelated = new OTB_SYS_ArgumentsRelated
{
OrgID = oEntity.OrgID,
ArgumentClassID = oEntity.ArgumentClassID,
ArgumentID = oEntity.ArgumentID,
Field1 = oEntity.ExFeild1,
Field2 = oEntity.ExFeild2,
Field3 = oEntity.ExFeild3,
Field4 = oEntity.ExFeild4,
};
_setEntityBase(oArgumentsRelated, i_crm);
var iRelUp = db.Updateable(oEntity)
.UpdateColumns(x => new
{
x.Memo,
x.Correlation,
x.ModifyUser,
x.ModifyDate
}).ExecuteCommand();
var bExsit = db.Queryable()
.Where(x => x.OrgID == i_crm.ORIGID && x.ArgumentClassID == oArgumentsRelated.ArgumentClassID && x.ArgumentID == oArgumentsRelated.ArgumentID)
.Any();
if (bExsit)
{
iRelUp += db.Updateable(oArgumentsRelated)
.UpdateColumns(x => new
{
x.Field1,
x.Field2,
x.Field3,
x.Field4,
x.ModifyUser,
x.ModifyDate
}).ExecuteCommand();
}
else
{
iRelUp += db.Insertable(oArgumentsRelated).ExecuteCommand();
}
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, iRelUp);
} while (false);
return rm;
});
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"UpdateLeaveSetting(請假規則設定(更新))", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 請假規則設定(更新)
#region 依據請假類別獲取請假規則設定
///
/// 依據請假類別獲取請假規則設定
///
///
///
public ResponseMessage GetLeaveSettingByType(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
var db = SugarBase.DB;
try
{
do
{
var sLeaveType = _fetchString(i_crm, @"LeaveType");
var oLeaveRuelsSetting = db.Queryable((a, b) =>
new object[] {
JoinType.Left,a.OrgID==b.OrgID && a.ArgumentClassID==b.ArgumentClassID && a.ArgumentID==b.ArgumentID
})
.Where((a, b) => a.OrgID == i_crm.ORIGID && a.ArgumentClassID == "LeaveType" && a.ArgumentID == sLeaveType && a.Effective == "Y")
.Select((a, b) => new OTB_SYS_Arguments
{
ArgumentID = SqlFunc.GetSelfAndAutoFill(a.ArgumentID),
ExFeild1 = b.Field1,
ExFeild2 = b.Field2,
ExFeild3 = b.Field3,
ExFeild4 = b.Field4
})
.OrderBy(a => a.OrderByValue)
.Single();
rm = new SuccessResponseMessage(null, i_crm);
rm.DATA.Add(BLWording.REL, oLeaveRuelsSetting);
} while (false);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"GetLeaveSettingByType(依據請假類別獲取請假規則設定)", @"", @"", @"");
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
#endregion 依據請假類別獲取請假規則設定
}
}