cxfoot/models/msg/UserSysMsg.php
2023-10-27 14:25:12 +08:00

152 lines
5.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models\msg;
use Yii;
use app\models\Model;
/**
* This is the model class for table "{{%user_sys_msg}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $sys_msg_id 系统消息ID
* @property int $user_id 用户ID
* @property int $is_read 是否已读0=否1=是
* @property int $read_time 阅读时间
* @property int $is_sound 是否提醒0=否1=是
* @property int $sound_time 提醒时间
* @property int $created_at 添加时间
* @property int $updated_at 更新时间
* @property int $is_delete 是否删除0=否1=是
* @property int $deleted_at 删除时间
*/
class UserSysMsg extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%user_sys_msg}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['cx_mch_id', 'sys_msg_id', 'user_id', 'is_read', 'read_time', 'is_sound', 'sound_time', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
[['sys_msg_id', 'user_id'], 'required'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'sys_msg_id' => '系统消息ID',
'user_id' => '用户ID',
'is_read' => '是否已读0=否1=是',
'read_time' => '阅读时间',
'is_sound' => '是否提醒0=否1=是',
'sound_time' => '提醒时间',
'created_at' => '添加时间',
'updated_at' => '更新时间',
'is_delete' => '是否删除0=否1=是',
'deleted_at' => '删除时间',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
$this->created_at = time();
}
$this->updated_at = time();
if($this->is_delete == 1)
$this->deleted_at = time();
return true;
} else {
return false;
}
}
/**
* 阅读消息
*/
public static function handleRead($sys_msg_id, $user_id, $cx_mch_id = 0)
{
$model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]);
if($model != null && $model->is_read == 1){
return Model::asReturnError('消息已读');
}
$sys_msg = SysMsg::findOne(['id' => $sys_msg_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0, 'status' => SysMsg::STATUS_PUBLISHED]);
if($sys_msg == null){
return Model::asReturnError('系统消息不存在');
}
if($model == null){
$model = new UserSysMsg();
$model->cx_mch_id = $cx_mch_id;
$model->user_id = $user_id;
$model->sys_msg_id = $sys_msg_id;
}
$model->is_read = 1;
$model->read_time = time();
if(!$model->save()){
return (new Model())->getModelError($model);
}
return Model::asReturnSuccess();
}
/**
* 消息已提醒
*/
public static function handleSound($sys_msg_id, $user_id, $cx_mch_id = 0)
{
$model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]);
if($model != null && $model->is_sound == 1){
return Model::asReturnError('消息已提醒');
}
$sys_msg = SysMsg::findOne(['id' => $sys_msg_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0, 'status' => SysMsg::STATUS_PUBLISHED]);
if($sys_msg == null){
return Model::asReturnError('系统消息不存在');
}
if($model == null){
$model = new UserSysMsg();
$model->cx_mch_id = $cx_mch_id;
$model->user_id = $user_id;
$model->sys_msg_id = $sys_msg_id;
}
$model->is_sound = 1;
$model->sound_time = time();
if(!$model->save()){
return (new Model())->getModelError($model);
}
return Model::asReturnSuccess();
}
/**
* 获取消息状态
* @param integer $type 类型0=消息阅读状态1=消息提醒状态
* return boolean
*/
public static function getUserSysStatus($sys_msg_id, $user_id, $type, $cx_mch_id = 0)
{
$model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]);
if($model == null)
return false;
if($type == 0 && $model->is_read == 1)
return true;
if($type == 1 && $model->is_sound == 1)
return true;
return false;
}
}