diff --git a/application/admin/controller/score/Employee.php b/application/admin/controller/score/Employee.php index cc99687..b2134ad 100644 --- a/application/admin/controller/score/Employee.php +++ b/application/admin/controller/score/Employee.php @@ -63,31 +63,66 @@ class Employee extends Backend //得到查询值 $value = $opertime['value']; list($start_time,$end_time) = explode(" - ",$value); - } - $list = $this->model +// var_dump($sort); + // 判断是否按 score_count 排序,该字段为PHP计算值无法直接数据库排序 + $sortByScoreCount = ($sort === 'employee.score_count'); +// var_dump($sortByScoreCount); + if ($sortByScoreCount) { + + // 按积分统计排序:需取全量数据后在PHP层排序再手动分页 + $list = $this->model + ->with(['user','team']) + ->where($where) + ->select(); + + foreach ($list as $row) { + $row->getRelation('user')->visible(['nickname','mobile','avatar']); + $row->getRelation('team')->visible(['name']); + } + + $list = collection($list)->toArray(); + + // 为每条记录计算 score_count + foreach ($list as &$row) { + $row['score_count'] = \app\common\model\score\Employee::getPriceByCoachAndTime($row['id'], $start_time, $end_time); + $row['opertime'] = $start_time ? $start_time.'至'.$end_time : ''; + } + unset($row); + + // PHP层按 score_count 排序 + usort($list, function($a, $b) use ($order) { + if (strtolower($order) === 'desc') { + return $b['score_count'] <=> $a['score_count']; + } + return $a['score_count'] <=> $b['score_count']; + }); + + $total = count($list); + // 手动分页 + $list = array_slice($list, $offset, $limit); + + } else { + // 常规排序:直接数据库分页 + $list = $this->model ->with(['user','team']) ->where($where) ->order($sort, $order) ->paginate($limit); - foreach ($list as $row) { - - $row->getRelation('user')->visible(['nickname','mobile','avatar']); - $row->getRelation('team')->visible(['name']); - } - - $total = $list->total(); - $list = $list->items(); - - foreach ($list as &$row) { - $row['score_count'] = 0; - $row['opertime'] = ''; - if($start_time){ - $row['opertime'] = $start_time.'至'.$end_time; + foreach ($list as $row) { + $row->getRelation('user')->visible(['nickname','mobile','avatar']); + $row->getRelation('team')->visible(['name']); } - $row['score_count'] = \app\common\model\score\Employee::getPriceByCoachAndTime($row['id'],$start_time,$end_time); + $total = $list->total(); + $list = $list->items(); + + foreach ($list as &$row) { + $row['score_count'] = \app\common\model\score\Employee::getPriceByCoachAndTime($row['id'], $start_time, $end_time); + $row['opertime'] = $start_time ? $start_time.'至'.$end_time : ''; + } + unset($row); } $result = array("total" => $total, "rows" => $list); diff --git a/public/assets/js/backend/score/employee.js b/public/assets/js/backend/score/employee.js index 2d4a493..1d40e3c 100644 --- a/public/assets/js/backend/score/employee.js +++ b/public/assets/js/backend/score/employee.js @@ -22,6 +22,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'id', + pageSize: 30, fixedColumns: true, fixedRightNumber: 1, columns: [ @@ -36,7 +37,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin {field: 'opertime', title: __('统计时间查询'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,formatter:function (value, row, index) { return row.opertime ? row.opertime : '未查询'; }}, - {field: 'score_count', title: __('积分统计(根据具体统计时间筛选)'),formatter:function (value, row, index) { + {field: 'score_count', title: __('积分统计(根据具体统计时间筛选)'), sortable: true, formatter:function (value, row, index) { // if( !row.opertime){ // return "#请在点开【搜索栏】后选择【统计时间查询】提交查询#"; // }