|
@@ -1,183 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace app\common\model;
|
|
|
-
|
|
|
-use think\db\Query;
|
|
|
-use think\Model;
|
|
|
-
|
|
|
-/**
|
|
|
- * @method Model|static getByVideoPointId($id)
|
|
|
- * @method Query|self onlyUser(User $user)
|
|
|
- * @method Query|self check()
|
|
|
- * @method Query|self first($alias=null)
|
|
|
- * @method Query|self day(array $day)
|
|
|
- * @method Query|self sex($sex='')
|
|
|
- * @method Query|self isFirst($pointId)
|
|
|
- * @method Query|self filterUserAge($age_start,$age_end)
|
|
|
- * @method Query|self filterUserArea($province=null,$city=null)
|
|
|
- * @property User user
|
|
|
- * @property VideoPoint point
|
|
|
- */
|
|
|
-class VideoPointUser extends Model
|
|
|
-{
|
|
|
- protected $autoWriteTimestamp=true;
|
|
|
- protected $createTime='created_at';
|
|
|
- protected $updateTime=false;
|
|
|
- protected $hidden=[
|
|
|
- ];
|
|
|
- public function point(){
|
|
|
- return $this->belongsTo(VideoPoint::class);
|
|
|
- }
|
|
|
- public function user(){
|
|
|
- return $this->belongsTo(User::class);
|
|
|
- }
|
|
|
- public function setKeyAttr($data){
|
|
|
- if(is_array($data)){
|
|
|
- sort($data);
|
|
|
- return implode(',',$data);
|
|
|
- }
|
|
|
- return $data;
|
|
|
- }
|
|
|
- public function getKeyAttr($data){
|
|
|
- $type=$this['point']['type'];
|
|
|
- if(in_array($type,['choose','vote'])){
|
|
|
- return array_filter(explode(',',$data));
|
|
|
- }
|
|
|
- /*if($type=='decide'){
|
|
|
- return $data=='true';
|
|
|
- }*/
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- protected static function init()
|
|
|
- {
|
|
|
- self::afterInsert(function (self $pointUser){
|
|
|
- #增加答题总数目
|
|
|
- $user=$pointUser->user;
|
|
|
- $point=$pointUser->point;
|
|
|
- $type=$point['type'];
|
|
|
- if(in_array($type,['choose','decide'])){
|
|
|
- #用户正确率
|
|
|
- $userinfo=$user->userinfo()->lock(true)->find();
|
|
|
- $userinfo['hd_num']=$userinfo['hd_num']+1;
|
|
|
- if($pointUser['is_right']){
|
|
|
- $userinfo['hd_right_num']=$userinfo['hd_right_num']+1;
|
|
|
- }else{
|
|
|
- $userinfo['hd_wrong_num']=$userinfo['hd_wrong_num']+1;
|
|
|
- }
|
|
|
- $userinfo['right_per']=bcmul(bcdiv($userinfo['hd_right_num'],$userinfo['hd_num'],2),100,2);
|
|
|
- $userinfo['wrong_per']=bcmul(bcdiv($userinfo['hd_wrong_num'],$userinfo['hd_num'],2),100,2);
|
|
|
- $userinfo->save();
|
|
|
-
|
|
|
- /*$rate=new UserCorrectRate();
|
|
|
- $rate->replace();
|
|
|
- $rate->save([
|
|
|
- 'user_id'=>$user->id,
|
|
|
- 'day'=>strtotime(date('Y-m-d 00:00:00')),
|
|
|
- 'right_per'=>$userinfo['right_per'],
|
|
|
- ]);*/
|
|
|
- /* $time=strtotime(date('Y-m-d 00:00:00'));
|
|
|
- $newRate=$user->crate()->where($time)->find();
|
|
|
- if(!$newRate){
|
|
|
- $newRate=new UserCorrectRate();
|
|
|
- $newRate->save([
|
|
|
- 'user_id'=>$user->id,
|
|
|
- 'day'=>$time,
|
|
|
- 'right_per'=>$userinfo['right_per'],
|
|
|
- 'hd_num'=>1,
|
|
|
- 'hd_right_num'=>$pointUser['is_right']?1:0,
|
|
|
- ]);
|
|
|
- }else{
|
|
|
- $newRate['hd_num']=$newRate['hd_num']+1;
|
|
|
- $newRate['hd_right_num']=$newRate['hd_right_num']+($pointUser['is_right']?1:0);
|
|
|
- }
|
|
|
-
|
|
|
- $newRate['right_per']=bcmul(bcdiv($newRate['hd_right_num'],$newRate['hd_num'],2),100,2);*/
|
|
|
- #沿用上一次的正确率
|
|
|
- $time=strtotime(date('Y-m-d 00:00:00'));
|
|
|
- $newRate=$user->crate()->where($time)->find();
|
|
|
- if(!$newRate){
|
|
|
- $newRate=new UserCorrectRate();
|
|
|
- $newRate->save([
|
|
|
- 'user_id'=>$user->id,
|
|
|
- 'day'=>$time,
|
|
|
- 'right_per'=>$userinfo['right_per'],
|
|
|
- 'hd_num'=>1,
|
|
|
- 'hd_right_num'=>$pointUser['is_right']?1:0,
|
|
|
- ]);
|
|
|
- }else{
|
|
|
- $newRate['hd_num']=$newRate['hd_num']+1;
|
|
|
- $newRate['hd_right_num']=$newRate['hd_right_num']+($pointUser['is_right']?1:0);
|
|
|
- $newRate['right_per']=$userinfo['right_per'];
|
|
|
- }
|
|
|
-
|
|
|
- $newRate->save();
|
|
|
- }
|
|
|
- #总耗时
|
|
|
- $point['total_time']=$point['total_time']+$pointUser['time'];
|
|
|
- #平均耗时
|
|
|
- $point['ave_time']=$point->user()->avg('time');
|
|
|
- $point->save();
|
|
|
- });
|
|
|
- self::beforeInsert(function (self $pointUser){
|
|
|
- $pointUser['day']=strtotime(date('Y-m-d'));
|
|
|
- $pointUser['month']=strtotime(date('Y-m-01'));
|
|
|
- $pointUser['year']=strtotime(date('Y-01-01'));
|
|
|
- });
|
|
|
- }
|
|
|
- public function scopeOnlyUser(Query $query,User $user){
|
|
|
- $day=strtotime(date('Y-m-d 00:00:00'));
|
|
|
- $query->where('user_id',$user->id)->where('day','>=',$day);
|
|
|
- }
|
|
|
- /** 检查需要判断的因素 */
|
|
|
- public function scopeCheck(Query $query){
|
|
|
- $day=strtotime(date('Y-m-d 00:00:00'));
|
|
|
- $query->where('day',$day);
|
|
|
- }
|
|
|
- /** 是不是第一次答题 */
|
|
|
- public function scopeIsFirst(Query $query,$pointId){
|
|
|
- $query->first()->where('video_point_id',$pointId);
|
|
|
- }
|
|
|
- /** 第一次 */
|
|
|
- public function scopeFirst(Query $query,$alias=null){
|
|
|
- $table=$this->getTable();
|
|
|
- if($alias){
|
|
|
- $table=$alias;
|
|
|
- }
|
|
|
- $query->where("{$table}.is_first",1);
|
|
|
- }
|
|
|
- /** day */
|
|
|
- public function scopeDay(Query $query,$day){
|
|
|
- $query->whereBetween('day',$day);
|
|
|
- }
|
|
|
- #根据性别筛选
|
|
|
- public function scopeSex(Query $query,$sex){
|
|
|
- if(!$sex){
|
|
|
- return;
|
|
|
- }
|
|
|
- $query->whereExists("select * from user where gender={$sex} and user.id=video_point_user.user_id");
|
|
|
- }
|
|
|
- #年龄筛选
|
|
|
- public function scopeFilterUserAge(Query $query,$age_start,$age_end){
|
|
|
- if(!is_null($age_start) && $age_start!==''){
|
|
|
- $query->whereExists("select * from user where age>={$age_start} and user.id=video_point_user.user_id");
|
|
|
- }
|
|
|
- if(!is_null($age_end) && $age_end!==''){
|
|
|
- $query->whereExists("select * from user where age<={$age_end} and user.id=video_point_user.user_id");
|
|
|
- }
|
|
|
- }
|
|
|
- public function scopeFilterUserArea(Query $query,$province=null,$city=null){
|
|
|
- if($city){
|
|
|
- $query->whereExists("select id from user where id=video_point_user.user_id and user.city_id={$city}");
|
|
|
- }
|
|
|
- elseif($province){
|
|
|
- $query->whereExists("select id from user where id=video_point_user.user_id and user.province_id={$province}");
|
|
|
- }
|
|
|
- }
|
|
|
-}
|