MailService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCases;
  5. use common\models\BaseUser;
  6. use common\models\Mail;
  7. use Yii;
  8. use yii\db\ActiveQuery;
  9. class MailService
  10. {
  11. /**
  12. * @return ActiveQuery
  13. */
  14. public static function getMailQuery(): ActiveQuery
  15. {
  16. return Mail::find()->where([">=", "status", 0]);
  17. }
  18. public static function getMailAllQuery(): ActiveQuery
  19. {
  20. return Mail::find()->where([">=", "status", 0]);
  21. }
  22. public static function sendCommitMail($model, $remark): void
  23. {
  24. self::sendMail($model, "提交审核", $remark);
  25. }
  26. public static function sendRefuseMail($model, $remark): void
  27. {
  28. self::sendMail($model, "退回", $remark);
  29. }
  30. public static function getUnreadMessagesNumber($userInfo): int
  31. {
  32. /** @var $userInfo BaseUser */
  33. $redis = Yii::$app->redis;
  34. $redisKey = "unreadMessagesNumber_" . $userInfo->username;
  35. $number = $redis->get($redisKey);
  36. if (!$number) {
  37. $query = MailService::getMailQuery();
  38. $number = $query->andWhere(["receiver" => $userInfo->username, "status" => 0])->count();
  39. $redis->set($redisKey, $number);
  40. $redis->expire($redisKey, 86400); // 86400
  41. }
  42. return (int)$number;
  43. }
  44. /**
  45. * @throws AjaxException
  46. */
  47. public static function readMessage($id, $userInfo): void
  48. {
  49. $mail = Mail::findOne(["id" => $id, "receiver" => $userInfo->username]);
  50. if (!$mail) {
  51. throw new AjaxException("消息不存在!");
  52. }
  53. $mail->status = 1;
  54. $mail->save();
  55. //删除键值
  56. $redis = Yii::$app->redis;
  57. $redisKey = "unreadMessagesNumber_" . $userInfo->username;
  58. $redis->del($redisKey);
  59. }
  60. /**
  61. * @throws AjaxException
  62. */
  63. public static function delete($id, $userInfo): void
  64. {
  65. $mail = Mail::findOne(["id" => $id, "receiver" => $userInfo->username]);
  66. if (!$mail) {
  67. throw new AjaxException("消息不存在!");
  68. }
  69. $mail->status = -1;
  70. $mail->save();
  71. $redis = Yii::$app->redis;
  72. $redisKey = "unreadMessagesNumber_" . $userInfo->username;
  73. $redis->del($redisKey);
  74. }
  75. protected static function sendMail($caseModel, $caseType, $remark): void
  76. {
  77. /** @var $caseModel AccidentCases */
  78. if ($caseType == "提交审核" || $caseType == "退回") {
  79. $param = [
  80. "accident_id",
  81. "title",
  82. "content",
  83. "status",
  84. "start_time",
  85. "update_time",
  86. "sender",
  87. "sender_name",
  88. "receiver",
  89. "receiver_name",
  90. "case_type",
  91. ];
  92. $time = date("Y-m-d H:i:s");
  93. $receiverList = [];
  94. $receiverNameList = [];
  95. if ($caseType == "提交审核") {
  96. $sender = $caseModel->create_by != "" ? $caseModel->create_by : "admin";
  97. $senderName = $caseModel->create_name != "" ? $caseModel->create_name : "超级管理员";
  98. $checkList = json_decode($caseModel->check_list);
  99. $checkNameList = json_decode($caseModel->check_name_list);
  100. if ($checkList == []) {
  101. //发送给所有符合条件的
  102. $userList = UserService::getCanCheckList();
  103. foreach ($userList as $user) {
  104. /** @var $user BaseUser */
  105. $receiverList[] = $user->username;
  106. $receiverNameList[] = $user->name;
  107. }
  108. } else {
  109. $receiverList = $checkList;
  110. $receiverNameList = $checkNameList;
  111. }
  112. } else {
  113. $sender = $caseModel->check_by != "" ? $caseModel->check_by : "admin";
  114. $senderName = $caseModel->check_name != "" ? $caseModel->check_name : "超级管理员";
  115. $receiverList[] = $caseModel->create_by != "" ? $caseModel->create_by : "admin";;
  116. $receiverNameList[] = $caseModel->create_name != "" ? $caseModel->create_name : "超级管理员";;
  117. }
  118. //填写数据
  119. $paramData = [];
  120. foreach ($receiverList as $key => $value) {
  121. $paramData[] = [
  122. "accident_id" => $caseModel->id,
  123. "title" => $caseModel->title,
  124. "content" => $remark,
  125. "status" => 0,
  126. "start_time" => $time,
  127. "update_time" => $time,
  128. "sender" => $sender,
  129. "sender_name" => $senderName,
  130. "receiver" => $value,
  131. "receiver_name" => $receiverNameList[$key],
  132. "case_type" => $caseType,
  133. ];
  134. }
  135. //保存数据
  136. if ($paramData != []) {
  137. Yii::$app->db->createCommand()->batchInsert(Mail::tableName(), $param, $paramData)->execute();
  138. }
  139. }
  140. }
  141. }