commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use Yii; |
|
6 |
use yii\db\Exception; |
|
7 |
|
|
8 |
/** |
|
9 |
* This is the model class for table "{{%goods_cards}}". |
|
10 |
* |
|
11 |
* @property int $id |
|
12 |
* @property int $mall_id |
|
13 |
* @property int $mch_id |
|
14 |
* @property string $name |
|
15 |
* @property string $expire_type |
|
16 |
* @property string $expire_day |
|
17 |
* @property string $begin_time |
|
18 |
* @property string $end_time |
|
19 |
* @property int $total_count 卡券数量 |
|
20 |
* @property string $pic_url |
|
21 |
* @property string $description |
|
22 |
* @property string $created_at |
|
23 |
* @property string $updated_at |
|
24 |
* @property string $deleted_at |
|
25 |
* @property int $is_delete |
|
26 |
* @property int $number |
|
27 |
* @property string $app_share_title |
|
28 |
* @property string $app_share_pic |
|
29 |
* @property int $is_allow_send 是否允许转赠 |
|
30 |
*/ |
|
31 |
class GoodsCards extends ModelActiveRecord |
|
32 |
{ |
|
33 |
/** |
|
34 |
* {@inheritdoc} |
|
35 |
*/ |
|
36 |
public static function tableName() |
|
37 |
{ |
|
38 |
return '{{%goods_cards}}'; |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* {@inheritdoc} |
|
43 |
*/ |
|
44 |
public function rules() |
|
45 |
{ |
|
46 |
return [ |
|
47 |
[['mall_id', 'description', 'created_at', 'updated_at', 'deleted_at'], 'required'], |
|
48 |
[['mall_id', 'mch_id', 'expire_type', 'expire_day', 'total_count', 'is_delete', 'number', 'is_allow_send'], 'integer'], |
|
49 |
[['description'], 'string'], |
|
50 |
[['begin_time', 'end_time', 'created_at', 'updated_at', 'deleted_at'], 'safe'], |
|
51 |
[['name'], 'string', 'max' => 65], |
|
52 |
[['pic_url', 'app_share_title', 'app_share_pic'], 'string', 'max' => 255], |
|
53 |
]; |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* {@inheritdoc} |
|
58 |
*/ |
|
59 |
public function attributeLabels() |
|
60 |
{ |
|
61 |
return [ |
|
62 |
'id' => 'ID', |
|
63 |
'mall_id' => 'Mall ID', |
|
64 |
'mch_id' => '多商户ID', |
|
65 |
'name' => '卡券名称', |
|
66 |
'expire_type' => '有效期类型', |
|
67 |
'expire_day' => '有效期天数', |
|
68 |
'begin_time' => '有效期区间', |
|
69 |
'end_time' => '有效期区间', |
|
70 |
'pic_url' => '卡券图标', |
|
71 |
'description' => '卡券描述', |
|
72 |
'total_count' => '卡券数量', |
|
73 |
'created_at' => 'Created At', |
|
74 |
'updated_at' => 'Updated At', |
|
75 |
'deleted_at' => 'Deleted At', |
|
76 |
'is_delete' => 'Is Delete', |
|
77 |
'number' => '卡券可核销总次数', |
|
78 |
'app_share_title' => 'App Share Title', |
|
79 |
'app_share_pic' => 'App Share Pic', |
|
80 |
'is_allow_send' => '是否允许转赠', |
|
81 |
]; |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* @param $num integer 修改的数量 |
|
86 |
* @param $type string 增加add|减少sub |
|
87 |
* @param null|integer $cardId 卡券ID |
|
88 |
* @return self|null |
|
89 |
* @throws Exception |
|
90 |
*/ |
|
91 |
public function updateCount($type, $num, $cardId = null) |
|
92 |
{ |
|
93 |
if ($cardId) { |
|
94 |
$card = self::findOne(['id' => $cardId, 'is_delete' => 0]); |
|
95 |
} else { |
|
96 |
$card = $this; |
|
97 |
} |
|
98 |
if (!$card || !$card->id) { |
|
99 |
throw new Exception('错误的卡券信息'); |
|
100 |
} |
|
101 |
|
|
102 |
if ($card->total_count == -1) { |
|
103 |
return $card; |
|
104 |
} |
|
105 |
if ($type === 'add') { |
|
106 |
$card->total_count += $num; |
|
107 |
} elseif ($type === 'sub') { |
|
108 |
if ($card->total_count < $num) { |
|
109 |
throw new Exception('卡券可发放数量不足'); |
|
110 |
} |
|
111 |
$card->total_count -= $num; |
|
112 |
} else { |
|
113 |
throw new Exception('错误的$type'); |
|
114 |
} |
|
115 |
if ($card->save()) { |
|
116 |
return $card; |
|
117 |
} else { |
|
118 |
throw new Exception($card->errors[0]); |
|
119 |
} |
|
120 |
} |
|
121 |
} |