<?php

namespace app\command;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-08 16:19
// +----------------------------------------------------------------------
use app\model\api\Order;
use app\model\system\OrderInfo;
use app\model\system\Product;
use app\model\system\Site;
use library\utils\Qiniu;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

/**
 * 生成业绩报表
 * @author huangjianfeng
 */
class TotalXh extends Command
{
    protected function configure()
    {
        $this
            ->setName('TotalXh')
            ->setDescription('每日统计分站业绩报表！')
            ->addArgument('action', null, "");
    }


    protected function execute(Input $input, Output $output)
    {
        $time = strtotime(date('Y-m-d', strtotime('-1 day')));
        $this->expData($time);
        /*
        for ($i = 1; $i < 5; $i++) {
            $time = strtotime('2021-03-' . $i);
            $this->expData($time);
        } */

    }


    /**
     * 统计数据
     * @param $time
     */
    private function expData($time)
    {
        $endTime = $time + 3600 * 24;
        //历史消耗客户数
        $count1             = Db::name("order")
            ->where('status', '>', 0)
            ->where('pay_time','between',[strtotime('2021-02-22'),$time])
            ->group("uid")
            ->count();
        $save['khs_total'] = $count1;
        //当日消耗客户数
        $count2            = Db::name("order")
            ->where('status', '>', 0)
            ->where('pay_time','between',[$time,$endTime])
            ->group("uid")
            ->count();
        $save['dr_total'] = $count2;
        //流失率 历史消耗客户数-当日消耗客户数）/历史消耗客户数
        $ls_lv         = $save['khs_total'] == 0 ? 0 : (($save['khs_total'] - $save['dr_total']) / $save['khs_total']);
        $save['ls_lv'] = $ls_lv;
        //今日消费金额
        $save['dr_money'] = (new Order)
            ->where('status', '>', 0)
            ->where('pay_time','between',[$time,$endTime])
            ->sum('all_price');
        $save['dr_money'] = empty($save['dr_money']) ? 0 : $save['dr_money'];
        //历史最大日消耗
        $max_khs           = Db::name("allTotal")
            ->order('dr_money', 'desc')
            ->value('dr_money');
        $save['khs_money'] = empty($max_khs) ? 0 : $max_khs;

        //今日总子订单数
        $save['dr_order'] = OrderInfo::alias("i")
            ->leftjoin('order o','o.id=i.o_id')
            ->where('i.status', '>', 0)
            ->where('o.pay_time','between',[$time,$endTime])
            ->count();

        //日均客消耗
        $save['dr_avg'] = $save['dr_money'] / $save['dr_total'];

        if (Db::name("allTotal")->where('time', $time)->count() > 0) {
            Db::name("allTotal")->where('time', $time)->update($save);
        } else {
            $save['time'] = $time;
            Db::name("allTotal")->insert($save);
        }

        echo "时间:" . date('Y-m-d', $time) . " 历史消耗客户数:" . $save['khs_total'] .
            ", 当日消耗客户数:" . $save['dr_total'] .
            ", 流失率:" . $save['ls_lv'] .
            ", 历史最大日消耗:" . $max_khs .
            ", 今日消费:" . $save['dr_money'] .
            ", 今日总子订单数:" . $save['dr_order'] .
            ",日均客消耗:" . $save['dr_avg'] . PHP_EOL;

    }


}