quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
use app\common\model\user\User as UserModel;
use app\common\model\branch\Branch as BranchModel;
use app\common\model\settings\Region;
 
/**
 * 连盟成员模型
 */
class Member extends BaseModel
{
    protected $name = 'branch_member';
    protected $pk = 'user_id';
 
    /**
     * 追加字段
     * @var string[]
     */
    protected $append = ['region'];
    
    /**
     * 关联所属分会表
     */
    public function branch()
    {
        return $this->belongsTo('app\\common\\model\\branch\\Branch', 'branch_id', 'branch_id');
    }
 
    /**
     * 关联所属分会表
     */
    public function supplier()
    {
        return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
    }
    
    /**
     * 关联会员记录表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User');
    }
 
    /**
     * 关联职务
     */
    public function position()
    {
        return $this->belongsTo('app\\common\\model\\branch\\Position', 'position_id', 'position_id');
    }
 
    /**
     * 籍贯
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        if (!isset($data['province_id'])) {
            return false;
        }
        return [
            'province' => Region::getNameById($data['province_id']),
            'city' => Region::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : Region::getNameById($data['region_id']),
        ];
    }
 
    /**
     * 详情
     */
    public static function detail($user_id, $with = ['user'])
    {
        return (new static())->with($with)->find($user_id);
    }
 
    /**
     * 是否已经是分会成员
     */
    public static function isMember($user_id)
    {
        $member = self::detail($user_id);
        return !!$member && !$member['is_delete'];
    }
 
    /**
     * 统计成员数量
     */
    public static function memberCount($branch_id = 0)
    {
        $model = new static();
        if($branch_id > 0) {
            $model->where('branch_id', '=', $branch_id);
        }
        return $model->where('is_delete', '=', 0)
        ->count();
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        if (self::isMember($data['user_id'])) {
            $this->error = '该用户已入会';
            return false;
        }
        $data['app_id'] = self::$app_id;
        $this->save($data);
        // 更新分会人数
        BranchModel::incTotal($data['branch_id']);
        // 检查真实姓名和电话有没有更改
        $user = UserModel::detail($data['user_id']);
        $userData = [];
        if ($user['real_name'] != $data['real_name']) {
            $userData['real_name'] = $data['real_name'];
        }
        if ($user['mobile'] != $data['mobile']) {
            $userData['mobile'] = $data['mobile'];
        }
        if ($user['avatarUrl'] != $data['avatarUrl']) {
            $userData['avatarUrl'] = $data['avatarUrl'];
        }
        if ($userData) {
            $user->save($userData);
        }
        return true;
    }
 
    /**
     * 修改记录
     */
    public function edit($data)
    {
        $this->save($data);
        // 检查真实姓名和电话有没有更改
        $user = UserModel::detail($this['user_id']);
        $userData = [];
        if ($user['real_name'] != $data['real_name']) {
            $userData['real_name'] = $data['real_name'];
        }
        if ($user['mobile'] != $data['mobile']) {
            $userData['mobile'] = $data['mobile'];
        }
        if ($user['avatarUrl'] != $data['avatarUrl']) {
            $userData['avatarUrl'] = $data['avatarUrl'];
        }
        if ($userData) {
            $user->save($userData);
        }
        return true;
    }
 
    /**
     * 获取会员数据 (可指定某天)
     */
    public function getMemberTotal($startDate = null, $endDate = null, $branch_id = 0)
    {
        $model = $this;
 
        !is_null($startDate) && $model = $model->where('create_time', '>=', strtotime($startDate));
 
        if (is_null($endDate)) {
            !is_null($startDate) && $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('create_time', '<', strtotime($endDate) + 86400);
        }
 
        if ($branch_id > 0) {
            $model = $model->where('branch_id', '=', $branch_id);
        }
 
        $model = $model->where('is_delete', '=', 0);
        return $model->count();
    }
 
    public function memberGroupNum($date, $branch_id)
    {
        $field = 'count(*) as total,from_unixtime(create_time,\'%m-%d\') as `day`';
        if ($date == 'year') {
            $field = 'count(*) as total,from_unixtime(create_time,\'%m\') as `day`';
        }
        $model = $this->field($field)
            ->where('is_delete', '=', 0)
            ->when($date, function ($query, $date) {
                getModelTime($query, $date, 'create_time');
            })->when($branch_id, function ($query, $branch_id) {
                $query->where('branch_id', $branch_id);
            });
        return $model->order('create_time ASC')->group('day')->select();
    }
 
}