MailSettingsController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using Cksoft.Unity.Log4NetConfig;
  5. using DllEapDal;
  6. using DllEapEntity;
  7. using DllUfpDal;
  8. using DllUfpEntity;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Newtonsoft.Json;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Threading.Tasks;
  16. namespace DllEapBll.Controllers
  17. {
  18. /// <summary>
  19. /// 邮件设置
  20. /// </summary>
  21. [Authorize]
  22. [ApiController]
  23. [Route("eap/api/[controller]/[action]")]
  24. public class MailSettingsController : ControllerBase
  25. {
  26. private readonly string conn = "eap";
  27. /// <summary>
  28. /// 获取邮件设置项列表
  29. /// </summary>
  30. /// <param name="pageIndex"></param>
  31. /// <param name="pageSize"></param>
  32. /// <param name="filter"></param>
  33. /// <param name="sort"></param>
  34. /// <param name="order"></param>
  35. /// <returns></returns>
  36. [HttpGet]
  37. public LayuiModel<MailSettings> GetMailSettingsList(int pageIndex = 1, int pageSize = 10, string filter = "", string sort = "FCode", string order = "ASC")
  38. {
  39. if (string.IsNullOrEmpty(sort) || sort.ToString().ToLower() == "null")
  40. sort = "FCode";
  41. if (order == "descend")
  42. order = "desc";
  43. else
  44. {
  45. order = "asc";
  46. }
  47. IEnumerable<MailSettings> list = null;
  48. filter = filter ?? " ";
  49. using (IDatabase db = DbFactory.Base("eapslave"))
  50. {
  51. var dal = new MailSettingsDal(db);
  52. int start = (pageIndex - 1) * pageSize + 1;
  53. string errorinfo = string.Empty;
  54. list = dal.GetMailSettingsList(start, pageSize, filter, sort, order, ref errorinfo);
  55. var count = dal.GetMailSettingsCount(filter);
  56. var responseData = new LayuiModel<MailSettings>()
  57. {
  58. code = 0,
  59. count = count,
  60. data = list,
  61. msg = ""
  62. };
  63. return responseData;
  64. }
  65. }
  66. /// <summary>
  67. /// 设置详情
  68. /// </summary>
  69. /// <param name="id"></param>
  70. /// <returns></returns>
  71. [HttpGet]
  72. public MailSettings GetMailSettingsDetail(int id)
  73. {
  74. using (IDatabase db = DbFactory.Base("eapslave"))
  75. {
  76. var dal = new MailSettingsDal(db);
  77. MailSettings staff = dal.GetMailSettings(id);
  78. return staff;
  79. }
  80. }
  81. /// <summary>
  82. /// 新增/修改设置
  83. /// </summary>
  84. /// <param name="model"></param>
  85. /// <returns></returns>
  86. [HttpPost]
  87. public string AddOrUpdateMailSettings([FromBody] MailSettings model)
  88. {
  89. var id = model.ID;
  90. string errorinfo = string.Empty;
  91. var usercode = Request.Headers["usercode"];
  92. IDatabase db = DbFactory.Base(conn);
  93. try
  94. {
  95. db.BeginTrans();
  96. var dal = new MailSettingsDal(db);
  97. if (id == 0)
  98. {
  99. model.RecCode = usercode;
  100. model.ModCode = usercode;
  101. model.MailBody = string.IsNullOrEmpty(model.MailBody) ? "" : model.MailBody;
  102. model.Remark = string.IsNullOrEmpty(model.Remark) ? "" : model.Remark;
  103. var resInsert = dal.InsertMailSettings(model, ref errorinfo);
  104. if (resInsert > 0)
  105. {
  106. db.Commit();
  107. LogHelper<MailSettings>.LogFatal("新增MailSettings-->" + Json.ToJson(model), "用户操作", model.RecCode);
  108. return JsonConvert.SerializeObject(new { id = resInsert, msg = string.Empty });
  109. }
  110. db.Rollback();
  111. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo });
  112. }
  113. var oldModel = dal.GetMailSettings(id);
  114. if (oldModel == null)
  115. return JsonConvert.SerializeObject(new { id = "0", msg = "此邮件类别不存在" });
  116. model.ModCode = usercode;
  117. model.ModTime = DateTime.Now;
  118. var resUpdate = dal.UpdateMailSettings(model, usercode, ref errorinfo);
  119. if (resUpdate > 0)
  120. {
  121. db.Commit();
  122. LogHelper<MailSettings>.LogFatal("修改MailSettings-->原始数据:" + Json.ToJson(oldModel) + " 新数据:" + Json.ToJson(model), "用户操作", model.RecCode);
  123. return JsonConvert.SerializeObject(new { id = resUpdate });
  124. }
  125. db.Rollback();
  126. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ;
  127. }
  128. catch (Exception e)
  129. {
  130. db.Rollback();
  131. return JsonConvert.SerializeObject(new { id = "0", msg = e.Message });
  132. }
  133. finally
  134. {
  135. if (db != null)
  136. db.Close();
  137. }
  138. }
  139. /// <summary>
  140. /// 删除邮件设置
  141. /// </summary>
  142. /// <param name="id"></param>
  143. /// <returns></returns>
  144. [HttpPost]
  145. public async Task<EapResponse> Delete([FromBody] int id)
  146. {
  147. using (IDatabase db = DbFactory.Base("eap"))
  148. {
  149. db.BeginTrans();
  150. var dal = new MailSettingsDal(db);
  151. var res = await dal.DeleteMailSetting(id);
  152. db.Commit();
  153. return res;
  154. }
  155. }
  156. /// <summary>
  157. /// 获取邮件接收人
  158. /// </summary>
  159. /// <param name="pageIndex"></param>
  160. /// <param name="pageSize"></param>
  161. /// <param name="filter"></param>
  162. /// <param name="sort"></param>
  163. /// <param name="order"></param>
  164. /// <returns></returns>
  165. [HttpGet]
  166. public LayuiModel<MailReceiver> GetMailReceiverList(int pageIndex = 1, int pageSize = 10, string filter = "", string sort = "id", string order = "ASC")
  167. {
  168. if (string.IsNullOrEmpty(sort) || sort.ToString().ToLower() == "null")
  169. sort = "id";
  170. if (order == "descend")
  171. order = "desc";
  172. else
  173. {
  174. order = "asc";
  175. }
  176. IEnumerable<MailReceiver> list = null;
  177. filter += " and IsValid=1";
  178. filter = filter ?? " ";
  179. using (IDatabase db = DbFactory.Base("eapslave"))
  180. {
  181. var dal = new MailSettingsDal(db);
  182. int start = (pageIndex - 1) * pageSize + 1;
  183. string errorinfo = string.Empty;
  184. list = dal.GetMailReceiverList(start, pageSize, filter, sort, order, ref errorinfo);
  185. foreach (var model in list)
  186. {
  187. switch (model.SendType)
  188. {
  189. case 1:
  190. model.SendTypeName = "收件人";
  191. break;
  192. case 2:
  193. model.SendTypeName = "抄送人";
  194. break;
  195. }
  196. }
  197. var count = dal.GetMailReceiverCount(filter);
  198. var responseData = new LayuiModel<MailReceiver>()
  199. {
  200. code = 0,
  201. count = count,
  202. data = list,
  203. msg = ""
  204. };
  205. return responseData;
  206. }
  207. }
  208. /// <summary>
  209. /// 接收人详情
  210. /// </summary>
  211. /// <param name="id"></param>
  212. /// <returns></returns>
  213. [HttpGet]
  214. public MailReceiver GetMailReceiverDetail(int id)
  215. {
  216. using (IDatabase db = DbFactory.Base("eapslave"))
  217. {
  218. var dal = new MailSettingsDal(db);
  219. MailReceiver staff = dal.GetMailReceiver(id);
  220. return staff;
  221. }
  222. }
  223. /// <summary>
  224. /// 新增/修改接收人
  225. /// </summary>
  226. /// <param name="model"></param>
  227. /// <returns></returns>
  228. [HttpPost]
  229. public string AddOrUpdateMailReceiver([FromBody] MailReceiver model)
  230. {
  231. var id = model.ID;
  232. string errorinfo = string.Empty;
  233. var usercode = Request.Headers["usercode"];
  234. IDatabase db = DbFactory.Base(conn);
  235. try
  236. {
  237. db.BeginTrans();
  238. var dal = new MailSettingsDal(db);
  239. if (id == 0)
  240. {
  241. model.RecCode = usercode;
  242. model.ModCode = usercode;
  243. model.IsValid = 1;
  244. var resInsert = dal.InsertMailReceiver(model, ref errorinfo);
  245. if (resInsert > 0)
  246. {
  247. db.Commit();
  248. LogHelper<MailReceiver>.LogFatal("新增MailReceiver-->" + Json.ToJson(model), "用户操作", model.RecCode);
  249. return JsonConvert.SerializeObject(new { id = resInsert, msg = string.Empty });
  250. }
  251. db.Rollback();
  252. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo });
  253. }
  254. var oldModel = dal.GetMailReceiver(id);
  255. if (oldModel == null)
  256. return JsonConvert.SerializeObject(new { id = "0", msg = "此收件人不存在" });
  257. model.ModCode = usercode;
  258. model.ModTime = DateTime.Now;
  259. model.IsValid = 1;
  260. int resUpdate = dal.UpdateMailReceiver(model, usercode, ref errorinfo);
  261. if (resUpdate > 0)
  262. {
  263. db.Commit();
  264. LogHelper<MailReceiver>.LogFatal("修改MailReceiver-->原始值:" + Json.ToJson(oldModel) + " 新值:" + Json.ToJson(model), "用户操作", model.RecCode);
  265. return JsonConvert.SerializeObject(new { id = resUpdate });
  266. }
  267. db.Rollback();
  268. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo }); ;
  269. }
  270. catch (Exception e)
  271. {
  272. db.Rollback();
  273. return JsonConvert.SerializeObject(new { id = "0", msg = e.Message });
  274. }
  275. finally
  276. {
  277. if (db != null)
  278. db.Close();
  279. }
  280. }
  281. /// <summary>
  282. /// 删除接收人
  283. /// </summary>
  284. /// <param name="id"></param>
  285. /// <returns></returns>
  286. [HttpGet]
  287. public string DeleteMailReceiver(int id)
  288. {
  289. string errorinfo = string.Empty;
  290. var usercode = Request.Headers["usercode"];
  291. IDatabase db = DbFactory.Base(conn);
  292. try
  293. {
  294. db.BeginTrans();
  295. var dal = new MailSettingsDal(db);
  296. var model = dal.GetMailReceiver(id);
  297. var res = dal.DeleteMailReceiver(id, usercode, ref errorinfo);
  298. if (res > 0)
  299. {
  300. db.Commit();
  301. LogHelper<MailReceiver>.LogFatal("删除MailReceiver-->MailReceiver:" + Json.ToJson(model), "用户操作", usercode);
  302. return JsonConvert.SerializeObject(new { id = res, msg = string.Empty });
  303. }
  304. db.Rollback();
  305. return JsonConvert.SerializeObject(new { id = "0", msg = errorinfo });
  306. }
  307. catch (Exception e)
  308. {
  309. db.Rollback();
  310. return JsonConvert.SerializeObject(new { id = "0", msg = e.Message });
  311. }
  312. finally
  313. {
  314. if (db != null)
  315. db.Close();
  316. }
  317. }
  318. /// <summary>
  319. /// 发送邮件
  320. /// </summary>
  321. /// <param name="code"></param>
  322. /// <param name="type"></param>
  323. /// <returns></returns>
  324. [HttpGet]
  325. [AllowAnonymous]
  326. public string SendMail(string code, int type = 1)
  327. {
  328. if (code == AppConfigurtaionServices.Configuration["DisConnNoticeCode"])
  329. {
  330. var result = MailNotice.DisconnectionNotice(type).Result;
  331. return JsonConvert.SerializeObject(new { code = string.IsNullOrEmpty(result) ? 1 : 0, msg = string.IsNullOrEmpty(result) ? "发送成功" : result });
  332. }
  333. else
  334. {
  335. return JsonConvert.SerializeObject(new { code = 0, msg = "此邮件暂未开发" });
  336. }
  337. }
  338. /// <summary>
  339. /// 发送AA抛料率预警
  340. /// </summary>
  341. /// <returns></returns>
  342. [HttpGet]
  343. [AllowAnonymous]
  344. public async Task<string> SendAAMaterial()
  345. {
  346. var result = await MailNotice.AAMaterialUploadErrorNotice();
  347. return JsonConvert.SerializeObject(new { code = string.IsNullOrEmpty(result) ? 1 : 0, msg = string.IsNullOrEmpty(result) ? "发送成功" : result });
  348. }
  349. /// <summary>
  350. /// 发送断线、IP不一致、小程序未开启预警邮件
  351. /// </summary>
  352. /// <returns></returns>
  353. [HttpGet]
  354. [AllowAnonymous]
  355. public string SendMailNew()
  356. {
  357. var result = MailNotice.IPDifferentAndDiconnectionNotice().Result;
  358. return JsonConvert.SerializeObject(new { code = string.IsNullOrEmpty(result) ? 1 : 0, msg = string.IsNullOrEmpty(result) ? "发送成功" : result });
  359. }
  360. }
  361. }