<?php

namespace app\controller\manager;


use app\repository\AccountRepository;
use app\repository\CouponRepository;

/**
 * 统计图
 */
class Statistical extends Base
{
    /**
     * 签到
     * */
    public function sign()
    {
        $startTime = input("startTime/s", date("Y-m-d", strtotime("-1 year")));
        $endTime = input("endTime/s", date("Y-m-d"));
        $agencyCode = input("agencyCode/s");
        $this->data["startTime"] = $startTime;
        $this->data["endTime"] = $endTime;
        $xDay = diffBetweenTwoDays($startTime, $endTime);
        if ($xDay > 366) {
            return $this->error("日期不要超过");
        }
        //生成折现点
        $x = ["product"];
        $releaseDataArray = ["zero"=>"发布数量"];
        $signDataArray = ["zero"=>"签到数量"];
        $receiveDataArray = ["zero"=>"领取数量"];
        for ($aa = 0; $aa <= $xDay; $aa++) {
            $key = date("Y-m-d", strtotime($startTime . " +" . $aa . " day"));
            $x[] = $key;
            $releaseDataArray[$key] = 0;
            $signDataArray[$key] = 0;
            $receiveDataArray[$key] = 0;
        }

        $this->data["x"] = json_encode($x);

        //拿数据

        //发布
        $release = CouponRepository::getInstance()->getReleaseStatistics($startTime,$endTime,$agencyCode);
        foreach ($release as $item){
            if(isset($releaseDataArray[$item["create_time"]])){
                $releaseDataArray[$item["create_time"]]++;
            }
        }
        $releaseDataArray= array_values($releaseDataArray);
        $this->data["releaseDataArray"]=json_encode($releaseDataArray);

        //签到数量
        $sign = CouponRepository::getInstance()->getSignStatistics($startTime,$endTime,$agencyCode);
        foreach ($sign as $item){
            if(isset($signDataArray[$item["verificate_time"]])){
                $signDataArray[$item["verificate_time"]]++;
            }
        }
        $signDataArray= array_values($signDataArray);
        $this->data["signDataArray"]=json_encode($signDataArray);

        //领取数量
        $receive = CouponRepository::getInstance()->getReceiveStatistics($startTime,$endTime,$agencyCode);
        foreach ($receive as $item){
            if(isset($receiveDataArray[$item["received_time"]])){
                $receiveDataArray[$item["received_time"]]++;
            }
        }
        $receiveDataArray= array_values($receiveDataArray);
        $this->data["receiveDataArray"]=json_encode($receiveDataArray);
        return $this->view();
    }

    /**
     * 注册位置
     * */
    public function register(){
        if($this->request->isPost()){
            $page =input("page/d",1);
            $size =input("size/d",1000);
            $data = AccountRepository::getInstance()->findList([
                ["lat",">",0],
                ["lng",">",0],
            ],["lat","lng","id as value"],$page,$size,function ($q){
                return $q->withAttr("value",function ($value){
                    return 100;
                });
            },["id"=>"desc"]);
            return $this->json(0,"success",$data);
        }
        return $this->view();
    }

    /**
     * 领取优惠券位置
     * */
    public function receive(){
        if($this->request->isPost()){
            $page =input("page/d",1);
            $size =input("size/d",1000);
            $data = CouponRepository::getInstance()->findList([
                ["lat",">",0],
                ["lng",">",0],
            ],["lat","lng","id as value"],$page,$size,function ($q){
                return $q->withAttr("value",function ($value){
                    return 100;
                });
            },["id"=>"desc"]);
            return $this->json(0,"success",$data);
        }
        return $this->view();
    }

}
