<?php
declare (strict_types = 1);

namespace app\model\system;

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,st.name as site_name,p.title as pro_title,p.img as pro_img,
                m.mobile,m.avatar,
                (select count(*) from table_order_info where o_id = o.id) as order_count,
                (select count(*) from table_order_info where o_id = o.id and status = 2) as send_count,
                (SELECT title from table_express where id = o.express_id) as exp_name,
                (SELECT name from table_warehouse where id = o.warehouse_id) as  ck_name
                "
            )
            ->alias("o")
            ->join('platform pf',"pf.id=o.platform_id")
            ->join('member m','m.uid=o.uid')
            ->when(!empty($where),function ($query) use($where){
                //分站
                if(!empty($where['sassid'])) {
                    $query->where('o.sassid',$where['sassid']);
                }
                //用户
                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['warehouse_id'])) {
                    $query->whereLike('o.warehouse_id',$where['warehouse_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']);
                } else {
                    $query->where('o.status','>',0);
                }
                //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")
            ->leftJoin('site st',"st.sassid=o.sassid")
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
//        echo $this->getLastSql();exit;
        return [$data['total'],$data['data']];
    }


    /**
     * 获取推送数据
     * @param $page
     * @param array $where
     * @param int $pageCount
     * @param string $filed
     * @param string $desc
     * @return array
     */
    public function getPushList($page,$where = [],$pageCount = 20,$filed = '*',$desc = 'id desc'){
        $data =  $this
            ->field("o.*,pf.name as platfrom_name,st.name as site_name,p.title as pro_title,p.img as pro_img,
                m.mobile,m.avatar,
                (select count(*) from table_order_info where o_id = o.id) as order_count,
                (select count(*) from table_order_info where o_id = o.id and is_push = 1) as push_count,
                (SELECT title from table_express where id = o.express_id) as exp_name,
                (SELECT name from table_warehouse where id = o.warehouse_id) as  ck_name
                "
            )
            ->alias("o")
            ->join('platform pf',"pf.id=o.platform_id")
            ->join('member m','m.uid=o.uid')
            ->when(!empty($where),function ($query) use($where){
                //分站
                if(!empty($where['sassid'])) {
                    $query->where('o.sassid',$where['sassid']);
                }
                //用户
                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['warehouse_id'])) {
                    $query->whereLike('o.warehouse_id',$where['warehouse_id']);
                }
                //查询日期
                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']);
                } else {
                    $query->where('o.status','>',0);
                }
            })
            ->leftJoin('product p',"p.id = o.pro_id")
            ->leftJoin('site st',"st.sassid=o.sassid")
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
//        echo $this->getLastSql();exit;
        return [$data['total'],$data['data']];
    }


}
