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.

410 lines
18 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Newtonsoft.Json.Linq;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. namespace EasyBL.WEBAPP.SYS
  8. {
  9. public class ProgramMaintain_UpdService : ServiceBase
  10. {
  11. #region 獲取模組信息
  12. /// <summary>
  13. /// 函式名稱:Login
  14. /// </summary>
  15. /// <param name="i_crm">todo: describe i_crm parameter on GetModuleInfo</param>
  16. /// <returns>
  17. /// 回傳 rm(Object)
  18. ///</returns>
  19. public ResponseMessage GetModuleInfo(RequestMessage i_crm)
  20. {
  21. ResponseMessage rm = null;
  22. string sError = null;
  23. var db = SugarBase.GetIntance();
  24. try
  25. {
  26. do
  27. {
  28. var sp = db.Ado.GetParameters(i_crm.DATA);
  29. var moduleInfo = db.Ado.SqlQuery<ModuleInfo>(@"SELECT ModuleID,case when ModuleID=dbo.OFN_SYS_GetParentIDByModuleID(OrgID,ModuleID) then '' else dbo.OFN_SYS_ModuleNameByModuleID(OrgID,dbo.OFN_SYS_GetParentIDByModuleID(OrgID,ModuleID))+'-' end +ModuleName as ModuleName,(SELECT COUNT(0) FROM OTB_SYS_ModuleForProgram WHERE ModuleID=M.ModuleID and OrgID=M.OrgID) AS PrgCount, (SELECT OrderByValue FROM OTB_SYS_ModuleForProgram WHERE OrgID=m.OrgID AND ModuleID=m.ModuleID AND ProgramID=@ProgramID) AS OrderByValue FROM dbo.OTB_SYS_ModuleList M WHERE OrgID=@OrgID AND CHARINDEX(','+M.ModuleID+',',','+@AllModuleID+',')>0", sp);
  30. rm = new SuccessResponseMessage(null, i_crm);
  31. rm.DATA.Add(BLWording.REL, moduleInfo);
  32. } while (false);
  33. }
  34. catch (Exception ex)
  35. {
  36. sError = Util.GetLastExceptionMsg(ex);
  37. LogAndSendEmail(sError + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), @"", @"GetModuleInfo(獲取模組信息)", @"", @"", @"");
  38. }
  39. finally
  40. {
  41. if (null != sError)
  42. {
  43. rm = new ErrorResponseMessage(sError, i_crm);
  44. }
  45. }
  46. return rm;
  47. }
  48. #endregion 獲取模組信息
  49. #region 程式管理(單筆查詢)
  50. /// <summary>
  51. /// 程式管理(單筆查詢)
  52. /// </summary>
  53. /// <param name="i_crm"></param>
  54. /// <returns></returns>
  55. public ResponseMessage QueryOne(RequestMessage i_crm)
  56. {
  57. ResponseMessage rm = null;
  58. string sMsg = null;
  59. var db = SugarBase.GetIntance();
  60. try
  61. {
  62. do
  63. {
  64. var sProgramID = _fetchString(i_crm, @"ProgramID");
  65. var oEntity = db.Queryable<OTB_SYS_ProgramList, OTB_SYS_Members, OTB_SYS_Members>
  66. ((t1, t2, t3) =>
  67. new object[] {
  68. JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  69. JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
  70. }
  71. )
  72. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.ProgramID == sProgramID)
  73. .Select((t1, t2, t3) => new OTB_SYS_ProgramList
  74. {
  75. ProgramID = SqlFunc.GetSelfAndAutoFill(t1.ProgramID),
  76. CreateUserName = t2.MemberName,
  77. ModifyUserName = t3.MemberName
  78. })
  79. .Single();
  80. var saAuth = db.Queryable<OTB_SYS_Arguments>()
  81. .Where(x => x.OrgID == i_crm.ORIGID && x.ArgumentClassID == "99999" && x.Effective == "Y")
  82. .ToList();
  83. rm = new SuccessResponseMessage(null, i_crm);
  84. rm.DATA.Add(BLWording.REL, oEntity);
  85. rm.DATA.Add("actions", saAuth);
  86. } while (false);
  87. }
  88. catch (Exception ex)
  89. {
  90. sMsg = Util.GetLastExceptionMsg(ex);
  91. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), "", "QueryOne(程式管理(單筆查詢))", "", "", "");
  92. }
  93. finally
  94. {
  95. if (null != sMsg)
  96. {
  97. rm = new ErrorResponseMessage(sMsg, i_crm);
  98. }
  99. }
  100. return rm;
  101. }
  102. #endregion 程式管理(單筆查詢)
  103. #region 程式管理(新增)
  104. /// <summary>
  105. /// 程式管理(新增)
  106. /// </summary>
  107. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  108. /// <returns></returns>
  109. public ResponseMessage Insert(RequestMessage i_crm)
  110. {
  111. ResponseMessage rm = null;
  112. string sMsg = null;
  113. try
  114. {
  115. rm = SugarBase.ExecTran(db =>
  116. {
  117. do
  118. {
  119. var oEntity = _fetchEntity<OTB_SYS_ProgramList>(i_crm);
  120. _setEntityBase(oEntity, i_crm);
  121. var saUpdOrder = i_crm.DATA["updorder"] as JArray;
  122. foreach (JObject updorder in saUpdOrder)
  123. {
  124. var sModuleID = updorder.GetValue("ModuleID").ToString();
  125. var iOldOrderByValue = int.Parse(updorder.GetValue("OldOrderByValue").ToString());
  126. var iNewOrderByValue = int.Parse(updorder.GetValue("NewOrderByValue").ToString());
  127. if (iNewOrderByValue > iOldOrderByValue)
  128. {
  129. var iRelUp = db.Updateable<OTB_SYS_ModuleForProgram>()
  130. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue - 1 })
  131. .Where(x => x.OrgID == i_crm.ORIGID && x.ModuleID == sModuleID && x.OrderByValue <= iNewOrderByValue && x.OrderByValue > iOldOrderByValue).ExecuteCommand();
  132. }
  133. else
  134. {
  135. var iRelDown = db.Updateable<OTB_SYS_ModuleForProgram>()
  136. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue + 1 })
  137. .Where(x => x.OrgID == i_crm.ORIGID && x.ModuleID == sModuleID && x.OrderByValue >= iNewOrderByValue && x.OrderByValue < iOldOrderByValue).ExecuteCommand();
  138. }
  139. var oModuleForProgram = new OTB_SYS_ModuleForProgram
  140. {
  141. OrgID = i_crm.ORIGID,
  142. ModuleID = sModuleID,
  143. ProgramID = oEntity.ProgramID,
  144. OrderByValue = iNewOrderByValue
  145. };
  146. _setEntityBase(oModuleForProgram, i_crm);
  147. db.Insertable(oModuleForProgram).ExecuteCommand();
  148. }
  149. var iRel = db.Insertable(oEntity).ExecuteCommand();
  150. rm = new SuccessResponseMessage(null, i_crm);
  151. rm.DATA.Add(BLWording.REL, iRel);
  152. } while (false);
  153. return rm;
  154. });
  155. }
  156. catch (Exception ex)
  157. {
  158. sMsg = Util.GetLastExceptionMsg(ex);
  159. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), @"程式管理", @"Add(程式管理(新增))", @"", @"", @"");
  160. }
  161. finally
  162. {
  163. if (null != sMsg)
  164. {
  165. rm = new ErrorResponseMessage(sMsg, i_crm);
  166. }
  167. }
  168. return rm;
  169. }
  170. #endregion 程式管理(新增)
  171. #region 程式管理(修改)
  172. /// <summary>
  173. /// 程式管理(修改)
  174. /// </summary>
  175. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  176. /// <returns></returns>
  177. public ResponseMessage Update(RequestMessage i_crm)
  178. {
  179. ResponseMessage rm = null;
  180. string sMsg = null;
  181. try
  182. {
  183. rm = SugarBase.ExecTran(db =>
  184. {
  185. do
  186. {
  187. var oNewEntity = _fetchEntity<OTB_SYS_ProgramList>(i_crm);
  188. _setEntityBase(oNewEntity, i_crm);
  189. var saUpdOrder = i_crm.DATA["updorder"] as JArray;
  190. //更新和新增當前模組
  191. foreach (JObject updorder in saUpdOrder)
  192. {
  193. var sModuleID = updorder.GetValue("ModuleID").ToString();
  194. var iOldOrderByValue = int.Parse(updorder.GetValue("OldOrderByValue").ToString());
  195. var iNewOrderByValue = int.Parse(updorder.GetValue("NewOrderByValue").ToString());
  196. if (iNewOrderByValue > iOldOrderByValue)
  197. {
  198. var iRelUp = db.Updateable<OTB_SYS_ModuleForProgram>()
  199. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue - 1 })
  200. .Where(x => x.OrgID == i_crm.ORIGID && x.ModuleID == sModuleID && x.OrderByValue <= iNewOrderByValue && x.OrderByValue > iOldOrderByValue).ExecuteCommand();
  201. }
  202. else
  203. {
  204. var iRelDown = db.Updateable<OTB_SYS_ModuleForProgram>()
  205. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue + 1 })
  206. .Where(x => x.OrgID == i_crm.ORIGID && x.ModuleID == sModuleID && x.OrderByValue >= iNewOrderByValue && x.OrderByValue < iOldOrderByValue).ExecuteCommand();
  207. }
  208. var oModuleForProgram = new OTB_SYS_ModuleForProgram
  209. {
  210. OrgID = i_crm.ORIGID,
  211. ModuleID = sModuleID,
  212. ProgramID = oNewEntity.ProgramID,
  213. OrderByValue = iNewOrderByValue
  214. };
  215. _setEntityBase(oModuleForProgram, i_crm);
  216. var oOldEntity = db.Queryable<OTB_SYS_ModuleForProgram>()
  217. .Single(x => x.OrgID == i_crm.ORIGID && x.ModuleID == sModuleID && x.ProgramID == oNewEntity.ProgramID);
  218. if (oOldEntity == null)
  219. {
  220. db.Insertable(oModuleForProgram).ExecuteCommand();
  221. }
  222. else
  223. {
  224. db.Updateable(oModuleForProgram)
  225. .IgnoreColumns(x => new
  226. {
  227. x.CreateUser,
  228. x.CreateDate
  229. }).ExecuteCommand();
  230. }
  231. }
  232. //更新和刪除已刪除的模組
  233. var saDelEntitys = db.Queryable<OTB_SYS_ModuleForProgram>()
  234. .Where(x => x.OrgID == i_crm.ORIGID && !oNewEntity.ModuleID.Contains(x.ModuleID) && x.ProgramID == oNewEntity.ProgramID)
  235. .ToList();
  236. foreach (var item in saDelEntitys)
  237. {
  238. db.Deleteable<OTB_SYS_ModuleForProgram>()
  239. .Where(x => x.OrgID == item.OrgID && x.ModuleID == item.ModuleID && x.ProgramID == item.ProgramID)
  240. .ExecuteCommand();
  241. db.Updateable<OTB_SYS_ModuleForProgram>()
  242. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue - 1 })
  243. .Where(x => x.OrgID == item.OrgID && x.ModuleID == item.ModuleID && x.OrderByValue > item.OrderByValue)
  244. .ExecuteCommand();
  245. }
  246. var iRel = db.Updateable(oNewEntity)
  247. .IgnoreColumns(x => new
  248. {
  249. x.CreateUser,
  250. x.CreateDate
  251. }).ExecuteCommand();
  252. rm = new SuccessResponseMessage(null, i_crm);
  253. rm.DATA.Add(BLWording.REL, iRel);
  254. } while (false);
  255. return rm;
  256. });
  257. }
  258. catch (Exception ex)
  259. {
  260. sMsg = Util.GetLastExceptionMsg(ex);
  261. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), @"程式管理", @"Update(程式管理(修改))", @"", @"", @"");
  262. }
  263. finally
  264. {
  265. if (null != sMsg)
  266. {
  267. rm = new ErrorResponseMessage(sMsg, i_crm);
  268. }
  269. }
  270. return rm;
  271. }
  272. #endregion 程式管理(修改)
  273. #region 程式管理(刪除)
  274. /// <summary>
  275. /// 程式管理(刪除)
  276. /// </summary>
  277. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  278. /// <returns></returns>
  279. public ResponseMessage Delete(RequestMessage i_crm)
  280. {
  281. ResponseMessage rm = null;
  282. string sMsg = null;
  283. try
  284. {
  285. rm = SugarBase.ExecTran(db =>
  286. {
  287. do
  288. {
  289. var sProgramID = _fetchString(i_crm, @"ProgramID");
  290. var iRel = db.Deleteable<OTB_SYS_ProgramList>().Where(x => x.ProgramID == sProgramID).ExecuteCommand();
  291. //更新和刪除已刪除的模組
  292. var saDelEntitys = db.Queryable<OTB_SYS_ModuleForProgram>()
  293. .Where(x => x.OrgID == i_crm.ORIGID && x.ProgramID == sProgramID)
  294. .ToList();
  295. foreach (var item in saDelEntitys)
  296. {
  297. db.Deleteable<OTB_SYS_ModuleForProgram>()
  298. .Where(x => x.OrgID == item.OrgID && x.ModuleID == item.ModuleID && x.ProgramID == item.ProgramID)
  299. .ExecuteCommand();
  300. db.Updateable<OTB_SYS_ModuleForProgram>()
  301. .UpdateColumns(x => new OTB_SYS_ModuleForProgram { OrderByValue = x.OrderByValue - 1 })
  302. .Where(x => x.OrgID == item.OrgID && x.ModuleID == item.ModuleID && x.OrderByValue > item.OrderByValue)
  303. .ExecuteCommand();
  304. }
  305. rm = new SuccessResponseMessage(null, i_crm);
  306. rm.DATA.Add(BLWording.REL, iRel);
  307. } while (false);
  308. return rm;
  309. });
  310. }
  311. catch (Exception ex)
  312. {
  313. sMsg = Util.GetLastExceptionMsg(ex);
  314. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), @"程式管理", @"Delete(程式管理(刪除))", @"", @"", @"");
  315. }
  316. finally
  317. {
  318. if (null != sMsg)
  319. {
  320. rm = new ErrorResponseMessage(sMsg, i_crm);
  321. }
  322. }
  323. return rm;
  324. }
  325. #endregion 程式管理(刪除)
  326. #region 程式管理(查詢筆數)
  327. /// <summary>
  328. /// 程式管理(查詢筆數)
  329. /// </summary>
  330. /// <param name="i_crm"></param>
  331. /// <returns></returns>
  332. public ResponseMessage QueryCout(RequestMessage i_crm)
  333. {
  334. ResponseMessage rm = null;
  335. string sMsg = null;
  336. var db = SugarBase.GetIntance();
  337. try
  338. {
  339. do
  340. {
  341. var sProgramID = _fetchString(i_crm, @"ProgramID");
  342. var iCout = db.Queryable<OTB_SYS_ProgramList>()
  343. .Where(x => x.OrgID == i_crm.ORIGID)
  344. .WhereIF(!string.IsNullOrEmpty(sProgramID), x => x.ProgramID == sProgramID)
  345. .Count();
  346. rm = new SuccessResponseMessage(null, i_crm);
  347. rm.DATA.Add(BLWording.REL, iCout);
  348. } while (false);
  349. }
  350. catch (Exception ex)
  351. {
  352. sMsg = Util.GetLastExceptionMsg(ex);
  353. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ProgramMaintain_UpdService), "", "QueryCout(程式管理(查詢筆數))", "", "", "");
  354. }
  355. finally
  356. {
  357. if (null != sMsg)
  358. {
  359. rm = new ErrorResponseMessage(sMsg, i_crm);
  360. }
  361. }
  362. return rm;
  363. }
  364. #endregion 程式管理(查詢筆數)
  365. private class ModuleInfo
  366. {
  367. public ModuleInfo()
  368. {
  369. ModuleID = @"";
  370. ModuleName = @"";
  371. PrgCount = 0;
  372. OrderByValue = 1;
  373. }
  374. public string ModuleID { get; set; }
  375. public string ModuleName { get; set; }
  376. public int PrgCount { get; set; }
  377. public int OrderByValue { get; set; }
  378. }
  379. }
  380. }