PkOrder.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace addons\Pk\common\models;
  3. use addons\Pk\common\enums\PkEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Yii;
  7. use Exception;
  8. /**
  9. * This is the model class for table "{{%addons_pk_order}}".
  10. *
  11. * @property int $id
  12. * @property int|null $trade_no
  13. * @property string $order_no 订单编号
  14. * @property int $order_type 1个人报名;2战队创建
  15. * @property int $is_pay 是否支付;0未支付;1已支付;2已退款
  16. * @property float $money 支付金额
  17. * @property string $pay_type wechat微信;balance余额
  18. * @property int $created_at
  19. * @property int $updated_at
  20. * @property int $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $mall_id 商城id
  22. * @property int $user_id 用户id
  23. * @property int $activity_id 活动id
  24. * @property int|null $performance_goal 目标值
  25. */
  26. class PkOrder extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_pk_order}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [[ 'order_type', 'is_pay', 'created_at', 'updated_at', 'status', 'mall_id', 'user_id', 'activity_id', 'performance_goal'], 'integer'],
  42. [['order_no', 'is_pay', 'money', 'mall_id', 'user_id', 'activity_id'], 'required'],
  43. [['money'], 'number'],
  44. [['trade_no','order_no'], 'string', 'max' => 255],
  45. [['pay_type'], 'string', 'max' => 125],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'trade_no' => 'Trade No',
  56. 'order_no' => '订单编号',
  57. 'order_type' => '1个人报名;2战队创建',
  58. 'is_pay' => '是否支付;0未支付;1已支付;2已退款',
  59. 'money' => '支付金额',
  60. 'pay_type' => 'wechat微信;balance余额',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'mall_id' => '商城id',
  65. 'user_id' => '用户id',
  66. 'activity_id' => '活动id',
  67. 'performance_goal' => '目标值',
  68. ];
  69. }
  70. /**
  71. * 生成订单号
  72. * @return string
  73. */
  74. public static function getOrderNo()
  75. {
  76. $randLen = 6;
  77. $id = base_convert(substr(uniqid(), 0 - $randLen), 16, 10);
  78. if (strlen($id) > 10) {
  79. $id = substr($id, -10);
  80. } elseif (strlen($id) < 10) {
  81. $rLen = 10 - strlen($id);
  82. $id = $id . rand(pow(10, $rLen - 1), pow(10, $rLen) - 1);
  83. }
  84. $dateTimeStr = date('YmdHis');
  85. return 'PK' . $dateTimeStr . $id;
  86. }
  87. public function getUser()
  88. {
  89. return $this->hasOne(User::ClassName(), ['id' => 'user_id']);
  90. }
  91. public function getActivity()
  92. {
  93. return $this->hasOne(PkActivity::ClassName(), ['id' => 'activity_id']);
  94. }
  95. /**
  96. * 保存数据
  97. * @Author: ltm
  98. * @DateTime: 2022/6/8 0015 9:26
  99. * @Copyright: copyright (c) 2021
  100. * @param $mall_id
  101. * @param array $params
  102. * @return Rebate|bool
  103. */
  104. public static function setData(array $params){
  105. try{
  106. //生成流水订单
  107. $order_trade_info[] = [
  108. 'order_no' => $params['order_no'],
  109. 'amount' => $params['money'],
  110. 'title' => '参加pk费用',
  111. 'notify_class' => PkEnum::NOTIFY_CLASS,
  112. 'order_type' => PkEnum::ADDONS_NAME
  113. ];
  114. $data = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
  115. $mall_id = $params['mall_id'];
  116. if(!empty($params['id'])){
  117. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status', StatusEnum::ENABLED]]);
  118. if(empty($model)) throw new \Exception('活动不存在或已删除');
  119. }else{
  120. $model = new self();
  121. $model->loadDefaultValues();
  122. $model->mall_id = $mall_id;
  123. }
  124. $model->trade_no = $data['trade_no'];
  125. if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  126. //生成支付流水
  127. return $data;
  128. }catch(\Exception $e){
  129. self::setStaticError($e->getMessage());
  130. return false;
  131. }
  132. }
  133. }