CommissionLog.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace addons\OperationCenter\common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%addons_operation_center_commission_log}}".
  6. *
  7. * @property int $id
  8. * @property int $mall_id Mall ID
  9. * @property int $user_id 用户ID
  10. * @property int $from_user_id 来源会员ID
  11. * @property int $level 运营中心等级
  12. * @property string|null $goods_name 商品名称
  13. * @property float $goods_price 商品售价
  14. * @property string|null $order_no 订单编号
  15. * @property int $commission_level 推荐佣金等级(直推,间推)
  16. * @property int $order_id 订单ID
  17. * @property int $order_detail_id 订单详情ID
  18. * @property int $setting_log_id 设置logID
  19. * @property float $commission_amount 佣金数量
  20. * @property int $status 0 待结算 1 已结算
  21. * @property int|null $deleted_at
  22. * @property int|null $updated_at
  23. * @property int|null $created_at
  24. */
  25. class CommissionLog extends BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_operation_center_commission_log}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'user_id', 'from_user_id', 'level', 'commission_level', 'order_id', 'order_detail_id', 'setting_log_id'], 'required'],
  41. [['mall_id', 'user_id', 'from_user_id', 'level', 'commission_level', 'order_id', 'order_detail_id', 'setting_log_id', 'status', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
  42. [['goods_price', 'commission_amount'], 'number'],
  43. [['goods_name'], 'string', 'max' => 64],
  44. [['order_no'], 'string', 'max' => 32],
  45. ];
  46. }
  47. /**
  48. * 关联用户
  49. * @Author: lun
  50. * @DateTime: 2023/8/18 0018 15:45
  51. * @Copyright: copyright (c) 2021 广东七件事集团
  52. * @return \yii\db\ActiveQuery
  53. */
  54. public function getFromUser()
  55. {
  56. return $this->hasOne(MallUser::class, ['id' => 'from_user_id'])->select('id, mobile, nickname, avatar_url');
  57. }
  58. /**
  59. * @return ActiveQueryInterface the relational query object.
  60. */
  61. public function getUser()
  62. {
  63. return $this->hasOne(MallUser::class, ['id' => 'user_id']);
  64. }
  65. /**
  66. * @return ActiveQueryInterface the relational query object.
  67. */
  68. public function getLevelInfo()
  69. {
  70. return $this->hasOne(Level::class, ['level' => 'level', 'mall_id' => 'mall_id']);
  71. }
  72. /**
  73. * @return ActiveQueryInterface the relational query object.
  74. */
  75. public function getOrder()
  76. {
  77. return $this->hasOne(MallOrder::class, ['id' => 'order_id']);
  78. }
  79. /**
  80. * @return ActiveQueryInterface the relational query object.
  81. */
  82. public function getApply()
  83. {
  84. return $this->hasOne(ApplyList::class, ['user_id' => 'user_id'])
  85. ->andWhere(['status' => ApplyList::PASSED]);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function attributeLabels()
  91. {
  92. return [
  93. 'id' => 'ID',
  94. 'mall_id' => 'Mall ID',
  95. 'user_id' => 'User ID',
  96. 'from_user_id' => 'From User ID',
  97. 'level' => 'Level',
  98. 'goods_name' => 'Goods Name',
  99. 'goods_price' => 'Goods Price',
  100. 'order_no' => 'Order No',
  101. 'commission_level' => 'Commission Level',
  102. 'order_id' => 'Order ID',
  103. 'order_detail_id' => 'Order Detail ID',
  104. 'setting_log_id' => 'Setting Log ID',
  105. 'commission_amount' => 'Commission Amount',
  106. 'status' => 'Status',
  107. 'deleted_at' => 'Deleted At',
  108. 'updated_at' => 'Updated At',
  109. 'created_at' => 'Created At',
  110. ];
  111. }
  112. /**
  113. * 添加分佣记录
  114. *
  115. * @param integer $mall_id
  116. * @param integer $user_id
  117. * @param integer $level
  118. * @param integer $commission_level
  119. * @param integer $order_id
  120. * @param integer $order_detail_id
  121. * @param integer $setting_log_id
  122. * @param float $commission_amount
  123. * @return int|static
  124. */
  125. public static function addCommissionLog(
  126. int $mall_id,
  127. int $user_id,
  128. int $from_user_id,
  129. string $goods_name,
  130. float $goods_price,
  131. string $order_no,
  132. int $level,
  133. int $commission_level,
  134. int $order_id,
  135. int $order_detail_id,
  136. int $setting_log_id,
  137. float $commission_amount
  138. )
  139. {
  140. $model = new static;
  141. $model->load(compact(
  142. 'mall_id',
  143. 'user_id',
  144. 'level',
  145. 'from_user_id',
  146. 'goods_name',
  147. 'goods_price',
  148. 'order_no',
  149. 'commission_level',
  150. 'order_id',
  151. 'order_detail_id',
  152. 'setting_log_id',
  153. 'commission_amount'
  154. ), '');
  155. $res = $model->save();
  156. return $res === true
  157. ? $model->id
  158. : $model;
  159. }
  160. /**
  161. * 通过订单详情ID查询是否已经创建佣金记录
  162. *
  163. * @param integer $order_detail_id
  164. * @param integer $level
  165. * @return boolean
  166. */
  167. public static function hasCommissonByDetailId(int $order_detail_id, int $level)
  168. {
  169. return (boolean) static::find()
  170. ->andWhere(['order_detail_id' => $order_detail_id, 'commission_level' => $level])
  171. ->select('id')
  172. ->scalar();
  173. }
  174. /**
  175. * 获取佣金列表
  176. *
  177. * @param integer $order_id
  178. * @return array
  179. */
  180. public static function getLogByOrderId(int $order_id)
  181. {
  182. return static::find()
  183. ->andWhere(['order_id' => $order_id, 'status' => 0])
  184. ->all();
  185. }
  186. /**
  187. * 获取佣金列表
  188. *
  189. * @param integer $order_detail_id
  190. * @return array
  191. */
  192. public static function getLogByOrderDetailId(int $order_detail_id)
  193. {
  194. return static::find()
  195. ->andWhere(['order_detail_id' => $order_detail_id, 'status' => 0])
  196. ->all();
  197. }
  198. /**
  199. * 修改状态
  200. *
  201. * @param array $ids
  202. * @param integer $status
  203. * @return void
  204. */
  205. public static function changeStatusByIds(array $ids, int $status = 1)
  206. {
  207. return static::updateAll(
  208. ['status' => $status],
  209. ['id' => $ids]
  210. );
  211. }
  212. }