<?php
|
namespace app\common\model\plus\release;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 消息模型
|
*/
|
class Chat extends BaseModel
|
{
|
protected $pk = 'chat_id';
|
protected $name = 'release_chat';
|
|
/**
|
* 关联会员表
|
*/
|
public function user()
|
{
|
return $this->belongsTo("app\\common\\model\\user\\User", 'user_id', 'user_id');
|
}
|
|
|
|
|
//添加信息
|
public function add($postdata,$user)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
|
$ChatRelation = new ChatRelation();
|
|
$info = $ChatRelation->where('user_id', '=', $user['user_id'])->where('other_user_id', '=', $postdata['you_user_id'])->find();
|
if(empty($info)){
|
$info = $ChatRelation->where('user_id', '=', $postdata['you_user_id'])->where('other_user_id', '=', $user['user_id'])->find();
|
}
|
if (!$info) {
|
$save_data=[
|
'user_id'=>$user['user_id'],
|
'other_user_id'=>$postdata['you_user_id'],
|
'app_id'=>self::$app_id,
|
];
|
$ChatRelation->save($save_data);
|
$relation_id = $ChatRelation['relation_id'];
|
} else {
|
$relation_id = $info['relation_id'];
|
$info->save(['update_time' => time()]);
|
}
|
|
$data=[
|
'user_id'=>$user['user_id'],
|
'content'=>$postdata['content'],
|
'relation_id'=>$relation_id,
|
'app_id'=>self::$app_id,
|
];
|
$this->save($data);
|
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
log_write($e->getMessage());
|
$this->rollback();
|
return false;
|
}
|
|
}
|
}
|