ProfitController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\WalletLogFilter;
  4. use App\Http\Resources\ProfitResource;
  5. use App\Models\AdminUser;
  6. use App\Models\AdminUserArea;
  7. use App\Models\Area;
  8. use App\Models\Statistic;
  9. use App\Models\WalletLog;
  10. use App\Utils\Admin;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. use Illuminate\Support\Facades\Log;
  15. /**
  16. * Class ProfitController
  17. * @package App\Http\Controllers\Admin
  18. */
  19. class ProfitController extends Controller
  20. {
  21. //
  22. /**
  23. * index 收益列表
  24. *
  25. * @param Request $request
  26. * @param WalletLogFilter $filter
  27. * @return \Illuminate\Http\JsonResponse
  28. * @author Fx
  29. */
  30. public function index(Request $request, WalletLogFilter $filter)
  31. {
  32. //收益明细
  33. // Log::info(22);
  34. $admin_id = Admin::user()->id;
  35. $profitOrder = WalletLog::query()
  36. ->filter($filter)
  37. ->whereIn('type', WalletLog::$subType)
  38. ->orderByDesc('id');
  39. if (!Admin::isAdministrator()) {
  40. //判断哪些区域是此管理员创建得
  41. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  42. if (count($area_ids) !== 0) {
  43. $profitOrder = $profitOrder->whereIn('area_id', $area_ids);
  44. } else {
  45. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  46. $area_id = $area_id->area_id ?? 0;
  47. $profitOrder = $profitOrder->where('area_id', $area_id);
  48. }
  49. }
  50. $profitOrder = $request->get('all') ? $profitOrder->get() : $profitOrder->paginate();
  51. return $this->ok(ProfitResource::collection($profitOrder));
  52. }
  53. /**
  54. * profitAnalysis 收益分析
  55. *
  56. * @param Request $request
  57. * @return \Illuminate\Http\JsonResponse
  58. * @author Fx
  59. *
  60. */
  61. public function profitAnalysis(Request $request)
  62. {
  63. $area_id = $request->get('area_id') ?? "";
  64. // Log::info($area_id);
  65. if (empty($area_id)) return $this->error(['缺少参数']);
  66. $today = Carbon::today();
  67. $first_day = date('Y-m-01', strtotime($today)); //每月一号
  68. $deposit_statistic_start = date('Y-m-d', strtotime("-13 day"));
  69. //当日新增
  70. $data['new_add_profit'] = WalletLog::query()
  71. ->where('created_at', '>', $today)
  72. ->whereIn('type', WalletLog::$subType);
  73. //当月总收益
  74. $data['this_month_total_profit'] = WalletLog::query()
  75. ->where('created_at', '>', $first_day)
  76. ->whereIn('type', WalletLog::$subType);
  77. if ($area_id == config('statistic.all')) {
  78. // 总和
  79. // 判断是否为超级管理员
  80. $admin_id = Admin::user()->id;
  81. if (!Admin::isAdministrator()) {
  82. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  83. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  84. $area_ids = array_merge($area_ids,$area_id);
  85. $data['new_add_profit'] = $data['new_add_profit']->whereIn('area_id', $area_ids);
  86. $data['this_month_total_profit'] = $data['this_month_total_profit']->whereIn('area_id', $area_ids);
  87. }
  88. $data['new_add_profit'] = $data['new_add_profit']->sum('money');
  89. $data['this_month_total_profit'] = $data['this_month_total_profit']->sum('money');
  90. $data['new_add_profit'] = abs($data['new_add_profit']);
  91. $data['this_month_total_profit'] = abs($data['this_month_total_profit']);
  92. } else {
  93. // 区域
  94. $data['new_add_profit'] = $data['new_add_profit']->where('area_id', $area_id)->sum('money');
  95. $data['this_month_total_profit'] = $data['this_month_total_profit']->where('area_id', $area_id)->sum('money');
  96. $data['new_add_profit'] = abs($data['new_add_profit']);
  97. $data['this_month_total_profit'] = abs($data['this_month_total_profit']);
  98. }
  99. // 统计图
  100. $data['profit_statistics'] = $this->profitStatistics($area_id);
  101. // Log::info($data['profit_statistics']);
  102. //
  103. return $this->ok($data);
  104. }
  105. /**
  106. * profitTotal 总收益
  107. *
  108. * @param Request $request
  109. * @return \Illuminate\Http\JsonResponse
  110. * @author Fx
  111. *
  112. */
  113. public function profitTotal(Request $request)
  114. {
  115. $area_id = $request->get('area_id') ?? "";
  116. // Log::info($area_id);
  117. if (empty($area_id)) return $this->error(['缺少参数']);
  118. $totalProfit = WalletLog::query()
  119. ->whereIn('type', WalletLog::$subType);
  120. if ($area_id == config('statistic.all')) {
  121. // 总和
  122. // 判断是否为超级管理员
  123. $admin_id = Admin::user()->id;
  124. if (!Admin::isAdministrator()) {
  125. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  126. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  127. $area_ids = array_merge($area_ids,$area_id);
  128. $totalProfit = $totalProfit->whereIn('area_id', $area_ids);
  129. }
  130. $totalProfit = $totalProfit
  131. ->sum('money');
  132. } else {
  133. // 区域
  134. $totalProfit = $totalProfit
  135. ->where('area_id', $area_id)
  136. ->sum('money');
  137. }
  138. return $this->ok(abs($totalProfit));
  139. }
  140. /**
  141. * profitStatistics 日收益统计信息
  142. *
  143. * @param $area_id
  144. * @return array
  145. * @author Fx
  146. *
  147. */
  148. private function profitStatistics($area_id)
  149. {
  150. $profit_statistic_date_start = date('Y-m-d', strtotime("-14 day"));
  151. if ($area_id == config('statistic.all')) {
  152. // 判断是否为超级管理员
  153. $admin_id = Admin::user()->id;
  154. if (!Admin::isAdministrator()) {
  155. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  156. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
  157. $area_ids = array_merge($area_ids,$area_id);
  158. $profit_statistics = Statistic::query()
  159. ->where('slug', Statistic::SLUG_PROFIT_STATIC)
  160. ->where('date', '>', $profit_statistic_date_start)
  161. // ->where('area_id',$area_id)
  162. ->whereIn('area_id', $area_ids)
  163. ->select(['date', 'body'])
  164. ->orderByDesc('date')
  165. ->get()
  166. ->toArray();
  167. // Log::info($profit_statistics);
  168. $arr1 = [];
  169. foreach ($profit_statistics as $v) {
  170. // 非超级管理员 需要重新组装叠加总收益统计
  171. $profit_statistics_arr = object_array(json_decode($v['body']));
  172. if (array_key_exists($v['date'], $arr1)) {
  173. $arr1[$v['date']] += $profit_statistics_arr['profit'] ?? 0;
  174. } else {
  175. $arr1[$v['date']] = $profit_statistics_arr['profit'] ?? 0;
  176. }
  177. }
  178. $res = [];
  179. foreach ($arr1 as $k => $v) {
  180. $arr2 = [];
  181. $arr2['profit'] = $v;
  182. $arr2['date'] = $k;
  183. $res[] = $arr2;
  184. }
  185. return $res;
  186. }
  187. }
  188. // 其他均走下面 按照区域id查询
  189. $profit_statistics = Statistic::query()
  190. ->where('slug', Statistic::SLUG_PROFIT_STATIC)
  191. ->where('date', '>', $profit_statistic_date_start)
  192. ->where('area_id', $area_id)
  193. ->select(['date', 'body'])
  194. ->orderByDesc('date')
  195. ->get()
  196. ->toArray();
  197. $res = [];
  198. // dd($profit_statistics);
  199. foreach ($profit_statistics as $v) {
  200. $profit_statistics_arr = object_array(json_decode($v['body']));
  201. $profit_arr_statistics = [];
  202. $profit_arr_statistics['profit'] = $profit_statistics_arr['profit'] ?? 0;
  203. $profit_arr_statistics['date'] = $v['date'];
  204. $res[] = $profit_arr_statistics;
  205. }
  206. return $res;
  207. }
  208. }