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
96
<?php
 
namespace app\operations\model\plus\business;
 
use app\common\model\plus\business\Grade as GradeModel;
 
/**
 * 名片等级模型
 */
class Grade extends GradeModel
{
    /**
     * 获取列表记录
     */
    public function getList($data = [])
    {
        $list = $this->selectList();
        // 如果为空,则插入默认等级
        if(count($list) == 0) {
            $this->save([
                'name' => '默认等级',
                'price' => 0.00,
                'weight' => 1,
                'app_id' => self::$app_id,
                'create_time' => time(),
                'update_time' => time()
            ]);
            $list = $this->selectList();
        }
        return $list;
    }
    
    /**
     * 新增等级
     */
    public function add($data)
    {
        // 验证数据
        if(empty($data['name'])) {
            $this->error = '等级名称不能为空';
            return false;
        }
        if(!isset($data['price']) || $data['price'] === '') {
            $data['price'] = 0.00;
        } else {
            $data['price'] = floatval($data['price']);
            if($data['price'] < 0) {
                $this->error = '查看联系方式价格不能小于0';
                return false;
            }
        }
        if(!isset($data['weight']) || $data['weight'] === '') {
            $data['weight'] = 100;
        } else {
            $data['weight'] = intval($data['weight']);
            if($data['weight'] < 0) {
                $this->error = '权重不能小于0';
                return false;
            }
        }
        
        return parent::add($data);
    }
    
    /**
     * 编辑等级
     */
    public function edit($data)
    {
        // 验证数据
        if(empty($data['name'])) {
            $this->error = '等级名称不能为空';
            return false;
        }
        if(!isset($data['price']) || $data['price'] === '') {
            $data['price'] = 0.00;
        } else {
            $data['price'] = floatval($data['price']);
            if($data['price'] < 0) {
                $this->error = '查看联系方式价格不能小于0';
                return false;
            }
        }
        if(!isset($data['weight']) || $data['weight'] === '') {
            $data['weight'] = 100;
        } else {
            $data['weight'] = intval($data['weight']);
            if($data['weight'] < 0) {
                $this->error = '权重不能小于0';
                return false;
            }
        }
        
        return parent::edit($data);
    }
}