commit | author | age
|
90c639
|
1 |
<?php |
Z |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use app\forms\common\AppImg; |
|
6 |
use Yii; |
|
7 |
use yii\db\Exception; |
|
8 |
|
|
9 |
/** |
|
10 |
* This is the model class for table "{{%share_setting}}". |
|
11 |
* |
|
12 |
* @property int $id |
|
13 |
* @property int $mall_id |
|
14 |
* @property string $key |
|
15 |
* @property string $value |
|
16 |
* @property string $created_at 创建时间 |
|
17 |
* @property string $updated_at 更新时间 |
|
18 |
* @property int $is_delete 是否删除 0--未删除 1--已删除 |
|
19 |
* @property string $deleted_at 删除时间 |
|
20 |
*/ |
|
21 |
class ShareSetting extends ModelActiveRecord |
|
22 |
{ |
|
23 |
/** |
|
24 |
* {@inheritdoc} |
|
25 |
*/ |
|
26 |
public static function tableName() |
|
27 |
{ |
|
28 |
return '{{%share_setting}}'; |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* {@inheritdoc} |
|
33 |
*/ |
|
34 |
public function rules() |
|
35 |
{ |
|
36 |
return [ |
|
37 |
[['mall_id', 'key', 'value', 'created_at', 'updated_at', 'deleted_at'], 'required'], |
|
38 |
[['mall_id', 'is_delete'], 'integer'], |
|
39 |
[['value'], 'string'], |
|
40 |
[['created_at', 'updated_at', 'deleted_at'], 'safe'], |
|
41 |
[['key'], 'string', 'max' => 255], |
|
42 |
]; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* {@inheritdoc} |
|
47 |
*/ |
|
48 |
public function attributeLabels() |
|
49 |
{ |
|
50 |
return [ |
|
51 |
'id' => 'ID', |
|
52 |
'mall_id' => 'Mall ID', |
|
53 |
'key' => 'Key', |
|
54 |
'value' => 'Value', |
|
55 |
'created_at' => '创建时间', |
|
56 |
'updated_at' => '更新时间', |
|
57 |
'is_delete' => '是否删除 0--未删除 1--已删除', |
|
58 |
'deleted_at' => '删除时间', |
|
59 |
]; |
|
60 |
} |
|
61 |
|
|
62 |
const LEVEL = 'level'; // 分销层级 |
|
63 |
const IS_REBATE = 'is_rebate'; // 分销内购 |
|
64 |
const PRICE_TYPE = 'price_type'; // 分销佣金类型 |
|
65 |
const FIRST = 'first'; // 一级佣金 |
|
66 |
const SECOND = 'second'; // 二级佣金 |
|
67 |
const THIRD = 'third'; // 三级佣金 |
|
68 |
const SHARE_CONDITION = 'share_condition'; // 成为分销商条件 |
|
69 |
const CONDITION = 'condition'; // 成为下线条件 |
|
70 |
const AUTO_SHARE_VAL = 'auto_share_val'; // 消费自动成为分销商 |
|
71 |
const TOTAL_CONSUME = 'total_consume'; // 累计消费自动成为分销商 |
|
72 |
const SHARE_GOODS_STATUS = 'share_goods_status'; // 购买商品自动成为分销商 |
|
73 |
const SHARE_GOODS_WAREHOUSE_ID = 'share_goods_warehouse_id'; // 需要购买的商品ID |
|
74 |
const PAY_TYPE = 'pay_type'; // 提现方式 |
|
75 |
const CASH_MAX_DAY = 'cash_max_day'; // 每日提现上限 |
|
76 |
const MIN_MONEY = 'min_money'; // 最少提现金额 |
|
77 |
const CASH_SERVICE_CHARGE = 'cash_service_charge'; // 提现手续费 |
|
78 |
const AGREE = 'agree'; // 申请协议 |
|
79 |
const CONTENT = 'content'; // 用户须知 |
|
80 |
const PIC_URL_APPLY = 'pic_url_apply'; // 申请页面背景图片 |
|
81 |
const PIC_URL_STATUS = 'pic_url_status'; // 审核页面背景图片 |
|
82 |
const PAY_TYPE_LIST = ['auto' => '自动打款', 'wechat' => '微信线下转账', 'alipay' => '支付宝线下转账', |
|
83 |
'bank' => '银行线下转账', 'balance' => '提现到余额']; |
|
84 |
const BECOME_CONDITION = 'become_condition'; |
|
85 |
const CAT_LIST = 'cat_list'; |
|
86 |
const IS_SHOW_SHARE_LEVEL = 'is_show_share_level'; // 是否显示分销商等级升级入口 |
|
87 |
const FORM_STATUS = 'form_status'; // 是否显示自定义表单 |
|
88 |
const FORM = 'form'; // 是否显示自定义表单 |
|
89 |
public const DEFAULT_LEVEL_NAME = 'default_level_name'; //自定义默认等级 |
|
90 |
public const CONSUME_CONDITION = 'consume_condition'; //消费条件 |
|
91 |
|
|
92 |
public const INFO = [ |
|
93 |
self::DEFAULT_LEVEL_NAME => '默认等级' |
|
94 |
]; |
|
95 |
|
|
96 |
public static function getDefaultList($mallId) |
|
97 |
{ |
|
98 |
$list = ShareSetting::getList($mallId); |
|
99 |
$default = self::getDefault(); |
|
100 |
|
|
101 |
foreach ($list as $index => &$item) { |
|
102 |
if ($item == '' && isset($default[$index])) { |
|
103 |
$item = $default[$index]; |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
return $list; |
|
108 |
} |
|
109 |
|
|
110 |
public static function getDefault() |
|
111 |
{ |
|
112 |
$appImg = AppImg::search(); |
|
113 |
return [ |
|
114 |
'pic_url_apply' => $appImg['share']['apply'], |
|
115 |
'pic_url_status' => $appImg['share']['status'] |
|
116 |
]; |
|
117 |
} |
|
118 |
|
|
119 |
public static function strToNumber($key, $str) |
|
120 |
{ |
|
121 |
$default = ['level', 'is_rebate', 'price_type', 'share_condition', 'condition', |
|
122 |
'share_goods_status', 'first', 'second', 'third', 'auto_share_val', 'cash_max_day', 'min_money', |
|
123 |
'cash_service_charge', 'become_condition', 'is_show_share_level', 'form_status', 'total_consume', |
|
124 |
'consume_condition']; |
|
125 |
if (in_array($key, $default)) { |
|
126 |
return round($str, 2); |
|
127 |
} |
|
128 |
return $str; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* @param $mallId |
|
133 |
* @param $key |
|
134 |
* @param null $default |
|
135 |
* @return \ArrayObject|mixed|null |
|
136 |
*/ |
|
137 |
public static function get($mallId, $key, $default = null) |
|
138 |
{ |
|
139 |
$model = self::findOne(['mall_id' => $mallId, 'key' => $key, 'is_delete' => 0]); |
|
140 |
if (!$model) { |
|
141 |
return $default; |
|
142 |
} |
|
143 |
if ($key == ShareSetting::SHARE_GOODS_WAREHOUSE_ID && is_numeric($model->value)) { |
|
144 |
$model->value = Yii::$app->serializer->encode([$model->value]); |
|
145 |
} |
|
146 |
return self::strToNumber($key, Yii::$app->serializer->decode($model->value)); |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* @param $mallId |
|
151 |
* @return array |
|
152 |
*/ |
|
153 |
public static function getList($mallId) |
|
154 |
{ |
|
155 |
$list = self::find()->where(['mall_id' => $mallId, 'is_delete' => 0])->all(); |
|
156 |
|
|
157 |
$newList = []; |
|
158 |
/* @var self[] $list */ |
|
159 |
foreach ($list as $item) { |
|
160 |
$newList[$item->key] = self::strToNumber($item->key, Yii::$app->serializer->decode($item->value)); |
|
161 |
} |
|
162 |
if (!isset($newList[ShareSetting::BECOME_CONDITION])) { |
|
163 |
$newList[ShareSetting::BECOME_CONDITION] = 3; |
|
164 |
$newList[ShareSetting::SHARE_GOODS_STATUS] = 1; |
|
165 |
} |
|
166 |
if ( |
|
167 |
!isset($newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID]) |
|
168 |
|| !$newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID] |
|
169 |
) { |
|
170 |
$newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID] = []; |
|
171 |
} elseif (is_numeric($newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID])) { |
|
172 |
$newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID] = [$newList[ShareSetting::SHARE_GOODS_WAREHOUSE_ID]]; |
|
173 |
} |
|
174 |
|
|
175 |
if (!isset($newList[self::IS_SHOW_SHARE_LEVEL])) { |
|
176 |
$newList[self::IS_SHOW_SHARE_LEVEL] = 1; |
|
177 |
} |
|
178 |
|
|
179 |
if (!isset($newList[self::LEVEL])) { |
|
180 |
$newList[self::LEVEL] = 0; |
|
181 |
} |
|
182 |
|
|
183 |
if (!isset($newList[self::DEFAULT_LEVEL_NAME])) { |
|
184 |
$newList[self::DEFAULT_LEVEL_NAME] = self::INFO[self::DEFAULT_LEVEL_NAME]; |
|
185 |
} |
|
186 |
if (!isset($newList[self::CONSUME_CONDITION])) { |
|
187 |
$newList[self::CONSUME_CONDITION] = $newList[self::BECOME_CONDITION] == 4 ? 2 : 1; |
|
188 |
} |
|
189 |
return $newList; |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* @param $mallId |
|
194 |
* @param $key |
|
195 |
* @param string $value |
|
196 |
* @return bool |
|
197 |
* @throws Exception |
|
198 |
*/ |
|
199 |
public static function set($mallId, $key, $value = '') |
|
200 |
{ |
|
201 |
if (empty($key)) { |
|
202 |
return false; |
|
203 |
} |
|
204 |
$model = self::findOne(['mall_id' => $mallId, 'key' => $key, 'is_delete' => 0]); |
|
205 |
if (!$model) { |
|
206 |
$model = new self(); |
|
207 |
$model->key = $key; |
|
208 |
$model->mall_id = $mallId; |
|
209 |
} |
|
210 |
$model->value = Yii::$app->serializer->encode(self::strToNumber($key, $value)); |
|
211 |
if ($model->save()) { |
|
212 |
return true; |
|
213 |
} else { |
|
214 |
throw new Exception($model->errors[0]); |
|
215 |
} |
|
216 |
} |
|
217 |
|
|
218 |
/** |
|
219 |
* @param $mallId |
|
220 |
* @param $list |
|
221 |
* @return bool |
|
222 |
* @throws Exception |
|
223 |
*/ |
|
224 |
public static function setList($mallId, $list) |
|
225 |
{ |
|
226 |
if (!is_array($list)) { |
|
227 |
return false; |
|
228 |
} |
|
229 |
foreach ($list as $item) { |
|
230 |
self::set(isset($item['mallId']) ? $item['mallId'] : $mallId, $item['key'], $item['value']); |
|
231 |
} |
|
232 |
return true; |
|
233 |
} |
|
234 |
} |