<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class DistributionController extends Controller
{
    /**
     * 一级页面 分销 所需数据
     */
    public function distribution()
    {
        $openid = $_GET["openid"];
        if ($openid != "undefined" && $openid != "NULL" && $openid) {
            $result = D("Users")->getDistribution($openid);
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "openid有误"
            );
        }
        apiReturn($data);
    }

    /**
     * 分销 进入 “总销售额”
     */
    public function earnings()
    {
        $openid = $_GET["openid"];
        if ($openid != "undefined" && $openid != "NULL" && $openid) {
            $result = D("Users")->getEarnings($openid);
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "openid有误"
            );
        }
        apiReturn($data);
    }

    /**
     * 分销 进入 “总粉丝数量”
     */
    public function retail()
    {
        $openid = $_GET["openid"];
        if ($openid != "undefined" && $openid != "NULL" && $openid) {
            $result = D("Users")->getRetail($openid);
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "openid有误"
            );
        }
        apiReturn($data);
    }

    /**
     * 分销 进入 “当前收益”
     */
    public function withdraw()
    {
        $openid = $_GET["openid"];
        if ($openid != "undefined" && $openid != "NULL" && $openid) {
            $userInfo = M("users")->where("`openid` = '$openid'")->find();
            // 累计收益 2级分销
            $result['benefitSum'] = M("benefits")->where("`openid` = '$openid' AND `type` < 3")->sum("benefit");
            // 当前收益 = 累计佣金 - 已提现佣金
            $result['benefitNow'] = $result['benefitSum'] - $userInfo['alreadyget'];
            // 可提现金额
            $result['fund'] = $userInfo['fund'];
            // 冻结金额
            $result['frozen'] = M("benefits")->where("`openid` = '$openid' AND `type` < 3 AND `status` = 1")->sum("benefit");
            // 提现记录数量
            $result['withdrawNum'] = M("withdraw")->where("`openid`='{$openid}'")->count();
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "openid有误"
            );
        }
        apiReturn($data);
    }

    /**
     * 提现记录
     */
    public function records()
    {
        $openid = $_GET['openid'];
        if (!$openid || $openid == "NULL" || $openid == "undefined") {
            $data = array("status" => -1, "msg" => "openid有误");
        } else {
            $result = D("Withdraw")->getRecords($openid);
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        }
        apiReturn($data);
    }
}