Question.php 14.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/10/18
 * Time: 18:33
 */

namespace app\api\controller;

use app\common\controller\Algorithm;
use app\common\controller\Api;
use think\Db;

/**
 * 测试题接口
 */
class Question extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 测试题接口
     * @ApiTitle    (测试题)
     * @ApiSummary  (测试题)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/Test)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function Test()
    {
        $UserId = $this->is_token($this->request->header());
        $Sex = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
        $i = 0;
        if (!empty($Sex['sex']) || $Sex['sex'] != 0) {
            $i++;
        }
        if (!empty($Sex['birthday']) || $Sex['birthday'] != 0) {
            $i++;
        }
        if (!empty($Sex['height']) || $Sex['height'] != 0) {
            $i++;
        }
        if (!empty($Sex['weight']) || $Sex['weight'] != 0) {
            $i++;
        }
        if ($Sex['sex'] == 1) {
            $Arr = Db::name('question_boy')->select();
        } elseif ($Sex['sex'] == 2) {
            $Arr = Db::name('question_girl')->select();
        } else {
            $return = [
                'Sex' => 0,
                'Choose' => [],
                'Count' => 0,
                'list' => []
            ];
            $this->success('成功', $return);
        }
        if (!empty($Sex)) {
            $ChooseArr = json_decode($Sex['choose']);
        }
        foreach ($Arr as $k => $v) {
            $list[$k]['id'] = $v['id'];
            $list[$k]['Question'] = $v['question'];
            if (!empty($v['config'])) {
                $list[$k]['Config'] = json_decode($v['config']);
            } else {
                $list[$k]['Config'] = [];
            }
            if (empty($v['config'])) {
                //填空
                if ($v['id'] == 1) {
                    if ($Sex['birthday'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = date('Y-m-d', $Sex['birthday']);
                    }
                } elseif ($v['id'] == 2) {
                    if ($Sex['height'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = $Sex['height'];
                    }
                } else {
                    if ($Sex['weight'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = $Sex['weight'];
                    }
                }
            }
        }
        if (empty($ChooseArr)) {
            $ChooseArr = [];
        }
        $return = [
            'Sex' => $Sex['sex'],
            'Choose' => $ChooseArr,
            'Count' => count($ChooseArr) + $i,
            'list' => $list
        ];
        $this->success('成功', $return);
    }


    /**
     *测试题接口
     * @ApiTitle    (基础资料问卷)
     * @ApiSummary  (基础资料问卷)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/QuestionNaire)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="int", required=true, description="1=新增,2=更改")
     * @ApiParams   (name="id", type="integer", required=true, description="题号/0开始")
     * @ApiParams   (name="Answer", type="string", required=true, description="答案")
     * @ApiParams   (name="type", type="int", required=true, description="是否最后一道题,0=否,1=是")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function QuestionNaire()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        if ($params['status'] == 2) {
            $Question = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
            if ($params['id'] < 4) {
                if ($params['id'] == 1) {
                    $Time = strtotime(date($params['Answer']));
                    $res = Db::name('sleep')->where(['user_id' => $UserId])->where('type', 0)->order('id desc')->update(['birthday' => $Time]);/*第一部分填空题*/
                } elseif ($params['id'] == 2) {
                    $res = Db::name('sleep')->where(['user_id' => $UserId])->where('type', 0)->order('id desc')->update(['height' => $params['Answer']]);/*第一部分填空题*/
                } elseif ($params['id'] == 3) {
                    $res = Db::name('sleep')->where(['user_id' => $UserId])->where('type', 0)->order('id desc')->update(['weight' => $params['Answer']]);/*第一部分填空题*/
                }
            } else {
                $ChooseArr = json_decode($Question['choose']);
                foreach ($ChooseArr as $k => $v) {
                    if ($ChooseArr[$k][0] == $params['id']) {
                        $ChooseArr[$k][1] = $params['Answer'];
                    }
                }
                $NewChooseArr = json_encode($ChooseArr);
                $res = Db::name('sleep')->where('user_id', $UserId)->where('id', $Question['id'])->where('type', 0)->update(['updatetime' => time(), 'choose' => $NewChooseArr]);
            }
            if (!$res) {
                $this->error('更改失败', 0);
            }
        } else {
            //填写测试
            $this->NaireInsert($UserId, $params['id'], $params['Answer']);
        }
        //已答题数
        $Sex = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
        $i = 0;
        if (!empty($Sex['sex']) || $Sex['sex'] != 0) {
            $i++;
        }
        if (!empty($Sex['birthday']) || $Sex['birthday'] != 0) {
            $i++;
        }
        if (!empty($Sex['height']) || $Sex['height'] != 0) {
            $i++;
        }
        if (!empty($Sex['weight']) || $Sex['weight'] != 0) {
            $i++;
        }
        if ($Sex['choose'] != null) {
            $ChooseArr111 = json_decode($Sex['choose']);
            $Count = count($ChooseArr111);
        } else {
            $Count = 0;
        }
        $data = [
            'Count' => $Count + $i,

        ];
        $this->success('成功', $data);
    }


    /**
     *测试题接口
     * @ApiTitle    (清空用户答题)
     * @ApiSummary  (清空用户答题)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/DeleteQuestionNaire)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function DeleteQuestionNaire()
    {
        $UserId = $this->is_token($this->request->header());
        $QuestionID = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
        $res = Db::name('sleep')->where('user_id', $UserId)->where('id', $QuestionID['id'])->delete();
        $this->Res($res);
    }

    /**
     *测试题接口
     * @ApiTitle    (生成报告)
     * @ApiSummary  (生成报告)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/GeneratingReports)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function GeneratingReports()
    {
        $UserId = $this->is_token($this->request->header());
        $QuestionID = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
        if (empty($QuestionID)) {
            $this->error('生成失败', 0);
        }
        $Api = new Algorithm();
        $return = $Api->TestQuestionAlgorithm($UserId, $QuestionID['id']);
        //调用算法type 更改为1  回答完所有题目
        $this->success('成功', $return);
    }


    /**
     *测试题接口
     * @ApiTitle    (获取报告)
     * @ApiSummary  (获取报告)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/HaveTestList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="0为获取最新")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function HaveTestList()
    {
        $UserId = $this->is_token($this->request->header());
        //测试题ID
        $ID = input('id');
        $map = [];
        if ($ID != 0) {
            //获取指定ID 测试题
            $map['id'] = ['EQ', $ID];
        }
        //测试题Array $QuestionArray
        $QuestionArray = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where($map)->where('type', 1)->find();
        if (empty($QuestionArray)) {
            $return = [];
        } else {
            $Api = new Algorithm();
            $return = $Api->InsertReport($QuestionArray);
        }
        $this->success('成功', $return);
    }


    /**
     *测试题接口
     * @ApiTitle    (历史盒子)
     * @ApiSummary  (历史盒子)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/HistoryBox)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="pages", type="integer", required=true, description="pages")
     * @ApiParams   (name="rows", type="integer", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function HistoryBox()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $Array = Db::name('sleep')
            ->alias('s')
            ->where('s.user_id', $UserId)
            ->order('s.id desc')
            ->where('s.type', 1)
            ->join('user u', 'u.id=s.user_id')
            ->join('sleep_box b', 'b.type=s.sleep_box_type')
            ->field('s.id,u.nickname,b.title,b.image,b.icon_image,s.createtime')
            ->select();
        if (empty($Array)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($Array as $k => $v) {
                $list[$k]['Id'] = $v['id'];
                $list[$k]['Nickname'] = $v['nickname'];
                $list[$k]['Title'] = $v['title'];
                $list[$k]['Image'] = cdnurl($v['image']);
                $list[$k]['IconImage'] = cdnurl($v['icon_image']);
                $list[$k]['Createtime'] = date('Y-m-d', $v['createtime']);
            }
            //分页
            $List = $this->page_array($params['rows'], $params['pages'], $list, 0);
            $data = [
                'Count' => count($Array),
                'List' => $List
            ];
        }
        $this->success('成功', $data);
    }


    /**
     *测试题接口
     * @ApiTitle    (长期能量)
     * @ApiSummary  (长期能量)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/LongConvsen)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function LongConvsen()
    {
        $UserId = $this->is_token($this->request->header());
        $Array = Db::name('sleep')->where('user_id', $UserId)->where('type', 1)->select();
        if (!empty($Array)) {
            foreach ($Array as $k => $v) {
                $DateDayByKey[$k]['date'] = date('Y-m-d', $v['createtime']);
                $DateMonthByKey[$k]['date'] = date('Y-m', $v['createtime']);
            }
            $DateDay = $this->second_array_unique_bykey($DateDayByKey, 'date');
            $DateMonth = $this->second_array_unique_bykey($DateMonthByKey, 'date');
            //日
            foreach ($DateDay as $k => $v) {
                //指定日起止时间戳
                $YMD = explode('-', $v['date']);
                $StarTime = mktime(0, 0, 0, $YMD[1], $YMD[2], $YMD[0]);
                $EndTime = mktime(0, 0, 0, $YMD[1], $YMD[2] + 1, $YMD[0]) - 1;
                $map['createtime'] = ['between', [$StarTime, $EndTime]];
                $List = Db::name('sleep')->where('user_id', $UserId)->where('type', 1)->where($map)->select();
                foreach ($List as $key => $value) {
                    $DayArray[$k]['id'][$key] = $value['id'];
                    $DayArray[$k]['date'][$key] = date('Y-m-d', $value['createtime']);
                    $DayArray[$k]['V'][$key] = $value['v'];
                    $DayArray[$k]['P'][$key] = $value['p'];
                    $DayArray[$k]['K'][$key] = $value['k'];
                }
            }
            dump($StarTime);
            dump($EndTime);
        } else {
            $Day = [];
            $Month = [];
        }
    }
}