Message.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Date;
  4. use Yii;
  5. use yii\base\Exception;
  6. /**
  7. * This is the model class for table "{{%MESSAGE}}".
  8. *
  9. * @property string $ID
  10. * @property string $USER_ID 会员ID
  11. * @property string $TEXT_ID 站内信内容ID
  12. * @property int $IS_READ 是否已读
  13. * @property int $IS_DEL 是否删除
  14. * @property int $CREATED_AT 创建时间
  15. * @property int $READ_AT 已读时间
  16. * @property int $DELETED_AT 删除时间
  17. */
  18. class Message extends \common\components\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%MESSAGE}}';
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['USER_ID', 'TEXT_ID', 'CREATED_AT'], 'required'],
  34. [['IS_READ', 'IS_DEL', 'CREATED_AT', 'READ_AT', 'DELETED_AT'], 'integer'],
  35. [['ID', 'USER_ID', 'TEXT_ID'], 'string', 'max' => 32],
  36. [['ID'], 'unique'],
  37. ];
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'ID' => 'ID',
  46. 'USER_ID' => '会员ID',
  47. 'TEXT_ID' => '站内信内容ID',
  48. 'IS_READ' => '是否已读',
  49. 'IS_DEL' => '是否删除',
  50. 'CREATED_AT' => '创建时间',
  51. 'READ_AT' => '已读时间',
  52. 'DELETED_AT' => '删除时间',
  53. ];
  54. }
  55. /**
  56. * 获取未读数量
  57. * @param $userId
  58. * @return int|string
  59. */
  60. public static function unreadNum($userId){
  61. return static::findUseSlaves()->where('USER_ID=:USER_ID AND IS_READ=0 AND IS_DEL=0', [':USER_ID'=>$userId])->asArray()->count();
  62. }
  63. /**
  64. * 抓取消息
  65. * @param $userId
  66. * @return bool
  67. * @throws \yii\db\Exception
  68. */
  69. public static function pullMsgByUser($userId){
  70. // 获取会员最后一次抓取信息的时间
  71. $userInfo = UserInfo::findUseSlaves()->select('PULLED_AT')->where('USER_ID=:USER_ID', [':USER_ID'=>$userId])->asArray()->one();
  72. if(!$userInfo){
  73. return false;
  74. }
  75. $allMessageText = MessageText::findUseSlaves()->select('ID')->where('CREATED_AT>=:LAST_PULL_TIME AND IS_DEL=0', [':LAST_PULL_TIME'=>$userInfo['PULLED_AT']])->asArray()->all();
  76. $db = Yii::$app->db;
  77. $transaction = $db->beginTransaction();
  78. try {
  79. $insertData = [];
  80. foreach($allMessageText as $messageText){
  81. $insertData[] = [
  82. 'USER_ID' => $userId,
  83. 'TEXT_ID' => $messageText['ID'],
  84. 'CREATED_AT' => Date::nowTime(),
  85. ];
  86. }
  87. Message::batchInsert($insertData);
  88. // 更新最后抓取时间
  89. $oneUserAr = UserInfo::findOne(['USER_ID'=>$userId]);
  90. $oneUserAr->PULLED_AT = Date::nowTime();
  91. if(!$oneUserAr->save()){
  92. throw new Exception('更新抓取消息时间失败');
  93. }
  94. $transaction->commit();
  95. } catch (Exception $e) {
  96. $transaction->rollBack();
  97. return false;
  98. }
  99. return true;
  100. }
  101. }