SecondaryCardOrder.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace addons\SecondaryCard\common\models;
  3. use common\enums\StatusEnum;
  4. use common\helpers\StringHelper;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\user\User;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_secondary_card_order".
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $user_id 会员id
  14. * @property int $secondary_card_id 次卡id
  15. * @property int $use_type 使用范围:0:全部店铺;1:指定店铺
  16. * @property string $order_no 次卡号
  17. * @property string $clerk_code 核销码
  18. * @property int|null $order_status 状态:0:待核销;1:已失效;2:已过期;3:已退款;4:已停卡
  19. * @property int $open_time 开卡时间:需要支付时,支付时间
  20. * @property int $open_type 开卡方式:1:下单购买;2:兑换码兑换
  21. * @property float $pay_money 购卡实收金额
  22. * @property int $pay_type 支付方式:1=微信支付,2=货到付款,3=余额支付,4=支付宝支付,5=银联支付,6=汇聚聚合支付,7=汇聚快捷支付
  23. * @property int $start_time 有效期开始时间
  24. * @property int $expire_time 有效期结束时间,过期时间
  25. * @property int $refund_source 退款来源:1:会员申请;2:过期,后台操作退款
  26. * @property float $refund_money 退款金额
  27. * @property int $refund_time 退款时间
  28. * @property int $verifiable_times 可核销次数
  29. * @property string $code 兑换码
  30. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at
  33. * @property int $trade_id
  34. * @property int $create_source_type 创建来源类型:0:平台;1:门店
  35. * @property int $create_source_id 创建来源id
  36. */
  37. class SecondaryCardOrder extends BaseActiveRecord
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%addons_secondary_card_order}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['mall_id', 'user_id'], 'required'],
  53. [['mall_id', 'user_id', 'use_type', 'secondary_card_id', 'order_status', 'open_time', 'open_type', 'pay_type',
  54. 'start_time', 'expire_time', 'refund_source', 'refund_time', 'verifiable_times', 'status', 'created_at', 'updated_at','trade_id',
  55. 'create_source_type','create_source_id'], 'integer'],
  56. [['pay_money', 'refund_money'], 'number'],
  57. [['order_no', 'code'], 'string', 'max' => 255],
  58. [['clerk_code'], 'string', 'max' => 12],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'mall_id' => 'Mall ID',
  69. 'user_id' => '会员id',
  70. 'secondary_card_id' => '次卡id',
  71. 'use_type' => '使用范围:0:全部店铺;1:指定店铺',
  72. 'order_no' => '次卡号',
  73. 'clerk_code' => '核销码',
  74. 'order_status' => '状态:0:待核销;1:已失效;2:已过期;3:已退款;4:已停卡',
  75. 'open_time' => '开卡时间:需要支付时,支付时间',
  76. 'open_type' => '开卡方式:1:下单购买;2:兑换码兑换',
  77. 'pay_money' => '购卡实收金额',
  78. 'pay_type' => '支付方式:1=微信支付,2=货到付款,3=余额支付,4=支付宝支付,5=银联支付,6=汇聚聚合支付,7=汇聚快捷支付',
  79. 'start_time' => '有效期开始时间',
  80. 'expire_time' => '有效期结束时间,过期时间',
  81. 'refund_source' => '退款来源:1:会员申请;2:过期,后台操作退款',
  82. 'refund_money' => '退款金额',
  83. 'refund_time' => '退款时间',
  84. 'verifiable_times' => '可核销次数',
  85. 'code' => '兑换码',
  86. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  87. 'created_at' => '创建时间',
  88. 'updated_at' => 'Updated At',
  89. 'trade_id' => 'trade_id',
  90. 'create_source_type' => '创建来源类型:0:平台;1:门店',
  91. 'create_source_id' => '创建来源id',
  92. ];
  93. }
  94. public static function getExportFields()
  95. {
  96. return [
  97. 'order_no' => '卡号',
  98. 'nickname' => '开卡用户',
  99. 'order_status' => '次卡状态',
  100. 'open_time' => '开卡时间',
  101. 'open_type' => '开卡方式',
  102. 'pay_money' => '购卡实收金额',
  103. 'expire_time' => '到期时间',
  104. 'refund_money' => '退款金额',
  105. 'refund_time' => '退款时间',
  106. ];
  107. }
  108. public function getSecondaryCard()
  109. {
  110. return $this->hasOne(SecondaryCard::class, ['id' => 'secondary_card_id']);
  111. }
  112. public function getUser()
  113. {
  114. return $this->hasOne(User::class, ['id' => 'user_id']);
  115. }
  116. public static function setData(array $params)
  117. {
  118. try {
  119. if (!empty($params['id'])) {
  120. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  121. if (empty($model)) throw new \Exception('服务不存在或已删除');
  122. } else {
  123. $model = new self();
  124. $model->loadDefaultValues();
  125. }
  126. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  127. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  128. return $model;
  129. } catch (\Exception $e) {
  130. self::$static_error = $e->getMessage();
  131. return false;
  132. }
  133. }
  134. /**
  135. *
  136. * @Author:hua
  137. * @Date:2024-03-08 14:35
  138. * @Copyright:copyright(c)2024 广东七件事集团
  139. * @param int $len
  140. * @return false|string
  141. */
  142. public static function getOrderNo()
  143. {
  144. while (true) {
  145. $order_no = StringHelper::generateRandCode(10);
  146. $rand = mt_rand(10000, 99999);
  147. $order_no = $rand . $order_no;
  148. $res = SecondaryCardOrder::findOne(['order_no' => $order_no, 'status' => StatusEnum::ENABLED]);
  149. if (empty($res)) {
  150. break;
  151. }
  152. }
  153. return $order_no;
  154. }
  155. }