quanwei
2025-10-30 204b4cb1fcf1234010f722e0c9d4e88d10e654b1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
 
namespace app\admin\model;
 
use app\admin\model\page\Page as PageModel;
use app\common\model\shop\Agent as AgentModel;
use app\common\model\agent\User as AgentUser;
use app\admin\model\user\Grade as GradeModel;
 
use app\common\model\agent\LoginLog as LoginLogModel;
use app\common\model\agent\Client as ClientModel;
 
class Agent extends AgentModel
{
    /**
     * 获取小程序列表
     */
    public function getList($limit, $is_recycle = false)
    {
        
        $data=$this->alias('agent')->field(['agent.*,user.user_name'])->where('is_recycle', '=', (int)$is_recycle)
            ->join('dlagent_user user', 'user.agent_id = agent.agent_id','left')
            ->where('agent.parent_id', '=', 0)
            ->where('user.is_super', '=', 1)
            ->where('agent.is_delete', '=', 0)
            ->order(['create_time' => 'asc'])
            ->paginate($limit);
        $data->each(function($item,$key){
                      $item['lastlogin_time']=$this->getlastLogin($item['agent_id']);
                      $item['client_num']=$this->getClinetTotal($item['agent_id']);
        });
        return $data;
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
        if (AgentUser::checkExist($data['user_name'])) {
            $this->error = '代理商用户名已存在';
            return false;
        }
        $this->startTrans();
        try {
            // 添加小程序记录
            $this->save($data);
           // 新增商家用户信息
           $AgentUser = new AgentUser;
           if (!$AgentUser->dladd($this['agent_id'], $data)) {
               $this->error = $AgentUser>error;
               return false;
           }
           // 新增应用diy配置
           (new PageModel)->insertDefault($this['agent_id']);
           // 默认等级
           (new GradeModel)->insertDefault($this['agent_id']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 修改记录
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            $save_data = [
                'agent_name' => $data['agent_name'],
                'agent_phone' => $data['agent_phone'],
                'email' => $data['email'],
                'duetime'=>$data['duetime'],
                'level'=>$data['level']
            ];
            $this->save($save_data);
            
            $user_data = [
                'user_name' => $data['user_name']
            ];
            if (!empty($data['password'])) {
                $user_data['password'] = salt_hash($data['password']);
            }
            $agent_user = (new AgentUser())->where('agent_id', '=', $this['agent_id'])->where('is_super', '=', 1)->find();
            if($agent_user['user_name'] != $data['user_name']){
                if (AgentUser::checkExist($data['user_name'])) {
                    $this->error = '代理商用户名已存在';
                    return false;
                }
            }
             
            $agent_user->save($user_data);
            
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
    /**
     * 移入移出回收站
     */
    public function recycle($is_recycle = true)
    {
        return $this->save(['is_recycle' => (int)$is_recycle]);
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
    /**
     * 获取最后登录时间
     */
    public function getlastLogin($agent_id)
    {
        $model = new LoginLogModel;
        
        $model = $model->where('agent_id', '=', $agent_id)->order(['create_time' => 'desc'])->find();
       
        return $model['create_time'];
    }
    //获取客户总数
    public function getClinetTotal($agent_id){
        $ClientModel = new ClientModel;
        $list = $ClientModel->where('agent_id','=',$agent_id)->where('is_delete', '=', 0)->select();
        //dd($agent_id);
        return count($list);
    }
}