<?php

namespace app\controller;

use app\model\ServiceFee;

use think\facade\Db;

class utils
{
    public function ResponseJson($data, $code, $msg): \think\response\Json
    {
        return json(['Code' => $code, 'Msg' => $msg, 'Data' => $data]);
    }

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

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

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

        return $date;
    }

    public function Fee($station_number, $openid): array
    {

        $table = 'zxc_user';

        $ElectricityFee = Db::table('charge_station')->where('charge_station_number', $station_number)->value('ElectricityFee');
        $station_type = Db::table('charge_station')->where('charge_station_number', $station_number)->value('station_type');
        $type = Db::table($table)->where('openid', $openid)->value('type');
        if ($type == 3) {
            $group_id = Db::table($table)->where('openid', $openid)->value('group_id');
            $ServiceFee = ServiceFee::where('type', $type)->where('group_id', $group_id)->where('station_type', $station_type)->value('service_fee');
        } else {
            $ServiceFee = ServiceFee::where('type', $type)->where('station_type', $station_type)->value('service_fee');
        }
//        $ServiceFee= Db::table('charge_station')->where('charge_station_number',$station_number)->value('ServiceFee');
        $ElectricityFee = str_replace('电费:', '', $ElectricityFee);
        $Elect = explode(',', $ElectricityFee);
        for ($index = 0; $index < count($Elect); $index++) {
            $kk['time_interval'] = substr($Elect[$index], 0, 11);
            $kk['univalence'] = explode(':', $Elect[$index])[3];
            $ss[] = $kk;
        }
        $ServiceFee = str_replace('服务费:', '', $ServiceFee);
        $Server = explode(',', $ServiceFee);
        $wk_day = date('w');
        $now_time = time();
        $start_time = '2023-03-18 00:00:00';
        $start_time = strtotime($start_time);
        $wkday_ar = array('日', '一', '二', '三', '四', '五', '六');
        for ($index = 0; $index < count($Server); $index++) {
            $ll['time_interval'] = substr($Server[$index], 0, -5);

            // if ($wkday_ar[$wk_day] == '三' && $now_time > $start_time && $type == 1) {
            //     $ll['univalence'] = number_format((float)(substr($Server[$index], Strlen($Server[$index]) - 4) - 0.20), 2);
            // } else {
                $ll['univalence'] = substr($Server[$index], Strlen($Server[$index]) - 4);
            // }
            $mm[] = $ll;
        }
        return ['code' => 200, 'ElectricityFee' => $ss, 'ServiceFee' => $mm];
    }
}