DepositController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\DepositFilter;
  4. use App\Http\Resources\DepositOrderResource;
  5. use App\Models\AdminUser;
  6. use App\Models\AdminUserArea;
  7. use App\Models\Area;
  8. use App\Models\DepositOrder;
  9. use App\Models\DepositRefund;
  10. use App\Models\Statistic;
  11. use App\Utils\Admin;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. use Illuminate\Support\Facades\Log;
  16. /**
  17. * Class DepositController
  18. * @package App\Http\Controllers\Admin
  19. */
  20. class DepositController extends Controller
  21. {
  22. /**
  23. * index
  24. *
  25. * @param Request $request
  26. * @param DepositFilter $filter
  27. * @return \Illuminate\Http\JsonResponse *@author Fx
  28. *
  29. */
  30. public function index(Request $request, DepositFilter $filter)
  31. {
  32. //
  33. $admin_id = Admin::user()->id;
  34. $depositOrder = DepositOrder::query()
  35. ->filter($filter)
  36. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  37. ->orderByDesc('id');
  38. if (!Admin::isAdministrator()) {
  39. //判断哪些区域是此管理员创建得
  40. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  41. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  42. if (count($area_ids) !== 0) {
  43. $area_idss = array_merge($area_ids,$area_id);
  44. $depositOrder = $depositOrder->whereIn('area_id', $area_idss);
  45. } else {
  46. //$area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  47. $depositOrder = $depositOrder->whereIn('area_id', $area_id);
  48. }
  49. }
  50. $depositOrder = $request->get('all') ? $depositOrder->get() : $depositOrder->paginate();
  51. return $this->ok(DepositOrderResource::collection($depositOrder));
  52. }
  53. /**
  54. * Show the form for creating a new resource.
  55. *
  56. * @return \Illuminate\Http\Response
  57. */
  58. public function create()
  59. {
  60. //
  61. }
  62. /**
  63. * Store a newly created resource in storage.
  64. *
  65. * @param \Illuminate\Http\Request $request
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function store(Request $request)
  69. {
  70. //
  71. }
  72. /**
  73. * Display the specified resource.
  74. *
  75. * @param int $id
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function show($id)
  79. {
  80. //
  81. }
  82. /**
  83. * Show the form for editing the specified resource.
  84. *
  85. * @param int $id
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function edit($id)
  89. {
  90. //
  91. }
  92. /**
  93. * Update the specified resource in storage.
  94. *
  95. * @param \Illuminate\Http\Request $request
  96. * @param int $id
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function update(Request $request, $id)
  100. {
  101. //
  102. }
  103. /**
  104. * Remove the specified resource from storage.
  105. *
  106. * @param int $id
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function destroy($id)
  110. {
  111. //
  112. }
  113. /**
  114. * depositAnalysis 押金分析
  115. *
  116. * @param Request $request
  117. * @return \Illuminate\Http\JsonResponse *@author Fx
  118. *
  119. */
  120. public function depositAnalysis(Request $request)
  121. {
  122. $area_id = $request->get('area_id') ?? "";
  123. // Log::info($area_id);
  124. if (empty($area_id)) return $this->error(['缺少参数']);
  125. $today = Carbon::today();
  126. $first_day = date('Y-m-01', strtotime($today)); //每月一号
  127. $deposit_statistic_start = date('Y-m-d', strtotime("-13 day"));
  128. //本日新增
  129. $data['new_add'] = DepositOrder::query()
  130. ->where('pay_time', '>', $today)
  131. ->where('pay_status', DepositOrder::PAY_STATUS_OK);
  132. //本日退还
  133. $data['new_refund'] = DepositRefund::query()
  134. ->where('pay_time', '>', $today)
  135. ->where('pay_status', DepositRefund::PAY_STATUS_OK);
  136. //本月累计押金
  137. $data['this_money_total'] = DepositOrder::query()
  138. ->where('pay_time', '>', $first_day)
  139. ->where('pay_status', DepositRefund::PAY_STATUS_OK)
  140. ->where('is_refund', DepositOrder::REFUND_NO);
  141. //总计押金(数据量大导致整个接口请求变慢 单独做一个接口)
  142. /*$data['deposit_total'] = DepositOrder::query()
  143. ->where('pay_status',DepositRefund::PAY_STATUS_OK)
  144. ->where('is_refund',DepositOrder::REFUND_NO);*/
  145. if ($area_id == config('statistic.all')) {
  146. $admin_id = Admin::user()->id;
  147. if (!Admin::isAdministrator()) {
  148. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  149. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  150. $area_ids = array_merge($area_ids,$area_id);
  151. //本日新增
  152. $data['new_add'] = $data['new_add']
  153. ->whereIn('area_id', $area_ids);
  154. //本日退还
  155. $data['new_refund'] = $data['new_refund']
  156. ->whereIn('area_id', $area_ids);
  157. //本月累计押金
  158. $data['this_money_total'] = $data['this_money_total']
  159. ->whereIn('area_id', $area_ids);
  160. }
  161. //本日新增
  162. $data['new_add'] = $data['new_add']
  163. ->sum('money');
  164. //本日退还
  165. $data['new_refund'] = $data['new_refund']
  166. ->sum('money');
  167. //本月累计押金
  168. $data['this_money_total'] = $data['this_money_total']
  169. ->sum('money');
  170. //总计押金
  171. /*$data['deposit_total'] = $data['deposit_total']
  172. ->sum('money');*/
  173. } else {
  174. //本日新增
  175. $data['new_add'] = $data['new_add']
  176. ->where('area_id', $area_id)
  177. ->sum('money');
  178. //本日退还
  179. $data['new_refund'] = $data['new_refund']
  180. ->where('area_id', $area_id)
  181. ->sum('money');
  182. //本月累计押金
  183. $data['this_money_total'] = $data['this_money_total']
  184. ->where('area_id', $area_id)
  185. ->sum('money');
  186. //总计押金
  187. /*$data['deposit_total'] = $data['deposit_total']
  188. ->where('area_id',$area_id)
  189. ->sum('money');*/
  190. }
  191. //押金增长统计图数据
  192. $data['deposit_statistics'] = $this->depositStatistics($area_id);
  193. return $this->ok($data);
  194. }
  195. /**
  196. * depositTotal 总计押金
  197. *
  198. * @param Request $request
  199. * @return \Illuminate\Http\JsonResponse *@author Fx
  200. *
  201. */
  202. public function depositTotal(Request $request)
  203. {
  204. $area_id = $request->get('area_id') ?? "";
  205. // Log::info($area_id);
  206. if (empty($area_id)) return $this->error(['缺少参数']);
  207. $depositTotal = DepositOrder::query()
  208. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  209. ->where('is_refund', DepositOrder::REFUND_NO);
  210. if ($area_id == config('statistic.all')) {
  211. // 总和
  212. // 判断是否为超级管理员
  213. $admin_id = Admin::user()->id;
  214. if (!Admin::isAdministrator()) {
  215. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  216. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  217. $area_ids = array_merge($area_ids,$area_id);
  218. $depositTotal = $depositTotal
  219. ->whereIn('area_id', $area_ids);
  220. }
  221. $depositTotal = $depositTotal
  222. ->sum('money');
  223. } else {
  224. // 区域
  225. $depositTotal = $depositTotal
  226. ->where('area_id', $area_id)
  227. ->sum('money');
  228. }
  229. return $this->ok($depositTotal);
  230. }
  231. //
  232. /**
  233. * depositStatistics 取出统计数据
  234. *
  235. * @param $area_id
  236. * @return array *@author Fx
  237. *
  238. */
  239. private function depositStatistics($area_id)
  240. {
  241. $deposit_statistic_date_start = date('Y-m-d', strtotime("-14 day"));
  242. if ($area_id == config('statistic.all')) {
  243. // 判断是否为超级管理员
  244. $admin_id = Admin::user()->id;
  245. if (!Admin::isAdministrator()) {
  246. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  247. $deposit_statistics = Statistic::query()
  248. ->where('slug', Statistic::SLUG_DEPOSIT_STATIC)
  249. ->where('date', '>', $deposit_statistic_date_start)
  250. ->whereIn('area_id', $area_ids)
  251. ->select(['date', 'body'])
  252. ->orderByDesc('date')
  253. ->get()
  254. ->toArray();
  255. $arr1 = [];
  256. foreach ($deposit_statistics as $v) {
  257. // 非超级管理员 需要重新组装叠加总收益统计
  258. $profit_statistics_arr = object_array(json_decode($v['body']));
  259. if (array_key_exists($v['date'], $arr1)) {
  260. $arr1[$v['date']]['money_all'] += $profit_statistics_arr['money_all'] ?? 0;
  261. $arr1[$v['date']]['count_id'] += $profit_statistics_arr['count_id'] ?? 0;
  262. $arr1[$v['date']]['money_refund_all'] += $profit_statistics_arr['money_refund_all'] ?? 0;
  263. } else {
  264. $arr1[$v['date']]['money_all'] = $profit_statistics_arr['money_all'] ?? 0;
  265. $arr1[$v['date']]['count_id'] = $profit_statistics_arr['count_id'] ?? 0;
  266. $arr1[$v['date']]['money_refund_all'] = $profit_statistics_arr['money_refund_all'] ?? 0;
  267. }
  268. }
  269. $res = [];
  270. foreach ($arr1 as $k => $v) {
  271. $arr2 = [];
  272. $arr2 = $v;
  273. $arr2['date'] = $k;
  274. $res[] = $arr2;
  275. }
  276. // Log::info($res);
  277. return $res;
  278. }
  279. }
  280. // 其他情况 均直接按照区域id查询
  281. $deposit_statistics = Statistic::query()
  282. ->where('slug', Statistic::SLUG_DEPOSIT_STATIC)
  283. ->where('date', '>', $deposit_statistic_date_start)
  284. ->where('area_id', $area_id)
  285. ->select(['date', 'body'])
  286. ->orderByDesc('date')
  287. ->get()
  288. ->toArray();
  289. $res = [];
  290. foreach ($deposit_statistics as $v) {
  291. $deposit_statistics_arr = object_array(json_decode($v['body']));
  292. // Log::info($deposit_statistics_arr);
  293. $deposit_statistics_arr = $deposit_statistics_arr ?? [];
  294. $deposit_statistics_arr['date'] = $v['date'];
  295. $res[] = $deposit_statistics_arr;
  296. }
  297. return $res;
  298. }
  299. }