<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\YuebaoItem;
use App\Models\YuebaoLog;
use Exception;
use App\Constants\HttpStatus;

class YueBao extends Controller
{

    /**
     * 余额宝持仓列表
     */
    public function item()
    {
        try {
            $params = request()->validate([
                'page' => ['nullable', 'integer', 'min:1'],
                'limit' => ['nullable', 'integer', 'min:1'],
                'id' => ['nullable', 'integer', 'min:1'],
                'user_id' => ['nullable'],
                'member_id' => ['nullable'],
                'first_name' => ['nullable'],
                'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
                'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 15);

            $query = YuebaoItem::join('users', 'users.user_id', '=', 'yuebao_item.user_id')
            ->select("yuebao_item.*", "users.first_name","users.member_id","users.admin_note as user_admin_note");
            if (!empty($params['id'])) {
                $query = $query->where('yuebao_item.id', $params['id']);
            }
            if (!empty($params['user_id'])) {
                $query = $query->where('yuebao_item.user_id', $params['user_id']);
            }
            if (!empty($params['member_id'])) {
                $query = $query->where('yuebao_item.user_id', $params['member_id']);
            }
            if (!empty($params['start_time'])) {
                $startTime = strtotime($params['start_time'] . " 00:00:00");
                $query = $query->where('yuebao_item.create_time', '>=', $startTime);
            }
            if (!empty($params['end_time'])) {
                $endTime = strtotime($params['end_time'] . " 23:59:59");
                $query = $query->where('yuebao_item.create_time', '<=', $endTime);
            }
            if (isset($params['first_name']) && !empty($params['first_name'])) {
                $query = $query->where('users.first_name', 'like', "%" . $params['first_name'] . "%");
            }
            $count = $query->count();
            $list = $query
                ->forPage($page, $limit)
                ->orderByDesc('yuebao_item.create_time')
                ->get();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success(['total' => $count, 'data' => $list]);

    }

    /**
     * 余额宝日志列表
     */
    public function log()
    {
        try {
            $params = request()->validate([
                'page' => ['nullable', 'integer', 'min:1'],
                'limit' => ['nullable', 'integer', 'min:1'],
                'id' => ['nullable', 'integer', 'min:1'],
                'item_id' => ['nullable', 'integer', 'min:1'],
                'user_id' => ['nullable'],
                'type' => ['nullable', 'integer'],
                'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
                'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 15);

            $query = new YuebaoLog();
            if (!empty($params['id'])) {
                $query = $query->where('id', $params['id']);
            }
            if (!empty($params['item_id'])) {
                $query = $query->where('item_id', $params['item_id']);
            }
            if (!empty($params['user_id'])) {
                $query = $query->where('user_id', $params['user_id']);
            }
            if (!empty($params['type'])) {
                $query = $query->where('type', $params['type']);
            }
            if (!empty($params['start_time'])) {
                $startTime = strtotime($params['start_time'] . " 00:00:00");
                $query = $query->where('create_time', '>=', $startTime);
            }
            if (!empty($params['end_time'])) {
                $endTime = strtotime($params['end_time'] . " 23:59:59");
                $query = $query->where('create_time', '<=', $endTime);
            }
            $count = $query->count();
            $list = $query
                ->forPage($page, $limit)
                ->orderByDesc('create_time')
                ->get();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success(['total' => $count, 'data' => $list]);

    }

}