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.

259 lines
11 KiB

2 years ago
  1. using Aspose.Cells;
  2. using EasyBL.WebApi.Message;
  3. using Entity.Sugar;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.IO;
  10. using System.Linq;
  11. namespace EasyBL.WEBAPP.EIP
  12. {
  13. public class WenZhongService : ServiceBase
  14. {
  15. #region 匯入特休假設定
  16. /// <summary>
  17. /// 匯入特休假設定
  18. /// </summary>
  19. /// <param name="i_crm"></param>
  20. /// <returns></returns>
  21. public ResponseMessage GetImport(RequestMessage i_crm)
  22. {
  23. ResponseMessage rm = null;
  24. string sMsg = null;
  25. var db = SugarBase.GetIntance();
  26. try
  27. {
  28. do
  29. {
  30. var sFileId = _fetchString(i_crm, @"FileId");
  31. var sFileName = _fetchString(i_crm, @"FileName");
  32. var sRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"OutFiles\Temporary\");//Word模版路徑
  33. var sfileName = sFileName.Split(new string[] { @"." }, StringSplitOptions.RemoveEmptyEntries);
  34. var sSubFileName = sfileName.LastOrDefault(); //副檔名
  35. sFileName = sRoot + sFileId + @"." + sSubFileName;
  36. var book = new Workbook(sFileName);
  37. //book.Open(sFileName);
  38. var sheet = book.Worksheets[0];
  39. var cells = sheet.Cells;
  40. var tbFeeItems = cells.ExportDataTableAsString(1, 0, cells.MaxDataRow, cells.MaxDataColumn + 1, false);
  41. var saWenZhong = new List<OTB_EIP_WenZhong>();
  42. var saDelete = new List<string>();
  43. if (tbFeeItems.Rows.Count > 0)
  44. {
  45. foreach (DataRow row in tbFeeItems.Rows)
  46. {
  47. try
  48. {
  49. var sWenZhongAcount = row[@"Column1"].ToString().Trim();
  50. var sUserName = row[@"Column2"].ToString();
  51. var sSeniority = row[@"Column6"].ToString();
  52. var sEnableDate = row[@"Column7"].ToString();
  53. var sExpirationDate = row[@"Column8"].ToString();
  54. var sPaymentHours = row[@"Column17"].ToString();
  55. var sUsedHours = row[@"Column18"].ToString();
  56. var oMembers = db.Queryable<OTB_SYS_Members>()
  57. .Single(it => it.OrgID == i_crm.ORIGID && it.WenZhongAcount == sWenZhongAcount);
  58. if (oMembers != null)
  59. {
  60. var oWenZhong = new OTB_EIP_WenZhong
  61. {
  62. Guid = Guid.NewGuid().ToString(),
  63. OrgID = i_crm.ORIGID,
  64. UserID = oMembers.MemberID,
  65. WenZhongAcount = sWenZhongAcount,
  66. UserName = sUserName,
  67. Seniority = Convert.ToDecimal(sSeniority),
  68. EnableDate = Convert.ToDateTime(sEnableDate.Split(' ')[0]),
  69. ExpirationDate = Convert.ToDateTime(sExpirationDate.Split(' ')[0]),
  70. PaymentHours = Convert.ToDecimal(sPaymentHours),
  71. UsedHours = Convert.ToDecimal(sUsedHours),
  72. DelFlag = true
  73. };
  74. oWenZhong.RemainHours = oWenZhong.PaymentHours - oWenZhong.UsedHours;
  75. var oLSet = db.Queryable<OTB_EIP_WenZhong>()
  76. .Single(it => it.OrgID == i_crm.ORIGID && it.UserID == oMembers.MemberID && it.EnableDate == oWenZhong.EnableDate && it.ExpirationDate == oWenZhong.ExpirationDate);
  77. if (oLSet == null)
  78. {
  79. saWenZhong.Add(oWenZhong);
  80. //saDelete.Add(oLSet.Guid);
  81. }
  82. }
  83. }
  84. catch { }
  85. }
  86. if (saWenZhong.Count > 0)
  87. {
  88. var iRel = db.Insertable<OTB_EIP_WenZhong>(saWenZhong).ExecuteCommand();
  89. }
  90. if (saDelete.Count > 0)
  91. {
  92. var iRel = db.Deleteable<OTB_EIP_WenZhong>(saDelete).ExecuteCommand();
  93. }
  94. }
  95. rm = new SuccessResponseMessage(null, i_crm);
  96. } while (false);
  97. }
  98. catch (Exception ex)
  99. {
  100. sMsg = Util.GetLastExceptionMsg(ex);
  101. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(WenZhongService), @"特休假設定", @"GetImport(匯入特休假設定)", @"", @"", @"");
  102. }
  103. finally
  104. {
  105. if (null != sMsg)
  106. {
  107. rm = new ErrorResponseMessage(sMsg, i_crm);
  108. }
  109. }
  110. return rm;
  111. }
  112. #endregion 匯入特休假設定
  113. #region 變更時數
  114. /// <summary>
  115. /// 變更時數
  116. /// </summary>
  117. /// <param name="i_crm"></param>
  118. /// <returns></returns>
  119. public ResponseMessage UpdLeaveHours(RequestMessage i_crm)
  120. {
  121. ResponseMessage rm = null;
  122. string sMsg = null;
  123. var db = SugarBase.GetIntance();
  124. try
  125. {
  126. do
  127. {
  128. var sId = _fetchString(i_crm, @"Guid");
  129. var sUpdLeaveHours = _fetchString(i_crm, nameof(UpdLeaveHours));
  130. var sMemo = _fetchString(i_crm, @"Memo");
  131. var iUpdLeaveHours = Convert.ToInt32(sUpdLeaveHours);
  132. var sdb = new SimpleClient<OTB_EIP_WenZhong>(db);
  133. var oWenZhong = sdb.GetById(sId);
  134. //增加特休是EnableDate年度為準。減少特休是ExpirationDate年度為準
  135. var YearDate = oWenZhong.EnableDate.Value.Year.ToString();
  136. if (iUpdLeaveHours < 0)
  137. YearDate = oWenZhong.ExpirationDate.Value.Year.ToString();
  138. var LeaveSet = db.Queryable<OTB_EIP_LeaveSet>()
  139. .Single(it => it.OrgID == i_crm.ORIGID && it.UserID == oWenZhong.UserID && it.TYear == YearDate);
  140. if (LeaveSet != null)
  141. {
  142. var oWenZhongUpd = new OTB_EIP_WenZhong
  143. {
  144. RemainHours = (oWenZhong.RemainHours ?? 0) + iUpdLeaveHours,
  145. Memo = sMemo
  146. };
  147. #region 增加異動Log
  148. var sChangeRecord = $"特休調整:{oWenZhong.EnableDate.Value.ToString("yyyy/MM/dd")}-{oWenZhong.ExpirationDate.Value.ToString("yyyy/MM/dd")}";
  149. if (i_crm.LANG == @"zh")
  150. {
  151. sChangeRecord = ChineseStringUtility.ToSimplified(sChangeRecord);
  152. }
  153. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  154. Dictionary<string, string> dic = new Dictionary<string, string>
  155. {
  156. { "ChangeRecord", sChangeRecord },
  157. { "ChangeUserName", member.MemberName },
  158. { "Memo", sMemo },
  159. { "ChangeHours", iUpdLeaveHours.ToString() }
  160. };
  161. var logInfo = new OTB_SYS_LogInfo
  162. {
  163. OrgID = i_crm.ORIGID,
  164. SouseId = LeaveSet.Guid,
  165. LogType = "WenZhongChange"
  166. };
  167. logInfo.LogInfo = JsonToString(dic);
  168. _setEntityBase(logInfo, i_crm);
  169. db.Insertable(logInfo).ExecuteCommand();
  170. #endregion
  171. var bRel = db.Updateable(oWenZhongUpd).UpdateColumns(it => new { it.Memo, it.RemainHours })
  172. .Where(it => it.Guid == sId).ExecuteCommandHasChange();
  173. rm = new SuccessResponseMessage(null, i_crm);
  174. rm.DATA.Add(BLWording.REL, bRel);
  175. }
  176. else
  177. {
  178. sMsg = $"因為帳號{oWenZhong.UserID}的{YearDate}年度出勤設定尚未初始化,所以無法變動特休時數。" +
  179. $"請先{YearDate}初始化設定再調整。";
  180. }
  181. } while (false);
  182. }
  183. catch (Exception ex)
  184. {
  185. sMsg = Util.GetLastExceptionMsg(ex);
  186. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(WenZhongService), @"特休假設定", @"UpdLeaveHours(變更時數)", @"", @"", @"");
  187. }
  188. finally
  189. {
  190. if (null != sMsg)
  191. {
  192. rm = new ErrorResponseMessage(sMsg, i_crm);
  193. }
  194. }
  195. return rm;
  196. }
  197. #endregion 變更時數
  198. #region 變更時數
  199. /// <summary>
  200. /// 變更時數
  201. /// </summary>
  202. /// <param name="i_crm"></param>
  203. /// <returns></returns>
  204. public ResponseMessage GetDel(RequestMessage i_crm)
  205. {
  206. ResponseMessage rm = null;
  207. string sMsg = null;
  208. var db = SugarBase.GetIntance();
  209. try
  210. {
  211. do
  212. {
  213. var sId = _fetchString(i_crm, @"Guid");
  214. var sdb = new SimpleClient<OTB_EIP_WenZhong>(db);
  215. var oWenZhong = sdb.GetById(sId);
  216. if (!(bool)oWenZhong.DelFlag)
  217. {
  218. sMsg = @"該筆資料已被系統修改,暫時不可刪除";
  219. break;
  220. }
  221. var bRel = sdb.DeleteById(oWenZhong.Guid);
  222. rm = new SuccessResponseMessage(null, i_crm);
  223. rm.DATA.Add(BLWording.REL, bRel);
  224. } while (false);
  225. }
  226. catch (Exception ex)
  227. {
  228. sMsg = Util.GetLastExceptionMsg(ex);
  229. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(WenZhongService), @"文中特休假設定", @"GetDel(刪除資料行)", @"", @"", @"");
  230. }
  231. finally
  232. {
  233. if (null != sMsg)
  234. {
  235. rm = new ErrorResponseMessage(sMsg, i_crm);
  236. }
  237. }
  238. return rm;
  239. }
  240. #endregion 變更時數
  241. }
  242. }