| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace addons\Course\common\models;
- use addons\Course\common\enums\CourseEnum;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%addons_course_bought}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property int $course_id
- * @property float $amount 支付金额
- * @property int $buy_type 课程购买方式,1:单独购买,2:商品赠送
- * @property int $order_id 购买该课程的订单 id,课程订单 id 或者公共订单 id
- * @property int $is_pay 状态,1:已支付,0:未支付
- * @property int $status 状态 0禁用,1启用,-1是删除
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsCourseBought extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_course_bought}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'course_id', 'order_id'], 'required'],
- [['mall_id', 'user_id', 'course_id', 'buy_type', 'order_id', 'is_pay', 'status', 'created_at', 'updated_at'], 'integer'],
- [['amount'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'course_id' => 'Course ID',
- 'amount' => '支付金额',
- 'buy_type' => '课程购买方式,1:单独购买,2:商品赠送',
- 'order_id' => '购买该课程的订单 id,课程订单 id 或者公共订单 id',
- 'is_pay' => '状态,1:已支付,0:未支付',
- 'status' => '状态 0禁用,1启用,-1是删除',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getCourse()
- {
- return $this->hasOne(AddonsCourse::class, ['id' => 'course_id']);
- }
- /**
- * 新增用户购买的课程
- *
- * @return boolean
- * @throws \Exception
- */
- public static function addBought($params, $order_id)
- {
- $bought = self::getOne([["order_id" => $order_id, "buy_type" => 1, "user_id" => $params['user_id'], 'status' => StatusEnum::ENABLED]]);
- if (empty($bought)) {
- $bought = new self();
- $bought->mall_id = $params['mall_id'];
- $bought->user_id = $params['user_id'];
- $bought->course_id = $params['course_id'];
- $bought->amount = $params['price'];
- $bought->buy_type = 1;
- $bought->order_id = $order_id;
- if ($params['order_status'] > 0) $bought->is_pay = 1;
- if (!$bought->save()) self::$static_error = $bought->getErrorMessage();;
- }
- return true;
- }
- public static function checkIsBought($ids, $user_id)
- {
- $where[] = ['=', 'user_id', $user_id];
- $where[] = ['in', 'course_id', $ids];
- $where[] = ['=', 'is_pay', CourseEnum::IS_PAY_YES];
- return self::lists(['where' => $where, 'index' => 'course_id', 'select' => 'course_id']);
- }
- }
|