ShortVideoOrderAward.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use common\enums\OrderStatusEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\order\Order;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%addons_short_video_order_award}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $video_id 视频id
  13. * @property int $order_id 子订单id
  14. * @property int $user_id 用户id
  15. * @property int $goods_id 商品id
  16. * @property int $goods_price 商品售价
  17. * @property float $money 奖励金额
  18. * @property int $award_type 奖励类型 0:金额 1:百分比
  19. * @property float $award_percentage 奖励百分比
  20. * @property float $award_money 奖励金额
  21. * @property int $is_settlement 是否结算 0:否 1:是
  22. * @property int|null $settlement_time 结算时间
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $goods_num 商品数量
  25. * @property int|null $is_award 是否带货奖励商品 0:否 1:是
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class ShortVideoOrderAward extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_short_video_order_award}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'video_id', 'order_id', 'user_id', 'goods_id'], 'required'],
  45. [['mall_id', 'video_id', 'order_id', 'user_id', 'goods_id', 'award_type', 'is_settlement', 'settlement_time', 'status', 'goods_num', 'is_award', 'created_at', 'updated_at'], 'integer'],
  46. [['money', 'award_percentage', 'award_money', 'goods_price'], 'number'],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => 'Mall ID',
  57. 'video_id' => 'Video ID',
  58. 'order_id' => 'Order ID',
  59. 'user_id' => 'User ID',
  60. 'goods_id' => 'Goods ID',
  61. 'goods_price' => '商品售价',
  62. 'money' => 'Money',
  63. 'award_type' => 'Award Type',
  64. 'award_percentage' => 'Award Percentage',
  65. 'award_money' => 'Award Money',
  66. 'is_settlement' => 'Is Settlement',
  67. 'settlement_time' => 'Settlement Time',
  68. 'status' => 'Status',
  69. 'goods_num' => 'Goods Num',
  70. 'is_award' => 'Is Award',
  71. 'created_at' => 'Created At',
  72. 'updated_at' => 'Updated At',
  73. ];
  74. }
  75. public static function getStatData(int $mall_id, string $type, array $params = [])
  76. {
  77. $query = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  78. if (!empty($params)) {
  79. foreach ($params as $where) {
  80. $query->andWhere($where);
  81. }
  82. }
  83. $order_awards = $query->asArray()->all();
  84. $order_ids = array_column($order_awards, 'order_id');
  85. // dd($order_ids);
  86. $query_o = Order::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids]);
  87. if ('order_sum_money' == $type || 'order_money' == $type) {
  88. $stat_data = $query_o->sum('pay_money') ?? 0;
  89. } elseif ('order_num' == $type) {
  90. $stat_data = $query_o->count() ?? 0;
  91. } elseif ('order_people' == $type) {
  92. $stat_data = $query_o->select('user_id')->groupBy('user_id')->asArray()->count();
  93. }elseif ('pay_money' == $type) {
  94. $query_o->andWhere(['pay_status' => OrderStatusEnum::PAY]);
  95. $stat_data = $query_o->sum('pay_money') ?? 0;
  96. } elseif ('pay_people' == $type) {
  97. $query_o->andWhere(['pay_status' => OrderStatusEnum::PAY]);
  98. $stat_data = $query_o->select('user_id')->groupBy('user_id')->asArray()->count();
  99. } else {
  100. $stat_data = $query_o->all();
  101. }
  102. return $stat_data;
  103. }
  104. public static function setData(int $mall_id, array $data)
  105. {
  106. if (!empty($data['id'])) {
  107. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  108. if (empty($model)) {
  109. self::$static_error = '带货订单记录不存在';
  110. return false;
  111. }
  112. } else {
  113. $model = new self();
  114. $model->loadDefaultValues();
  115. $model->mall_id = $mall_id;
  116. }
  117. foreach ($data as $key => $val) {
  118. $model->$key = $val;
  119. }
  120. if (!$model->save()) {
  121. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  122. return false;
  123. }
  124. return $model;
  125. }
  126. }