<?php
declare (strict_types = 1);

namespace app\model\system;

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 store_name from table_product where id = of.pro_id) as pro_title")
            ->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['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,".
                "p.title as pro_name,p.csno"
            )
            ->alias("of")
            ->when(!empty($where),function ($query) use($where){

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

                if(!empty($where['id'])) {
                    $query->whereIn('of.o_id',$where['id']);
                }
            })
            ->leftJoin('product p','p.id=of.pro_id')
            ->order($desc)
            ->select()
            ->toArray();
        return $data;
    }



    /**
     * 未揽件订单
     * @param array $where
     * @param string $desc
     */
    public function getWarningList($where = [],$page,$pageCount,$desc = 'id desc'){
        $data =  $this
            ->field("of.*,(SELECT title from table_express where id = of.exp_id) as exp_name,".
                "o.order_id,o.pay_time,w.name as warehouse_name,".
                "(select name from table_platform where id = of.platform_id) as platform_name,".
                "u.mobile as user_mobile,u.avatar as user_avatar"
            )
            ->alias("of")
            ->when(!empty($where),function ($query) use($where){

                if(!empty($where['order_id'])) {
                    $query->whereLike('o.order_id', $where['order_id']);
                }

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

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

                //查询日期
                if(!empty($where['data']) && !empty($where['data'][0]) && !empty($where['data'][1])) {
                    $query->whereTime('of.time','between',$where['data']);
                }

                if($where['orderType'] == 1) {
                    $query->whereDay('of.send_time','yesterday');
                }

                if($where['orderType'] == 2) {
                    $time = strtotime(date('Y-m-d', strtotime('-2 day')));
                    $query->whereDay('of.send_time',date('Y-m-d',$time));
                }

                if($where['orderType'] == 3) {
                    $time = strtotime(date('Y-m-d', strtotime('-2 day')));
                    $query->where('of.send_time','<=',$time);
                }
            })
            ->where('of.status', 2)
            ->where('of.exp_status',0)
            ->where('of.send_time', '>', strtotime(date('Y-m-d', strtotime('-3 day'))))
            ->where('of.send_time', '<', strtotime(date('Y-m-d')))
            ->where('of.warehouse_id','>',16)
            ->leftJoin('order o','o.id = of.o_id')
            ->leftJoin('warehouse w','w.id = of.warehouse_id')
            ->leftJoin('member u','u.uid=of.uid')
            ->order("of.send_time desc")
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        return [$data['total'],$data['data']];
    }

}
