OrderModel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\HuijuBill\common\models;
  3. use addons\HuijuBill\common\enums\StatusEnum;
  4. use common\models\common\PaymentTrade;
  5. use Yii;
  6. use yii\db\ActiveRecord;
  7. /**
  8. * This is the model class for table "{{%addons_huiju_bill_order}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property string $merchant_no 分账方账号
  13. * @property int $trade_id 支付流水ID
  14. * @property string $trade_type 分账支付类型
  15. * @property string $sys_order_no 系统支付订单号
  16. * @property string $trx_no 汇聚分账支付订单号
  17. * @property float $fee 手续费
  18. * @property int $status 100:成功,101:失败,102:已创建
  19. * @property int|null $updated_at
  20. * @property int|null $created_at
  21. */
  22. class OrderModel extends ActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_huiju_bill_order}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'merchant_no', 'trade_id', 'trade_type', 'sys_order_no', 'trx_no', 'status'], 'required'],
  38. [['mall_id', 'trade_id', 'status', 'updated_at', 'created_at'], 'integer'],
  39. [['fee'], 'number'],
  40. [['merchant_no', 'trade_type', 'sys_order_no', 'trx_no'], 'string', 'max' => 32],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'merchant_no' => 'Merchant No',
  52. 'trade_id' => 'Trade ID',
  53. 'trade_type' => 'Trade Type',
  54. 'sys_order_no' => 'Sys Order No',
  55. 'trx_no' => 'Trx No',
  56. 'fee' => 'Fee',
  57. 'status' => 'Status',
  58. 'updated_at' => 'Updated At',
  59. 'created_at' => 'Created At',
  60. ];
  61. }
  62. /**
  63. * 支付流水记录表
  64. *
  65. * @return ActiveQuery
  66. */
  67. public function getTrade()
  68. {
  69. return $this->hasOne(PaymentTrade::class, ['id' => 'trade_id']);
  70. }
  71. /**
  72. * 添加分账支付记录
  73. *
  74. * @param array $data
  75. * [
  76. * 'mall_id' => 'Mall ID',
  77. * 'merchant_no' => 'Merchant No',
  78. * 'trade_id' => 'Trade ID',
  79. * 'trade_type' => 'Trade Type',
  80. * 'sys_order_no' => 'Sys Order No',
  81. * 'trx_no' => 'Trx No'
  82. * 'fee' => 'Fee'
  83. * 'status' => 'Status'
  84. * ]
  85. * @return OrderModel
  86. */
  87. public static function addLog($data = [])
  88. {
  89. if (
  90. isset($data['trade_id']) &&
  91. self::find()->where(['trade_id' => $data['trade_id']])->select('id')->one()
  92. ) {
  93. return true;
  94. }
  95. $data['created_at'] = $data['updated_at'] = time();
  96. $model = new self();
  97. $model->load($data, '');
  98. return $model->save();
  99. }
  100. /**
  101. * 支付成功
  102. *
  103. * @return void
  104. */
  105. public function success()
  106. {
  107. $this->status = StatusEnum::STATUS_100;
  108. $this->updated_at = time();
  109. return $this->save();
  110. }
  111. }