ElectronCouponOrder.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  4. use Yii;
  5. use common\enums\StatusEnum;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_electron_coupon_order}}".
  9. *
  10. * @property int $id
  11. * @property int $order_id 订单id
  12. * @property int $user_id 用户id
  13. * @property int $user_e_coupon_id 会员电子券id
  14. * @property float $price 金额
  15. * @property int $created_at
  16. * @property int $updated_at
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $mall_id 商城id
  19. * @property int $is_pay 是否支付
  20. * @property int $number 数量
  21. */
  22. class ElectronCouponOrder extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_electron_coupon_order}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['order_no', 'user_id', 'user_e_coupon_id', 'price'], 'required'],
  38. [['user_id', 'user_e_coupon_id', 'pay_type', 'created_at', 'updated_at', 'status', 'mall_id', 'is_pay', 'number', 'pay_time'], 'integer'],
  39. [['price'], 'number'],
  40. [['order_no', 'trade_no'], 'string'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'order_no' => '订单号',
  51. 'trade_no' => '流水号',
  52. 'user_id' => '用户id',
  53. 'user_e_coupon_id' => '会员电子券id',
  54. 'price' => '金额',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. 'status' => '状态[-1:删除;0:禁用;1启用]',
  58. 'mall_id' => '商城id',
  59. 'is_pay' => '是否支付',
  60. 'pay_type' => '支付方式',
  61. 'pay_time' => '支付时间',
  62. 'number' => '数量',
  63. ];
  64. }
  65. // 数据设置
  66. public static function setData(array $params)
  67. {
  68. try{
  69. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  70. $mall_id = $params['mall_id'];
  71. if (isset($params['id']) && !empty($params['id'])) {
  72. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  73. if(empty($model)) throw new Exception('订单不存在或已删除');
  74. } else {
  75. $model = new self();
  76. $model->loadDefaultValues();
  77. $model->mall_id = $mall_id;
  78. }
  79. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  80. return $model;
  81. }catch(Exception $e){
  82. self::setStaticError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. // 创建激活订单
  87. public static function createActiveOrder($user_coupon_id, $user_id, $mall_id, $number)
  88. {
  89. try {
  90. // 查出券信息
  91. // 判断是否有效
  92. // 生成订单
  93. $config = Yii::$app->services->setting->addons->get($mall_id, ElectronCouponEnums::ADDONS_NAME, 'basic');
  94. $is_open = $config['is_open'] ?? 0;
  95. $customize_name = $config['customize_name'] ?? '电子券';
  96. if (!$is_open) {
  97. throw new Exception('暂时无法激活');
  98. }
  99. $user_coupon = ElectronCouponUserCoupon::getOne([
  100. ['id' => $user_coupon_id],
  101. ['mall_id' => $mall_id],
  102. ['user_id' => $user_id],
  103. ['>=', 'status', StatusEnum::DISABLED]
  104. ], false);
  105. if (!$user_coupon) {
  106. throw new Exception('找不到对应的' . $customize_name . '信息');
  107. }
  108. // 使用时间
  109. if ($user_coupon['use_end_time'] < time()) {
  110. throw new Exception('该' . $customize_name . '已过期,无法激活');
  111. }
  112. if ($number > $user_coupon['receive_amount']) {
  113. throw new Exception('激活数量过大,当前仅剩余' . $user_coupon['receive_amount']);
  114. }
  115. $order_data = [
  116. 'order_no' => self::getOrderNo($user_id),
  117. 'user_id' => $user_id,
  118. 'user_e_coupon_id' => $user_coupon_id,
  119. 'price' => bcmul($user_coupon['price'], $number, 2),
  120. 'status' => StatusEnum::ENABLED,
  121. 'mall_id' => $mall_id,
  122. 'is_pay' => ElectronCouponEnums::NOT_PAY,
  123. 'number' => $number
  124. ];
  125. $order_trade_info[] = [
  126. 'order_no' => $order_data['order_no'],
  127. 'amount' => $order_data['price'],
  128. 'title' => $customize_name . '激活订单',
  129. 'notify_class' => ElectronCouponEnums::NOTIFY_CLASS,
  130. 'order_type' => ElectronCouponEnums::ADDONS_NAME
  131. ];
  132. $trade_result = \Yii::$app->services->pay->createTrade($mall_id, $user_id, $order_trade_info);
  133. if ($trade_result == false) {
  134. throw new Exception('生成订单流失失败');
  135. }
  136. $order_data['trade_no'] = $trade_result['trade_no'];
  137. $result = self::setData($order_data);
  138. if ($result == false) {
  139. throw new Exception(self::getStaticError());
  140. }
  141. return $result;
  142. } catch (Exception $e) {
  143. self::setStaticError($e->getMessage());
  144. return false;
  145. }
  146. }
  147. /**
  148. * 生成订单号
  149. * @return string
  150. */
  151. public static function getOrderNo($user_id)
  152. {
  153. $randLen = 6;
  154. $id = base_convert(substr(uniqid($user_id), 0 - $randLen), 16, 10);
  155. if (strlen($id) > 10) {
  156. $id = substr($id, -10);
  157. } elseif (strlen($id) < 10) {
  158. $rLen = 10 - strlen($id);
  159. $id = $id . rand(pow(10, $rLen - 1), pow(10, $rLen) - 1);
  160. }
  161. $dateTimeStr = date('YmdHis');
  162. return 'EC' . $dateTimeStr . $id;
  163. }
  164. }