<?php
declare (strict_types = 1);

namespace app\model\api;

use library\basic\BaseModel;
use think\Model;

/**
 * @mixin \think\Model
 */
class OrderInfo extends BaseModel
{


    /**
     * 订单查询
     * @param $id
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItem($id) {
        $data =  $this
            ->field("of.*,(SELECT title from table_express where id = of.exp_id) as exp_name,".
                "(select order_id from table_order where id = of.o_id) as order_id,".
                "p.title as pro_title,p.img as pro_img"
            )
            ->alias("of")
            ->join('product p',"p.id = of.pro_id")
            ->where('of.id',$id)
            ->find();
        return $data;
    }


    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = 'id desc'){
        $data =  $this
            ->field("of.*,(SELECT title from table_express where id = of.exp_id) as exp_name,".
                "(select order_id from table_order where id = of.o_id) as order_id,".
                "(select name from table_member_shop where id = of.shop_id) as shop_name"
            )
            ->alias("of")
            ->when(!empty($where),function ($query) use($where){
                if(!empty($where['sassid'])) {
                    $query->where('of.sassid',$where['sassid']);
                }
                if(!empty($where['id'])) {
                    $query->where('of.o_id',$where['id']);
                }
                if(!empty($where['shop_id'])) {
                    $query->where('of.shop_id',$where['shop_id']);
                }
                if(!empty($where['uid'])) {
                    $query->where('of.uid',$where['uid']);
                }
                if(!empty($where['name'])) {
                    $query->whereLike('of.name',"%{$where['name']}%");
                }

                if(!empty($where['mobile'])) {
                    $query->whereLike('of.mobile',"%{$where['mobile']}%");
                }

                if(!empty($where['order_id'])) {
                    $query->whereLike('of.out_order_id',"%{$where['order_id']}%");
                }
            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        //echo $this->getLastSql();
        return [$data['total'],$data['data']];
    }

    /**
     * 导出某一项全部
     * @param array $where
     * @param string $desc
     */
    public function getAllList($where = [],$desc = 'id desc'){
        $data =  $this
            ->field("of.*,(SELECT title from table_express where id = of.exp_id) as exp_name,".
                "(select order_id from table_order where id = of.o_id) as order_id,".
                "(select name from table_platform where id = of.platform_id) as platform_name,".
                "(select title from table_product where id = of.pro_id) as pro_name,".
                "(select name from table_member_shop where id = of.shop_id) as shop_name"
            )
            ->alias("of")
            ->when(!empty($where),function ($query) use($where){

                if(!empty($where['sassid'])) {
                    $query->where('of.sassid',$where['sassid']);
                }

                if(!empty($where['id'])) {
                    $query->where('of.o_id',$where['id']);
                }

                if(!empty($where['shop_id'])) {
                    $query->where('of.shop_id',$where['shop_id']);
                }
            })
            ->order($desc)
            ->select()
            ->toArray();
        return $data;
    }

}
