UserTeamAchievementOrder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace common\models\user;
  3. use common\models\base\BaseActiveRecord;
  4. use common\models\order\Order;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%user_team_achievement_order_1}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id 下单会员ID
  12. * @property int $order_id 订单ID
  13. * @property int $superior_user_id 团队上级会员ID
  14. * @property int $superior_level 上级级别
  15. * @property int $status 1 已支付 2 已完成
  16. * @property int|null $created_at
  17. */
  18. class UserTeamAchievementOrder extends BaseActiveRecord
  19. {
  20. /**
  21. * @var integer
  22. */
  23. protected static $tb = 0;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%user_team_achievement_order_' . static::$tb .'}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'user_id', 'order_id', 'superior_user_id', 'superior_level', 'status'], 'required'],
  38. [['mall_id', 'user_id', 'order_id', 'superior_user_id', 'superior_level', 'status', 'created_at'], 'integer'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'user_id' => 'User ID',
  50. 'order_id' => 'Order ID',
  51. 'superior_user_id' => 'Superior User ID',
  52. 'superior_level' => 'Superior Level',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. ];
  56. }
  57. /**
  58. * @return ActiveQueryInterface the relational query object.
  59. */
  60. public function getOrder()
  61. {
  62. return $this->hasOne(Order::class, ['id' => 'order_id']);
  63. }
  64. /**
  65. * @return ActiveQueryInterface the relational query object.
  66. */
  67. public function getUser()
  68. {
  69. return $this->hasOne(User::class, ['id' => 'user_id']);
  70. }
  71. /**
  72. * 批量添加
  73. *
  74. * @param array $data
  75. * @return void
  76. */
  77. public static function batchInsertData(array $data)
  78. {
  79. $insert_data = [];
  80. foreach ($data as $item) {
  81. $insert_data[static::sliceRule($item['superior_user_id'])][] = $item;
  82. }
  83. foreach ($insert_data as $tb => $items) {
  84. static::find()
  85. ->createCommand()
  86. ->batchInsert(static::getTb($tb), array_keys(reset($items)), $items)->execute();
  87. }
  88. }
  89. /**
  90. * 切割规则
  91. *
  92. * @param integer $user_id
  93. * @return int
  94. */
  95. protected static function sliceRule(int $user_id)
  96. {
  97. return $user_id % 10 ? $user_id % 10 : 10;
  98. }
  99. /**
  100. * 获取tb
  101. *
  102. * @param integer $tb
  103. * @return string
  104. */
  105. protected static function getTb(int $tb)
  106. {
  107. return '{{%user_team_achievement_order_' . $tb . '}}';
  108. }
  109. /**
  110. * 删除改站点相关数据
  111. *
  112. * @param integer $mall_id
  113. * @return void
  114. */
  115. public static function deleteMall(int $mall_id)
  116. {
  117. for ($i = 1; $i <= 10; $i++) {
  118. static::$tb = $i;
  119. static::deleteAll(['mall_id' => $mall_id]);
  120. }
  121. }
  122. /**
  123. * 获取列表
  124. *
  125. * @param integer $limit
  126. * @param callable|null $callback
  127. * @param string $order_by
  128. * @return array
  129. */
  130. public static function getPaginationListByUid(
  131. int $uid, int $limit, callable $callback = null, string $order_by = 'id'
  132. ): array
  133. {
  134. static::$tb = static::sliceRule($uid);
  135. return static::getPaginationList($limit, $callback, $order_by);
  136. }
  137. public static function getTeamAchievementList(int $uid, array $where)
  138. {
  139. static::$tb = static::sliceRule($uid);
  140. return static::find()->where($where);
  141. }
  142. }