thinkPHP5 多条件自定义排序

public function lists($filter){

        $join = [
            ['__USER__ au', 'e.up_user_id = au.id'],
            ['train_carbon uc','e.up_carbon_id = uc.id'],
            ['train_line l','l.id=e.line_id'],
            ['train_subway s','s.id=e.subway_id'],
            ['train_position p','p.id=e.position_id'],
        ];

        $field = 'e.*,au.user_nickname as up_user_nickname,uc.sign as up_carbon_sign,uc.ini_ply,l.name as line_name,s.name as subway_name,p.name as position_name';

        $reason_id = empty($filter['reason_id']) ? 0 : intval($filter['reason_id']);

        if (!empty($reason_id)) {
            array_push($join, [
                'train_down d', 'd.id = e.down_reason_id'
            ]);
            $field .= 'd.name as reason_name';
        }

        if(!empty($filter['down_start_time']) || !empty($filter['down_end_time'])){
            array_push($join, [
                'train_carbon dc', 'dc.id = e.down_carbon_id'
            ]);
            $field .= 'dc.sign as down_carbon_sign';
        }

        $positions =implode(',',Db::name('position')->column('id'));
        $lines = implode(',',Db::name('subway')->column('id'));
        $exp =new \think\db\Expression('field(e.subway_id,'.$lines.'),field(e.position_id,'.$positions.')');


        $enters        = Db::name('enter')
            ->comment('查询录入列表数据源')
            ->alias('e')
            ->field($field)
            ->join($join)
            ->where('e.status',1)
            ->where('e.on_time', '>=', 0)
            ->where(function (Query $query) use ($filter) {
                $line_id = empty($filter['line_id']) ? '' : intval($filter['line_id']);
                if (!empty($line_id)) {
                    $query->where('e.line_id', $line_id);
                }

                $subway_id = empty($filter['subway_id']) ? '' : intval($filter['subway_id']);
                if (!empty($subway_id)) {
                    $query->where('e.subway_id', $subway_id);
                }

                $position_id = empty($filter['position_id']) ? '' : intval($filter['position_id']);
                if (!empty($position_id )) {
                    $query->where('e.position_id', $position_id );
                }

                $user_id = empty($filter['user_id']) ? '' : intval($filter['user_id']);
                if (!empty($user_id )) {
                    $query->where('e.up_user_id',$user_id );
                }


                $reason_id = empty($filter['reason_id']) ? 0 : intval($filter['reason_id']);
                if (!empty($reason_id)) {
                    $query->where('e.down_reason_id', $reason_id);
                }

                $upstartTime = empty($filter['up_start_time']) ? 0 : strtotime($filter['up_start_time']);
                $upendTime   = empty($filter['up_end_time']) ? 0 : strtotime($filter['up_end_time']);
                if (!empty($upstartTime)) {
                    $query->where('e.on_time', '>=', $upstartTime);
                }
                if (!empty($upendTime)) {
                    $query->where('e.on_time', '<=', $upendTime);
                }

                $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
                if (!empty($keyword)) {
                    $query->where('uc.sign', 'like', "%$keyword%");
                }
                $status = empty($filter['status']) ? '' : $filter['status'];
                if ($status ==  2) {
                    $query->where('e.status', 'eq', 2);
                }elseif ($status ==  1){
                    $query->where('e.status', 'eq', 1);
                }else{
                    $query->where('e.status', 'in', '1,2');
                }
                if(!empty($filter['down_start_time']) || !empty($filter['down_end_time'])){
                    $downstartTime = empty($filter['down_start_time']) ? 0 : strtotime($filter['down_start_time']);
                    $downendTime   = empty($filter['down_end_time']) ? 0 : strtotime($filter['down_end_time']);
                    if (!empty($downstartTime)) {
                        $query->where('e.time', '>=', $downstartTime);
                    }
                    if (!empty($downendTime)) {
                        $query->where('e.time', '<=', $downendTime);
                    }
                }
            })
            ->order($exp)
            ->paginate(10)->each(function($item){
                if(!empty($item['down_reason_id'])){
                    $item['reason_name'] = Db::name('down')->where('id',$item['down_reason_id'])->value('name');
                    $item['down_carbon_sign'] = Db::name('carbon')->where('id',$item['down_carbon_id'])->value('sign');
                }else{
                    $item['reason_name'] = null;
                    $item['down_carbon_sign'] = null;
                }
                if(!empty($item['down_user_id'])){
                    $item['down_user_nickname'] = Db::name('user')->where('id',$item['down_user_id'])->value('user_nickname');
                }else{
                    $item['down_user_nickname'] = null;
                }
                $item['on_time'] = date('Y-m-d',$item['on_time']);
                if (empty($item['time'])){
                    $item['time'] = null;
                }else{
                    $item['time'] = date('Y-m-d',$item['time']);
                }
                if (empty($item['down_length'])){
                    $item['down_length'] = null;
                }
                if (empty($item['down_ply'])){
                    $item['down_ply'] = null;
                }
                return $item;
            });
        return $enters;
    }

自定义排序为:如当前有N个人 每个人手里都有一个苹果,苹果颜色分为红黄蓝三色,我需要在所有人排列时红黄蓝为一组且不影响数据的多少、若这一集合缺少某一个颜色的苹果,还是属于上个集合。

上述:(列车为一个一条数据的第一子项 每个列车对应N多个安装工位,我需要安装工位的位置为一个集合排序。)tp5定义了解释性条件语句(think\db\Expression)可做到如下排序。

如下图所示内容:


QQ截图20190320104532.png

上述核心代码:

 $positions =implode(',',Db::name('position')->column('id'));
$lines = implode(',',Db::name('subway')->column('id'));
$exp =new \think\db\Expression('field(e.subway_id,'.$lines.'),field(e.position_id,'.$positions.')');

你可能感兴趣的:(thinkPHP5 多条件自定义排序)