quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\operations\model\plus\agent;
 
use app\common\model\plus\agent\Grade as AgentGradeModel;
use app\operations\model\plus\agent\User as AgentUserModel;
 
/**
 * 用户会员等级模型
 */
class Grade extends AgentGradeModel
{
    /**
     * 获取列表记录
     */
    public function getList()
    {
        $list = $this->selectList();
        // 如果为空,则插入默认等级
        if(count($list) == 0){
            $grade_id=Grade::getDefaultGradeId();
            $where['app_id'] = self::$app_id;
            $where['grade_id'] = 0;
            // 更新之前的默认为0的id为此等级id
            (new AgentUserModel())->where($where)->update([
                'grade_id' => $grade_id
            ]);
            $list = $this->selectList();
        }
        return $list;
    }
 
 
    private function selectList(){
        return $this->where('is_delete', '=', 0)
            ->order(['weight' => 'asc', 'create_time' => 'asc'])
            ->select();
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        $data['is_default'] = 0;
        $data['remark'] = $this->setRemark($data);
        return $this->save($data);
    }
 
    /**
     * 编辑记录
     */
    public function edit($data)
    {
        if($this['is_default'] == 0){
            $data['remark'] = $this->setRemark($data);
        }
        return $this->save($data);
    }
 
    private function setRemark($data){
        $remark = '';
        if($data['open_agent_money'] == 1){
            $money = sprintf('%.2f',$data['agent_money']);
            $remark .= "推广金额满{$money}元";
        }
        if($data['open_agent_user'] == 1){
            if(!empty($remark)){
                $remark .= '\r\n';
            }
            $remark .= "直推分销商满{$data['agent_user']}";
        }
        if($data['is_purchase_count'] == 1){
            if(!empty($remark)){
                $remark .= '\r\n';
            }
            $remark .= "购买VIP专区商品次数满{$data['purchase_count']}次";
        }
        return $remark;
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        // 判断该等级下是否存在会员
        if (AgentUserModel::checkExistByGradeId($this['grade_id'])) {
            return false;
        }
        return $this->save(['is_delete' => 1]);
    }
 
}