<?php

namespace app\api\controller\deepseek;

use app\common\model\deepseek\Question;
use app\common\model\deepseek\Stream;
use app\common\controller\Api;
class Deepseek extends Api{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    protected $model = null;
    protected $listmodel = null;

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {

        $this->model = new Question();
        $this->listmodel = new Stream();
        parent::_initialize();

        //判断登录用户是否是员工

    }


    /**
     * @ApiTitle(deepseek提问)
     * @ApiSummary(deepseek提问)
     * @ApiMethod(POST)
     * @ApiParams(name = "messages", type = "string",required=true,description = "提问数组 {'role': 'user', 'content': '中原算力是一个什么样的公司'},{'role': 'assistant', 'content': '</think>'} ")
     * @ApiParams(name = "key", type = "string",required=true,description = "单次提问唯一key")
     * @ApiParams(name = "session_key", type = "string",required=false,description = "会话唯一key:不传默认取当前日期")
     * @ApiReturn({

     *
     *})
     */
    public function question(){

        $this->setUrlLock("all","deepseek-question");

        $messages = $this->request->post('messages/s',"");
        $messages = htmlspecialchars_decode($messages);
        $key = $this->request->post('key/s',"");
        $session_key = $this->request->post('session_key/s',"");
        //不传默认取当前日期
        if(!$session_key) $session_key = date("Ymd");
//        $messages = "[{'role': 'user', 'content': '中原算力是一个什么样的公司'},{'role': 'assistant', 'content': '</think>'}]";
//        var_dump($messages);
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
//        if(empty($id)){
//            $this->error(__('缺少必要参数'));
//        }

        try {
            $res =  $this->model->question($messages,$user_id,$key,$session_key,false);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('请求成功,请求结果请轮询查询结果接口', ['detail' => $res]);
    }




    /**
     * @ApiTitle( 输出结果查询接口-list为空数组为在思考中)
     * @ApiSummary(输出结果查询接口)
     * @ApiMethod(GET)
     * @ApiParams(name = "key", type = "string",required=false,description = "提问时自定义的key或提问id")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "session_key", type = "string",required=false,description = "会话id")
     * @ApiReturn({
     *
     *})
     */
    public function answer_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $params = [];
        $my  =  $this->request->get('my/d', 0); //我的评价
        if($my && $user_id){
            $params['user_id'] = $user_id;
        }
        $page  =  $this->request->get('page/d', 1); //页数
        $limit =  $this->request->get('limit/d', 10); //条数
        $params["key"] = $this->request->get('key/s', ''); //搜索关键字
        $params["session_key"] = $this->request->get('session_key/s', ''); //搜索关键字


        try{
            //当前申请状态
            $res = $this->listmodel::answerList($page, $limit,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }



    /**
     * @ApiTitle( 查询我的所有会话)
     * @ApiSummary(查询我的所有会话)
     * @ApiMethod(GET)
     * @ApiReturn({
     *
     *})
     */
    public function seesion_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

//        $params = [];
//        $my  =  $this->request->get('my/d', 0); //我的评价
//        if($my && $user_id){
//            $params['user_id'] = $user_id;
//        }
//        $page  =  $this->request->get('page/d', 1); //页数
//        $limit =  $this->request->get('limit/d', 1000); //条数
//        $params["key"] = $this->request->get('key/s', ''); //搜索关键字
//        $params["session_key"] = $this->request->get('session_key/s', ''); //搜索关键字


        try{
            //当前申请状态
            $res = $this->model->getSeesionlist($user_id);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }





    /**
     * @ApiTitle( 提问记录记录)
     * @ApiSummary(提问记录)
     * @ApiMethod(GET)
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "session_key", type = "string",required=false,description = "会话id")
     * @ApiReturn({
     *
     *})
     */
    public function question_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $params = [];
//        $my  =  $this->request->get('my/d', 0); //我的评价
        if($user_id){
            $params['user_id'] = $user_id;
        }
        $page  =  $this->request->get('page/d', 1); //页数
        $limit =  $this->request->get('limit/d', 10); //条数
        $params["session_key"] = $this->request->get('session_key/s', ''); //搜索关键字


        try{
            //当前申请状态
            $res = $this->model::questionList($page, $limit,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }





    /**
     * @ApiTitle( 删除会话)
     * @ApiSummary(删除会话)
     * @ApiMethod(GET)
     * @ApiParams(name = "session_key", type = "string",required=false,description = "会话id")
     * @ApiReturn({
     *
     *})
     */
    public function delete_session()
    {        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $session_key = $this->request->get('session_key/s', ''); //搜索关键字


        try{
            //当前申请状态
            $res = $this->model->session_del($session_key,true);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);

    }



}