<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\admin\logic;

use app\admin\logic\statistic\TradeStatisticLogic;
use app\common\logic\BaseLogic;
use app\common\model\order\Cart;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_visit\StoreVisit;
use app\common\model\user\User;
use app\common\model\user\UserVisit;
use app\common\service\ConfigService;
use app\common\service\FileService;


/**
 * 工作台
 * Class WorkbenchLogic
 * @package app\admin\logic
 */
class WorkbenchLogic extends BaseLogic
{
    /**
     * @notes 工作套
     * @param $adminInfo
     * @return array
     * @date 2021/12/29 15:58
     */
    public static function index()
    {       
        $logic=(new TradeStatisticLogic());
        $storeOrder=(new StoreOrder());
        $user=(new User());
        $userVisit=(new UserVisit());
        $today = $logic->tradeTotalMoney(['create_time' => 'today'], 'sum');
        $month = $logic->tradeTotalMoney(['create_time'=>'month'], 'sum');
        $todayOrder =  $storeOrder->where(['paid'=>1])->whereDay('create_time')->count();
        $MonthOrder =  $storeOrder->where(['paid'=>1])->whereMonth('create_time')->count();
        $todayUser =  $user->whereDay('create_time')->count();
        $MonthUser =  $user->whereMonth('create_time')->count();
        $todayUserVisit =  $userVisit->whereDay('create_time')->group('uid')->count();
        $MonthUserVisit =  $userVisit->whereMonth('create_time')->group('uid')->count();
        return [
            'today' => $today, //今日销售额
            'month' => $month, //本月销售额
            'todayOrder' => $todayOrder, //今日订单量
            'MonthOrder' => $MonthOrder, //本月订单量
            'todayUser' => $todayUser, //今日新增用户量
            'MonthUser' => $MonthUser, //本月新增用户量
            'todayUserVisit' => $todayUserVisit, //今日活跃用户量
            'MonthUserVisit' => $MonthUserVisit, //本月活跃用户量
        ];
    }

    //-------------------------------商品统计---------------------------------------//

    /**
     * 商品概况
     */
    public static function get_basic($where)
    {
        $browse = StoreVisit::where($where)->count();
        $user = 0;
        $cart = Cart::where($where)->where('is_fail', 0)->sum('cart_num');
        $order = StoreOrder::where($where)->count();
        $pay = StoreOrder::where($where)->where('paid', 1)->where('refund_status', 0)->count();
        $payPrice = StoreOrder::where($where)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
        $cost = StoreOrder::where($where)->where('paid', 1)->sum('cost');
        $refundPrice = StoreOrder::where($where)->where('status', 'in', [-1, -2])->sum('refund_price');
        $refund = StoreOrder::where($where)->where('status', 'in', [-1, -2])->count();
        $payPercent = 0;
        return [
            'browse' => ['num' => $browse, 'title' => '浏览量'], //浏览量
            'user' => ['num' => $user, 'title' => '访客数'], //访客数
            'cart' => ['num' => $cart, 'title' => '加购人数'], //加购人数
            'order' => ['num' => $order, 'title' => '订单量'], //订单量
            'pay' => ['num' => $pay, 'title' => '支付订单量'], //支付订单量
            'payPrice' => ['num' => $payPrice, 'title' => '支付金额'], //支付金额
            'cost' => ['num' => $cost, 'title' => '成本'], //成本
            'refundPrice' => ['num' => $refundPrice, 'title' => '退款金额'], //退款金额
            'refund' => ['num' => $refund, 'title' => '退款订单量'], //退款订单量
            'payPercent' => ['num' => $payPercent, 'title' => '支付转化率'], //支付转化率
        ];
    }

    /**
     * 商品浏览量
     */
    public static function store_visit_count($dates)
    {
        $data = [];
        foreach ($dates as $date) {
            $data[] = StoreVisit::whereDay('create_time', $date)->cache('store_visit_count_' . $date, 300)->sum('count');
        }
        return $data;
    }

    /**
     * 商品浏览量
     */
    public static function store_visit_user($dates)
    {
        $data = [];
        foreach ($dates as $date) {
            $data[] = StoreVisit::whereDay('create_time', $date)->cache('store_visit_user_' . $date, 300)->count('uid');
        }
        return $data;
    }
    /**
     * 支付金额
     */
    public static function payPrice($dates)
    {
        $data = [];
        foreach ($dates as $date) {
            $data[] = StoreOrder::whereDay('create_time', $date)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
        }
        return $data;
    }
    /**
     * 退款金额
     */
    public static function refundPrice($dates)
    {
        $data = [];
        foreach ($dates as $date) {
            $data[] = StoreOrder::whereDay('create_time', $date)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price');
        }
        return $data;
    }

    /**
     * 当日订单金额
     */
    public static function day_order_pay_price($where, $time)
    {
        $money = StoreOrder::whereDay('create_time', $time)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
        $data = [];
        foreach ($where as $date) {
            $data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_pay_price_' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
        }

        return ['money' => $money, 'value' => $data];
    }

    /**
     * 今日订单数
     */
    public static function day_order_count($where,$name)
    {
        $now_money = StoreOrder::whereDay('create_time')->where('paid', 1)->where('refund_status', 0)->count();
        $last_money = StoreOrder::whereDay('create_time', 'yesterday')->where('paid', 1)->where('refund_status', 0)->count();
        $data = [];
        foreach ($where as $date) {
            $data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_count' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->count();
        }
        if ($now_money > $last_money) {
            if($last_money == 0){
                $rate = bcmul($now_money, '100');
            }else{
                $rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
            }
        } else {

            $rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
        }
        return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
    }

    /**
     * 今日支付人数
     */
    public static function day_order_user($where,$name)
    {
        $now_money = StoreOrder::whereDay('create_time')->where('paid', 1)->where('refund_status', 0)->count('uid');
        $last_money = StoreOrder::whereDay('create_time', 'yesterday')->where('paid', 1)->where('refund_status', 0)->count('uid');
        $data = [];
        foreach ($where as $date) {
            $data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_count' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->count('uid');
        }
        if ($now_money > $last_money) {
            if($last_money == 0){
                $rate = bcmul($now_money, '100');
            }else{
                $rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
            }
        } else {

            $rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
        }
        return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
    }
        /**
     * 本月订单数
     */
    public static function month_order_count($name)
    {
        $now_money = StoreOrder::whereMonth('create_time')->where('paid', 1)->where('refund_status', 0)->count();
        $last_money = StoreOrder::whereMonth('create_time', 'last month')->where('paid', 1)->where('refund_status', 0)->count();
        $data = [];
        if ($now_money > $last_money) {
            if($last_money == 0){
                $rate = bcmul($now_money, '100');
            }else{
                $rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
            }
        } else {
            $rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
        }
        return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
    }

        /**
     * 本月支付人数
     */
    public static function month_order_user($name)
    {
        $now_money = StoreOrder::whereMonth('create_time')->where('paid', 1)->where('refund_status', 0)->count('uid');
        $last_money = StoreOrder::whereMonth('create_time','last month')->where('paid', 1)->where('refund_status', 0)->count('uid');
        $data = [];
        if ($now_money > $last_money) {
            if($last_money == 0){
                $rate = bcmul($now_money, '100');
            }else{
                $rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
            }
        } else {

            $rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
        }
        return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
    }
}