UsedDayCountExportController.class.php 3.88 KB
<?php
/**
 * 企业每日资费使用情况导出
 */

namespace Apicp\Controller\HomePage;

use Com\PythonExcel;
use VcySDK\Enterprise;
use VcySDK\Service;

class UsedDayCountExportController extends AbstractController
{
    /**
     * @author zhoutao
     * @desc 企业每日资费使用情况导出
     * @param Int beginTime:true 开始日期
     * @param Int endTime:true 结束日期
    */
    public function index()
    {
        $enterpriseSdk = new Enterprise(Service::instance());
        $tempList = $enterpriseSdk->useDayCountExport([
            'beginTime' => I('get.beginTime'),
            'endTime' => I('get.endTime'),
        ]);

        if (empty($tempList)) {
            return true;
        }

        $list = [];
        foreach ($tempList as $item) {
            $list[] = [
                rgmdate($item['cutdTime'], 'Y-m-d H:i:s'),
                $this->convertUnit($item['cutdAddDisk'], 2),
                $this->convertUnit($item['cutdSurplusDisk'], 2),
                $this->convertUnit($item['cutdAddNetwork'], 2),
                $this->convertUnit($item['cutdSurplusNetwork'], 2),
            ];
        }
        $filename = rgmdate(NOW_TIME, 'Ymd') . '_企业每日资费使用情况';
        $filename = $filename . '.xls';
        PythonExcel::instance()->write(get_sitedir() . $filename, [
            '日期', '当日使用空间容量', '剩余空间容量', '当日使用访问流量', '剩余访问流量'
        ], $list);

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="' . $filename . '"');

        // 根据浏览器类型来判断是否需要特殊处理中文字符
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua) || preg_match("/rv:11.0/", $ua) || preg_match("/Edge/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }

        header("Content-Transfer-Encoding:binary");
        echo file_get_contents(get_sitedir() . $filename);
        exit;
    }

    /**
     * 字节 -> 吉, 加单位
     * @param $num int 数量
     * @param $decimals int 精确到几位小数点
     * @return float|string
     */
    protected function convertUnit($num, $decimals)
    {
        $p = 0;
        $format = 'bytes';
        $unit = 1024;
        if ($num >= 0 && $num < $unit) {

            return $num . ' ' . $format;
        }
        if ($num >= $unit && $num < $unit ** 2) {
            $p = 1;
            $format = 'KB';
        }
        if ($num >= $unit ** 2 && $num < $unit ** 3) {
            $p = 2;
            $format = 'MB';
        }
        if ($num >= $unit ** 3 && $num < $unit ** 4) {
            $p = 3;
            $format = 'GB';
        }
        if ($num >= $unit ** 4 && $num < $unit ** 5) {
            $p = 4;
            $format = 'TB';
        }
        $num /= $unit ** $p;

        return $this->notRounded($num, $decimals) . ' ' . $format;
    }

    /**
     * 精确到小数点位数 但是不 四舍五入
     * @param $num
     * @param $decimals
     * @return float|int
     */
    protected function notRounded($num, $decimals)
    {
        $len = 10 ** $decimals;

        return floor($num * $len) / $len;
    }
}