DepositController.php 13 KB

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