| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 | <?phpnamespace App\Modules\Order\Services;use App\Modules\Order\Models\ChannelOrderStatistic;use App\Modules\Order\Models\ChannelOrderStatisticDate;use App\Modules\Order\Models\ChannelSendOrderStatistic;use App\Modules\Order\Models\ChannelSendOrderStatisticDate;use App\Modules\Order\Models\ReportUserBindRecord;use App\Modules\Order\Models\ReportUserChargeRecord;use App\Modules\Trade\Models\Order;use App\Modules\User\Models\User;use DB;use Illuminate\Support\Collection;class OrderService{    /**     * 增加派单统计金额     */    public function increaseSendOrderAmount(string $date, int $channel_id, int $send_order_id, float $amount, string $pay_time)    {        $date_statistic = ChannelSendOrderStatisticDate::where([            'date'          => $date,            'channel_id'    => $channel_id,            'send_order_id' => $send_order_id,        ])->first();        if ($date_statistic) {            $date_statistic->amount += $amount;            $date_statistic->save();        } else {            ChannelSendOrderStatisticDate::create([                'date'          => $date,                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,                'amount'        => $amount,            ]);        }        $statistic = ChannelSendOrderStatistic::where([            'channel_id'    => $channel_id,            'send_order_id' => $send_order_id,        ])->first();        if ($statistic) {            $statistic->amount += $amount;            $statistic->save();        } else {            ChannelSendOrderStatistic::create([                'channel_id'     => $channel_id,                'send_order_id'  => $send_order_id,                'first_pay_time' => $pay_time,                'amount'         => $amount,            ]);        }    }    /**     * 增加派单用户统计金额     */    public function increaseUserSendOrderAmount(string $date, int $channel_id, int $send_order_id, float $amount, string $pay_time)    {        $date_statistic = ChannelSendOrderStatisticDate::where([            'date'          => $date,            'channel_id'    => $channel_id,            'send_order_id' => $send_order_id,        ])->first();        if ($date_statistic) {            $date_statistic->user_amount += $amount;            $date_statistic->save();        } else {            ChannelSendOrderStatisticDate::create([                'date'          => $date,                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,                'user_amount'   => $amount,            ]);        }        $statistic = ChannelSendOrderStatistic::where([            'channel_id'    => $channel_id,            'send_order_id' => $send_order_id,        ])->first();        if ($statistic) {            $statistic->user_amount += $amount;            $statistic->save();        } else {            ChannelSendOrderStatistic::create([                'channel_id'     => $channel_id,                'send_order_id'  => $send_order_id,                'first_pay_time' => $pay_time,                'user_amount'    => $amount,            ]);        }    }    /**     * 增加站点统计金额     */    public function increaseChannelAmount(string $date, int $channel_id, float $amount)    {        $date_statistic = ChannelOrderStatisticDate::where([            'date'       => $date,            'channel_id' => $channel_id,        ])->first();        if ($date_statistic) {            $date_statistic->amount += $amount;            $date_statistic->save();        } else {            ChannelOrderStatisticDate::create([                'date'       => $date,                'channel_id' => $channel_id,                'amount'     => $amount,            ]);        }        $statistic = ChannelOrderStatistic::where('channel_id', $channel_id)->first();        if ($statistic) {            $statistic->amount += $amount;            $statistic->save();        } else {            ChannelOrderStatistic::create([                'channel_id' => $channel_id,                'amount'     => $amount,            ]);        }    }    /**     * 查找当日订单     */    private function findOrders(string $date)    {        return Order::where([            ['status', '=', 'PAID'],            ['created_at', '>=', $date],            ['created_at', '<=', $date . ' 23:59:59'],        ])            ->select('send_order_id', 'distribution_channel_id', 'created_at', 'uid', 'price')            ->get();    }    /**     * @param string $startTime     * @param string $endTime     * @return array     */    public function getSendOrdersByDate(string $startTime, string $endTime): array    {        $result = Order::where([            ['created_at', '>=', $startTime],            ['created_at', '<=', $endTime],            ['send_order_id', '>', 0],            //            ['status', '=', 'PAID'],        ])->get();        return $result ? $result->toArray() : [];    }    /**     * 查找用户     */    private function findUsers(array $uids)    {        return User::whereIn('id', $uids)->select('id', 'send_order_id')->get();    }    /**     * 重算站点充值金额     */    private function reCalcChannelOrderAmount(string $date, int $channel_id, float $amount)    {        ChannelOrderStatisticDate::updateOrCreate([            'date'       => $date,            'channel_id' => $channel_id,        ], [            'amount' => $amount,        ]);        $amount = ChannelOrderStatisticDate::where('channel_id', $channel_id)->sum('amount');        ChannelOrderStatistic::updateOrCreate([            'channel_id' => $channel_id,        ], [            'amount' => $amount,        ]);    }    /**     * 重算派单用户充值金额     */    private function reCalcUserSendOrderAmount(string $date, int $channel_id, Collection $orders)    {        $uids             = $orders->pluck('uid')->all();        $users            = $this->findUsers($uids);        $send_order_users = $users->groupBy('send_order_id')->toArray();        foreach ($send_order_users as $send_order_id => $item) {            $uids   = collect($item)->pluck('id')->all();            $amount = $orders->whereIn('uid', $uids)->sum('price');            ChannelSendOrderStatisticDate::updateOrCreate([                'date'          => $date,                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,            ], [                'user_amount' => $amount,            ]);            $amount = ChannelSendOrderStatisticDate::where('send_order_id', $send_order_id)->sum('user_amount');            ChannelSendOrderStatistic::updateOrCreate([                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,            ], [                'user_amount' => $amount,            ]);        }    }    /**     * 重算派单充值金额     */    private function reCalcSendOrderAmount(string $date, int $channel_id, Collection $orders)    {        $send_order_users = $orders->groupBy('send_order_id')->toArray();        foreach ($send_order_users as $send_order_id => $item) {            $amount = collect($item)->sum('price');            ChannelSendOrderStatisticDate::updateOrCreate([                'date'          => $date,                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,            ], [                'amount' => $amount,            ]);            $amount         = ChannelSendOrderStatisticDate::where('send_order_id', $send_order_id)->sum('amount');            $first_pay_time = Order::where(['send_order_id' => $send_order_id, 'status' => 'PAID'])->min('created_at');            ChannelSendOrderStatistic::updateOrCreate([                'channel_id'    => $channel_id,                'send_order_id' => $send_order_id,            ], [                'amount'         => $amount,                'first_pay_time' => $first_pay_time,            ]);        }    }    public function reCalcOrderStatisticAmount(string $date)    {        $orders         = $this->findOrders($date);        $channel_orders = $orders->groupBy('distribution_channel_id')->toArray();        foreach ($channel_orders as $channel_id => $channel_order) {            $colllection = collect($channel_order);            $amount      = $colllection->sum('price');            $this->reCalcChannelOrderAmount($date, $channel_id, $amount);            $this->reCalcSendOrderAmount($date, $channel_id, $colllection);            $this->reCalcUserSendOrderAmount($date, $channel_id, $colllection);        }    }    #region 派单按用户注册统计    /**     * 获取时间段内的派单号     * @param $start     * @param $end     * @return array     */    public function getSendOrderIdByTime($start, $end): array    {        $order  = new Order();        $result = $order->getSendOrderIdByTime($start, $end);        return $result;    }    /**     * 获取时间段内派单首充数,和首充金额     * @param $start     * @param $end     * @return array     */    public function getSendOrderFirstPayCountAndPriceByTime($start, $end): array    {        $order  = new Order();        $result = $order->getSendOrderFirstPayCountAndPriceByTime($start, $end);        return $result;    }    /**     * 根据派单ID获取截止时间点前派单首充数,和首充金额     * @param $send_order_id     * @param $end     * @return array     */    public function getSendOrderFirstPayCountAndPriceByID($send_order_id, $end): array    {        $order  = new Order();        $result = $order->getSendOrderFirstPayCountAndPriceByID($send_order_id, $end);        return $result;    }    /**     * 获取时间段内派单充值数和总金额     * @param $start     * @param $end     * @return array     */    public function getSendOrderSuccessPayCountByTime($start, $end): array    {        $order  = new Order();        $result = $order->getSendOrderSuccessPayCountByTime($start, $end);        return $result;    }    /**     * 根据派单ID获取时间段内派单充值数和总金额     * @param $send_order_id     * @param $end     * @return array     */    public function getSendOrderSuccessPayCountByID($send_order_id, $end): array    {        $order  = new Order();        $result = $order->getSendOrderSuccessPayCountByID($send_order_id, $end);        return $result;    }    /**     * 获取时间段内派单付费人数     * @param $start     * @param $end     * @return array     */    public function getSendOrderSuccessPayUserCountByTime($start, $end): array    {        $order  = new Order();        $result = $order->getSendOrderSuccessPayUserCountByTime($start, $end);        return $result;    }    /**     * 获取时间段内派单N小时充值金额     * @param $start     * @param $end     * @param $hour     * @return array     */    public function getSendOrderPayPriceByHour($start, $end, $hour): array    {        $order  = new Order();        $result = $order->getSendOrderPayPriceByHour($start, $end, $hour);        return $result;    }    /**     * 获取时间段内派单N小时充值金额     * @param $send_order_id     * @param $end     * @param $hour     * @return array     */    public function getSendOrderPayPriceByIdAndHour($send_order_id, $end, $hour): array    {        $order  = new Order();        $result = $order->getSendOrderPayPriceByIdAndHour($send_order_id, $end, $hour);        return $result;    }    /**     * 获取时间段内派单N小时(首充或非首充33)用户数     * @param $start     * @param $end     * @param $hour     * @param int $first_pay_type 首充类型 -1全部 0非首充  1首充     * @return array     */    public function getSendOrderPayUserCountByHour($start, $end, $hour, $first_pay_type = -1): array    {        $order  = new Order();        $result = $order->getSendOrderPayUserCountByHour($start, $end, $hour, $first_pay_type);        return $result;    }    /**     * 获取时间段内派单N小时(首充或非首充33)用户数     * @param $send_order_id     * @param $end     * @param $hour     * @param int $first_pay_type 首充类型 -1全部 0非首充  1首充     * @return array     */    public function getSendOrderPayUserCountByIdAndHour($send_order_id, $end, $hour, $first_pay_type = -1): array    {        $order  = new Order();        $result = $order->getSendOrderPayUserCountByIdAndHour($send_order_id, $end, $hour, $first_pay_type);        return $result;    }    #endregion    public function saveReportUserChargeRecord(array $data)    {        ReportUserChargeRecord::updateOrCreate([            'order_no' => $data['order_no'],            'uid' => $data['uid'],        ], $data);    }    public function saveReportUserRecord(array $data)    {        ReportUserBindRecord::updateOrCreate([            'uid' => $data['uid'],        ], $data);    }}
 |