<?php
namespace Smi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class StatisticsController extends BaseController
{
    /**
     * 销售统计
     */
    public function sales()
    {
        // x轴分组
        $xAxis = array();
        // 数据列 series
        $series = array();
        if ($_GET['key'] == 2) {
            // 近10年数据
            for ($i=9; $i >= 0; $i--) { 
                $year = date("Y") - $i;
                $element = "{$year}";
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01-01");
                $nextYear = $value + 1;
                $endTimeStamp = strtotime("{$nextYear}-01-01");
                $data = M("orderGood")
                    ->join("og LEFT JOIN `am_order` o ON og.`order_id`=o.`id`")
                    ->where("o.`create_time` >= {$startTimeStamp} AND o.`create_time` < {$endTimeStamp} AND og.`refund_status`=1 AND o.`status`=5")
                    ->sum("og.`kind_price`");
                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
                $xAxis[$key] = $value . "年";
            }
            $this->assign("key", $_GET['key']);
        } else {
            // 本年数据
            for ($i=0; $i < 12; $i++) { 
                $month = $i + 1;
                $element = date("Y-") . $month;
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01");              
                $thisMonth = substr(strstr($value, "-"), 1);
                $nextMonth = $thisMonth + 1;
                if ($nextMonth > 12) {
                    $nextYear = substr($value, 0, strpos($value, "-")) + 1;
                    $endTimeStamp = strtotime("{$nextYear}-01-01");
                } else {
                    $endTimeStamp = strtotime(date("Y-") . "{$nextMonth}-01");
                }
                $data = M("orderGood")
                    ->join("og LEFT JOIN `am_order` o ON og.`order_id`=o.`id`")
                    ->where("o.`create_time` >= {$startTimeStamp} AND o.`create_time` < {$endTimeStamp} AND og.`refund_status`=1 AND o.`status`=5")
                    ->sum("og.`kind_price`");

                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
            }
        }
        $this->assign("xAxis", JSON($xAxis));
        $this->assign("series", json_encode($series));  // 这里需要把整型(int)数组输出, JSON()会把整型转为字符型
        $this->display("sales");die;
    }

    /**
     * 订单统计
     */
    public function orders()
    {
        // x轴分组
        $xAxis = array();
        // 数据列 series
        $series = array();
        if ($_GET['key'] == 2) {
            // 近10年数据
            for ($i=9; $i >= 0; $i--) { 
                $year = date("Y") - $i;
                $element = "{$year}";
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01-01");
                $nextYear = $value + 1;
                $endTimeStamp = strtotime("{$nextYear}-01-01");
                $data = M("order")
                    ->where("`create_time` >= {$startTimeStamp} AND `create_time` < {$endTimeStamp} AND `status`=5")
                    ->count();
                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
                $xAxis[$key] = $value . "年";
            }
            $this->assign("key", $_GET['key']);
        } else {
            // 本年数据
            for ($i=0; $i < 12; $i++) { 
                $month = $i + 1;
                $element = date("Y-") . $month;
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01");
                $thisMonth = substr(strstr($value, "-"), 1);
                $nextMonth = $thisMonth + 1;
                if ($nextMonth > 12) {
                    $nextYear = substr($value, 0, strpos($value, "-")) + 1;
                    $endTimeStamp = strtotime("{$nextYear}-01-01");
                } else {
                    $endTimeStamp = strtotime(date("Y-") . "{$nextMonth}-01");
                }
                $data = M("order")
                    ->where("`create_time` >= {$startTimeStamp} AND `create_time` < {$endTimeStamp} AND `status`=5")
                    ->count();
                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
            }
        }
        $this->assign("xAxis", JSON($xAxis));
        $this->assign("series", json_encode($series));  // 这里需要把整型(int)数组输出, JSON()会把整型转为字符型
        $this->display("orders");die;
    }

    /**
     * 会员增长趋势
     */
    public function users()
    {
        // x轴分组
        $xAxis = array();
        // 数据列 series
        $series = array();
        if ($_GET['key'] == 2) {
            // 近10年数据
            for ($i=9; $i >= 0; $i--) { 
                $year = date("Y") - $i;
                $element = "{$year}";
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01-01");
                $nextYear = $value + 1;
                $endTimeStamp = strtotime("{$nextYear}-01-01");
                $data = M("users")
                    ->where("`create_time` >= {$startTimeStamp} AND `create_time` < {$endTimeStamp}")
                    ->count();
                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
                $xAxis[$key] = $value . "年";
            }
            $this->assign("key", $_GET['key']);
        } else {
            // 本年数据
            for ($i=0; $i < 12; $i++) { 
                $month = $i + 1;
                $element = date("Y-") . $month;
                array_push($xAxis, $element);
            }
            foreach ($xAxis as $key => $value) {
                $startTimeStamp = strtotime("{$value}-01");
                $thisMonth = substr(strstr($value, "-"), 1);
                $nextMonth = $thisMonth + 1;
                if ($nextMonth > 12) {
                    $nextYear = substr($value, 0, strpos($value, "-")) + 1;
                    $endTimeStamp = strtotime("{$nextYear}-01-01");
                } else {
                    $endTimeStamp = strtotime(date("Y-") . "{$nextMonth}-01");
                }
                $data = M("users")
                    ->where("`create_time` >= {$startTimeStamp} AND `create_time` < {$endTimeStamp}")
                    ->count();
                if (!$data) {
                    $data = 0;
                } else {
                    // 转为整型, 否则 highcharts 不识别
                    $data = intval($data);
                }
                array_push($series, $data);
            }
        }
        $this->assign("xAxis", JSON($xAxis));
        $this->assign("series", json_encode($series));  // 这里需要把整型(int)数组输出, JSON()会把整型转为字符型
        $this->display("users");die;
    }
}