Question.php 15.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
<?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][$key]['date'] = date('Y-m-d', $value['createtime']);
                    $DayArray[$k][$key]['V'] = $value['current_wind'];
                    $DayArray[$k][$key]['P'] = $value['current_fire'];
                    $DayArray[$k][$key]['K'] = $value['current_water'];
                }
            }
            foreach ($DayArray as $k => $v) {
                $Day[$k]['date'] = $v[0]['date'];
                $Day[$k]['V'] = round((array_sum(array_column($v, 'V'))) / count($v), 1);
                $Day[$k]['P'] = round((array_sum(array_column($v, 'P'))) / count($v), 1);
                $Day[$k]['K'] = round((array_sum(array_column($v, 'K'))) / count($v), 1);
            }
            //月
            foreach ($DateMonth as $k => $v) {
                //指定月起止时间戳
                $StarTime = strtotime($v['date']);
                $EndTime = mktime(23, 59, 59, date('m', strtotime($v['date'])) + 1, 00);//指定月份月末时间戳
                $map['createtime'] = ['between', [$StarTime, $EndTime]];
                $List = Db::name('sleep')->where('user_id', $UserId)->where('type', 1)->where($map)->select();
                foreach ($List as $key => $value) {
                    $MonthArray[$k][$key]['date'] = date('Y-m', $value['createtime']);
                    $MonthArray[$k][$key]['V'] = $value['current_wind'];
                    $MonthArray[$k][$key]['P'] = $value['current_fire'];
                    $MonthArray[$k][$key]['K'] = $value['current_water'];
                }
            }
            foreach ($MonthArray as $k => $v) {
                $Month[$k]['date'] = $v[0]['date'];
                $Month[$k]['V'] = round((array_sum(array_column($v, 'V'))) / count($v), 1);
                $Month[$k]['P'] = round((array_sum(array_column($v, 'P'))) / count($v), 1);
                $Month[$k]['K'] = round((array_sum(array_column($v, 'K'))) / count($v), 1);
            }
        } else {
            $Day = [];
            $Month = [];
        }
        $data = [
            'Day' => array_values($Day),
            'Month' => array_values($Month)
//            'Day' => $Day,
//            'Month' => $Month
        ];
        $this->success('成功', $data);
    }
}