StoreLogController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace addons\ElectronCoupon\backend\controllers;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use addons\Store\common\models\StoreClerkIncome;
  6. use addons\Store\common\models\StoreClerkIncomeData;
  7. use common\models\user\User;
  8. class StoreLogController extends BaseController
  9. {
  10. public $enableCsrfValidation = false;
  11. /**
  12. * 收益明细
  13. *
  14. * @return string
  15. */
  16. public function actionIndex()
  17. {
  18. if (Yii::$app->request->isAjax) {
  19. $get = \Yii::$app->request->get();
  20. $where = [
  21. ['mall_id' => $this->mall_id],
  22. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  23. ];
  24. if (!empty($get['store_id'])) $where[] = ['store_id' => $get['store_id']];
  25. $params = ['where' => $where, 'order' => 'id desc', 'with' => 'store'];
  26. $list = StoreClerkIncomeData::listPage($params);
  27. if ($list === false) return $this->error(StoreClerkIncomeData::getStaticError());
  28. return $this->success('ok', $list);
  29. }
  30. return $this->render($this->action->id);
  31. }
  32. public function actionDetail()
  33. {
  34. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  35. $params = Yii::$app->request->post();
  36. $store_id=$params['store_id'];
  37. $res = Yii::$app->services->validate->validate($params, [
  38. [['page', 'user_id','clerk_user_id'], 'integer'],
  39. [['user_name', 'e_coupon_name', 'check_user_name'], 'string'],
  40. [['usage_time'], 'safe'],
  41. ]);
  42. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  43. $where = [
  44. ['mall_id' => $this->mall_id],
  45. ['store_id' => $store_id],
  46. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  47. ];
  48. if ($params['user_id']) {
  49. $where[] = ['=', 'user_id', (int)$params['user_id']];
  50. }
  51. if ($params['clerk_user_id']) {
  52. $where[] = ['clerk_user_id'=>(int)$params['clerk_user_id']];
  53. }
  54. // 使用者 昵称、手机号
  55. if ($params['user_name']) {
  56. $user_ids = User::find()
  57. ->orWhere(['like', 'nickname', "%" . $params['user_name'] . "%", false])
  58. ->orWhere(['like', 'mobile', "%" . $params['user_name'] . "%", false])
  59. ->select('id')
  60. ->asArray()
  61. ->column();
  62. $where[] = ['in', 'user_id', $user_ids];
  63. }
  64. // 核销员 昵称、手机号
  65. if ($params['check_user_name']) {
  66. $user_ids = User::find()
  67. ->orWhere(['like', 'nickname', "%" . $params['check_user_name'] . "%", false])
  68. ->orWhere(['like', 'mobile', "%" . $params['check_user_name'] . "%", false])
  69. ->select('id')
  70. ->asArray()
  71. ->column();
  72. $where[] = ['in', 'clerk_user_id', $user_ids];
  73. }
  74. if ($params['e_coupon_name']) {
  75. $where[] = ['like', 'e_coupon_name', "%" . $params['e_coupon_name'] . "%", false];
  76. }
  77. // 使用时间
  78. $start_time = strtotime($params['usage_time'][0] ?? 0);
  79. $end_time = strtotime($params['usage_time'][1] ?? 0);
  80. if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
  81. $where[] = ['>=', 'created_at', $start_time];
  82. $where[] = ['<=', 'created_at', $end_time];
  83. }
  84. $condition = ['where' => $where, 'order' => 'id desc'];
  85. $page_data = StoreClerkIncome::listPage($condition);
  86. $user_id_array1 = array_column($page_data['list'], 'user_id');
  87. $user_id_array2 = array_column($page_data['list'], 'clerk_user_id');
  88. $user_id_array3 = array_column($page_data['list'], 'proper_user_id');
  89. $user_id_array = array_unique(array_filter(array_merge($user_id_array1, $user_id_array2, $user_id_array3)));
  90. $users = User::find()
  91. ->select('id,nickname,avatar_url,mobile')
  92. ->where(['>=', 'status', StatusEnum::DISABLED])
  93. ->where(['mall_id' => $this->mall_id])
  94. ->where(['in', 'id', $user_id_array])
  95. ->asArray()
  96. ->all();
  97. $users_data = array_column($users, null, 'id');
  98. $page_data['list'] = is_array($page_data['list']) ? $page_data['list'] : [];
  99. foreach ($page_data['list'] as $index => $item) {
  100. $item['user'] = $users_data[$item['user_id']] ?? [];
  101. $item['clerk_user'] = $users_data[$item['clerk_user_id']] ?? [];
  102. $item['proper_user'] = $users_data[$item['proper_user_id']] ?? [];
  103. $page_data['list'][$index] = $item;
  104. }
  105. if ($page_data === false) return $this->error(StoreClerkIncome::getStaticError());
  106. return $this->success('ok', compact('page_data'));
  107. }
  108. return $this->render($this->action->id);
  109. }
  110. public function actionGetTopData()
  111. {
  112. if (Yii::$app->request->isAjax) {
  113. if (Yii::$app->request->isGet) {
  114. $get = \Yii::$app->request->get();
  115. $store_id=$get['store_id'];
  116. $params=$get;
  117. $where[] = 'and';
  118. $where[] = ['mall_id' => $this->mall_id];
  119. $where[] = ['store_id' => $store_id];
  120. $where[] = ['status' => StatusEnum::ENABLED];
  121. if ($params['user_id']) {
  122. $where[] = ['=', 'user_id', (int)$params['user_id']];
  123. }
  124. if ($params['clerk_user_id']) {
  125. $where[] = ['clerk_user_id'=>(int)$params['clerk_user_id']];
  126. }
  127. // 使用者 昵称、手机号
  128. if ($params['user_name']) {
  129. $user_ids = User::find()
  130. ->orWhere(['like', 'nickname', "%" . $params['user_name'] . "%", false])
  131. ->orWhere(['like', 'mobile', "%" . $params['user_name'] . "%", false])
  132. ->select('id')
  133. ->asArray()
  134. ->column();
  135. $where[] = ['in', 'user_id', $user_ids];
  136. }
  137. // 核销员 昵称、手机号
  138. if ($params['check_user_name']) {
  139. $user_ids = User::find()
  140. ->orWhere(['like', 'nickname', "%" . $params['check_user_name'] . "%", false])
  141. ->orWhere(['like', 'mobile', "%" . $params['check_user_name'] . "%", false])
  142. ->select('id')
  143. ->asArray()
  144. ->column();
  145. $where[] = ['in', 'clerk_user_id', $user_ids];
  146. }
  147. if ($params['e_coupon_name']) {
  148. $where[] = ['like', 'e_coupon_name', "%" . $params['e_coupon_name'] . "%", false];
  149. }
  150. // 使用时间
  151. $start_time = strtotime($params['usage_time'][0] ?? 0);
  152. $end_time = strtotime($params['usage_time'][1] ?? 0);
  153. if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
  154. $where[] = ['>=', 'created_at', $start_time];
  155. $where[] = ['<=', 'created_at', $end_time];
  156. }
  157. $total_amount = StoreClerkIncome::find()
  158. ->where($where)
  159. ->sum('amount');
  160. $total_amount = $total_amount ? $total_amount : 0;
  161. $total_income = StoreClerkIncome::find()
  162. ->where($where)
  163. ->sum('income');
  164. $total_time = StoreClerkIncome::find()
  165. ->where($where)
  166. ->count();
  167. $data = [
  168. 'total_time'=>$total_time??0,
  169. 'total_amount' => $total_amount??0,
  170. 'total_income'=>$total_income??0,
  171. ];
  172. return $this->success('获取成功', $data);
  173. }
  174. }
  175. }
  176. }