| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace addons\DoubleCopy\common\models;
- use addons\DoubleCopy\common\enums\DoubleCopyEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_double_copy_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 0 未选择、1满足其一 2、满足所有
- * @property string|null $checked_condition_keys 升级条件选中的key集合
- * @property string|null $checked_condition_values 升级条件
- * @property int $upgrade_type_goods 购买商品升级 0关闭 1 开启
- * @property int $buy_goods_type 0订单完成 1下单 完成
- * @property int $goods_type 商品升级类型
- * @property string|null $goods_ids 商品仓库的ID
- * @property string|null $goods_list 商品列表
- * @property string|null $explain 等级说明
- * @property int $open_ladder 是否开启阶梯解冻设置[0=否,1=是]
- * @property string|null $ladder_setting 阶梯解冻设置
- * @property int $status 是否启用
- * @property int $created_at
- * @property int $updated_at
- * @property int $store_cashier_order_is_up 收银台支付任意一笔订单升级:0否;1:是
- */
- class DoubleCopyLevel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_double_copy_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'buy_goods_type', 'goods_type', 'open_ladder', 'status',
- 'created_at', 'updated_at','upgrade_type_store_goods','buy_store_goods_type','store_goods_type','store_cashier_order_is_up'], 'integer'],
- [['checked_condition_keys', 'checked_condition_values', 'goods_ids', 'goods_list', 'explain', 'ladder_setting','store_goods_ids','store_goods_list'], 'string'],
- [['name'], 'string', 'max' => 45],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'level' => '等级权重',
- 'name' => '等级名称',
- 'is_auto_upgrade' => '是否启用自动升级:0-不;1-是;',
- 'upgrade_type_condition' => '条件升级 0关闭 1 开启',
- 'condition_type' => '0 未选择、1满足其一 2、满足所有',
- 'checked_condition_keys' => '升级条件选中的key集合',
- 'checked_condition_values' => '升级条件',
- 'upgrade_type_goods' => '购买商品升级 0关闭 1 开启',
- 'buy_goods_type' => '0订单完成 1下单 完成',
- 'goods_type' => '商品升级类型',
- 'goods_ids' => '商品仓库的ID',
- 'goods_list' => '商品列表',
- 'explain' => '等级说明',
- 'open_ladder' => '是否开启阶梯解冻设置[0=否,1=是]',
- 'ladder_setting' => '阶梯解冻设置',
- 'status' => '是否启用',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'store_cashier_order_is_up' => '收银台支付任意一笔订单升级:0否;1:是',
- ];
- }
- /**
- * 获取等级配置
- * @Author: lun
- * @DateTime: 2022/5/14 0014 9:57
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getSetting()
- {
- return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'commission']);
- }
- /**
- * 获取等级配置
- * @Author: lun
- * @DateTime: 2022/5/14 0014 9:57
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getScoreSetting()
- {
- return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'score']);
- }
- /**
- * 获取等级配置
- * @Author: lun
- * @DateTime: 2022/5/14 0014 9:57
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getAdditionalScoreSetting()
- {
- return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'additional_score']);
- }
- /**
- * 获取权重
- * @Author: lun
- * @DateTime: 2022/5/14 0014 10:00
- * @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 <= 15; $i++) {
- $newList[] = [
- 'name' => '权重' . $i,
- 'level' => $i,
- 'disabled' => in_array($i, $list),
- ];
- }
- return $newList;
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/5/14 0014 10:09
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return array|bool
- */
- public static function setData(array $params){
- // 因为创建等级时, 可能等级设置等数据没有成功导致数据无法显示所以这里需要事务
- $transaction = \Yii::$app->db->beginTransaction();
- 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']) {
- // 判断该等级下是否有股东存在
- $user = DoubleCopyUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($user) 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_id' => $model->id, 'level' => $model->level];
- $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
- $setting['commission_type'] = isset($setting['commission_type']) ? $setting['commission_type'] : 'commission';
- $setting['status'] = $model->status;
- $res = DoubleCopyLevelSetting::setData($setting);
- if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
- $score_setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level];
- $score_setting = empty($params['score_setting']) ? $score_setting : array_merge($score_setting, Json::decode($params['score_setting'], true));
- $score_setting['status'] = $model->status;
- $score_setting['freeze_fund_prize'] = $setting['freeze_fund_prize'] ?? 0;
- $score_setting['freeze_fund_num'] = $setting['freeze_fund_num'] ?? 0;
- $score_setting['is_thaw'] = $setting['is_thaw'] ?? 0;
- $score_setting['first_level'] = $setting['first_level'] ?? '';
- $score_setting['push_num'] = $setting['push_num'] ?? 0;
- $score_setting['second_level'] = $setting['second_level'] ?? '';
- $score_setting['commission_type'] = isset($score_setting['commission_type']) ? $score_setting['commission_type'] : 'score';
- $res = DoubleCopyLevelSetting::setData($score_setting);
- if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
- // 额外积分
- $additional_score_setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level];
- $additional_score_setting = empty($params['additional_score_setting']) ? $additional_score_setting : array_merge($additional_score_setting, Json::decode($params['additional_score_setting'], true));
- $additional_score_setting['status'] = $model->status;
- $additional_score_setting['freeze_fund_prize'] = $setting['freeze_fund_prize'] ?? 0;
- $additional_score_setting['freeze_fund_num'] = $setting['freeze_fund_num'] ?? 0;
- $additional_score_setting['is_thaw'] = $setting['is_thaw'] ?? 0;
- $additional_score_setting['first_level'] = $setting['first_level'] ?? '';
- $additional_score_setting['push_num'] = $setting['push_num'] ?? 0;
- $additional_score_setting['second_level'] = $setting['second_level'] ?? '';
- $additional_score_setting['commission_type'] = isset($additional_score_setting['commission_type']) ? $additional_score_setting['commission_type'] : 'additional_score';
- $res = DoubleCopyLevelSetting::setData($additional_score_setting);
- if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
- $transaction->commit();
- return $model->toArray();
- }catch(\Exception $e){
- $transaction->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 安装插件初始化等级
- * @Author: lun
- * @DateTime: 2022/5/14 0014 11:03
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- public static function InitLevel($init_level)
- {
- try {
- foreach ($init_level as $item) {
- $level_id = self::find()->select('id')->where(['mall_id' => $item['mall_id'], 'level' => $item['level']])->andWhere(['<>', 'status', StatusEnum::DELETE])->scalar();
- if (empty($level_id)) {
- $res = self::setData($item);
- if ($res == false) throw new \Exception(self::getStaticError());
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError("初始化等级失败:" . $e->getMessage());
- return false;
- }
- }
- /**
- * 获取等级
- * @Author: lun
- * @DateTime: 2022/5/14 0014 15:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getLevelMap($mall_id, $type = 1)
- {
- $select = "level,name";
- $list = self::find()->select($select)->where(['mall_id' => $mall_id,'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->orderBy('level asc')->asArray()->all();
- if ($type == 2) {
- $new_list = [];
- foreach ($list as $item) {
- $new_list[$item['level']] = $item['name'];
- }
- return $new_list;
- }
- return $list;
- }
- /**
- * 根据等级获取等级名称
- * @Author: lun
- * @DateTime: 2022/5/16 0016 17:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $level
- * @return false|int|string|null
- */
- public static function getLevelNameByLevel($mall_id, $level)
- {
- return self::find()->select('name')->where(['mall_id' => $mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED])->scalar();
- }
- /**
- * 获取推荐的位数
- * @param $mall_id
- * @return void
- */
- public static function getUpgradeNum($mall_id)
- {
- $boss_level = self::find()->select('upgrade_type_condition, checked_condition_values')->where(['mall_id' => $mall_id, 'level' => DoubleCopyEnum::LEVEL_BOSS, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($boss_level['upgrade_type_condition'] == 1) {
- // json解析升级条件数量
- $json = Json::decode($boss_level['checked_condition_values'], true);
- return !empty($json[0]['value']['val0']) ? intval($json[0]['value']['val0']) : 0;
- }
- return 0;
- }
- }
|