| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace common\models\addons;
- use common\enums\addons\MallAddonsEnum;
- use common\enums\StatusEnum;
- use common\models\common\Addons;
- use common\models\mall\MallAddons;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%mall_addons_combo_relation}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $addons_combo_id 套餐id,对应qimall_addons_combo.id
- * @property int|null $operator_id 操作人id
- * @property string|null $operator_username 操作人昵称
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class MallAddonsComboRelation extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%mall_addons_combo_relation}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'addons_combo_id', 'operator_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['operator_username'], 'string', 'max' => 50],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'addons_combo_id' => '套餐id,对应qimall_addons_combo.id',
- 'operator_id' => '操作人id',
- 'operator_username' => '操作人昵称',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '添加时间戳',
- 'updated_at' => '更新时间戳',
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/3/28 0028 19:35
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $params
- * @return array|bool
- */
- public static function setData($mall_id, $params)
- {
- try {
- $model = self::findOne(['mall_id' => $mall_id, 'addons_combo_id' => $params['addons_combo_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- // 获取套餐内容
- $combo = AddonsCombo::findOne(['id' => $params['addons_combo_id'], 'status' => StatusEnum::ENABLED]);
- $addons_arr = Json::decode($combo['addons'], true);
- $expire = Json::decode($combo['expire'], true);
- $month = -1;// 过期时间,月份
- if ($expire['is_forever'] == false) {
- $month = $expire['unit'] == 1 ? $expire['duration'] : $expire['duration'] * 12;
- }
- // 保存成功后自动安装插件
- foreach ($addons_arr as $addon) {
- // 获取插件版本号
- $version = Addons::find()->select('version')->where(['name' => $addon, 'status' => StatusEnum::ENABLED])->scalar();
- $addons_params = [
- 'mall_id' => $mall_id,
- 'addons_name' => $addon,
- 'month' => $month,
- 'version' => $version,
- 'addons_status' => MallAddonsEnum::INSTALL,
- ];
- $res = MallAddons::setData($addons_params);// 保存数据
- if ($res == false) throw new Exception("安装{$addon}应用失败:" . MallAddons::getStaticError());
- // 初始化安装插件
- $install_key = "addons\\" . $addon . "\Install";
- if (class_exists($install_key)) {
- $install = new $install_key();
- $install->runApp($mall_id, $addon);
- }
- }
- // 增加使用套餐的商城数量
- $combo->mall_num = $combo->mall_num + 1;
- if (!$combo->save()) throw new Exception('增加使用套餐的商城数量失败:'.$combo->getErrorMessage());
- return $model->toArray();
- } catch (\Exception $e) {
- self::setStaticError('应用付费套餐绑定失败:'.$e->getMessage());
- return false;
- }
- }
- }
|