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.

427 lines
18 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Newtonsoft.Json;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace EasyBL.WEBAPP.EIP
  9. {
  10. public class LeaveSetService : ServiceBase
  11. {
  12. #region 初始化差勤設定
  13. /// <summary>
  14. /// 初始化差勤設定
  15. /// </summary>
  16. /// <param name="i_crm"></param>
  17. /// <returns></returns>
  18. public ResponseMessage InitLeaveSet(RequestMessage i_crm)
  19. {
  20. ResponseMessage rm = null;
  21. string sMsg = null;
  22. var db = SugarBase.GetIntance();
  23. try
  24. {
  25. do
  26. {
  27. var sCurrentYear = _fetchString(i_crm, @"CurrentYear");
  28. var saMembers = db.Queryable<OTB_SYS_Members>()
  29. .Where(t => t.OrgID == i_crm.ORIGID && t.Effective == @"Y" && t.ServiceCode != @"API").ToList();
  30. var saArguments = db.Queryable<OTB_SYS_Arguments>()
  31. .Where(t => t.OrgID == i_crm.ORIGID && t.Effective == @"Y" && t.ArgumentClassID == @"LeaveType").ToList();
  32. var saLeaveSet = new List<OTB_EIP_LeaveSet>();
  33. foreach (OTB_SYS_Members user in saMembers)
  34. {
  35. var iCount = db.Queryable<OTB_EIP_LeaveSet>()
  36. .Count(t => t.OrgID == i_crm.ORIGID && t.UserID == user.MemberID && t.TYear == sCurrentYear);
  37. if (iCount == 0)
  38. {
  39. var oLeaveSet = new OTB_EIP_LeaveSet
  40. {
  41. Guid = Guid.NewGuid().ToString(),
  42. OrgID = i_crm.ORIGID,
  43. UserID = user.MemberID,
  44. TYear = sCurrentYear,
  45. CreateUser = @"Transfer",
  46. CreateDate = DateTime.Now
  47. };
  48. var dicSetInfo = new List<Dictionary<string, object>>();
  49. foreach (OTB_SYS_Arguments arg in saArguments)
  50. {
  51. var dicInfo = new Dictionary<string, object>
  52. {
  53. { @"Id", arg.ArgumentID },
  54. { @"Name", arg.ArgumentValue },
  55. { @"PaymentHours", arg.Correlation ?? @"" },
  56. { @"UsedHours", @"0" },
  57. { @"RemainHours", arg.Correlation ?? @"0" },
  58. { @"Memo", arg.Memo }
  59. };
  60. dicSetInfo.Add(dicInfo);
  61. }
  62. oLeaveSet.SetInfo = JsonConvert.SerializeObject(dicSetInfo, Formatting.Indented);
  63. saLeaveSet.Add(oLeaveSet);
  64. }
  65. }
  66. if (saLeaveSet.Count > 0)
  67. {
  68. var iRel = db.Insertable<OTB_EIP_LeaveSet>(saLeaveSet).ExecuteCommand();
  69. }
  70. rm = new SuccessResponseMessage(null, i_crm);
  71. } while (false);
  72. }
  73. catch (Exception ex)
  74. {
  75. sMsg = Util.GetLastExceptionMsg(ex);
  76. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetImportFeeitems(初始化差勤設定)", @"", @"", @"");
  77. }
  78. finally
  79. {
  80. if (null != sMsg)
  81. {
  82. rm = new ErrorResponseMessage(sMsg, i_crm);
  83. }
  84. }
  85. return rm;
  86. }
  87. #endregion 初始化差勤設定
  88. #region 修改特休假時數
  89. /// <summary>
  90. /// 修改特休假時數
  91. /// </summary>
  92. /// <param name="i_crm"></param>
  93. /// <returns></returns>
  94. public ResponseMessage UpdateLeaveTX(RequestMessage i_crm)
  95. {
  96. ResponseMessage rm = null;
  97. string sMsg = null;
  98. try
  99. {
  100. rm = SugarBase.ExecTran(db =>
  101. {
  102. do
  103. {
  104. var sUserId = _fetchString(i_crm, @"UserId");
  105. var sAddLeaveHours = _fetchString(i_crm, @"AddLeaveHours");
  106. var sRemainHours = _fetchString(i_crm, @"RemainHours");
  107. var sSouseId = _fetchString(i_crm, @"SouseId");
  108. var sMmeo = _fetchString(i_crm, @"Memo");
  109. var sCurYear = DateTime.Now.Year;
  110. var iAddLeaveHours = -decimal.Parse(sAddLeaveHours);
  111. var saWenZhong1 = db.Queryable<OTB_EIP_WenZhong>()
  112. .Where(x => x.OrgID == i_crm.ORIGID && x.UserID == sUserId).OrderBy(x => x.EnableDate)
  113. .ToList();
  114. var saWenZhong = db.Queryable<OTB_EIP_WenZhong>()
  115. .Where(x => x.OrgID == i_crm.ORIGID && x.UserID == sUserId && (x.EnableDate.Value.Year == sCurYear || x.ExpirationDate.Value.Year == sCurYear)).OrderBy(x => x.EnableDate)
  116. .ToList();
  117. var saWenZhong_Upd = new List<OTB_EIP_WenZhong>();
  118. decimal? iOffer = iAddLeaveHours;
  119. foreach (var item in saWenZhong)
  120. {
  121. if (item.RemainHours < iOffer)
  122. {
  123. iOffer -= item.RemainHours;
  124. item.UsedHours += item.RemainHours;
  125. item.RemainHours = 0;
  126. }
  127. else
  128. {
  129. item.RemainHours = item.RemainHours - iOffer;
  130. item.UsedHours += iOffer;
  131. iOffer = 0;
  132. }
  133. item.ModifyUser = i_crm.USERID;
  134. item.ModifyDate = DateTime.Now;
  135. saWenZhong_Upd.Add(item);
  136. if (iOffer <= 0)
  137. {
  138. break;
  139. }
  140. }
  141. db.Updateable(saWenZhong_Upd).UpdateColumns(x => new
  142. {
  143. x.RemainHours,
  144. x.UsedHours
  145. }).ExecuteCommand();
  146. var logInfo = new OTB_SYS_LogInfo
  147. {
  148. OrgID = i_crm.ORIGID,
  149. SouseId = sSouseId,
  150. LogType = "leavesetchange"
  151. };
  152. Dictionary<string, string> dic = new Dictionary<string, string>();
  153. decimal? iRemainHoursr = decimal.Parse(sRemainHours);
  154. var sChangeRecord = $"特休假可用時數:{iRemainHoursr + iAddLeaveHours}小時 => {sRemainHours}小時";
  155. if (i_crm.LANG == @"zh")
  156. {
  157. sChangeRecord = ChineseStringUtility.ToSimplified(sChangeRecord);
  158. }
  159. dic.Add("ChangeRecord", sChangeRecord);
  160. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  161. dic.Add("ChangeUserName", member.MemberName);
  162. dic.Add("Memo", sMmeo);
  163. logInfo.LogInfo = JsonToString(dic);
  164. _setEntityBase(logInfo, i_crm);
  165. db.Insertable(logInfo).ExecuteCommand();
  166. rm = new SuccessResponseMessage(null, i_crm);
  167. rm.DATA.Add(BLWording.REL, "");
  168. } while (false);
  169. return rm;
  170. });
  171. }
  172. catch (Exception ex)
  173. {
  174. sMsg = Util.GetLastExceptionMsg(ex);
  175. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetImportFeeitems(修改特休假時數)", @"", @"", @"");
  176. }
  177. finally
  178. {
  179. if (null != sMsg)
  180. {
  181. rm = new ErrorResponseMessage(sMsg, i_crm);
  182. }
  183. }
  184. return rm;
  185. }
  186. public ResponseMessage GetChangLog(RequestMessage i_crm)
  187. {
  188. ResponseMessage rm = null;
  189. string sMsg = null;
  190. var db = SugarBase.GetIntance();
  191. try
  192. {
  193. do
  194. {
  195. var slookupGuid = _fetchString(i_crm, @"SouseId");
  196. var sLogType = _fetchString(i_crm, @"LogType");
  197. var logInfos = db.Queryable<OTB_SYS_LogInfo>()
  198. .Where(x => x.OrgID == i_crm.ORIGID && x.SouseId == slookupGuid && sLogType.Contains(x.LogType) ).OrderBy(x => x.CreateDate)
  199. .ToList();
  200. rm = new SuccessResponseMessage(null, i_crm);
  201. var sJson = JsonConvert.SerializeObject(logInfos, Formatting.Indented); //把list轉成Json字串
  202. rm.DATA.Add(BLWording.REL, sJson);
  203. } while (false);
  204. }
  205. catch (Exception ex)
  206. {
  207. sMsg = Util.GetLastExceptionMsg(ex);
  208. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"差勤設定", @"GetChangLog(取得變動紀錄)", @"", @"", @"");
  209. }
  210. finally
  211. {
  212. if (null != sMsg)
  213. {
  214. rm = new ErrorResponseMessage(sMsg, i_crm);
  215. }
  216. }
  217. return rm;
  218. }
  219. #endregion 修改特休假時數
  220. #region 獲取請假規則設定
  221. /// <summary>
  222. /// 獲取請假規則設定
  223. /// </summary>
  224. /// <param name="i_crm"></param>
  225. /// <returns></returns>
  226. public ResponseMessage GetLeaveSetting(RequestMessage i_crm)
  227. {
  228. ResponseMessage rm = null;
  229. string sMsg = null;
  230. var db = SugarBase.DB;
  231. try
  232. {
  233. do
  234. {
  235. var saLeaveRuelsSetting = db.Queryable<OTB_SYS_Arguments, OTB_SYS_ArgumentsRelated>((a, b) =>
  236. new object[] {
  237. JoinType.Left,a.OrgID==b.OrgID && a.ArgumentClassID==b.ArgumentClassID && a.ArgumentID==b.ArgumentID
  238. })
  239. .Where((a, b) => a.OrgID == i_crm.ORIGID && a.ArgumentClassID == "LeaveType" && a.Effective == "Y")
  240. .Select((a, b) => new OTB_SYS_Arguments
  241. {
  242. ArgumentID = SqlFunc.GetSelfAndAutoFill(a.ArgumentID),
  243. ExFeild1 = b.Field1,
  244. ExFeild2 = b.Field2,
  245. ExFeild3 = b.Field3,
  246. ExFeild4 = b.Field4
  247. })
  248. .OrderBy(a => a.OrderByValue)
  249. .ToList();
  250. rm = new SuccessResponseMessage(null, i_crm);
  251. rm.DATA.Add(BLWording.REL, saLeaveRuelsSetting);
  252. } while (false);
  253. }
  254. catch (Exception ex)
  255. {
  256. sMsg = Util.GetLastExceptionMsg(ex);
  257. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"GetLeaveSetting(獲取請假規則設定)", @"", @"", @"");
  258. }
  259. finally
  260. {
  261. if (null != sMsg)
  262. {
  263. rm = new ErrorResponseMessage(sMsg, i_crm);
  264. }
  265. }
  266. return rm;
  267. }
  268. #endregion 獲取請假規則設定
  269. #region 請假規則設定(更新)
  270. /// <summary>
  271. /// 請假規則設定(更新)
  272. /// </summary>
  273. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  274. /// <returns></returns>
  275. public ResponseMessage UpdateLeaveSetting(RequestMessage i_crm)
  276. {
  277. ResponseMessage rm = null;
  278. string sMsg = null;
  279. try
  280. {
  281. rm = SugarBase.ExecTran(db =>
  282. {
  283. do
  284. {
  285. var oEntity = _fetchEntity<OTB_SYS_Arguments>(i_crm);
  286. _setEntityBase(oEntity, i_crm);
  287. var oArgumentsRelated = new OTB_SYS_ArgumentsRelated
  288. {
  289. OrgID = oEntity.OrgID,
  290. ArgumentClassID = oEntity.ArgumentClassID,
  291. ArgumentID = oEntity.ArgumentID,
  292. Field1 = oEntity.ExFeild1,
  293. Field2 = oEntity.ExFeild2,
  294. Field3 = oEntity.ExFeild3,
  295. Field4 = oEntity.ExFeild4,
  296. };
  297. _setEntityBase(oArgumentsRelated, i_crm);
  298. var iRelUp = db.Updateable(oEntity)
  299. .UpdateColumns(x => new
  300. {
  301. x.Memo,
  302. x.Correlation,
  303. x.ModifyUser,
  304. x.ModifyDate
  305. }).ExecuteCommand();
  306. var bExsit = db.Queryable<OTB_SYS_ArgumentsRelated>()
  307. .Where(x => x.OrgID == i_crm.ORIGID && x.ArgumentClassID == oArgumentsRelated.ArgumentClassID && x.ArgumentID == oArgumentsRelated.ArgumentID)
  308. .Any();
  309. if (bExsit)
  310. {
  311. iRelUp += db.Updateable(oArgumentsRelated)
  312. .UpdateColumns(x => new
  313. {
  314. x.Field1,
  315. x.Field2,
  316. x.Field3,
  317. x.Field4,
  318. x.ModifyUser,
  319. x.ModifyDate
  320. }).ExecuteCommand();
  321. }
  322. else
  323. {
  324. iRelUp += db.Insertable(oArgumentsRelated).ExecuteCommand();
  325. }
  326. rm = new SuccessResponseMessage(null, i_crm);
  327. rm.DATA.Add(BLWording.REL, iRelUp);
  328. } while (false);
  329. return rm;
  330. });
  331. }
  332. catch (Exception ex)
  333. {
  334. sMsg = Util.GetLastExceptionMsg(ex);
  335. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"UpdateLeaveSetting(請假規則設定(更新))", @"", @"", @"");
  336. }
  337. finally
  338. {
  339. if (null != sMsg)
  340. {
  341. rm = new ErrorResponseMessage(sMsg, i_crm);
  342. }
  343. }
  344. return rm;
  345. }
  346. #endregion 請假規則設定(更新)
  347. #region 依據請假類別獲取請假規則設定
  348. /// <summary>
  349. /// 依據請假類別獲取請假規則設定
  350. /// </summary>
  351. /// <param name="i_crm"></param>
  352. /// <returns></returns>
  353. public ResponseMessage GetLeaveSettingByType(RequestMessage i_crm)
  354. {
  355. ResponseMessage rm = null;
  356. string sMsg = null;
  357. var db = SugarBase.DB;
  358. try
  359. {
  360. do
  361. {
  362. var sLeaveType = _fetchString(i_crm, @"LeaveType");
  363. var oLeaveRuelsSetting = db.Queryable<OTB_SYS_Arguments, OTB_SYS_ArgumentsRelated>((a, b) =>
  364. new object[] {
  365. JoinType.Left,a.OrgID==b.OrgID && a.ArgumentClassID==b.ArgumentClassID && a.ArgumentID==b.ArgumentID
  366. })
  367. .Where((a, b) => a.OrgID == i_crm.ORIGID && a.ArgumentClassID == "LeaveType" && a.ArgumentID == sLeaveType && a.Effective == "Y")
  368. .Select((a, b) => new OTB_SYS_Arguments
  369. {
  370. ArgumentID = SqlFunc.GetSelfAndAutoFill(a.ArgumentID),
  371. ExFeild1 = b.Field1,
  372. ExFeild2 = b.Field2,
  373. ExFeild3 = b.Field3,
  374. ExFeild4 = b.Field4
  375. })
  376. .OrderBy(a => a.OrderByValue)
  377. .Single();
  378. rm = new SuccessResponseMessage(null, i_crm);
  379. rm.DATA.Add(BLWording.REL, oLeaveRuelsSetting);
  380. } while (false);
  381. }
  382. catch (Exception ex)
  383. {
  384. sMsg = Util.GetLastExceptionMsg(ex);
  385. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"GetLeaveSettingByType(依據請假類別獲取請假規則設定)", @"", @"", @"");
  386. }
  387. finally
  388. {
  389. if (null != sMsg)
  390. {
  391. rm = new ErrorResponseMessage(sMsg, i_crm);
  392. }
  393. }
  394. return rm;
  395. }
  396. #endregion 依據請假類別獲取請假規則設定
  397. }
  398. }