| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace addons\Rebate\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $level
- * @property string $name 等级名称
- * @property int $is_auto_upgrade 是否开启自动升级
- * @property int $upgrade_type_condition 是否开启条件升级
- * @property string $checked_condition_values 升级条件
- * @property string $checked_condition_keys 升级条件选中的key集合
- * @property int $condition_type 0 未选择、1满足其一 2、满足所有
- * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
- * @property int $buy_goods_type 购买升级商品触发升级:0订单完成 1下单完成
- * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
- * @property string $goods_ids 商品id集合
- * @property string $goods_list 商品列表json
- * @property string $explain 等级说明
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class RebateUserLevel extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_rebate_user_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'checked_condition_values'], 'required'],
- [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods',
- 'buy_goods_type', 'goods_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['checked_condition_values'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['checked_condition_keys'], 'string', 'max' => 1000],
- [['goods_ids'], 'string', 'max' => 3000],
- [['goods_list', 'explain'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'level' => 'Level',
- 'name' => '等级名称',
- 'is_auto_upgrade' => '是否开启自动升级',
- 'upgrade_type_condition' => '是否开启条件升级',
- 'checked_condition_values' => '升级条件',
- 'checked_condition_keys' => '升级条件选中的key集合',
- 'condition_type' => '0 未选择、1满足其一 2、满足所有',
- 'upgrade_type_goods' => '购买升级商品,0关闭,1开启',
- 'buy_goods_type' => '购买升级商品触发升级:0订单完成 1下单完成',
- 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
- 'goods_ids' => '商品id集合',
- 'goods_list' => '商品列表json',
- 'explain' => '等级说明',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if(!empty($params['id'])){
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (empty($params['checked_condition_keys'])) $params['checked_condition_keys'] = [];
- if (empty($params['checked_condition_values'])) $params['checked_condition_values'] = [];
- if (empty($params['goods_ids'])) $params['goods_ids'] = [];
- if (empty($params['goods_list'])) $params['goods_list'] = [];
- $model->checked_condition_keys = json_encode($params['checked_condition_keys'], JSON_NUMERIC_CHECK);
- $model->checked_condition_values = json_encode($params['checked_condition_values'], JSON_NUMERIC_CHECK);
- $model->goods_ids = json_encode($params['goods_ids'], JSON_NUMERIC_CHECK);
- $model->goods_list = json_encode($params['goods_list'], JSON_NUMERIC_CHECK);
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function del($params){
- try{
- $model = self::findOne(['id'=>$params['id']]);
- if(!empty($model)){
- $res = RebateUser::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if(!empty($res)) throw new \Exception('该等级关联了推三返一用户');
- $model->status = StatusEnum::DELETE;
- $res = $model->save();
- if($res==false) throw new \Exception($model->getErrorMessage());
- }
- return true;
- }catch (\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getLevelWeights($mall_id)
- {
- $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
- $newList = [];
- for ($i = 1; $i <= 10; $i++) {
- $newList[] = [
- 'name' => '等级' . $i,
- 'level' => $i,
- 'disabled' => in_array($i, $list),
- ];
- }
- return $newList;
- }
- public static function getLevelList(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
- {
- $default_cond = [['<>', 'status', StatusEnum::DELETE]];
- $cond = array_merge($default_cond, $cond);
- $query = self::find()->select($select);
- self::paramPack(['where' => $cond, 'with' => $with], $query);
- return $query->asArray($is_array)->all();
- }
- public static function getLevelByColumn($mall_id){
- return self::find()
- ->select('name')
- ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
- ->asArray()
- ->indexBy('level')
- ->orderBy('level desc')
- ->column();
- }
- public static function getRebateUserLevelArr($mall_id, $type = 1)
- {
- $rebate_user_level_arr = RebateUserLevel::lists(['where'=>[['mall_id'=>$mall_id], ['status'=>StatusEnum::ENABLED]], 'select'=>'level,name']);
- if ($type == 1) {
- return array_column($rebate_user_level_arr, 'name', 'level');
- } else {
- return $rebate_user_level_arr;
- }
- }
- }
|