<?php

namespace app\statistics\controller;

use app\admin\logic\statistic\TradeStatisticLogic;
use app\common\controller\BaseLikeController;
use app\common\model\user\User;
use app\common\model\user_recharge\UserRecharge;
use app\statistics\logic\OrderLogic;
use app\statistics\logic\ProductLogic;
use app\statistics\logic\UserLogic;
use DateTime;

class IndexController extends BaseLikeController
{

    public $store_id = 0;

    public function index()
    {
        $time = $this->request->get('date', date('Y-m-d'));
        $store_id = $this->store_id;
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $where['paid'] = 1;
        $res = OrderLogic::dayPayPrice($where, $time);
        if (OrderLogic::hasError()) {
            return $this->fail(OrderLogic::getError()); //获取错误信息并返回错误信息
        }
        return $this->success('ok', ['dayPayPrice' => $res, 'title' => '百合镇农(特)产品交易大数据']);
    }
    public function user()
    {
        $time = $this->request->get('date', date('Y-m-d'));
        $today = strtotime($time);
        $dates = [];
        // 循环输出前5天的日期
        for ($i = 0; $i <= 4; $i++) {
            // 计算前第$i天的日期时间戳
            $timestamp = $today - ($i * 86400); // 86400秒等于1天

            // 将时间戳格式化为日期
            $date = date('Y-m-d', $timestamp);
            $dates[] = $date;
        }
        $store_id = $this->store_id;
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $res = UserLogic::userCount($where, $dates);
        if (UserLogic::hasError()) {
            return $this->fail(UserLogic::getError()); //获取错误信息并返回错误信息
        }
        return $this->success('ok', $res);
    }

    /**
     * 中间商品统计
     */
    public function product_count()
    {
        $time = $this->request->get('date', date('Y-m-d'));
        $store_id = $this->store_id;
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $res = ProductLogic::Count($where, $time);
        if (ProductLogic::hasError()) {
            return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
        }
        return $this->success('ok', $res);
    }
    /**
     * 订单统计
     */
    public function order_user_num_count()
    {
        $time = $this->request->get('date', date('Y-m-d'));
        $store_id = $this->store_id;
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $where['recharge_type']='INDUSTRYMEMBERS';
        $where['status']=1;
        $where['paid']=1;
        // $res = OrderLogic::Count($where,$time);
        // if (ProductLogic::hasError()) {
        //     return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
        // }
        $res = UserRecharge::where($where)->whereTime('create_time', $time)->select()->each(function ($item) {
            if($item['uid']){
                $item['nickname']=User::where('id',$item['uid'])->value('nickname');
            }else{
                $item['nickname']='';
            }
        });
        return $this->success('ok', $res?->toArray());
    }
    /**
     * 商品销量排行榜统计
     */
    public function sales_ranking()
    {
        $time = $this->request->get('date');
        $store_id = $this->store_id;
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $res = OrderLogic::sales($where, $time);
        if (ProductLogic::hasError()) {
            return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
        }
        return $this->success('ok', $res);
    }
    /**
     * 成交用户数据
     */
    public function user_trade_count()
    {
        $logic = (new TradeStatisticLogic());
        $leftToday = $logic->getTopLeftTrade(['create_time' => 'today']);
        $leftyestoday = $logic->getTopLeftTrade(['create_time' => 'yestoday']);
        $totalleft = [$leftToday, $leftyestoday];
        foreach ($totalleft as $k => $v) {
            $left['name'] = "当日订单金额";
            $left['x'] = $v['curve']['x'];
            $left['series'][$k]['money'] = round($v['total_money'], 2);
            $left['series'][$k]['value'] = array_values($v['curve']['y']);
        }

        return $this->data($left);
    }
    /**
     * 当日订单金额
     */
    public function street_currday_order_count()
    {
        $time = $this->request->get('date', date('Y-m-d'));
        $store_id = $this->store_id;
        $where = [];
        if ($store_id) {
            $where['store_id'] = $store_id;
        }
        $res = OrderLogic::Currday($where, $time);
        if (ProductLogic::hasError()) {
            return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
        }
        return $this->success('ok', $res);
    }
}