<?php
declare (strict_types = 1);

namespace app\model\api;

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

/**
 * @mixin \think\Model
 */
class Order extends BaseModel
{
    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = 'id desc'){

        $data =  $this
            ->field("o.*,pf.name as platfrom_name,p.title as pro_title,p.img as pro_img"
                .",(select count(*) from table_order_info where o_id = o.id " .
                (empty($where['shop_id']) ? '' : (' AND shop_id='.$where['shop_id']) )
                .  ") as order_count"
            )
            ->alias("o")
            ->join('platform pf',"pf.id=o.platform_id")
            ->when(!empty($where),function ($query) use($where){
                //分站
                if(!empty($where['sassid'])) {
                    $query->where('o.sassid',$where['sassid']);
                }
                //店铺信息
                if(!empty($where['shop_id'])) {
                    $query->whereRaw("(SELECT count(*) from table_order_info where o_id = o.id AND shop_id =:shop_id) > 0",
                        ['shop_id'=>$where['shop_id']]);
                }

                //用户
                if(!empty($where['uid'])) {
                    $query->where('o.uid',$where['uid']);
                }
                //搜索订单
                if(!empty($where['order_id'])) {
                    $query->whereLike('o.order_id',"%{$where['order_id']}%");
                }
                //搜索平台
                if(!empty($where['platform_id'])) {
                    $query->whereLike('o.platform_id',$where['platform_id']);
                }
                //搜索备注信息
                if(!empty($where['mono'])) {
                    $query->whereLike('o.order_id',"%{$where['mono']}%");
                }
                //查询日期
                if(!empty($where['data']) && !empty($where['data'][0]) && !empty($where['data'][1])) {
                    $query->whereTime('o.time','between',$where['data']);
                }
                //订单类型
                if(is_numeric($where['orderType'])) {
                    $query->where('o.status',$where['orderType']);
                }
                //timeType[今日，昨日，最近7天，最近30天]
                if(!empty($where['timeType'])) {

                    if($where['timeType'] == 'day') {
                        $query->whereDay('o.time');
                    }
                    if($where['timeType'] == 'yesterday') {
                        $query->whereDay('o.time','yesterday');
                    }
                    if($where['timeType'] == 'last7') {
                        $query->whereTime('o.time','-7 day');
                    }
                    if($where['timeType'] == 'last30') {
                        $query->whereTime('o.time','-30 day');
                    }
                }
            })
            ->leftJoin('product p',"p.id = o.pro_id")
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        //echo $this->getLastSql();
        return [$data['total'],$data['data']];
    }




}
