<?php

namespace app\controller\api\v1;

use support\Request;
use hg\apidoc\annotation as Apidoc;

use App\Utils\ApiResponse;

use App\model\ExchangeRate;
// use App\dao\UserDao;

/**
 * @Apidoc\Title("汇率")
 */
class ExchangeRateController
{  
     /**
     * @Apidoc\Title("查找所有汇率")
     * @Apidoc\Url("/api/v1/ExchangeRate/lists")
     * @Apidoc\Method("POST")
     */
    public function lists()
    {
        //根据id查找用户
        $db = ExchangeRate::all();
        //操作不成功
        if (!$db) {
            return ApiResponse::error (402, '操作失败');
        }
        return ApiResponse::success (200, $db );
    }

         /**
     * @Apidoc\Title("根据id更新汇率")
     * @Apidoc\Url("/api/v1/ExchangeRate/updateId")
     * @Apidoc\Method("POST")
     * @Apidoc\Query("id", type="string",require=true, desc="id",default="1")
     * @Apidoc\Query("points", type="string",require=true, desc="points",default="1")
     * @Apidoc\Query("type", type="string",require=true, desc="type",default="1")
     */
    public function updateId(Request $request)
    {
        $data = $request->post();
        foreach ($data as $key => $value) {
            $db = ExchangeRate::where('type', $key)->first();
            $db->points = $value;
            $db->save();
        }
        //根据id改数据库中的数据
        if (!$db) {
            return ApiResponse::error (402, '操作失败');
        }
        return ApiResponse::success (200, $db );
    }
}
