Mail.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace backend\models;
  3. use backend\helpers\DateTimeHelper;
  4. use backend\helpers\ValidatorHelper;
  5. use Yii;
  6. use yii\helpers\Html;
  7. /**
  8. * This is the model class for table "crm_mail".
  9. *
  10. * @property string $id
  11. * @property integer $type
  12. * @property string $subject
  13. * @property string $content
  14. * @property integer $state
  15. * @property string $in_time
  16. * @property string $other
  17. */
  18. class Mail extends \yii\db\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return 'crm_mail';
  26. }
  27. /**
  28. * @return \yii\db\Connection the database connection used by this AR class.
  29. */
  30. public static function getDb()
  31. {
  32. return Yii::$app->get('dbXcrm');
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['type', 'subject', 'content', 'state', 'in_time'], 'required'],
  41. [['type', 'state', 'in_time'], 'integer'],
  42. [['content', 'other'], 'string'],
  43. [['subject'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'type' => 'Type',
  54. 'subject' => 'Subject',
  55. 'content' => 'Content',
  56. 'state' => 'State',
  57. 'in_time' => 'In Time',
  58. 'other' => 'Other',
  59. ];
  60. }
  61. public function sendEvent($event)
  62. {
  63. $mail = Mail::findOne(['id' => $event->mailId]);
  64. $columns = ['subject', 'content', 'receiver', 'status', 'in_time'];
  65. $members = [];
  66. // 防止去数据库查询的时候内存报错
  67. ini_set ('memory_limit', '1024M');
  68. if ((int)$event->type == 0) {
  69. $members = Member::find()->select('username')->where(['type' => 1])
  70. ->asArray()->all();
  71. } elseif ((int)$event->type == 1) {
  72. $members = Member::find()->select('username')->where(['type' => 2])
  73. ->asArray()->all();
  74. } elseif ((int)$event->type == 2) {
  75. $members = Member::find()->select('username')->where(['type' => [1, 2]])
  76. ->asArray()->all();
  77. } elseif ((int)$event->type == 3) {
  78. if (!empty($mail->other)) {
  79. $username = explode(',', $mail->other);
  80. foreach ($username as $member) {
  81. $members[] = ['username' => $member];
  82. }
  83. }
  84. }
  85. // 解决mysql server has gone away 问题
  86. if (count($members) > 5000) {
  87. $this->bigDataInsert($members, $mail);
  88. } else {
  89. $dateHelper = new DateTimeHelper();
  90. $rows = [];
  91. foreach ($members as $member) {
  92. if (!ValidatorHelper::isEmail($member['username'])) {
  93. continue;
  94. }
  95. $rows[] = [
  96. $mail->subject,
  97. $mail->content,
  98. $member['username'],
  99. 0,
  100. $dateHelper->microtime_float(),
  101. ];
  102. }
  103. $connection = MailRecord::getDb()->createCommand();
  104. $connection->batchInsert(MailRecord::tableName(), $columns, $rows)->execute();
  105. }
  106. }
  107. /**
  108. * 分批插入数据
  109. */
  110. public function bigDataInsert($members, $mail)
  111. {
  112. $affectedNum = 0;
  113. $dateHelper = new DateTimeHelper();
  114. $chu = (int)(count($members) / 5000);//取整
  115. $yu = count($members) % 5000;// 取余
  116. $time = $dateHelper->microtime_float();
  117. $transaction = $this->getDb()->beginTransaction();
  118. try {
  119. // 防止插入的时候内存报错
  120. ini_set ('memory_limit', '1024M');
  121. // 设置响应时间
  122. set_time_limit(0);
  123. for ($i = 0; $i < $chu; $i++) {
  124. $rows = [];
  125. for ($j = $i * 5000; $j < ($i + 1) * 5000; $j++) {
  126. if (!ValidatorHelper::isEmail($members[$j]['username'])) {
  127. continue;
  128. }
  129. $rows[] = [
  130. 'subject' => $mail->subject,
  131. 'content' => $mail->content,
  132. 'receiver' => $members[$j]['username'],
  133. 'status' => 0,
  134. 'in_time' => $time,
  135. ];
  136. }
  137. if ($rows) {
  138. $command = $this->getDb()->createCommand();
  139. $columns = array_combine(array_keys($rows[0]), array_keys($rows[0]));
  140. $sql = $command->batchInsert(MailRecord::tableName(), $columns, $rows)->getSql();
  141. $affectedNum += $command->setSql(str_ireplace("INSERT INTO", "INSERT IGNORE INTO", $sql))->execute();
  142. }
  143. }
  144. if ($yu > 0) {
  145. $rows_yu = [];
  146. for ($m = 0; $m < $yu; $m++) {
  147. if (!ValidatorHelper::isEmail($members[$affectedNum+$m]['username'])) {
  148. continue;
  149. }
  150. $rows_yu[] = [
  151. 'subject' => $mail->subject,
  152. 'content' => $mail->content,
  153. 'receiver' => $members[$affectedNum+$m]['username'],
  154. 'status' => 0,
  155. 'in_time' => $time,
  156. ];
  157. }
  158. if ($rows_yu) {
  159. $command = $this->getDb()->createCommand();
  160. $columns = array_combine(array_keys($rows_yu[0]), array_keys($rows_yu[0]));
  161. $sql = $command->batchInsert(MailRecord::tableName(), $columns, $rows_yu)->getSql();
  162. $command->setSql(str_ireplace("INSERT INTO", "INSERT IGNORE INTO", $sql))->execute();
  163. }
  164. }
  165. $transaction->commit();
  166. } catch (\Exception $e) {
  167. $transaction->rollBack();
  168. }
  169. }
  170. }