<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic;


use app\common\logic\BaseLogic;
use app\common\service\ConfigService;
use app\common\service\FileService;
use think\facade\Db;

/**
 * 工作台
 * Class WorkbenchLogic
 * @package app\adminapi\logic
 */
class WorkbenchLogic extends BaseLogic
{
    /**
     * @notes 工作套
     * @param $adminInfo
     * @return array
     * @author 段誉
     * @date 2021/12/29 15:58
     */
    public static function index()
    {
        return [
            // 今日数据
            'today' => self::today(),
            // 快捷入口
            'menu' => self::menu(),
            // 图表数据
            'chart_data' => self::chartData(),
        ];
    }


    /**
     * @notes 快捷入口
     * @return array[]
     * @author 段誉
     * @date 2021/12/29 16:40
     */
    public static function menu(): array
    {
        return [
            [
                'name' => '基地列表',
                'image' => FileService::getFileUrl(config('project.default_image.menu_dict')),
                'url' => '/land/farm'
            ],
            [
                'name' => '产品列表',
                'image' => FileService::getFileUrl(config('project.default_image.menu_file')),
                'url' => '/land/product'
            ],
            [
                'name' => '设备列表',
                'image' => FileService::getFileUrl(config('project.default_image.menu_web')),
                'url' => '/device/device'
            ],
            [
                'name' => '监控报警',
                'image' => FileService::getFileUrl(config('project.default_image.menu_auth')),
                'url' => '/device/monitor_alarm'
            ]
        ];
    }

    /**
     * @notes 今日数据
     * @return int[]
     * @author 段誉
     * @date 2021/12/29 16:15
     */
    public static function today(): array
    {
        $todayStart = strtotime(date('Y-m-d 00:00:00')); 
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));
        $userWhere['user_id'] = -1;
        // 超级管理员数据
        if ((request()->adminInfo)['root'] && !(request()->adminInfo)['user_id']) {
            unset($userWhere['user_id']);
        }
        // 普通用户数据
        if (!(request()->adminInfo)['root'] && (request()->adminInfo)['user_id']) {
            $userWhere['user_id'] = (request()->adminInfo)['user_id'];
        }
        $totalLand = Db::name('farm')->where($userWhere)->count();
        $todayLand = Db::name('farm')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
        $totalProduct = Db::name('product')->where($userWhere)->count();
        $todayProduct = Db::name('product')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
        $totalDevice = Db::name('device')->where($userWhere)->count();
        $todayDevice = Db::name('device')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
        $totalAlarm = Db::name('monitor_alarm')->where($userWhere)->count();
        $todayAlarm = Db::name('monitor_alarm')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
        return [
            'time' => date('Y-m-d H:i:s'),
            'today_land' => $todayLand,
            'total_land' => $totalLand,
            'today_product' => $todayProduct,
            'total_product' => $totalProduct,
            'today_device' => $todayDevice,
            'total_device' => $totalDevice,
            'today_alarm' => $todayAlarm,
            'total_alarm' => $totalAlarm
        ];
    }


    /**
     * @notes 图表数据
     * @return array
     * @author 段誉
     * @date 2021/12/29 16:57
     */
    public static function chartData(): array
    {
        $land = $device = $product = $alarm = [];
        $date = [];
        $userWhere['user_id'] = -1;
        if ((request()->adminInfo)['root'] && !(request()->adminInfo)['user_id']) {
            unset($userWhere['user_id']);
        }
        if (!(request()->adminInfo)['root'] && (request()->adminInfo)['user_id']) {
            $userWhere['user_id'] = (request()->adminInfo)['user_id'];
        }
        for ($i = 0; $i < 7; $i++) {
            $where_start = strtotime(date('Y-m-d 00:00:00', strtotime('-' . $i . ' day')));
            $date[] = date('Y-m-d', $where_start);
            $todayStart = $where_start; 
            $todayEnd   = $where_start + 86400 -1;
            $todayLand  = Db::name('farm')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
            $land[$i]   = $todayLand;
            $todayProduct = Db::name('product')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
            $product[$i]  = $todayProduct;
            $todayDevice  = Db::name('device')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
            $device[$i]   = $todayDevice;
            $todayAlarm   = Db::name('monitor_alarm')->where($userWhere)->where('create_time', 'between', [$todayStart, $todayEnd])->count();
            $alarm[$i]    = $todayAlarm;
        }
        return [
            'date'  => $date,
            'list'  => [
                ['name' => '土地数', 'data' => $land],
                ['name' => '产品数', 'data' => $product],
                ['name' => '设备数', 'data' => $device],
                ['name' => '报警数', 'data' => $alarm]
            ]
        ];
    }


    /**
     * @notes 服务支持
     * @return array[]
     * @author 段誉
     * @date 2022/7/18 11:18
     */
    public static function support()
    {
        return [
            [
                'image' => FileService::getFileUrl(config('project.default_image.qq_group')),
                'title' => '官方公众号',
                'desc' => '关注官方公众号',
            ],
            [
                'image' => FileService::getFileUrl(config('project.default_image.customer_service')),
                'title' => '添加企业客服微信',
                'desc' => '想了解更多请添加客服',
            ]
        ];
    }

}