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.

308 lines
13 KiB

2 years ago
2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using SqlSugar;
  4. using SqlSugar.Base;
  5. using System;
  6. namespace EasyBL.WEBAPP.WSM
  7. {
  8. public class DepartmentsMaintain_UpdService : ServiceBase
  9. {
  10. #region 部門管理(單筆查詢)
  11. /// <summary>
  12. /// 部門管理(單筆查詢)
  13. /// </summary>
  14. /// <param name="i_crm"></param>
  15. /// <returns></returns>
  16. public ResponseMessage QueryOne(RequestMessage i_crm)
  17. {
  18. ResponseMessage rm = null;
  19. string sMsg = null;
  20. var db = SugarBase.GetIntance();
  21. try
  22. {
  23. do
  24. {
  25. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  26. var oEntity = db.Queryable<OTB_SYS_Departments, OTB_SYS_Members, OTB_SYS_Members>
  27. ((t1, t2, t3) =>
  28. new object[] {
  29. JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  30. JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
  31. }
  32. )
  33. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.DepartmentID == sDepartmentID)
  34. .Select((t1, t2, t3) => new OTB_SYS_Departments
  35. {
  36. DepartmentID = SqlFunc.GetSelfAndAutoFill(t1.DepartmentID),
  37. CreateUserName = t2.MemberName,
  38. ModifyUserName = t3.MemberName
  39. })
  40. .Single();
  41. rm = new SuccessResponseMessage(null, i_crm);
  42. rm.DATA.Add(BLWording.REL, oEntity);
  43. } while (false);
  44. }
  45. catch (Exception ex)
  46. {
  47. sMsg = Util.GetLastExceptionMsg(ex);
  48. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ArgumentMaintain_UpdService), "", "QueryOne(部門管理(單筆查詢))", "", "", "");
  49. }
  50. finally
  51. {
  52. if (null != sMsg)
  53. {
  54. rm = new ErrorResponseMessage(sMsg, i_crm);
  55. }
  56. }
  57. return rm;
  58. }
  59. #endregion 部門管理(單筆查詢)
  60. #region 部門管理(新增)
  61. /// <summary>
  62. /// 部門管理(新增)
  63. /// </summary>
  64. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  65. /// <returns></returns>
  66. public ResponseMessage Insert(RequestMessage i_crm)
  67. {
  68. ResponseMessage rm = null;
  69. string sMsg = null;
  70. try
  71. {
  72. rm = SugarBase.ExecTran(db =>
  73. {
  74. do
  75. {
  76. var oEntity = _fetchEntity<OTB_SYS_Departments>(i_crm);
  77. _setEntityBase(oEntity, i_crm);
  78. oEntity.DelStatus = "N";
  79. var iOldCout = db.Queryable<OTB_SYS_Departments>().Count(x => x.OrgID == i_crm.ORIGID && x.DelStatus == "N");
  80. if (oEntity.OrderByValue <= iOldCout)
  81. {
  82. var iRelUp = db.Updateable<OTB_SYS_Departments>()
  83. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue + 1 })
  84. .Where(x => x.OrgID == i_crm.ORIGID && x.DelStatus == "N" && x.OrderByValue >= oEntity.OrderByValue)
  85. .ExecuteCommand();
  86. }
  87. var oEntity_Del = db.Queryable<OTB_SYS_Departments>()
  88. .Single(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == oEntity.DepartmentID && x.DelStatus == "Y");
  89. var iRel = 0;
  90. if (oEntity_Del != null)
  91. {
  92. oEntity_Del.OrderByValue = oEntity.OrderByValue;
  93. oEntity_Del.LevelOfDepartment = oEntity.LevelOfDepartment;
  94. oEntity_Del.DelStatus = "N";
  95. oEntity_Del.Memo = oEntity.Memo;
  96. oEntity_Del.DepartmentName = oEntity.DepartmentName;
  97. oEntity_Del.DepartmentShortName = oEntity.DepartmentShortName;
  98. oEntity_Del.ChiefOfDepartmentID = oEntity.ChiefOfDepartmentID;
  99. oEntity_Del.ParentDepartmentID = oEntity.ParentDepartmentID;
  100. oEntity_Del.Effective = oEntity.Effective;
  101. iRel = db.Updateable(oEntity_Del)
  102. .IgnoreColumns(x => new
  103. {
  104. x.CreateUser,
  105. x.CreateDate
  106. }).ExecuteCommand();
  107. }
  108. else
  109. {
  110. iRel = db.Insertable(oEntity).ExecuteCommand();
  111. }
  112. rm = new SuccessResponseMessage(null, i_crm);
  113. rm.DATA.Add(BLWording.REL, iRel);
  114. } while (false);
  115. return rm;
  116. });
  117. }
  118. catch (Exception ex)
  119. {
  120. sMsg = Util.GetLastExceptionMsg(ex);
  121. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_UpdService), @"部門管理", @"Add(部門管理(新增))", @"", @"", @"");
  122. }
  123. finally
  124. {
  125. if (null != sMsg)
  126. {
  127. rm = new ErrorResponseMessage(sMsg, i_crm);
  128. }
  129. }
  130. return rm;
  131. }
  132. #endregion 部門管理(新增)
  133. #region 部門管理(修改)
  134. /// <summary>
  135. /// 部門管理(修改)
  136. /// </summary>
  137. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  138. /// <returns></returns>
  139. public ResponseMessage Update(RequestMessage i_crm)
  140. {
  141. ResponseMessage rm = null;
  142. string sMsg = null;
  143. try
  144. {
  145. rm = SugarBase.ExecTran(db =>
  146. {
  147. do
  148. {
  149. var oNewEntity = _fetchEntity<OTB_SYS_Departments>(i_crm);
  150. oNewEntity.DelStatus = "N";
  151. _setEntityBase(oNewEntity, i_crm);
  152. var iOldEntity = db.Queryable<OTB_SYS_Departments>().Single(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == oNewEntity.DepartmentID);
  153. if (oNewEntity.OrderByValue > iOldEntity.OrderByValue)
  154. {
  155. var iRelUp = db.Updateable<OTB_SYS_Departments>()
  156. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue - 1 })
  157. .Where(x => x.OrgID == oNewEntity.OrgID && x.DelStatus == "N" && x.OrderByValue <= oNewEntity.OrderByValue && x.OrderByValue > iOldEntity.OrderByValue).ExecuteCommand();
  158. }
  159. else
  160. {
  161. var iRelDown = db.Updateable<OTB_SYS_Departments>()
  162. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue + 1 })
  163. .Where(x => x.OrgID == oNewEntity.OrgID && x.DelStatus == "N" && x.OrderByValue >= oNewEntity.OrderByValue && x.OrderByValue < iOldEntity.OrderByValue).ExecuteCommand();
  164. }
  165. var iRel = db.Updateable(oNewEntity)
  166. .IgnoreColumns(x => new
  167. {
  168. x.CreateUser,
  169. x.CreateDate
  170. }).ExecuteCommand();
  171. rm = new SuccessResponseMessage(null, i_crm);
  172. rm.DATA.Add(BLWording.REL, iRel);
  173. } while (false);
  174. return rm;
  175. });
  176. }
  177. catch (Exception ex)
  178. {
  179. sMsg = Util.GetLastExceptionMsg(ex);
  180. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_UpdService), @"部門管理", @"Update(部門管理(修改))", @"", @"", @"");
  181. }
  182. finally
  183. {
  184. if (null != sMsg)
  185. {
  186. rm = new ErrorResponseMessage(sMsg, i_crm);
  187. }
  188. }
  189. return rm;
  190. }
  191. #endregion 部門管理(修改)
  192. #region 部門管理(刪除)
  193. /// <summary>
  194. /// 部門管理(刪除)
  195. /// </summary>
  196. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  197. /// <returns></returns>
  198. public ResponseMessage Delete(RequestMessage i_crm)
  199. {
  200. ResponseMessage rm = null;
  201. string sMsg = null;
  202. try
  203. {
  204. rm = SugarBase.ExecTran(db =>
  205. {
  206. do
  207. {
  208. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  209. var iExsitChild = db.Queryable<OTB_SYS_Members>().Count(x => x.OrgID == i_crm.ORIGID && x.MemberID == sDepartmentID);
  210. if (iExsitChild > 0)
  211. {
  212. sMsg = "message.ToDelMembers";//請先刪除該部門下的人員資料
  213. break;
  214. }
  215. var oEntity = db.Queryable<OTB_SYS_Departments>().Single(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == sDepartmentID);
  216. var iRel = db.Updateable<OTB_SYS_Departments>()
  217. .UpdateColumns(x => new OTB_SYS_Departments { DelStatus = "Y" })
  218. .Where(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == sDepartmentID).ExecuteCommand();
  219. var iRelUp = db.Updateable<OTB_SYS_Departments>()
  220. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue - 1 })
  221. .Where(x => x.OrgID == oEntity.OrgID && x.DelStatus == "N" && x.OrderByValue > oEntity.OrderByValue)
  222. .ExecuteCommand();
  223. rm = new SuccessResponseMessage(null, i_crm);
  224. rm.DATA.Add(BLWording.REL, iRel);
  225. } while (false);
  226. return rm;
  227. });
  228. }
  229. catch (Exception ex)
  230. {
  231. sMsg = Util.GetLastExceptionMsg(ex);
  232. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_UpdService), @"部門管理", @"Delete(部門管理(刪除))", @"", @"", @"");
  233. }
  234. finally
  235. {
  236. if (null != sMsg)
  237. {
  238. rm = new ErrorResponseMessage(sMsg, i_crm);
  239. }
  240. }
  241. return rm;
  242. }
  243. #endregion 部門管理(刪除)
  244. #region 部門管理(查詢筆數)
  245. /// <summary>
  246. /// 部門管理(查詢筆數)
  247. /// </summary>
  248. /// <param name="i_crm"></param>
  249. /// <returns></returns>
  250. public ResponseMessage QueryCout(RequestMessage i_crm)
  251. {
  252. ResponseMessage rm = null;
  253. string sMsg = null;
  254. var db = SugarBase.GetIntance();
  255. try
  256. {
  257. do
  258. {
  259. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  260. var iCout = db.Queryable<OTB_SYS_Departments>()
  261. .Where(x => x.OrgID == i_crm.ORIGID && x.DelStatus == "N")
  262. .WhereIF(!string.IsNullOrEmpty(sDepartmentID), x => x.DepartmentID == sDepartmentID)
  263. .Count();
  264. rm = new SuccessResponseMessage(null, i_crm);
  265. rm.DATA.Add(BLWording.REL, iCout);
  266. } while (false);
  267. }
  268. catch (Exception ex)
  269. {
  270. sMsg = Util.GetLastExceptionMsg(ex);
  271. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_UpdService), "", "QueryCout(部門管理(查詢筆數))", "", "", "");
  272. }
  273. finally
  274. {
  275. if (null != sMsg)
  276. {
  277. rm = new ErrorResponseMessage(sMsg, i_crm);
  278. }
  279. }
  280. return rm;
  281. }
  282. #endregion 部門管理(查詢筆數)
  283. }
  284. }