<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;

use app\BaseController;
use app\model\api\MemberDetail;
use app\model\admin\Order as OrderModel;
use app\model\admin\OrderInfo;
use app\model\admin\OrderInfoAsw;
use app\model\admin\OrderInfoDd;
use app\model\admin\OrderInfoMono;
use app\Request;
use library\services\UtilService;
use library\utils\Region;
use think\db\exception\DbException;
use think\Exception;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Order extends  BaseController
{
    /**
     * 获取订单数据
     * @param Request $request
     */
    public function getOrderList(Request $request){
        $pageSize = 20;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['order_id',''],
                ['mono',''],
                ['platform_id',''],
                ['warehouse_id',''],
                ['data',[]],
                ['orderType',''],
                ['timeType',''],
                ['uid','']
            ],$request
        );
        $where = $post;
        $where['sassid'] = $request->site['sassid'];
        $order = new OrderModel;
        list($pageCount,$data) = $order->getList($post['page'],$where,$pageSize,'id desc');
        $result = UtilService::getParam([
            "id",
            ['time','time','date("Y-m-d H:i:s",$1)'],
            "order_id",
            "mobile",
            "avatar",
            "order_count",
            "all_price",
            "status",
            "platfrom_name",
            "ck_name",
            "exp_name",
            "site_name",
            "pro_title",
            "send_count",
            "uid",
            "pro_img",
            "mono",
            "err_msg"
            ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }


    public function getOrderPushList(Request $request) {
        $pageSize = 20;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['order_id',''],
                ['mono',''],
                ['platform_id',''],
                ['warehouse_id',''],
                ['data',[]],
                ['orderType',''],
                ['timeType','']
            ],$request
        );
        $where = $post;
        $where['sassid'] = $request->site['sassid'];
        $order = new OrderModel;
        list($pageCount,$data) = $order->getPushList($post['page'],$where,$pageSize,'id desc');
        $result = UtilService::getParam([
            "id",
            ['time','time','date("Y-m-d H:i:s",$1)'],
            "order_id",
            "mobile",
            "avatar",
            "order_count",
            "all_price",
            "status",
            "platfrom_name",
            "ck_name",
            "exp_name",
            "site_name",
            "pro_title",
            "push_count",
            "pro_img",
            "is_push",
            "pro_img",
            "mono",
            "err_msg"
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 导出数据
     * @param Request $request
     */
    public function orderDown(Request $request) {
        $post = UtilService::getMore([['id','','empty','参数错误'],],$request);
        $where = [];
        $idsAr = explode(',',$post['id'].'');
        $where['id'] = $idsAr;
        $where['sassid'] = $request->site['sassid'];
        $data = (new OrderInfo)->getAllList($where,'id desc');
        $tAr = [];
        foreach ($data as $v) {
            $d = [];
            $d['name']          = $v['name'];
            $d['mobile']        = $v['mobile'];
            $d['out_order_id']  = $v['out_order_id'];
            $d['in_order_id']   = $v['in_order_id'];
            $d['address']       = $v['address'];
            $d['exp_name']      = $v['exp_name'];
            $d['exp_number']    = $v['exp_number'];
            $d['pro_name']      = $v['pro_name'];
            $d['pro_code']      = $v['csno'];
            $d['pro_price']     = $v['pro_price'];
            $d['platform_name'] = $v['platform_name'];
            $d['time']     =date('Y-m-d H:i:s',$v['time']);
            $d['count']    = 1;
            $d['money'] = $v['money'];
            $d['exp_price'] = $v['exp_price'];
            $tAr[] = $d;
        }
        return app('json')->success($tAr);
    }
    
    /**
     * 子订单数据
     * @param Request $request
     */
    public function orderSubItem(Request $request) {
        $pageSize = 20;
        $post =  UtilService::getMore(
            [
                ['id',''],
                ['page',1],
                ['order_id',''],
                ['mobile',''],
                ['name','']
            ],$request
        );
        $where = $post;
        $where['id']     = $post['id'];
        $where['sassid'] = $request->site['sassid'];
        list($pageCount,$data) = (new OrderInfo)->getList($post['page'],$where,$pageSize,"*","id desc");
        $result = UtilService::getParam([
            "id",
            "exp_name",
            "order_id",
            "out_order_id",
            "name",
            "mobile",
            "address",
            "is_push",
            "mono",
            "time",
            "send_time",
            "exp_number",
            "msg_err",
            "money",
            "status",
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }
    
    /**
     * 售后列表
     * @param Request $request
     */
    public function orderMonoList(Request $request) {
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['timeType',''],
                ['aswStatus','']
            ],$request
        );
        $tx = new OrderInfoAsw();
        $where = $post;
        $where['sassid'] = $request->site['sassid'];
        list($pageCount,$data) = $tx->getList($post['page'],$where,$pageSize,"*");
        $result = UtilService::getParam([
            "id",
            "o_id",
            "mono",
            "status",
            "cl_time",
            "cl_admin_time",
            "out_order_id",
            "name",
            "mobile",
            "address",
            "time",
            "send_time",
            "exp_number",
            'msg_mono',
            'msg_img',
            'admin_id',
            "exp_name"
        ],$data);
        foreach ($result as $k=>$v){
            $result[$k]['msg_img_ar'] =  empty($v["msg_img"])?[]:explode(",",$v["msg_img"]);
        }
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }
    /**
     * 订单撤销数据
     * @param Request $request
     */
    public function orderMonoCancel(Request $request){
        $post =  UtilService::getMore(
            [
                ['id',"","empty","参数错误"],
                ['mono',""]
            ],$request
        );
        (new OrderInfoAsw)->where('id',$post['id'])->delete();
        return app('json')->success('撤销成功');
    }
    public function orderMonoHandle(Request $request){
        $post =  UtilService::getMore(
            [
                ['id',"","empty","参数错误"],
                ['msg_mono',""],
                ['msg_img',""],
            ],$request
        );
        $post["admin_time"]=NOW_TIME;
        $post["status"]=1;
        $post["admin_id"]=$request->adminInfo["id"];
        $id = $post['id'];
        unset($post['id']);
        (new OrderInfoAsw)->where('id',$id)->save($post);
        return app('json')->success('处理成功');
    }
    
    
    /**
     * 底单申请列表
     * @param Request $request
     */
    public function orderDdList(Request $request) {
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['timeType',''],
                ['aswStatus','']
            ],$request
        );
        $tx = new OrderInfoDd();
        $where = $post;
        $where['sassid'] = $request->site['sassid'];
        list($pageCount,$data) = $tx->getList($post['page'],$where,$pageSize,"*");
        $result = UtilService::getParam([
            "id",
            "o_id",
            "mono",
            "status",
            "cl_time",
            "cl_admin_time",
            "out_order_id",
            "name",
            "mobile",
            "address",
            "time",
            "send_time",
            "exp_number",
            'msg_mono',
            'msg_img',
            'admin_id',
            "exp_name"
        ],$data);
        foreach ($result as $k=>$v){
            $result[$k]['msg_img_ar'] =  empty($v["msg_img"])?[]:explode(",",$v["msg_img"]);
        }
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }
}