<?php

namespace app\api\controller;

use app\admin\model\device\Chargedevice;
use app\admin\model\order\Notice;
use app\admin\model\order\Order;
use app\common\controller\Api;
use \app\admin\model\users\Users as UsersModel;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $page = $this->request->post('page', 1);
        /*$start_today = strtotime(date('Y-m-d').'00:00:00');//开始的时间戳
        $end_today = strtotime(date('Y-m-d').'23:59:59');//结束的时间*/
        $userinfo = UsersModel::where(['id' => $this->auth->id])->field('total_money,realname,nickname')->find();
        //今日充电总收入
        $user_devices = Chargedevice::where(['user_id' => $this->auth->id])->select(); //获取我的设备
        if (!$user_devices) {
            $this->error('暂无充电桩设备');
        }
        $user_device_count = Chargedevice::where(['user_id' => $this->auth->id])->count(); //获取我的设备总数
        $recharge_sum = Order::where(['user_id' => $this->auth->id, 'order_type' => 0])->whereTime('createtime', 'today')->sum('need_pay');
        $recharge_count = Order::where(['user_id' => $this->auth->id, 'order_type' => ['neq', 5]])->whereTime('createtime', 'today')->count();;
        $recharge_month_sum = 0;
        $recharge_month_money = Order::where(['user_id' => $this->auth->id, 'order_type' => 2])->whereTime('createtime', 'today')->sum('need_pay');
        $total_money = Order::where(['user_id' => $this->auth->id, 'order_type' => ['neq', 5]])->sum('need_pay');
        $reward_money = Order::where(['user_id' => $this->auth->id, 'order_type' => ['in', [7, 8]]])->whereTime('createtime', 'today')->sum('need_pay');
        $total_order_list = [];
        foreach ($user_devices as $key => $val) {
            $order_where = ['device_sign' => $val['charge_device_sn']];
            //            $recharge_sum += Order::where($order_where)->whereTime('createtime','today')->sum('need_pay');
            //            $recharge_month_sum += Order::where(['device_sign' =>$val['charge_device_sn'],'order_type'=>2])->whereTime('createtime','today')->sum('need_pay');
            //            $recharge_count += Order::where($order_where)->whereTime('createtime','today')->count();
            //            $total_money += Order::where(['device_sign' =>$val['charge_device_sn']])->sum('need_pay');
            $order_list = Order::alias('o')
                ->join('charge_device cd', 'o.device_sign = cd.charge_device_sn', 'left')
                ->field('o.*,FROM_UNIXTIME(o.createtime,"%Y-%m-%d %H:%i") as createtime,cd.charge_device_name,cd.area_name,cd.detail_address')
                ->field("REPLACE(o.mobile, SUBSTR(mobile,4,4), '****') as mobile")
                ->where($order_where)
                ->whereTime('o.createtime', 'today')
                ->order('o.createtime desc')
                ->page($page)
                ->limit($this->pageSize)
                ->select();
            $total_order_list = array_merge($total_order_list, $order_list);
        }
        $Advert_money = Order::where(['user_id' => $this->auth->id, 'order_type' => ['in', [4, 6]]])->whereTime('createtime', 'today')->sum('need_pay');
        $userinfo['recharge_money'] = $recharge_sum; //今日充电总收入
        $userinfo['recharge_count'] = $recharge_count; //今日订单总数
        $userinfo['user_device_count'] = $user_device_count; //我的设备总数
        $userinfo['total_money'] = $total_money; //总收入
        $userinfo['recharge_month_money'] = $recharge_month_money; //今日包月订单总收入
        $userinfo['Advert_money'] = $Advert_money; //今日广告总收入
        $userinfo['total_today_money'] = sprintf('%.2f', $recharge_sum + $recharge_month_money + $Advert_money); //今日总收入
        $userinfo['reward_money'] = $reward_money; //今日奖励分佣


        $this->success('请求成功', ['userinfo' => $userinfo, 'total_order_list' => $total_order_list]);
    }

    /**
     * 公告
     */
    public function notice()
    {
        $notice = Notice::where(['status' => 1])->select();
        $this->success('获取成功', ['notice' => $notice]);
    }
}
