CommissionService.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\models\WechatVideoShopCommission;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use common\models\order\Order;
  7. class CommissionService
  8. {
  9. /**
  10. * 分页
  11. * @param int $mall_id
  12. * @param array $params
  13. * @return array|false|mixed|string|null
  14. */
  15. public function page(int $mall_id, array $params)
  16. {
  17. $where = [
  18. ['mall_id' => $mall_id],
  19. ['status' => StatusEnum::ENABLED],
  20. ];
  21. if (!empty($params['user_id'])) {
  22. $where[] = ['user_id' => $params['user_id']];
  23. }
  24. if (!empty($params['mobile'])) {
  25. $uids = User::find()
  26. ->where(['mall_id' => $mall_id])
  27. ->andWhere(['or', ['like', 'username', $params['mobile']], ['like', 'mobile', $params['mobile']], ['like', 'nickname', $params['mobile']]])
  28. ->select('id')
  29. ->column();
  30. if (empty($uids)) return [];
  31. $where[] = ['user_id' => $uids];
  32. }
  33. if (!empty($params['settle_status'])) {
  34. $where[] = ['settle_status' => $params['settle_status']];
  35. }
  36. if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
  37. if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
  38. $ret = WechatVideoShopCommission::listPage([
  39. 'where' => $where,
  40. 'order' => 'id desc',
  41. 'limit' => $params['limit'] ?? 10,
  42. 'with' => ['user', 'buyUser']
  43. ]);
  44. if (!empty(WechatVideoShopCommission::getStaticError())) return WechatVideoShopCommission::getStaticError();
  45. return $ret;
  46. }
  47. /**
  48. * @param Order $order
  49. * @return bool
  50. * @throws \Exception
  51. */
  52. public function create(Order $order)
  53. {
  54. // 这里是创建分佣
  55. $model = new WechatVideoShopCommission();
  56. $model->mall_id = $order->mall_id;
  57. $model->user_id = $order->mall_id;
  58. $model->buy_user_id = $order->mall_id;
  59. $model->order_no = $order->mall_id;
  60. $model->order_id = $order->mall_id;
  61. $model->order_amount = $order->mall_id;
  62. $model->remark = $order->mall_id;
  63. $model->settle_status = $order->mall_id;
  64. $model->commission = $order->mall_id;
  65. $model->commission_type = $order->mall_id;
  66. $model->shop_id = 0;
  67. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  68. return true;
  69. }
  70. }