1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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;
        }
 
    }
}