StoreVideoOrder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Store\common\events\order\VideoOrderPaidEvent;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "qimall_addons_store_video_order".
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $store_id 门店id
  14. * @property int $user_id 会员id
  15. * @property int $video_id 视频id
  16. * @property string $order_no 订单编号
  17. * @property int $pay_status 支付状态:0:否;1:是
  18. * @property int $pay_time 付款时间
  19. * @property float $pay_money 实付金额
  20. * @property int $payment_type 支付类型
  21. * @property string $remark 备注
  22. * @property int $status 状态[0禁用 1启用 -1删除]
  23. * @property int $created_at 创建时间
  24. * @property int $updated_at 修改时间
  25. */
  26. class StoreVideoOrder extends BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_store_video_order}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'store_id', 'user_id','video_id', 'pay_status', 'pay_time', 'payment_type', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['pay_money'], 'number'],
  43. [['order_no'], 'string', 'max' => 100],
  44. [['remark'], 'string', 'max' => 255],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'store_id' => 'Store ID',
  56. 'user_id' => 'User ID',
  57. 'video_id' => 'Video ID',
  58. 'order_no' => 'Order No',
  59. 'pay_status' => 'Pay Status',
  60. 'pay_time' => 'Pay Time',
  61. 'pay_money' => 'Pay Money',
  62. 'payment_type' => 'Payment Type',
  63. 'remark' => 'Remark',
  64. 'status' => 'Status',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. public static function setData($params)
  70. {
  71. try{
  72. $model = new self();
  73. $model->loadDefaultValues();
  74. if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
  75. return $model;
  76. }catch(\Exception $e){
  77. self::$static_error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. public static function pay($params, $peyment_type)
  82. {
  83. try {
  84. $trans = Yii::$app->db->beginTransaction();
  85. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
  86. if (empty($model)) throw new Exception('订单不存在或已删除');
  87. if ($model->pay_status == StatusEnum::ENABLED) throw new Exception('订单已支付,请不要重复支付');
  88. $model->payment_type = $peyment_type;
  89. $model->pay_status = StatusEnum::ENABLED;
  90. $model->pay_time = time();
  91. $res = $model->save();
  92. if ($res === false) throw new Exception('修改订单失败:' . $model->getErrorMessage());
  93. $trans->commit();
  94. // 触发订单支付事件,事务提交完成后触发
  95. $event = new VideoOrderPaidEvent($model['mall_id']);
  96. $order['id'] = $model['id'];
  97. $order['operator_id'] = $params['operator_id'] ?? 0;
  98. $order['operator_name'] = $params['operator_name'] ?? '';
  99. Yii::$app->services->events->dispatch($event, $order, $event->listeners);
  100. return $model;
  101. } catch (Exception $e) {
  102. if (isset($trans)) $trans->rollback();
  103. self::$static_error = $e->getMessage();
  104. return false;
  105. }
  106. }
  107. }