| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace addons\ServeBonus\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "qimall_addons_servebonus_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|null $goods_ids 指定商品id集合
- * @property string|null $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
- * @property int $serve_bonus_number //服务商人数
- * @property int $is_to_bonus 服务商分红
- * @property float $to_bonus_price 达到指定金额停止分红
- */
- class ServeBonusLevel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_servebonus_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'serve_bonus_number', 'condition_type', 'upgrade_type_goods', 'goods_type', 'buy_goods_type', 'status', 'created_at', 'updated_at','is_to_bonus'], 'integer'],
- [['checked_condition_values', 'checked_condition_keys', 'explain'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['to_bonus_price'], 'number'],
- [['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',
- 'servebonus_muber' => '服务商人数',
- 'is_to_bonus' => '服务商分红',
- 'to_bonus_price' => '达到指定金额停止分红',
- ];
- }
- public function getSetting()
- {
- return $this->hasOne(ServeBonusLevelSetting::class, ['level' => 'level']);
- }
- /**
- * 获取等级列表
- * @Author: ltm
- * @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 getServeBonusLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
- $default_cond = [['=','status',StatusEnum::ENABLED]];
- $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: ltm
- * @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),
- ];
- }
- return $newList;
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2021/10/20 0020 18:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return ServeBonusLevel|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 (!empty($params['level']) && $model->level != $params['level'] || in_array($params['status'], [StatusEnum::DISABLED, StatusEnum::DELETE])) {
- // 判断该等级下是否有服务商存在
- $ServeBonus = ServeBonus::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($ServeBonus) 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));
- $res = ServeBonusLevelSetting::setData($setting);
- if ($res == false) throw new Exception(ServeBonusLevelSetting::getStaticError());
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/2/10 0010 15:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $level['level'] //等级 $level['num']//等级数量
- * @return string
- */
- public static function statisticsServeBonus($level,$last_level=[],$mall_id=0){
- //升级后等级进行加
- if($level&&$level['level']){
- $model = self::findOne(['level' => $level['level'],'mall_id' => $mall_id,'status' => StatusEnum::ENABLED]);
- if($model){
- $model -> serve_bonus_number += $level['num'];
- $model->save();
- }
- }
- //升级前等级进行扣除
- if($last_level&&$last_level['level']){
- $last_model = self::findOne(['level' => $last_level['level'],'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- if($last_model){
- $last_model -> serve_bonus_number -= $last_level['num'];
- $last_model->save();
- }
- }
- }
- /**
- * 获取升级列表
- * @Author: ltm
- * @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: ltm
- * @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();
- }
- }
|