审查视图

application/api/controller/Question.php 15.8 KB
王智 authored
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/10/18
 * Time: 18:33
 */

namespace app\api\controller;
王智 authored
11
use app\common\controller\Algorithm;
王智 authored
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
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());
王智 authored
41
        $Sex = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
王智 authored
42 43 44 45 46 47 48 49 50 51 52 53 54
        $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++;
        }
王智 authored
55 56
        if ($Sex['sex'] == 1) {
            $Arr = Db::name('question_boy')->select();
王智 authored
57
        } elseif ($Sex['sex'] == 2) {
王智 authored
58
            $Arr = Db::name('question_girl')->select();
王智 authored
59 60
        } else {
            $return = [
王智 authored
61
                'Sex' => 0,
王智 authored
62 63 64 65 66
                'Choose' => [],
                'Count' => 0,
                'list' => []
            ];
            $this->success('成功', $return);
王智 authored
67
        }
王智 authored
68
        if (!empty($Sex)) {
王智 authored
69 70
            $ChooseArr = json_decode($Sex['choose']);
        }
王智 authored
71 72 73 74 75 76 77 78 79 80 81
        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) {
王智 authored
82 83 84 85 86
                    if ($Sex['birthday'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = date('Y-m-d', $Sex['birthday']);
                    }
王智 authored
87
                } elseif ($v['id'] == 2) {
王智 authored
88 89 90 91 92
                    if ($Sex['height'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = $Sex['height'];
                    }
王智 authored
93
                } else {
王智 authored
94 95 96 97 98
                    if ($Sex['weight'] == null) {
                        $list[$k]['Back'] = '';
                    } else {
                        $list[$k]['Back'] = $Sex['weight'];
                    }
王智 authored
99 100 101
                }
            }
        }
王智 authored
102 103 104
        if (empty($ChooseArr)) {
            $ChooseArr = [];
        }
王智 authored
105
        $return = [
王智 authored
106
            'Sex' => $Sex['sex'],
王智 authored
107
            'Choose' => $ChooseArr,
王智 authored
108
            'Count' => count($ChooseArr) + $i,
王智 authored
109 110 111 112 113 114 115 116 117 118 119 120 121
            'list' => $list
        ];
        $this->success('成功', $return);
    }


    /**
     *测试题接口
     * @ApiTitle    (基础资料问卷)
     * @ApiSummary  (基础资料问卷)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Question/QuestionNaire)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
王智 authored
122
     * @ApiParams   (name="status", type="int", required=true, description="1=新增,2=更改")
王智 authored
123 124
     * @ApiParams   (name="id", type="integer", required=true, description="题号/0开始")
     * @ApiParams   (name="Answer", type="string", required=true, description="答案")
王智 authored
125
     * @ApiParams   (name="type", type="int", required=true, description="是否最后一道题,0=否,1=是")
王智 authored
126 127 128 129 130 131 132 133 134 135 136
     * @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();
王智 authored
137
        if ($params['status'] == 2) {
王智 authored
138
            $Question = Db::name('sleep')->where('user_id', $UserId)->order('id desc')->where('type', 0)->find();
王智 authored
139 140 141 142 143 144 145 146
            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']]);/*第一部分填空题*/
王智 authored
147
                }
王智 authored
148 149 150
            } else {
                $ChooseArr = json_decode($Question['choose']);
                foreach ($ChooseArr as $k => $v) {
王智 authored
151 152
                    if ($ChooseArr[$k][0] == $params['id']) {
                        $ChooseArr[$k][1] = $params['Answer'];
王智 authored
153
                    }
王智 authored
154 155 156
                }
                $NewChooseArr = json_encode($ChooseArr);
                $res = Db::name('sleep')->where('user_id', $UserId)->where('id', $Question['id'])->where('type', 0)->update(['updatetime' => time(), 'choose' => $NewChooseArr]);
王智 authored
157
            }
王智 authored
158
            if (!$res) {
王智 authored
159
                $this->error('更改失败', 0);
王智 authored
160
            }
王智 authored
161 162
        } else {
            //填写测试
王智 authored
163
            $this->NaireInsert($UserId, $params['id'], $params['Answer']);
王智 authored
164
        }
王智 authored
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        //已答题数
        $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++;
        }
王智 authored
180 181 182 183 184 185
        if ($Sex['choose'] != null) {
            $ChooseArr111 = json_decode($Sex['choose']);
            $Count = count($ChooseArr111);
        } else {
            $Count = 0;
        }
王智 authored
186
        $data = [
王智 authored
187
            'Count' => $Count + $i,
王智 authored
188 189 190

        ];
        $this->success('成功', $data);
王智 authored
191
    }
王智 authored
192
王智 authored
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    /**
     *测试题接口
     * @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);
    }
王智 authored
216 217 218 219 220 221 222 223 224 225 226 227 228 229
    /**
     *测试题接口
     * @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':'返回成功'
    })
     */
王智 authored
230
    public function GeneratingReports()
王智 authored
231
    {
王智 authored
232 233 234 235 236
        $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);
        }
王智 authored
237
        $Api = new Algorithm();
王智 authored
238
        $return = $Api->TestQuestionAlgorithm($UserId, $QuestionID['id']);
王智 authored
239
        //调用算法type 更改为1  回答完所有题目
王智 authored
240
        $this->success('成功', $return);
王智 authored
241
    }
王智 authored
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


    /**
     *测试题接口
     * @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']);
王智 authored
321
                $list[$k]['IconImage'] = cdnurl($v['icon_image']);
王智 authored
322 323 324 325 326 327 328 329 330 331 332
                $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);
    }
王智 authored
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354


    /**
     *测试题接口
     * @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) {
王智 authored
355 356 357 358 359 360 361 362
                $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) {
                //指定日起止时间戳
王智 authored
363
                $YMD = explode('-', $v['date']);
王智 authored
364
                $StarTime = mktime(0, 0, 0, $YMD[1], $YMD[2], $YMD[0]);
王智 authored
365
                $EndTime = mktime(0, 0, 0, $YMD[1], $YMD[2] + 1, $YMD[0]) - 1;
王智 authored
366 367
                $map['createtime'] = ['between', [$StarTime, $EndTime]];
                $List = Db::name('sleep')->where('user_id', $UserId)->where('type', 1)->where($map)->select();
王智 authored
368
                foreach ($List as $key => $value) {
王智 authored
369
                    $DayArray[$k][$key]['date'] = date('Y-m-d', $value['createtime']);
王智 authored
370 371 372
                    $DayArray[$k][$key]['V'] = $value['current_wind'];
                    $DayArray[$k][$key]['P'] = $value['current_fire'];
                    $DayArray[$k][$key]['K'] = $value['current_water'];
王智 authored
373
                }
王智 authored
374
            }
王智 authored
375
            foreach ($DayArray as $k => $v) {
王智 authored
376
                $Day[$k]['date'] = $v[0]['date'];
王智 authored
377 378 379
                $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);
王智 authored
380 381 382 383 384
            }
            //月
            foreach ($DateMonth as $k => $v) {
                //指定月起止时间戳
                $StarTime = strtotime($v['date']);
王智 authored
385
                $EndTime = mktime(23, 59, 59, date('m', strtotime($v['date'])) + 1, 00);//指定月份月末时间戳
王智 authored
386 387 388 389
                $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']);
王智 authored
390 391 392
                    $MonthArray[$k][$key]['V'] = $value['current_wind'];
                    $MonthArray[$k][$key]['P'] = $value['current_fire'];
                    $MonthArray[$k][$key]['K'] = $value['current_water'];
王智 authored
393 394 395
                }
            }
            foreach ($MonthArray as $k => $v) {
王智 authored
396
                $Month[$k]['date'] = $v[0]['date'];
王智 authored
397 398 399
                $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);
王智 authored
400
            }
王智 authored
401 402 403 404
        } else {
            $Day = [];
            $Month = [];
        }
王智 authored
405
        $data = [
王智 authored
406 407 408 409
            'Day' => array_values($Day),
            'Month' => array_values($Month)
//            'Day' => $Day,
//            'Month' => $Month
王智 authored
410 411
        ];
        $this->success('成功', $data);
王智 authored
412
    }
王智 authored
413
}