| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- namespace addons\Distribution\common\models;
- use addons\Agent\common\models\AgentCommissionLevelSetting;
- use addons\Distribution\common\enums\DistributionEnum;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_distribution_level".
- *
- * @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
- * @property int $store_upgrade_type_goods 购买门店商品升级,0关闭,1开启
- * @property int $store_buy_goods_type 门店订单完成0;支付完成:1
- * @property int $store_goods_type 门店商品升级类型,1- 任意商品 2- 指定商品
- * @property int $store_goods_ids 门店商品id集合
- * @property int $store_goods_list 门店商品集合
- * @property int $is_custom_condition_num 是否自定义升级商品条件数量
- */
- class DistributionLevel extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_distribution_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', 'is_custom_condition_num'], 'integer'],
- [['checked_condition_values'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['checked_condition_keys'], 'string', 'max' => 1000],
- [['goods_ids'], 'string', 'max' => 3000],
- [[ 'explain'], 'string', 'max' => 5000],
- [['goods_list'], 'string'],
- [['store_upgrade_type_goods','store_buy_goods_type','store_goods_type'],'integer'],
- [['store_goods_ids','store_goods_list'],'string']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'level' => 'Level',
- 'name' => '等级名称',
- 'price_type' => '佣金类型 1百分比 2 固定金额',
- 'score_type' => '积分类型',
- 'first_price' => 'First Price',
- 'second_price' => 'Second Price',
- 'third_price' => 'Third Price',
- 'first_score' => '一级积分',
- 'second_score' => '二级积分',
- 'third_score' => '三级积分',
- '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',
- 'is_custom_condition_num' => '是否自定义升级商品条件数量',
- ];
- }
- public function getCommissionLevelSetting()
- {
- return $this->hasOne(DistributionCommissionLevelSetting::class, ['level' => 'level','mall_id'=>'mall_id']);
- }
- public function getCommissionLevelSettingList()
- {
- return $this->hasMany(DistributionCommissionLevelSetting::class, ['level' => 'level','mall_id'=>'mall_id']);
- }
- /**
- * @desc:保存数据
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 15:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return DistributionLevel|false|null
- */
- public static function setData(array $params)
- {
- 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);
- $model->store_goods_ids = json_encode($params['store_goods_ids'], JSON_NUMERIC_CHECK);
- $model->store_goods_list = json_encode($params['store_goods_list'], JSON_NUMERIC_CHECK);
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $status = $params['status']??1;
- if (isset($params['score']) && !empty($params['score'])) {
- $params['from_type'] = DistributionEnum::ADDONS_NAME;
- $params['commission_type'] = 'score';
- unset($params['score']['level']);
- $params = array_merge($params,$params['score']);
- $params['status'] = $status;
- $res = DistributionCommissionLevelSetting::setData($params);
- if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
- $params['commission_type'] = 'commission';
- unset($params['commission']['level']);
- $params = array_merge($params,$params['commission']);
- $params['status'] = $status;
- $res = DistributionCommissionLevelSetting::setData($params);
- if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
- $params['commission_type'] = 'drive_cy';
- unset($params['drive_cy']['level']);
- $params = array_merge($params,$params['drive_cy']);
- $params['status'] = $status;
- $res = DistributionCommissionLevelSetting::setData($params);
- if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
- $params['commission_type'] = 'digital';
- unset($params['digital']['level']);
- $params = array_merge($params,$params['digital']);
- $params['status'] = $status;
- $res = DistributionCommissionLevelSetting::setData($params);
- if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
- }
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:获取等级权重列表
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 15:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param int $mall_id
- * @return array[]
- */
- public static function getLevelWeights(int $mall_id)
- {
- $list = self::find()->select('level,name')->where([
- 'mall_id' => $mall_id,
- 'status' =>[StatusEnum::ENABLED,StatusEnum::DISABLED]
- ])->all();
- $level_list = array_column($list, 'level');
- $newList = [];
- $matchList = [];
- for ($i = 1; $i <= 10; $i++) {
- $newList[] = [
- 'name' => '权重' . $i,
- 'level' => $i,
- 'disabled' => in_array($i, $level_list),
- ];
- if (isset($list[$i - 1])) {
- $matchList[] = [
- 'name' => $list[$i - 1]['name'],
- 'level' => "$i",
- 'disabled' => in_array($i, $level_list),
- ];
- }
- }
- return ['newList' => $newList, 'matchList' => $matchList];
- }
- /**
- * @desc:条件升级数据源使用
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 15:30
- * @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;
- }
- /**
- * @desc:
- * @Author: hua
- * @Date: 2021/10/26
- * @Time: 14:52
- * @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 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);
- $list = $query->asArray($is_array)->all();
- $mall_id = 0;
- foreach ($cond as $val){
- if(isset($val['mall_id'])) {
- $mall_id = $val['mall_id'];
- break;
- }
- }
- $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
- return array_merge([['id' => 0, 'level' => 0, 'name' => $configs['default_level_name']??'默认等级']],$list);
- }
- public static function del($params){
- try{
- $model = self::findOne(['id'=>$params['id']]);
- if(!empty($model)){
- $res = Distribution::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());
- $params['level'] = $model->level;
- $res = DistributionCommissionLevelSetting::del($params);
- if($res==false) throw new \Exception(DistributionCommissionLevelSetting::getStaticError());
- }
- return true;
- }catch (\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取等级
- * @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();
- }
- //供模式公共调用
- public static function getList($params){
- // dd($params);
- $default_level_name = '默认等级';
- if(isset($params['mall_id'])){
- $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
- if(!empty( $configs['default_level_name'])){
- $default_level_name = $configs['default_level_name'];
- }
- }
- $list = self::lists($params);
- $row = ['level'=>'0','name'=>$default_level_name];
- array_unshift($list,$row);
- return $list;
- }
- }
|