AddonsCourseBought.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace addons\Course\common\models;
  3. use addons\Course\common\enums\CourseEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_course_bought}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id
  12. * @property int $course_id
  13. * @property float $amount 支付金额
  14. * @property int $buy_type 课程购买方式,1:单独购买,2:商品赠送
  15. * @property int $order_id 购买该课程的订单 id,课程订单 id 或者公共订单 id
  16. * @property int $is_pay 状态,1:已支付,0:未支付
  17. * @property int $status 状态 0禁用,1启用,-1是删除
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class AddonsCourseBought extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_course_bought}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_id', 'course_id', 'order_id'], 'required'],
  37. [['mall_id', 'user_id', 'course_id', 'buy_type', 'order_id', 'is_pay', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['amount'], 'number'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'user_id' => 'User ID',
  50. 'course_id' => 'Course ID',
  51. 'amount' => '支付金额',
  52. 'buy_type' => '课程购买方式,1:单独购买,2:商品赠送',
  53. 'order_id' => '购买该课程的订单 id,课程订单 id 或者公共订单 id',
  54. 'is_pay' => '状态,1:已支付,0:未支付',
  55. 'status' => '状态 0禁用,1启用,-1是删除',
  56. 'created_at' => 'Created At',
  57. 'updated_at' => 'Updated At',
  58. ];
  59. }
  60. public function getCourse()
  61. {
  62. return $this->hasOne(AddonsCourse::class, ['id' => 'course_id']);
  63. }
  64. /**
  65. * 新增用户购买的课程
  66. *
  67. * @return boolean
  68. * @throws \Exception
  69. */
  70. public static function addBought($params, $order_id)
  71. {
  72. $bought = self::getOne([["order_id" => $order_id, "buy_type" => 1, "user_id" => $params['user_id'], 'status' => StatusEnum::ENABLED]]);
  73. if (empty($bought)) {
  74. $bought = new self();
  75. $bought->mall_id = $params['mall_id'];
  76. $bought->user_id = $params['user_id'];
  77. $bought->course_id = $params['course_id'];
  78. $bought->amount = $params['price'];
  79. $bought->buy_type = 1;
  80. $bought->order_id = $order_id;
  81. if ($params['order_status'] > 0) $bought->is_pay = 1;
  82. if (!$bought->save()) self::$static_error = $bought->getErrorMessage();;
  83. }
  84. return true;
  85. }
  86. public static function checkIsBought($ids, $user_id)
  87. {
  88. $where[] = ['=', 'user_id', $user_id];
  89. $where[] = ['in', 'course_id', $ids];
  90. $where[] = ['=', 'is_pay', CourseEnum::IS_PAY_YES];
  91. return self::lists(['where' => $where, 'index' => 'course_id', 'select' => 'course_id']);
  92. }
  93. }