quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
namespace app\api\controller\branch\admin;
 
use app\api\controller\branch\Admin;
use app\api\service\branch\MemberService;
use app\api\model\branch\ActivityUser as ActivityUserModel;
use app\api\model\branch\Member as MemberModel;
use app\branch\model\position\Position as PositionModel;
use app\common\model\settings\Region as RegionModel;
 
/**
 * 会员数据控制器
 */
class Member extends Admin
{
    /**
     * 成员列表
     */
    public function index()
    {
        $model = new MemberModel;
        $postData = $this->postData();
        $postData['branch_id'] = $this->branch['branch_id'];
        $list = $model->getList($postData);
        
        return $this->renderSuccess('', compact('list'));
    }
    
    /**
     * 新增会员,用于line-chart
     */
    public function newMember($date = '')
    {
        $res = (new MemberService($this->branch['branch_id']))->getNewMemberByDate($date);
        return $this->renderSuccess('', [
            // 日期
            'days' => $res['days'],
            // 数据
            'data' => $res['data'],
        ]);
    }
 
    /**
     * 排行榜
     */
    public function ranking($date = null) {
        $model = new ActivityUserModel;
        $list = $model->getUserRankList($date, $this->branch['branch_id']);
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 新增股东
     */
    public function add()
    {
        if ($this->request->isGet()) {
            $positionList = PositionModel::getAll();
            $regionData = RegionModel::getRegionForApi();
            return $this->renderSuccess('', compact('positionList', 'regionData'));
        }
        $model = new MemberModel;
        // 新增记录
        $data = json_decode($this->request->post('formData', '', null), true);
        $data['branch_id'] = $this->branch['branch_id'];
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
 
    /**
     * 编辑成员
     */
    public function edit($user_id)
    {
        $model = MemberModel::detail($user_id, ['user', 'supplier', 'position']);
        if ($this->request->isGet()) {
            $positionList = PositionModel::getAll();
            $regionData = RegionModel::getRegionForApi();
            return $this->renderSuccess('', compact('positionList', 'regionData', 'model'));
        }
       $data = json_decode($this->request->post('formData', '', null), true);
       if ($model->edit($data)) {
           return $this->renderSuccess('更新成功');
       }
       return $this->renderError($model->getError() ?: '更新失败');
    }
 
    /**
     * 软删除成员
     */
    public function delete($user_id)
    {
        $model = MemberModel::detail($user_id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}