| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace addons\BossWeight\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_boss_weight_level}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $level 股东等级
- * @property string $name 等级名称
- * @property int $is_auto_upgrade 是否自动升级【0否,1是】
- * @property int $upgrade_type_condition 是否开启条件升级【0否,1是】
- * @property int $condition_type 满足条件:1满足其一,2满足所有
- * @property string|null $checked_condition_values 升级条件的值集合
- * @property string|null $checked_condition_keys 被选中的升级条件的key集合
- * @property int $upgrade_type_goods 是否开启购买商品升级【0否,1是】
- * @property string $goods_ids 指定商品id集合
- * @property string $goods_list 指定商品信息列表json
- * @property int $goods_type 商品升级类型:1任意商品,2指定商品
- * @property int $buy_goods_type 购买商品升级条件:0订单完成,1支付完成
- * @property string|null $explain 等级说明
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsBossLevel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_boss_weight_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'goods_type', 'buy_goods_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['checked_condition_values', 'checked_condition_keys', 'explain'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['goods_ids'], 'string', 'max' => 3000],
- [['goods_list'], 'string', 'max' => 5000],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'level' => '股东等级',
- 'name' => '等级名称',
- 'is_auto_upgrade' => '是否自动升级【0否,1是】',
- 'upgrade_type_condition' => '是否开启条件升级【0否,1是】',
- 'condition_type' => '满足条件:1满足其一,2满足所有',
- 'checked_condition_values' => '升级条件的值集合',
- 'checked_condition_keys' => '被选中的升级条件的key集合',
- 'upgrade_type_goods' => '是否开启购买商品升级【0否,1是】',
- 'goods_ids' => '指定商品id集合',
- 'goods_list' => '指定商品信息列表json',
- 'goods_type' => '商品升级类型:1任意商品,2指定商品',
- 'buy_goods_type' => '购买商品升级条件:0订单完成,1支付完成',
- 'explain' => '等级说明',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getSetting()
- {
- return $this->hasOne(AddonsBossLevelSetting::class, ['level' => 'level']);
- }
- /**
- * 获取等级列表
- * @Author: lun
- * @DateTime: 2021/10/21 0021 9:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param bool $is_array
- * @param string $select
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getBossLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond, 'with'=>$with],$query);
- isset($order) && $query->orderBy($order);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- /**
- * 获取等级等级和权重
- * @Author: lun
- * @DateTime: 2021/10/21 0021 9:22
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array
- */
- 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),
- ];
- }
- array_unshift($newList, [
- 'name' => "默认等级",
- 'level' => 0,
- 'disabled' => true,
- ]);
- return $newList;
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/10/20 0020 18:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsBossLevel|bool
- */
- public static function setData(array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('等级不存在或已删除');
- // 判断等级是否有被修改
- if ($model->level != $params['level']) {
- // 判断该等级下是否有股东存在
- $boss = AddonsBoss::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($boss) throw new Exception('该等级下存在股东,不能被修改');
- }
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- // 插入配置详情
- $setting = ['mall_id' => $params['mall_id'], 'level' => $model->level, 'status' => $model->status];
- $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
- $setting['checked_weight_values'] = json_encode($setting['checked_weight_values']);
- $res = AddonsBossLevelSetting::setData($setting);
- if ($res == false) throw new Exception(AddonsBossLevelSetting::getStaticError());
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取升级列表
- * @Author: lun
- * @DateTime: 2021/10/21 0021 15:05
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return array
- */
- public static function getListByUpgrade($params)
- {
- $rows = [];
- if (!empty($params['mall_id'])) {
- $where = ['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED];
- $list = self::findAll($where);
- foreach ($list as $v) {
- $rows[] = ['level' => $v['level'], 'name' => $v['name']];
- }
- }
- return $rows;
- }
- /**
- * 获取等级
- * @Author: lun
- * @DateTime: 2021/12/16 0016 9:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array
- */
- 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();
- }
- }
|