AgentStatisticService.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2023/11/23 20:57
  7. */
  8. namespace App\Http\Services\Admin\Task;
  9. use App\Http\Services\BaseService;
  10. use App\Models\Menu;
  11. use App\Models\Order;
  12. use App\Models\StatisticIncome;
  13. use Illuminate\Support\Facades\DB;
  14. class AgentStatisticService extends BaseService
  15. {
  16. public function daily($data)
  17. {
  18. try {
  19. if (empty($data['date']))
  20. $yesterday = date('Y-m-d', strtotime("-1 day")); // 获取昨天日期
  21. else
  22. $yesterday = $data['date'];
  23. $startTime = strtotime($yesterday . ' 00:00:00'); // 设置开始时间为昨天0点
  24. $endTime = strtotime($yesterday . ' 23:59:59'); // 设置结束时间为昨天24点
  25. $order = DB::table('js_order as order')
  26. ->whereBetween('create_time', [$startTime, $endTime])
  27. ->whereIn('order.status', ['3', '4'])
  28. ->select(['province', 'city', 'district'])
  29. // ->selectRaw("DATE_FORMAT(MAX(create_time), '%Y-%m-%d') as time")
  30. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(MAX(create_time)), '%Y-%m-%d') as time")
  31. ->selectRaw("COUNT(*) as order_count, SUM(pay_price) as pay_price")
  32. ->selectRaw("SUM(xh_user_money_log.money) as artificer_income")
  33. ->selectRaw("SUM(pay_price - IFNULL(xh_user_money_log.money, 0) - IFNULL(xh_promotion_log.money, 0)) as total_income")
  34. ->selectRaw("SUM(pay_price - IFNULL(xh_user_money_log.money, 0) - IFNULL(xh_promotion_log.money, 0)) * 0.4 as platform_income")
  35. ->selectRaw("SUM(pay_price - IFNULL(xh_user_money_log.money, 0) - IFNULL(xh_promotion_log.money, 0)) * 0.4 as agent_income")
  36. ->selectRaw("SUM(pay_price - IFNULL(xh_user_money_log.money, 0) - IFNULL(xh_promotion_log.money, 0)) * 0.2 as market_income")
  37. ->selectRaw("SUM(IFNULL(xh_promotion_log.money, 0)) as promotion_income")
  38. ->leftJoin('user_money_log as user_money_log', function ($join) {
  39. // $join->on('user_money_log.order_sn', '=', 'order.order_sn')->where('user_money_log.type', 8);
  40. $join->on('user_money_log.obj_id', '=', 'order.id')->where('user_money_log.type', 8);
  41. })
  42. ->leftJoin('user_money_log as promotion_log', function ($join) {
  43. $join->on('promotion_log.obj_id', '=', 'order.id')
  44. ->where('promotion_log.is_type', 1)
  45. ->whereIn('promotion_log.type', [3, 4]);
  46. })
  47. ->groupBy(['province', 'city', 'district'])
  48. ->get()->map(function ($value) {
  49. return (array)$value;
  50. })->toArray();
  51. StatisticIncome::query()->insert($order);
  52. return $yesterday . '-统计完成';
  53. } catch (\Exception $e) {
  54. return $yesterday . '-统计失败';
  55. }
  56. }
  57. }