laralvel7.2 导出 导入 Matrix\Exception 3.1

composer require maatwebsite/excel

导出

php artisan make:export OrderExport

导入

php artisan make:import OrderExport

 控制器OrderContorller

namespace App\Http\Controllers\Admin;

use App\Exports\OrderExport;
use App\Http\Controllers\Controller;
use App\Imports\OrderImport;
use App\Model\Source\Order\Order;
use App\Model\Source\Order\OrderAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;
class OrderController extends BaseController
{
public function export(Request $request)
    {
        $request = $request->post();
        if ($request) {
            $status = $request['status'];
            $time = $request['created_at'];
            $time_start = '';
            $time_end = '';
            if ($time) {
                $time_arr = explode(' - ', $time);
                $time_start = $time_arr[0] . ' 00:00:00';
                $time_end = $time_arr[1] . ' 23:59:59';
            } else {
                $time_start = date('Y-m-d ') . ' 00:00:00';
                $time_end = date('Y-m-d ') . ' 23:59:59';
            }
            return $this->export_data($status,$time_start, $time_end);
        }
        $order_status_flag = DB::table('flag')->where('types', 4)->get(['name', 'values'])->toArray();
        //$sql = Order::with(['toAction', 'toGoods', 'toRefund', 'toAddress']);
        return view('admin.order.export', ['orderstatusflag' => $order_status_flag]);

    }

    private function export_data($status, $time_start,$time_end)
    {

        $row = [[
            "order_id" => '订单ID',
            "order_sn" => '订单编号',
            "nickname" => '收件人',
            "ship_phone" => '手机号',
            "ship_addr" => '收货地址',
            "ship_post" => '邮编',
            "payment_method" => '支付方式',
            "status" => '订单状态',
            "order_price_total" => '总金额',
            "order_price_sub" => '优惠',
            "order_express_price" => '快递费',
            "order_true_pay" => '订单实付金额',
            "desc" => '订单备注',
            "payment_at" => '支付时间',
            "created_at" => '创建时间',
            "express_name" => '快递名称',
            "express_number" => '运单号',
        ]];

        $list = [];


        $sql = Order::where('id', '>', 0)->with(['toAction', 'toGoods', 'toRefund', 'toAddress']);
        $sql = $sql->whereBetween('created_at',[$time_start,$time_end]);
        if(isset($status)){
            $sql = $sql->where('status', '=', $status);
        }
        $rowData = $sql->get();
        foreach ($rowData as $k => $v) {
            $list[$k]['order_id'] = $v['id'];
            $list[$k]['order_sn'] = $v['order_sn'];
            $list[$k]['nickname'] = $v->toAddress->ship_name;
            $list[$k]['ship_phone'] = $v->toAddress->ship_phone;
            $list[$k]['ship_addr'] = $v->toAddress->ship_addr;
            $list[$k]['ship_post'] = $v->toAddress->ship_post;
            $list[$k]['payment_method'] = $v['payment_method'];
            $list[$k]['status'] = $v['status'];
            $list[$k]['order_price_total'] = $v['order_price_total'];
            $list[$k]['order_price_sub'] = $v['order_price_sub'];
            $list[$k]['order_express_price'] = $v['order_express_price'];
            $list[$k]['order_true_pay'] = $v['order_true_pay'];
            $list[$k]['desc'] = $v->toAddress->desc;
            $list[$k]['payment_at'] = $v['payment_at'];
            $list[$k]['created_at'] = $v['created_at'];
            $list[$k]['express_name'] = '';
            $list[$k]['express_number'] = '';
        }
        return Excel::download(new OrderExport($row, $list), date('Y-m-d') . '-orders.xls');
    }

    public function import(Request $request)
    {
        $request = $request->post();
        if ($request) {
            $orders_excel = $request['orders_excel'];
            $orders_excel = strstr($orders_excel,'storage/');
            $res = Excel::import(new OrderImport,$orders_excel);
            if($res){
                $order_import_msg = session()->get('order_import');
                //dd(session()->all());
                if(isset($order_import_msg)){
                    if($order_import_msg->code == -1){
                    return back()->with('msg',$order_import_msg->msg);
                }
                }else{
                    return redirect('/admin/order')->with('msg','导入成功');
                }
            }

        }
        return view('admin.order.import');
    }

laralvel7.2 导出 导入 Matrix\Exception 3.1_第1张图片

导出 OrderExport 

row = $row;
        $this->data = $data;
    }

    public function collection()
    {
        $row = $this->row;
        $data = $this->data;

        //设置表头
        foreach ($row[0] as $key => $value) {
            $key_arr[] = $key;
        }

        //输入数据
        foreach ($data as $key => &$value) {
            $js = [];
            for ($i=0; $i < count($key_arr); $i++) {
                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
            }
            array_push($row, $js);
            unset($val);
        }
        return collect($row);
    }

}

导入 OrderImport 

createData($collection);
    }

    public function createData($rows)
    {
        Session::start();
        $obj = new \stdClass();
        //todo
        $rows = $rows->toArray();
        foreach ($rows as $k => $v) {
            try {
                DB::beginTransaction();
                $order = Order::find($v[0]);
                if ($order) {
                    $info['express_name'] = $v[15];
                    $info['express_number'] = $v[16];
                    if ($v[15] == null || $v[15] == '' || $v[16] == null || $v[16] == '') {
                        \Log::info('订单号:' . $v[0] . 'excel表格快递名和运单号不能为空');
                        $obj->code = -1;
                        $obj->msg = 'excel表格快递名和运单号不能为空';
                        session()->flash('order_import', $obj);
                        return false;
                    }
                    $res0 = $order->update(['status' => 3]);
                    if (!$res0) {
                        \Log::info('订单号:' . $v[0] . '无法写入');
                        DB::rollBack();
                        $obj->code = -1;
                        $obj->msg = '订单导入status异常';
                        session()->flash('order_import', $obj);
                        return false;
                    }
                    $res = $order->toAddress()->update($info);
                    if (!$res) {
                        \Log::info('订单号:' . $v[0] . '无法写入');
                        DB::rollBack();
                        $obj->code = -1;
                        $obj->msg = '订单导入异常';
                        session()->flash('order_import', $obj);
                        return false;
                    }
                    DB::commit();
                    return true;
                } else {
                    $obj->code = -1;
                    $obj->msg = '订单号:' . $v[0] . '不存在';
                    session()->flash('order_import', $obj);
                    \Log::info('订单号:' . $v[0] . '不存在');
                }


            } catch (Exception $e) {
                $obj->code = -1;
                $obj->msg = '批量导入发货订单异常:' . $e->getMessage();
                session()->flash('order_import', $obj);
                \Log::info('批量导入发货订单异常:' . $e->getMessage());
            }

        }


    }
}

 

laralvel7.2 导出 导入 Matrix\Exception 3.1_第2张图片

 

 

 

 

你可能感兴趣的:(laravel)