IncomeLogController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace addons\ElectronCoupon\backend\controllers;
  3. use addons\ElectronCoupon\common\models\ElectronCoupon;
  4. use addons\ElectronCoupon\common\models\ElectronCouponApprovalIncomeLog;
  5. use Yii;
  6. use common\controllers\AddonsController;
  7. use common\enums\StatusEnum;
  8. use common\models\user\User;
  9. class IncomeLogController extends BaseController
  10. {
  11. public $enableCsrfValidation = false;
  12. /**
  13. * 收益明细
  14. *
  15. * @return string
  16. */
  17. public function actionIndex()
  18. {
  19. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  20. $params = Yii::$app->request->post();
  21. $res = Yii::$app->services->validate->validate($params, [
  22. [['page', 'user_id'], 'integer'],
  23. [['profit_time'], 'safe'],
  24. [['user_name'], 'string'],
  25. ]);
  26. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  27. $where = [
  28. ['=', 'mall_id', $this->mall_id],
  29. ['>=', 'status', StatusEnum::DISABLED],
  30. ];
  31. if ($params['user_id']) {
  32. $where[] = ['=', 'user_id', (int)$params['user_id']];
  33. }
  34. // 昵称、手机号
  35. if ($params['user_name']) {
  36. $user_ids = User::find()
  37. ->orWhere(['like', 'nickname', "%" . $params['user_name'] . "%", false])
  38. ->orWhere(['like', 'mobile', "%" . $params['user_name'] . "%", false])
  39. ->select('id')
  40. ->asArray()
  41. ->column();
  42. $where[] = ['in', 'user_id', $user_ids];
  43. }
  44. // 收益时间
  45. $start_time = strtotime($params['profit_time'][0] ?? 0);
  46. $end_time = strtotime($params['profit_time'][1] ?? 0);
  47. if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
  48. $where[] = ['>=', 'created_at', $start_time];
  49. $where[] = ['<=', 'created_at', $end_time];
  50. }
  51. $condition = ['where' => $where, 'with' => ['user'], 'order' => 'id desc'];
  52. $page_data = ElectronCouponApprovalIncomeLog::listPage($condition, false, true);
  53. $page_data['list'] = is_array($page_data['list']) ? $page_data['list'] : [];
  54. // 关联电子券
  55. $coupon_id_array = array_unique(array_filter(array_column($page_data['list'], 'e_coupon_id')));
  56. $e_coupon = ElectronCoupon::find()
  57. ->select('id,name,is_proper')
  58. ->where(['>=', 'status', StatusEnum::DISABLED])
  59. ->where(['mall_id' => $this->mall_id])
  60. ->where(['in', 'id', $coupon_id_array])
  61. ->asArray()
  62. ->all();
  63. $coupon_name_data = array_column($e_coupon, null, 'id');
  64. foreach ($page_data['list'] as $index => $item) {
  65. // 电子券名称
  66. $item['coupon'] = $coupon_name_data[$item['e_coupon_id']] ?? '';
  67. $page_data['list'][$index] = $item;
  68. }
  69. $this->success('获取成功', compact('page_data'));
  70. } else {
  71. return $this->render('index');
  72. }
  73. }
  74. }