Tp3.2 组合查询

// $where['status'] = 6;
// // or
// $where['final_price'] = ['gt', 0];
// $where['status'] = 4;

$where['_string'] = ' (status = 6)  OR ( status = 4 AND final_price > 0) ';

用string最好实现了!

请欣赏优美的代码!

/**
 * 获取订单列表
 */
public function getOrderList()
{
    if (!$type = $_POST['type']) { // 1 待上门 2 待报价 3 待确认 4 待服务 5 待支付 6 已完成 7 已取消
        $this->json->E('缺少参数');
    }
    if (!in_array($type, [1, 2, 3, 4, 5, 6, 7])) {
        $this->json->E('类型数值有误');
    }

    if (!isset($_POST['page']) || $_POST['page'] < 1) {
        $_POST['page'] = 1;
    }

    if (!isset($_POST['page_size']) || $_POST['page_size'] < 1) {
        $_POST['page_size'] = C('PAGE_NORMAL_COUNT');
    }

    $where['master_uid'] = $this->uid;

    switch ($type) {
        case 1:
            $where['status'] = 3;
            break;
        case 2:
            $where['final_price'] = 0; // 报价服务
            $where['status'] = 4;
            break;
        case 3:
            $where['status'] = 5;
            break;
        case 4:
            // $where['status'] = 6;
            // // or
            // $where['final_price'] = ['gt', 0];
            // $where['status'] = 4;

            $where['_string'] = ' (status = 6)  OR ( status = 4 AND final_price > 0) ';
            break;
        case 5:
            $where['status'] = 7;
            break;
        case 6:
            $where['status'] = 8;
            break;
        case 7:
            $where['status'] = ['lt',0];
            break;
    }

    $page = $_POST['page'];
    $page_size = $_POST['page_size'];

    // 获取总数
    $service_order = M('service_order');
    $count = $service_order->where($where)->count();
    $total_page = ceil($count / $page_size);

    if ($page > $total_page) {
        $this->json->setErr(0, '没有更多了');
        $return_data = ['data_list' => [], 'total_page' => $total_page, 'current_page' => $page];
        $this->json->setAttr('data', $return_data);
        $this->json->Send();
    }

    $data_list = $service_order->where($where)
        ->order('id desc')
        ->limit((($page - 1) * $page_size) . ',' . $page_size)
        ->select();

    $serviceService = new ServiceService();
    if ($data_list) {
        // 获取服务信息
        $service = M('service');
        $service_type = M('service_type');
        $tmp_list = [];
        foreach ($data_list as $k => &$v) {
            $tmp = [];
            // 订单id、订单号
            $tmp['id'] = $v['id'];
            $tmp['order_num'] = $v['order_num'];

            // 地址信息
            $tmp['address'] = $serviceService->getAddressFromJson($v['address_json']);

            // 上门时间
            $tmp['visit_date'] = $serviceService->getVisitDate($v['order_visit_time']);
            $tmp['visit_section'] = $v['order_visit_section'];

            // 预计佣金
            if ($type != 7) {
                $tmp['commission_price'] = $serviceService->getCommissionFromOrder($v);
            }
            
            // 类别
            $service_info = $service->where(['id' => $v['service_id']])->find();
            $tmp['type_name'] = $service_type->where(['id' => $service_info['type_id']])->getField('name');

            // 下单时间
            $tmp['first_pay_time'] = date('Y-m-d H:i', $v['first_pay_time']);

            $tmp['is_quote'] = $v['final_price'] == 0 ? 1 : 0;

            $tmp['status'] = $v['status'];
            $tmp['status_str'] = $serviceService->getOrderStatus($v['status'], $tmp['is_quote']);

            $tmp_list[] = $tmp;
        }
    }

    $out_data = $tmp_list ?: [];
    $return_data = ['data_list' => $out_data, 'total_page' => $total_page, 'current_page' => $page];
    $this->json->S($return_data);
}
posted @ 2020-03-26 18:40  TBHacker  阅读(264)  评论(0编辑  收藏  举报