| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | <?phpnamespace App\Service;use App\Exports\ExportOrder;use App\Exports\MyExport;use App\Model\BasicType;use App\Model\Construction;use App\Model\ConstructionInfo;use App\Model\ConstructionProductInfo;use App\Model\Customer;use App\Model\Depart;use App\Model\Employee;use App\Model\InOutRecord;use App\Model\Inventory;use App\Model\InventorySub;use App\Model\InvoiceOrder;use App\Model\InvoiceOrderInfo;use App\Model\PaymentReceipt;use App\Model\PaymentReceiptInfo;use App\Model\Product;use App\Model\PurchaseOrder;use App\Model\PurchaseOrderInfo;use App\Model\PurchaseOrderSpecialInfo;use App\Model\ReturnExchangeOrder;use App\Model\ReturnExchangeOrderProductInfo;use App\Model\SalesOrder;use App\Model\SalesOrderOtherFee;use App\Model\SalesOrderProductInfo;use App\Model\Storehouse;use App\Model\Supplier;use App\Model\U8Job;use Illuminate\Support\Facades\DB;use Maatwebsite\Excel\Facades\Excel;class ExportFileService extends Service{    //导出文件    const type_one = 1;    const type_two = 2;    const type_three = 3;    const type_four = 4;    const type_five = 5;    const type_six = 6;    const type_seven = 7;    const type_eight = 8;    const type_nine = 9;    const type_ten = 10;    const type_eve = 11;    const type_twl = 12;    //导出文件方法    protected static $fuc = [        self::type_one => 'one',        self::type_two => 'two',        self::type_three => 'three',        self::type_four => 'four',        self::type_five => 'five',        self::type_six => 'six',        self::type_seven => 'seven',        self::type_eight => 'eight',        self::type_nine => 'nine',        self::type_ten => 'ten',        self::type_eve => 'eve',        self::type_twl => 'twl',    ];    protected static $fuc_name = [        self::type_one => '存货',        self::type_two => '派单合同',        self::type_three => '收付款单',        self::type_four => '施工单',        self::type_five => '采购单',        self::type_six => '退换货单',        self::type_seven => '发货单',        self::type_eight => '进销存报表',        self::type_nine => '虚拟采购单',        self::type_ten => '客户',        self::type_eve => '现存量',        self::type_twl => '库存台账',    ];    public static $filename = "";    //导出的方式 0 选择导出的数据 1 查询后 导出指定的数据(最多每次一千条)    public static $export_type = [        0,        1    ];    public function exportAll($data,$user){        if(empty($data['type']) || ! isset(self::$fuc[$data['type']])) return [false,'导出文件类型错误或者不存在'];        self::$filename = self::$fuc_name[$data['type']] ?? "";        $export_type = $data['export_type'] ?? 0;        if(! isset(self::$export_type[$export_type])) return [false,'导出文件方式错误或者不存在'];        if(empty($export_type)){            if(empty($data['id'])) return [false,'请选择导出数据'];        }else{            if(empty($data['order_search'])) return [false,'搜索条件不能为空'];            $search = $data['order_search'];            if(empty($search['page_index'])) return [false,'请选择导出数据的开始页码'];            if(empty($search['page_size'])) return [false,'请选择导出数据的条数'];            if($search['page_size'] > 5000) return [false,'请选择导出数据的条数每次最多5000条'];            $id = $this->getListForSearch($data, $user);            $data['id'] = $id;        }        $function = self::$fuc[$data['type']];        $return = $this->$function($data,$user);        return [true, $return];    }    public function one($ergs,$user){        $id = $ergs['id'];        // 导出数据        $return = [];        DB::table('product')            ->whereIn('id', $id)            ->select(Product::$field)            ->orderBy('id','desc')            ->chunkById(500,function ($data) use(&$return){                $data = Collect($data)->map(function ($object) {                    return (array)$object;                })->toArray();                foreach ($data as $value) {                    $return[] = [                        0 => $value['code'],                        1 => $value['title'],                        2 => $value['size'],                        3 => $value['category'],                        4 => $value['unit'],                        5 => $value['cost'],                        6 => $value['business_cost'],                        7 => $value['major_client_settlement_price'],                        8 => $value['write_off_price'],                        9 => $value['return_change_price'],                        10 => $value['mark'],                    ];                }            });        $header = ['存货编码','存货名称','规格类型','所属类别','计量单位','成本单价','业务成本单价','大客户结算单价','核销单价','退换货损耗单价','备注'];        return $this->saveExportData($return,$header);    }    public function getListForSearch($ergs, $user){        $data = $ergs['order_search'];        $id = [];        if($ergs['type'] == self::type_one){            $service = new ProductService();            $model = $service->productCommon($data, $user, ['id']);            $return = $this->limitData($model,'',$data);            $id = array_column($return,'id');        }        return $id;    }    public function saveExportData($data, $headers, $type = 'default',$file_name = ''){        if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);        $filename =  $file_name . '.' . 'xlsx';        $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);        return $filename;    }}
 |