<?php

namespace app\admin\service;

use app\admin\service\setting\IPOService;
use app\admin\validate\FlowValidate;
use app\home\service\BaseHomeService;
use app\model\TradeFeeModel;
use app\model\UserBrokerageModel;
use app\model\UserContractLogModel;
use app\model\UserDigitalLogModel;
use app\model\UserForexLogModel;
use app\model\UserModel;
use app\model\UserMoneyLogModel;
use app\model\UserStockBlockLogModel;
use app\model\UserStockFundLogModel;
use app\model\UserStockHkdLogModel;
use app\model\UserStockIdnLogModel;
use app\model\UserStockInLogModel;
use app\model\UserStockLogModel;
use app\model\UserStockMysLogModel;
use app\model\UserStockOptionInrLogModel;
use app\model\UserStockSgdLogModel;
use app\model\UserStockSgdModel;
use app\model\UserStockThaLogModel;
use app\model\UserTransferModel;
use think\exception\ValidateException;
use think\facade\Db;

class FlowService extends AdminBaseService
{
    public function digital($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('digital')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserDigitalLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['digital_id'])) {
                $where['digital_id'] = $param['digital_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserDigitalLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserDigitalLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', []);
        }
    }

    public function stock($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserStockLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserStockLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserStockLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function contract($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('contract')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserContractLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['contract_id'])) {
                $where['contract_id'] = $param['contract_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserContractLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserContractLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function forex($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('forex')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserForexLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['contract_id'])) {
                $where['contract_id'] = $param['contract_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserForexLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserForexLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function transfer($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('transfer')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            if (!empty($param['change_type'])) $where['change_type'] = $param['change_type'];


            // 转出 转入账户
            if (!empty($param['from_account'])) {
                $where['from_account'] = $param['from_account'];
            }

            if (!empty($param['to_account'])) {
                $where['to_account'] = $param['to_account'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserTransferModel::where($where)->where($whereU)->order('tran_id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserTransferModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function fee($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('fee')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            if (!empty($param['change_type'])) $where['change_type'] = $param['change_type'];


            // 账户类型
            if (!empty($param['account_type'])) {
                $where['account_type'] = $param['account_type'];
            }

            // 交易类型
            if (!empty($param['trade_type'])) {
                $where['trade_type'] = $param['trade_type'];
            }

            // 交易单号
            if (!empty($param['trade_no'])) {
                $where['trade_no'] = $param['trade_no'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = TradeFeeModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = TradeFeeModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function brokerage($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('brokerage')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            if (!empty($param['change_type'])) $where['change_type'] = $param['change_type'];


            // 返佣级别
            if (!empty($param['level_type'])) {
                $where['level_type'] = $param['level_type'];
            }

            // 返佣类型
            if (!empty($param['back_type'])) {
                $where['back_type'] = $param['back_type'];
            }


            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserBrokerageModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserBrokerageModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows]);

        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
        }
    }


    ######################################## 美股资产流水 ################################


    // 基金流水
    public function fundStock($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('fund_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserStockFundLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserStockFundLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserStockFundLogModel::where($where)->where($whereU)->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage()]);
        }
    }


    // 香港股票
    public function StockLogs($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('us_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserStockHkdLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            $market_type=intval($param['market_type']);
            $table_obj=(new IPOService())->getStockModel($market_type);
            if (empty($table_obj)) {
                return $this->toData('1', '数据异常');
            }

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = Db::table($table_obj['log_table'])->where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = Db::table($table_obj['log_table'])->where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage(),$exception->getTrace()]);
        }
    }

    // 印度期权
    public function inOptionStock($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('hk_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserStockOptionInrLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }


            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserStockOptionInrLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserStockOptionInrLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage()]);
        }
    }

    // 香股
    public function blockStock($param, $adminId)
    {
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('block_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserStockBlockLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserStockBlockLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserStockBlockLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage()]);
        }
    }

    public function forexList($param, $adminId){
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('block_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserForexLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserForexLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserForexLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage()]);
        }
    }

    public function allList($param, $adminId){
        try {
            // 参数校验
            validate(FlowValidate::class)->scene('block_stock')->check($param);

            $where = [];
            $userId = 0;
            // 用户号精确搜索
            if (!empty($param['user_no'])) {
                $user = UserModel::where('user_no', $param['user_no'])->find();
                if (empty($user)) {
                    return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => [], ]);
                }
                $userId = $user['user_id'];
            }

            // 判断是否是代理 如果是代理 只能看他自己管理的用户
            $whereU = $this->getWhereByIsAgentAndUserId($adminId, $where, $userId);
            if (!is_array($whereU)) {
                return $this->toData('0', 'SUCCESS', ['total' => 0, 'list' => []]);
            }

            $changeTypeArr = UserMoneyLogModel::group('change_type')->column('change_type');

            if (!empty($param['change_type']) && in_array($param['change_type'], $changeTypeArr)) $where['change_type'] = $param['change_type'];

            // 交易对
            if (!empty($param['stock_id'])) {
                $where['stock_id'] = $param['stock_id'];
            }

            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where['create_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }

            // 列表
            $list = UserMoneyLogModel::where($where)->where($whereU)->order('id', 'desc')->page($param['page'], $param['limit'])->select();
            // 总数
            $total = UserMoneyLogModel::where($where)->where($whereU)
                ->count();

            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
                // 获取用户号
                $userIdArr = [];
                foreach ($list as $idItem) {
                    $userIdArr[] = $idItem['user_id'];
                }

                $userNoArr = UserModel::where('user_id', 'in', $userIdArr)->column('user_no', 'user_id');
                foreach ($rows as $key => $item) {
                    $rows[$key]['user_no'] = $userNoArr[$item['user_id']] ?? '-'; // 用户号
                }
            }

            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $rows, 'extend' => [
                'change_type_list' => (new BaseHomeService())->getCapitalTypeList($changeTypeArr)
            ]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统异常 请稍后重试', [$exception->getMessage()]);
        }
    }
}