ExportFileService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\ExportOrder;
  4. use App\Exports\MyExport;
  5. use App\Model\Area;
  6. use App\Model\BasicMaterial;
  7. use App\Model\BasicRollFilm;
  8. use App\Model\BasicType;
  9. use App\Model\CarDepart;
  10. use App\Model\CarFiles;
  11. use App\Model\CarType;
  12. use App\Model\Company;
  13. use App\Model\Construction;
  14. use App\Model\ConstructionInfo;
  15. use App\Model\ConstructionOrder;
  16. use App\Model\ConstructionOrderImg;
  17. use App\Model\ConstructionOrderSub;
  18. use App\Model\ConstructionProductInfo;
  19. use App\Model\Employee;
  20. use App\Model\FoursShop;
  21. use App\Model\InOutRecord;
  22. use App\Model\Inventory;
  23. use App\Model\InventorySub;
  24. use App\Model\Material;
  25. use App\Model\MaterialCharge;
  26. use App\Model\MaterialChargeSub;
  27. use App\Model\MaterialOrder;
  28. use App\Model\MaterialOrderApply;
  29. use App\Model\MaterialOrderIn;
  30. use App\Model\MaterialOrderSend;
  31. use App\Model\MaterialOrderSendSub;
  32. use App\Model\MaterialReturn;
  33. use App\Model\MaterialReturnSub;
  34. use App\Model\PaymentReceipt;
  35. use App\Model\PaymentReceiptInfo;
  36. use App\Model\PurchaseOrder;
  37. use App\Model\PurchaseOrderInfo;
  38. use App\Model\RollFilm;
  39. use App\Model\RollFilmCombine;
  40. use App\Model\RollFilmCompany;
  41. use App\Model\RollFilmInventory;
  42. use App\Model\SalesOrder;
  43. use App\Model\SalesOrderOtherFee;
  44. use App\Model\SalesOrderProductInfo;
  45. use App\Model\Storehouse;
  46. use App\Model\Supplier;
  47. use App\Model\Transfer;
  48. use App\Model\TransferSub;
  49. use Illuminate\Support\Facades\DB;
  50. use Maatwebsite\Excel\Facades\Excel;
  51. class ExportFileService extends Service
  52. {
  53. //导出文件
  54. const type_one = 1;
  55. const type_two = 2;
  56. const type_three = 3;
  57. const type_four = 4;
  58. const type_five = 5;
  59. const type_six = 6;
  60. const type_seven = 7;
  61. //导出文件方法
  62. protected static $fuc = [
  63. self::type_one => 'kqList',
  64. self::type_two => '',
  65. self::type_three => '',
  66. self::type_four => '',
  67. self::type_five => '',
  68. ];
  69. protected static $fuc_name = [
  70. self::type_one => '考勤',
  71. self::type_two => '',
  72. self::type_three => '',
  73. self::type_four => '',
  74. self::type_five => '',
  75. ];
  76. public static $filename = "";
  77. public function exportAll($data,$user){
  78. if(empty($data['id'])) return [false,'请选择导出数据'];
  79. if(empty($data['type']) || ! isset(self::$fuc[$data['type']])) return [false,'导出文件类型错误或者不存在'];
  80. self::$filename = self::$fuc_name[$data['type']] ?? "";
  81. //不超时
  82. ini_set('max_execution_time', 0);
  83. //内存设置
  84. ini_set('memory_limit', -1);
  85. $function = self::$fuc[$data['type']];
  86. $return = $this->$function($data);
  87. return [true, $return];
  88. }
  89. public function kqList($ergs){
  90. $id = $ergs['id'];
  91. // 导出数据
  92. $return = [];
  93. DB::table('kq_list')
  94. ->whereIn('id', $id)
  95. ->select('id','order_number','model_type','sales_order_type','sign_time','plat_order','plat_type','product_total','other_fee','discount_fee','contract_fee','crt_time','crt_id','state','invoice_state','pay_way')
  96. ->orderBy('id','desc')
  97. ->chunk(500,function ($data) use(&$return){
  98. $data = Collect($data)->map(function ($object) {
  99. return (array)$object;
  100. })->toArray();
  101. $list['data'] = $data;
  102. });
  103. $header = ['合同单号'];
  104. return $this->saveExportData($return,$header);
  105. }
  106. public function saveExportData($data, $headers, $type = 'default',$file_name = ''){
  107. if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
  108. $filename = $file_name . '.' . 'xlsx';
  109. $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);
  110. return $filename;
  111. }
  112. }