<commit_before><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Zhiyi\Plus\Models\CommonConfig;
use Zhiyi\Plus\Support\Configuration;
use Zhiyi\Plus\Repository\CurrencyConfig;
use Zhiyi\Plus\Http\Controllers\Controller;
use Zhiyi\Plus\Models\CurrencyOrder as OrderModel;

class CurrencyController extends Controller
{
    protected $rep;

    public function __construct(CurrencyConfig $config)
    {
        $this->rep = $config;
    }

    /**
     * 获取积分配置项.
     *
     * @param  CurrencyConfig $config
     * @return mixed
     */
    public function showConfig()
    {
        $config = [];
        $config['basic_conf'] = $this->basicConfig();
        $config['detail_conf'] = $this->rep->get();

        return response()->json($config, 200);
    }

    /**
     * 更新积分基础配置.
     *
     * @param  Request $request
     * @return mixed
     */
    public function updateConfig(Request $request, Configuration $configuration)
    {
        $type = (string) $request->query('type');

        if ($type == 'detail') {
            foreach ($request->except('type') as $key => $value) {
                $config = CommonConfig::where('name', sprintf('currency:%s', $key))
                ->where('namespace', 'currency')
                ->first();
                $config->value = $value;
                $config->save();
            }
            $this->rep->flush();
        } else {
            $data = $request->all();

            $config = $configuration->getConfiguration();
            $config->set('currency.rule', (string) $data['rule']);
            $config->set('currency.cash.rule', (string) $data['cash']['rule']);
            $config->set('currency.cash.status', (bool) $data['cash']['status']);
            $config->set('currency.recharge.rule', (string) $data['recharge']['rule']);
            $config->set('currency.recharge.status', (bool) $data['recharge']['status']);

            $configuration->save($config);
        }

        return response()->json(['message' => '更新成功'], 201);
    }

    /**
     * 积分开关相关配置数据.
     *
     * @return array
     */
    protected function basicConfig(): array
    {
        $bootstrappers = [];

        $bootstrappers['rule'] = config('currency.rule', null);
        $bootstrappers['cash.rule'] = config('currency.cash.rule', null);
        $bootstrappers['cash.status'] = config('currency.cash.status', true);
        $bootstrappers['recharge.rule'] = config('currency.recharge.rule', null);
        $bootstrappers['recharge.status'] = config('currency.recharge.status', true);

        return $bootstrappers;
    }

    /**
     * 积分流水.
     *
     * @param Request $request
     * @param OrderModel $orderModel
     * @return mixed
     * @author BS <<EMAIL>>
     */
    public function list(Request $request, OrderModel $orderModel)
    {
        $limit = $request->query('limit');
        $after = $request->query('after');
        $user = (int) $request->query('user');
        $name = $request->query('name');
        $action = $request->query('action');

        $orders = $orderModel->when($after, function ($query) use ($after) {
            return $query->where('id', '<', $after);
        })
        ->when($user, function ($query) use ($user) {
            return $query->where('user_id', $user);
        })
        ->when($name, function ($query) use ($name) {
            return $query->whereHas('user', function ($query) use ($name) {
                return $query->where('name', 'like', '%'.$name);
            });
        })
        ->when(in_array($action, [1, -1]), function ($query) use ($action) {
            return $query->where('type', $action);
        })
        ->with('user')
        ->limit($limit)
        ->orderBy('id', 'desc')
        ->get();

        return response()->json($orders, 200);
    }<commit_msg>feat: 增加积分概览接口<commit_after><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Http\Controllers\Admin;

use DB;
use Illuminate\Http\Request;
use Zhiyi\Plus\Models\CommonConfig;
use Zhiyi\Plus\Support\Configuration;
use Zhiyi\Plus\Repository\CurrencyConfig;
use Zhiyi\Plus\Http\Controllers\Controller;
use Zhiyi\Plus\Models\CurrencyOrder as OrderModel;

class CurrencyController extends Controller
{
    protected $rep;

    public function __construct(CurrencyConfig $config)
    {
        $this->rep = $config;
    }

    /**
     * 获取积分配置项.
     *
     * @param  CurrencyConfig $config
     * @return mixed
     */
    public function showConfig()
    {
        $config = [];
        $config['basic_conf'] = $this->basicConfig();
        $config['detail_conf'] = $this->rep->get();

        return response()->json($config, 200);
    }

    /**
     * 更新积分基础配置.
     *
     * @param  Request $request
     * @return mixed
     */
    public function updateConfig(Request $request, Configuration $configuration)
    {
        $type = (string) $request->query('type');

        if ($type == 'detail') {
            foreach ($request->except('type') as $key => $value) {
                $config = CommonConfig::where('name', sprintf('currency:%s', $key))
                ->where('namespace', 'currency')
                ->first();
                $config->value = $value;
                $config->save();
            }
            $this->rep->flush();
        } else {
            $data = $request->all();

            $config = $configuration->getConfiguration();
            $config->set('currency.rule', (string) $data['rule']);
            $config->set('currency.cash.rule', (string) $data['cash']['rule']);
            $config->set('currency.cash.status', (bool) $data['cash']['status']);
            $config->set('currency.recharge.rule', (string) $data['recharge']['rule']);
            $config->set('currency.recharge.status', (bool) $data['recharge']['status']);

            $configuration->save($config);
        }

        return response()->json(['message' => '更新成功'], 201);
    }

    /**
     * 积分开关相关配置数据.
     *
     * @return array
     */
    protected function basicConfig(): array
    {
        $bootstrappers = [];

        $bootstrappers['rule'] = config('currency.rule', null);
        $bootstrappers['cash.rule'] = config('currency.cash.rule', null);
        $bootstrappers['cash.status'] = config('currency.cash.status', true);
        $bootstrappers['recharge.rule'] = config('currency.recharge.rule', null);
        $bootstrappers['recharge.status'] = config('currency.recharge.status', true);

        return $bootstrappers;
    }

    /**
     * 积分流水.
     *
     * @param Request $request
     * @param OrderModel $orderModel
     * @return mixed
     * @author BS <<EMAIL>>
     */
    public function list(Request $request, OrderModel $orderModel)
    {
        $limit = $request->query('limit');
        $after = $request->query('after');
        $user = (int) $request->query('user');
        $name = $request->query('name');
        $action = $request->query('action');

        $orders = $orderModel->when($after, function ($query) use ($after) {
            return $query->where('id', '<', $after);
        })
        ->when($user, function ($query) use ($user) {
            return $query->where('user_id', $user);
        })
        ->when($name, function ($query) use ($name) {
            return $query->whereHas('user', function ($query) use ($name) {
                return $query->where('name', 'like', '%'.$name);
            });
        })
        ->when(in_array($action, [1, -1]), function ($query) use ($action) {
            return $query->where('type', $action);
        })
        ->with('user')
        ->limit($limit)
        ->orderBy('id', 'desc')
        ->get();

        return response()->json($orders, 200);
    }

    /**
     * 积分概览.
     *
     * @param Request $request
     * @return mixed
     * @author BS <<EMAIL>>
     */
    public function overview(Request $request, OrderModel $orderModel)
    {
        $recharge = $orderModel->where('target_type', 'recharge')->select(DB::raw('count(id) as count, sum(amount) as sum'))->get();
        $cash = $orderModel->where('target_type', 'cash')->select(DB::raw('count(id) as count, sum(amount) as sum'))->get();

        return response()->json([
            'recharge' => $recharge,
            'cash' => $cash,
        ], 200);
    }
{'fraction_non_alphanumeric': 0.23224924309014552, 'fraction_numerical': 0.004199628870006837, 'mean_word_length': 1.7263045793397231, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2552471', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2805, 'n_words': 555}