Bladeren bron

人事管理解决n+1问题

haozi 5 jaren geleden
bovenliggende
commit
49bb1f927b

+ 4 - 0
app/Http/Controllers/LaborReportController.php

@@ -48,6 +48,8 @@ class LaborReportController extends Controller
         $permittingWorkgroups=UserWorkgroup::whereIn('id',$userWorkgroupIds)->get();
         if($permittingWorkgroups->isEmpty())$permittingWorkgroups="[]";
         $laborReports=app('laborReportService')->paginate($request->input());
+        LaborReport::processing($laborReports);
+
         foreach ($laborReports as $laborReport){
             if (!$laborReport->group_user_id){
                 //2小时之内组长未审核实行软删除
@@ -82,8 +84,10 @@ class LaborReportController extends Controller
             $params = $request->input();
             unset($params["checkAllSign"]);
             $laborReports=app('laborReportService')->get($params);
+            LaborReport::processing($laborReports);
         }else{
             $laborReports=app('laborReportService')->get(["id"=>$request->data]);
+            LaborReport::processing($laborReports);
         }
         $row = ['本次工作时长','小组','临时工','电话','身份证号','劳务所','进场时间','进组时间','退组时间','进组取整时间','退组取整时间','退场时间','审核时间','审核人','晚饭时间(分)','在线时长','进场编号','备注'];
         $list = [];

+ 87 - 66
app/LaborReport.php

@@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Gate;
 
 use App\Traits\ModelLogChanging;
+use Tightenco\Collect\Support\Collection;
 
 class LaborReport extends Model
 {
@@ -24,8 +25,8 @@ class LaborReport extends Model
         'user_duty_check_id','relax_time','remark'
     ];
     protected $appends = [
-        'is_exportGroup','is_export', 'exit_at','enter_at','sequence', 'amountOfJoined','thisRecordOnlineTime','thisRecordWorkingTime','thisRoundRecordWorkingTime',
-        'totalOnlineTime','verifyPerson','userDutyCheckVerifyUserId','isAdult','round_check_in_at','round_check_out_at','has_group_verify_right',
+        'is_exportGroup',/*'sequence',*//*'amountOfJoined',*/'thisRecordOnlineTime','thisRecordWorkingTime','thisRoundRecordWorkingTime',
+        /*'totalOnlineTime',*/'verifyPerson','userDutyCheckVerifyUserId','isAdult','round_check_in_at','round_check_out_at','has_group_verify_right',
     ];
     protected $tempFields = [
         'temEnteringRecord',
@@ -52,7 +53,9 @@ class LaborReport extends Model
     public function laborReportStatus(){
         return $this->hasMany('App\LaborReportStatus','labor_report_id','id');
     }
-
+    public function laborReports(){
+        return $this->hasMany(get_class($this),'enter_number','enter_number');
+    }
 
     public function getRoundCheckInAtAttribute(){
         if (!$this['check_in_at'])return null;
@@ -68,7 +71,7 @@ class LaborReport extends Model
         if ($round_check_out_at>=25&&$round_check_out_at<=55) return Carbon::parse($this['check_out_at'])->clone()->setMinutes(30)->setSeconds(00)->format('Y-m-d H:i:s');
         if ($round_check_out_at>55&&$round_check_out_at<=59) return Carbon::parse($this['check_out_at'])->clone()->addHour()->setMinutes(00)->setSeconds(00)->format('Y-m-d H:i:s');
     }
-    //has_group_verify_right'
+
     public function getHasGroupVerifyRightAttribute(){
         if (!Gate::allows('人事管理-临时工报表')){return null; }
         if (Gate::allows('人事管理-临时工报表-管理全部组')){return true;}
@@ -77,6 +80,16 @@ class LaborReport extends Model
         if (count($userWorkgroupIds)!=0) return in_array($this['user_workgroup_id'],$userWorkgroupIds);
 
     }
+    public function getEnteringRecordAttribute()
+    {
+        if(self::$withoutAppends) {
+            $laborReport = LaborReport::with(['laborReportStatus' => function ($query) {
+                return $query->whereIn('status', ['已入场', '未审核']);
+            }])->where('user_id', $this['user_id'])->orderBy('id', 'desc')->first();
+            if (empty($laborReport)) return null;
+            return $laborReport;
+        }
+    }
 
     public function getIsExportGroupAttribute(){
         return $this['check_out_at']? true:false;
@@ -89,20 +102,18 @@ class LaborReport extends Model
         return $this['userDutyCheck']['verify_user_id']?$this['userDutyCheck']['verify_user_id']:'';
     }
     //总在线时长
-    public function getTotalOnlineTimeAttribute(){
-        if(self::$withoutAppends) {
-            $laborReports = LaborReport::where('enter_number', $this['enter_number'])->get();
-            $totalOnlineTime = $laborReports->reduce(function ($value, $laborReport) {
-                return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
-            });
-            return $totalOnlineTime;
-        }
-    }
+//    public function getTotalOnlineTimeAttribute(){
+//        if(self::$withoutAppends) {
+//            $totalOnlineTime = $this->laborReports->reduce(function ($value, $laborReport) {
+//                return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
+//            });
+//            return $totalOnlineTime;
+//        }
+//    }
     //总工作时长
     public function getTotalWorkingTimeAttribute(){
         if(self::$withoutAppends) {
-            $laborReports = LaborReport::where('enter_number', $this['enter_number'])->orderBy('id', 'asc')->get();
-            $totalWorkingTime = $laborReports->reduce(function ($value, $laborReport) {
+            $totalWorkingTime = $this->laborReports->reduce(function ($value, $laborReport) {
                 return ($value ?? 0) + $laborReport['thisRecordWorkingTime'];
             });
             return $totalWorkingTime;
@@ -111,8 +122,7 @@ class LaborReport extends Model
     //本轮工作起始时间
     public function getThisRoundOnlineStartTimeAttribute(){
         if(self::$withoutAppends) {
-            $laborReports = LaborReport::where('enter_number', $this['enter_number'])->orderBy('id', 'asc')->get();
-            foreach ($laborReports as $laborReport) {
+            foreach ($this->laborReports as $laborReport) {
                 if ($laborReport['enter_at'] && $laborReport['check_in_at'])
                     return $laborReport['check_in_at'];
             }
@@ -121,8 +131,7 @@ class LaborReport extends Model
     //本轮工作结束时间
     public function getThisRoundOnlineEndTimeAttribute(){
         if(self::$withoutAppends) {
-            $laborReports = LaborReport::where('enter_number', $this['enter_number'])->get();
-            foreach ($laborReports as $laborReport) {
+            foreach ($this->laborReports as $laborReport) {
                 if ($laborReport['exit_at'] && $laborReport['check_out_at'])
                     return $laborReport['check_out_at'];
             }
@@ -201,55 +210,74 @@ class LaborReport extends Model
     }
 
     //转场序号
-    public function getSequenceAttribute()
+//    public function getSequenceAttribute()
+//    {
+//        if(self::$withoutAppends) {
+//            if (Arr::has($this->tempFields, 'sequence')) return $this->tempFields['sequence'];
+//            foreach ($this->laborReports as $i => $laborReport) {
+//                if ($laborReport['id'] == $this['id']) {
+//                    $this->tempFields['sequence'] = $i + 1;
+//                    return $this->tempFields['sequence'];
+//                }
+//            }
+//        }
+//    }
+//    public function getAmountOfJoinedAttribute()
+//    {
+//        if(self::$withoutAppends) {
+//            if (Arr::has($this->tempFields, 'amountOfJoined')) return $this->tempFields['amountOfJoined'];
+//            $this->tempFields['amountOfJoined'] = count($this->laborReports);
+//            return $this->tempFields['amountOfJoined'];
+//        }
+//    }
+
+
+    private function checkExitStatus()
     {
-        if(self::$withoutAppends) {
-            if (Arr::has($this->tempFields, 'sequence')) return $this->tempFields['sequence'];
-            $laborReports = LaborReport::where('enter_number', $this['enter_number'])->orderBy('id', 'asc')->get();
-            foreach ($laborReports as $i => $laborReport) {
-                if ($laborReport['id'] == $this['id']) {
-                    $this->tempFields['sequence'] = $i + 1;
-                    return $this->tempFields['sequence'];
-                }
-            }
-        }
+        $status = $this->laborReportStatus->sortByDesc('id')->first();
+        $this->is_export = $status['status'] == '已退场' ? true : false;
+        if ($status['status'] == '已退场')$this->exit_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
     }
-    public function getAmountOfJoinedAttribute()
-    {
-        if(self::$withoutAppends) {
-            if (Arr::has($this->tempFields, 'amountOfJoined')) return $this->tempFields['amountOfJoined'];
-            $this->tempFields['amountOfJoined'] = LaborReport::where('enter_number', $this['enter_number'])->orderBy('id', 'asc')->count();
-            return $this->tempFields['amountOfJoined'];
-        }
+    private function checkEnterStatus(){
+        $status = $this->laborReportStatus->sortBy('id')->first();
+        if ($status['status'] == '未审核')$this->enter_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
     }
-
-    //进场时间
-    public function getEnterAtAttribute()
-    {
-        if(self::$withoutAppends) {
-            $laborReportStatus = LaborReportStatus::where('labor_report_id', $this['id'])->where('status', '未审核')->orderBy('id', 'desc')->first();
-            if (empty($laborReportStatus)) return null;
-            return Carbon::parse($laborReportStatus['created_at'])->format('Y-m-d H:i:s');
+    private function checkAmountOfJoined(){
+        if ($this->amountOfJoined!=count($this->laborReports))$this->amountOfJoined=count($this->laborReports);
+    }
+    private function checkSequence(){
+        if (!$this->sequence) foreach ($this->laborReports as $i => $laborReport) {
+            if ($laborReport['id'] == $this['id']) {
+                $this->sequence = $i + 1;
+            }
         }
     }
+    private function checkTotalOnlineTime(){
+        if (!$this->totalOnlineTime) $this->totalOnlineTime = $this->laborReports->reduce(function ($value, $laborReport) {
+            return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
+        });
+    }
 
-    public function getEnteringRecordAttribute()
+    public static function processing($laborReports)
     {
-        if(self::$withoutAppends) {
-            $laborReport = LaborReport::with(['laborReportStatus' => function ($query) {
-                return $query->whereIn('status', ['已入场', '未审核']);
-            }])->where('user_id', $this['user_id'])->orderBy('id', 'desc')->first();
-            if (empty($laborReport)) return null;
-            return $laborReport;
+        foreach ($laborReports as $laborReport){
+            /** @var LaborReport $laborReport */
+            $laborReport->checkEnterStatus(); //校验进场并赋值
+            $laborReport->checkAmountOfJoined();
+            $laborReport->checkSequence();
+            $laborReport->checkTotalOnlineTime();
+            $laborReport->checkExitStatus(); //校验出场并赋值
         }
     }
-
-    public function getExitAtAttribute()
+    public static function enter($laborReports)
     {
-        if(self::$withoutAppends) {
-            $laborReportStatus = LaborReportStatus::where('labor_report_id', $this['id'])->where('status', '已退场')->orderBy('id', 'desc')->first();
-            if (empty($laborReportStatus)) return null;
-            return Carbon::parse($laborReportStatus['created_at'])->format('Y-m-d H:i:s');
+        foreach ($laborReports as $laborReport){
+            /** @var LaborReport $laborReport */
+            $laborReport->checkEnterStatus(); //校验进场并赋值
+            $laborReport->checkAmountOfJoined();
+            $laborReport->checkSequence();
+            $laborReport->checkTotalOnlineTime();
+            $laborReport->checkExitStatus(); //校验出场并赋值
         }
     }
 
@@ -262,13 +290,6 @@ class LaborReport extends Model
         $this['enter_number']=$enteringNumber;
         return $enteringNumber;
     }
-
-    public function getIsExportAttribute(){
-        if(self::$withoutAppends) {
-            $laborReportStatus = $this->laborReportStatus()->where('labor_report_id', $this['id'])->orderBy('id', 'desc')->first();
-            return $laborReportStatus['status'] == '已退场' ? true : false;
-        }
-    }
     //出场更新临时工报表信息
     static function exitAndChangeLaborReport($laborReport,$userDutyCheck){
         $laborReportStatus=new LaborReportStatus([
@@ -287,7 +308,7 @@ class LaborReport extends Model
             $laborReport->online_duration=$online_duration;
         }
         $laborReport->update();
-        $laborReport->is_export=$laborReport->getIsExportAttribute();
+        $laborReport->is_export=$laborReport->checkExitStatus();
         return $laborReport;
     }
 

+ 1 - 1
app/Services/LaborReportService.php

@@ -32,7 +32,7 @@ class LaborReportService
     private function conditionQuery(array $params){
         $user=Auth::user();
         $laborReports=LaborReport::query()
-            ->with(['user','userDutyCheck','userWorkgroup.signs','laborCompany','laborReportStatus'])->orderBy('labor_reports.id','DESC');
+            ->with(['user','userDutyCheck','userWorkgroup.signs','laborCompany','laborReportStatus','laborReports'])->orderBy('labor_reports.id','DESC');
         if (!($params["is_export"] ?? false)&&!($params['id']??null)){
             $laborReports = $laborReports->whereNotIn('labor_reports.enter_number',function ($builder)use($params){
                 $builder->select('enter_number')->from('labor_reports')

+ 2 - 2
laravel-echo-server.lock

@@ -1,3 +1,3 @@
 {
-	"process": 19904
-}
+	"process": 23296
+}

+ 22 - 16
resources/views/personnel/laborReport/index.blade.php

@@ -110,13 +110,13 @@
                                                 @can('人事管理-门卫审核')
                                                     <span >
                                 <b v-if="laborReport.isAdult==1"   class="text-danger">童工</b>
-                                <b v-else-if="laborReport.userDutyCheckVerifyUserId&&laborReport.amountOfJoined==1"   class="text-success">已审核</b>
+                                <b v-else-if="laborReport.userDutyCheckVerifyUserId{{--&&laborReport.amountOfJoined==1--}}"   class="text-success">已审核</b>
                                 <b v-else-if="laborReport.userDutyCheckVerifyUserId&&laborReport.amountOfJoined&&laborReport.sequence"  class="text-black">&nbsp;&nbsp;&nbsp;@{{laborReport.sequence}}/@{{laborReport.amountOfJoined}}</b>
                                 <button v-else style="transform:scale(1.1)" class="btn btn-lg btn-outline-secondary"  @click="guardClockAudit(laborReport.id,laborReport.user_duty_check_id)">门卫审核</button>
                                 </span>
                                                 @else
                                                     <b v-if="laborReport.isAdult==1"   class="text-danger">童工</b>
-                                                    <b v-else-if="laborReport.userDutyCheckVerifyUserId&&laborReport.amountOfJoined==1"  class="text-success">已审核</b>
+                                                    <b v-else-if="laborReport.userDutyCheckVerifyUserId{{--&&laborReport.amountOfJoined==1--}}"  class="text-success">已审核</b>
                                                     <b v-else-if="laborReport.userDutyCheckVerifyUserId&&laborReport.amountOfJoined&&laborReport.sequence"  class="text-black">&nbsp;&nbsp;&nbsp;@{{ laborReport.sequence }}/@{{ laborReport.amountOfJoined }}</b>
                                                     <span v-else class="text-center"><b class="text-danger">门卫未审核</b></span>
                                                 @endcan
@@ -330,24 +330,30 @@
                     let tokenOfBroadcastEnterAndLeave='{{$tokenOfBroadcastEnterAndLeave}}';
                     if(tokenOfBroadcastEnterAndLeave){
                         Echo.channel('{{$laravelEchoPrefix}}'+tokenOfBroadcastEnterAndLeave).listen('ImportEvent',(e)=>{
-                            let labor=e.laborReport;
-                            laborReports.push(labor);
+                            // let labor=e.laborReport;
+                            // laborReports.push(labor);
+                            setTimeout(function () {
+                                window.location.reload();
+                            },500);
                         });
                     }
                     //退场
                     Echo.channel('{{$laravelEchoPrefix}}laborReport').listen('ExportEvent',(e)=>{
-                        let labor=e.laborReport;
-                        laborReports.every(function (laborReport) {
-                            if (laborReport.id==labor.id){
-                                laborReport.thisRecordOnlineTime=labor.thisRecordOnlineTime;
-                                laborReport.thisRecordWorkingTime=labor.thisRecordWorkingTime;
-                                laborReport.is_export=labor.is_export;
-                                laborReport.exit_at=labor.exit_at;
-                                laborReport.remark=labor.remark;
-                                return false
-                            }
-                            return true;
-                        });
+                        setTimeout(function () {
+                            window.location.reload();
+                        },500);
+                        // let labor=e.laborReport;
+                        // laborReports.every(function (laborReport) {
+                        //     if (laborReport.id==labor.id){
+                        //         laborReport.thisRecordOnlineTime=labor.thisRecordOnlineTime;
+                        //         laborReport.thisRecordWorkingTime=labor.thisRecordWorkingTime;
+                        //         laborReport.is_export=labor.is_export;
+                        //         laborReport.exit_at=labor.exit_at;
+                        //         laborReport.remark=labor.remark;
+                        //         return false
+                        //     }
+                        //     return true;
+                        // });
                     });
                     //门卫审核
                     Echo.channel('{{$laravelEchoPrefix}}userDutyCheck').listen('GuardAuditEvent',(e)=>{