<?php

namespace app\admin\service\statistics;

use app\model\Order;
use app\model\OrderReturn;
use app\model\PayDetail;
use support\Db;

class OrderService
{
    public static function getOrder()
    {
        // 今日订单
        $todayTimeUnix = strtotime(date('Y-m-d'));
        // 今日订单
        $data['todayOrder'] = Order::where('order_addtimes', '>', $todayTimeUnix)->where('order_category', '<>', 'RECHARGE')->count();
        // 今日营业额
        $data['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
            ->whereIn('pay_category', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'MEALS'])
            ->where('join_pay_order_id', '<>', '')
            ->where('pay_status', 'SUCCESS')
            ->where('pay_category' ,'<>', 'REFUND')
            ->sum('pay_amount');
        $refundAmount = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
            ->where('pay_status', 'SUCCESS')
            ->whereIn('pay_category', ['REFUND', 'ROLE_REFUND'])
            ->sum('pay_amount');
        $data['todayRevenue'] = round($data['todayRevenue'], 2);
        // 今日充值额
        $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
            ->where('order_addtimes', '>', $todayTimeUnix)
            ->whereIn('order_category', ['RECHARGE', 'COMBINE', 'PARTNER', 'REFERRER', 'VIP'])
            ->where('order_status_payment', 'SUCCESS')
            ->sum('order_amount_pay');
        // 今日退单
        $data['todayRefund'] = $refundAmount;

        // 今日销售额
        $data['todaySales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
            ->whereIn('pay_prepayid', ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
            ->where('join_pay_order_id', '<>', '')
            ->where('pay_status', 'SUCCESS')
            ->where('pay_category' ,'<>', 'REFUND')
            ->sum('pay_amount');

        $data['todaySales'] = round($data['todaySales'], 2);
        // 今日余额收入
        $data['todayCashSales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
            ->whereNotIn('pay_prepayid',['WXPAY','ALIPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
            ->where('join_pay_order_id', '<>', '')
            ->where('pay_category','<>','REFUND')
            ->where('pay_category','<>','ROLE_REFUND')
            ->where('pay_status', 'SUCCESS')
            ->sum('pay_amount');

        $data['todayCashSales'] = round($data['todayCashSales'], 2);

        // 柱状图
        // 产品订单
        $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
//        $newAddition = Db::select("
//            SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
//                left join app_goods as g on s.join_sheet_goods_id = g.goods_id
//                    where g.goods_classify = 'GOODS'
//                group by month
//        ");
        $category = [];
        $newAdditionData = [];
        foreach ($newAddition as $item) {
            if (!in_array($item->month, $category)) {
                $category[] = $item->month;
            }
//            $newAdditionData['category'][] = $item->month;
//            $newAdditionData['data'][] = $item->num;
        }

        // 服务订单
        $newAdditionService = Db::select("
            SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s 
                left join app_goods as g on s.join_sheet_goods_id = g.goods_id
                    where g.goods_classify = 'SERVICE'
                group by month
        ");
        $newAdditionServiceData = [];
        foreach ($newAdditionService as $item) {
            if (!in_array($item->month, $category)) {
                $category[] = $item->month;
            }
//            $newAdditionServiceData['category'][] = $item->month;
//            $newAdditionServiceData['data'][] = $item->num;
        }

        sort($category);

        foreach ($category as $item2) {
            $isHaveService = false;
            $isHaveGoods = false;
            foreach ($newAdditionService as $item) {
                if ($item2 == $item->month) {
                    $isHaveService = true;
                    $newAdditionServiceData['category'][] = $item->month;
                    $newAdditionServiceData['data'][] = $item->num;
                }
            }
            foreach ($newAddition as $item3) {
                if ($item2 == $item3->month) {
                    $isHaveGoods = true;
                    $newAdditionData['category'][] = $item3->month;
                    $newAdditionData['data'][] = $item3->num;
                }
            }
            if (!$isHaveService) {
                $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
                $newAdditionServiceData['data'][] = 0;
            }

            if (!$isHaveGoods) {
                $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
                $newAdditionData['data'][] = 0;
            }

        }

        $data['addition']['goods'] = $newAdditionData;
        $data['addition']['service'] = $newAdditionServiceData;

        return $data;
    }
}