<?php

namespace app\controller\api\v1;

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

use App\Utils\ApiResponse;

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

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

         /**
     * @Apidoc\Title("根据id更新客服字典")
     * @Apidoc\Url("/api/v1/dictionary/updateId")
     * @Apidoc\Method("POST")
     * @Apidoc\Query("id", type="string",require=true, desc="id",default="1")
     * @Apidoc\Query("key", type="string",require=true, desc="key",default="1")
     * @Apidoc\Query("value", type="string",require=true, desc="value",default="1")
     */
    public function updateId(Request $request)
    {


        $data = $request->post();


        foreach ($data as $key => $value) {
            $db = Dictionary::where('key', $key)->first();
            $db->value = $value;
            $db->save();
        }
        //根据id改数据库中的数据
        if (!$db) {
            return ApiResponse::error (402, '操作失败');
        }
        return ApiResponse::success (200, $db );
    }
}
