MallAddonsComboRelation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace common\models\addons;
  3. use common\enums\addons\MallAddonsEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\common\Addons;
  6. use common\models\mall\MallAddons;
  7. use Yii;
  8. use yii\db\Exception;
  9. use yii\helpers\Json;
  10. /**
  11. * This is the model class for table "{{%mall_addons_combo_relation}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城id
  15. * @property int $addons_combo_id 套餐id,对应qimall_addons_combo.id
  16. * @property int|null $operator_id 操作人id
  17. * @property string|null $operator_username 操作人昵称
  18. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at 添加时间戳
  20. * @property int $updated_at 更新时间戳
  21. */
  22. class MallAddonsComboRelation extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%mall_addons_combo_relation}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'addons_combo_id', 'operator_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['operator_username'], 'string', 'max' => 50],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => '商城id',
  49. 'addons_combo_id' => '套餐id,对应qimall_addons_combo.id',
  50. 'operator_id' => '操作人id',
  51. 'operator_username' => '操作人昵称',
  52. 'status' => '状态[-1:删除;0:禁用;1启用]',
  53. 'created_at' => '添加时间戳',
  54. 'updated_at' => '更新时间戳',
  55. ];
  56. }
  57. /**
  58. * 保存数据
  59. * @Author: lun
  60. * @DateTime: 2022/3/28 0028 19:35
  61. * @Copyright: copyright (c) 2021 广东七件事集团
  62. * @param $mall_id
  63. * @param $params
  64. * @return array|bool
  65. */
  66. public static function setData($mall_id, $params)
  67. {
  68. try {
  69. $model = self::findOne(['mall_id' => $mall_id, 'addons_combo_id' => $params['addons_combo_id'], 'status' => StatusEnum::ENABLED]);
  70. if (empty($model)) {
  71. $model = new self();
  72. $model->mall_id = $mall_id;
  73. $model->loadDefaultValues();
  74. }
  75. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  76. // 获取套餐内容
  77. $combo = AddonsCombo::findOne(['id' => $params['addons_combo_id'], 'status' => StatusEnum::ENABLED]);
  78. $addons_arr = Json::decode($combo['addons'], true);
  79. $expire = Json::decode($combo['expire'], true);
  80. $month = -1;// 过期时间,月份
  81. if ($expire['is_forever'] == false) {
  82. $month = $expire['unit'] == 1 ? $expire['duration'] : $expire['duration'] * 12;
  83. }
  84. // 保存成功后自动安装插件
  85. foreach ($addons_arr as $addon) {
  86. // 获取插件版本号
  87. $version = Addons::find()->select('version')->where(['name' => $addon, 'status' => StatusEnum::ENABLED])->scalar();
  88. $addons_params = [
  89. 'mall_id' => $mall_id,
  90. 'addons_name' => $addon,
  91. 'month' => $month,
  92. 'version' => $version,
  93. 'addons_status' => MallAddonsEnum::INSTALL,
  94. ];
  95. $res = MallAddons::setData($addons_params);// 保存数据
  96. if ($res == false) throw new Exception("安装{$addon}应用失败:" . MallAddons::getStaticError());
  97. // 初始化安装插件
  98. $install_key = "addons\\" . $addon . "\Install";
  99. if (class_exists($install_key)) {
  100. $install = new $install_key();
  101. $install->runApp($mall_id, $addon);
  102. }
  103. }
  104. // 增加使用套餐的商城数量
  105. $combo->mall_num = $combo->mall_num + 1;
  106. if (!$combo->save()) throw new Exception('增加使用套餐的商城数量失败:'.$combo->getErrorMessage());
  107. return $model->toArray();
  108. } catch (\Exception $e) {
  109. self::setStaticError('应用付费套餐绑定失败:'.$e->getMessage());
  110. return false;
  111. }
  112. }
  113. }