<?php

namespace app\controller\Order;

use Overtrue\Pinyin\Pinyin;
use think\facade\Db;

class CountExcel
{
    public function AmountExcel($start_time,$end_time){
        $end_time = date("Y-m-d",strtotime("+1day",strtotime($end_time)));
        $date = $this->getDateFromRange($start_time,$end_time);
        $RechargeAmountTotal = 0;
        $RefundAmountTotal = 0;
        $RechargeNumTotal = 0;
        $RefundNumTotal = 0;
        $OrderAmountTotal = 0;
        $OrderNumTotal = 0;
        $ServiceAmountTotal = 0;
        $mess =array();
        foreach ($date as $d){
            $area = Db::table('area_three')->column('area');
            $RechargeAmount = 0;
            $RefundAmount = 0;
            $RechargeNum = 0;
            $RefundNum = 0;
            $OrderAmount = 0;
            $OrderNum = 0;
            $ServiceAmount = 0;
            $degree_pri = 0;
            foreach ($area as $a){
                $s = $this->GetArea($a);
                $table_refund = 'refund_'.$s;
                $table_order = 'charge_order_'.$s;
                $table_recharge = 'recharge_'.$s;
                $recharge = Db::table($table_recharge);
                $RechargeAmount += $recharge->whereDay('success_time',$d)->sum('total');
                $RechargeNum += $recharge->whereDay('success_time',$d)->count();
                $order = Db::table($table_order);
                $OrderAmount += $order->whereDay('charge_date',$d)->sum('TotalMoney');
                $OrderNum += $order->whereDay('charge_date',$d)->count();
                $refund = Db::table($table_refund);
                $RefundAmount += $refund->whereDay('create_time',$d)->sum('refund_total');
                $RefundNum += $refund->whereDay('create_time',$d)->count();
                if (strtotime($d)<strtotime('2023-02-03 00:00:00')){
                    $degree_pri += $order->whereDay('charge_date',$d)->sum('TotalPower');
                    $ServiceAmount += round($degree_pri*0.2,2);
                }else{
                    $ServiceAmount += $order->whereDay('charge_date',$d)->sum('SeviceMoney');
                }
            }
            $kk['RechargeAmount'] = round($RechargeAmount/100,2);
            $kk['RechargeNum'] = $RechargeNum;
            $kk['OrderAmount'] = round($OrderAmount,2);
            $kk['OrderNum'] = $OrderNum;
            $kk['RefundAmount'] = round($RefundAmount/100,2);
            $kk['RefundNum'] = $RefundNum;
            $kk['ServiceAmount'] = round($ServiceAmount,2);
            $kk['Time'] = $d;
            $mess['per'][] = $kk;
                //['time'=>$d,'data' => $kk];
            $RechargeAmountTotal += $RechargeAmount;
            $RefundAmountTotal += $RefundAmount;
            $RechargeNumTotal += $RechargeNum;
            $RefundNumTotal += $RefundNum;
            $OrderAmountTotal += $OrderAmount;
            $OrderNumTotal += $OrderNum;
            $ServiceAmountTotal += $ServiceAmount;
        }
        $ss['RechargeAmountTotal']=round($RechargeAmountTotal/100,2);
        $ss['RefundAmountTotal']=round($RefundAmountTotal/100,2);
        $ss['RechargeNumTotal']=$RechargeNumTotal;
        $ss['RefundNumTotal']=$RefundNumTotal;
        $ss['OrderAmountTotal']=round($OrderAmountTotal,2);
        $ss['OrderNumTotal']=$OrderNumTotal;
        $ss['ServiceAmountTotal'] = round($ServiceAmountTotal,2);
        $mess['total'] = [$ss];
        return json($mess);
    }

    private function GetArea($area){
        $pinyin = new Pinyin();
        $s = $pinyin->sentence($area);
        $s = strtr($s,array(' '=>''));
        return $s;
    }

    private function getDateFromRange($startdate, $enddate): array
    {

        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400;
        // 保存每天日期
        $date = array();
        for($i=0; $i<$days; $i++){

            $date[] = date('Y-m-d', $stimestamp+(86400*$i));
        }

        return $date;
    }
}