ExportFileService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\ExportOrder;
  4. use App\Exports\MyExport;
  5. use App\Model\BasicType;
  6. use App\Model\Construction;
  7. use App\Model\ConstructionInfo;
  8. use App\Model\ConstructionProductInfo;
  9. use App\Model\Customer;
  10. use App\Model\Depart;
  11. use App\Model\Employee;
  12. use App\Model\InOutRecord;
  13. use App\Model\Inventory;
  14. use App\Model\InventorySub;
  15. use App\Model\InvoiceOrder;
  16. use App\Model\InvoiceOrderInfo;
  17. use App\Model\PaymentReceipt;
  18. use App\Model\PaymentReceiptInfo;
  19. use App\Model\Product;
  20. use App\Model\PurchaseOrder;
  21. use App\Model\PurchaseOrderInfo;
  22. use App\Model\PurchaseOrderSpecialInfo;
  23. use App\Model\ReturnExchangeOrder;
  24. use App\Model\ReturnExchangeOrderProductInfo;
  25. use App\Model\SalesOrder;
  26. use App\Model\SalesOrderOtherFee;
  27. use App\Model\SalesOrderProductInfo;
  28. use App\Model\Storehouse;
  29. use App\Model\Supplier;
  30. use App\Model\U8Job;
  31. use Illuminate\Support\Facades\DB;
  32. use Maatwebsite\Excel\Facades\Excel;
  33. class ExportFileService extends Service
  34. {
  35. //导出文件
  36. const type_one = 1;
  37. const type_two = 2;
  38. const type_three = 3;
  39. const type_four = 4;
  40. const type_five = 5;
  41. const type_six = 6;
  42. const type_seven = 7;
  43. const type_eight = 8;
  44. const type_nine = 9;
  45. const type_ten = 10;
  46. const type_eve = 11;
  47. const type_twl = 12;
  48. //导出文件方法
  49. protected static $fuc = [
  50. self::type_one => 'one',
  51. self::type_two => 'two',
  52. self::type_three => 'three',
  53. self::type_four => 'four',
  54. self::type_five => 'five',
  55. self::type_six => 'six',
  56. self::type_seven => 'seven',
  57. self::type_eight => 'eight',
  58. self::type_nine => 'nine',
  59. self::type_ten => 'ten',
  60. self::type_eve => 'eve',
  61. self::type_twl => 'twl',
  62. ];
  63. protected static $fuc_name = [
  64. self::type_one => '存货',
  65. self::type_two => '派单合同',
  66. self::type_three => '收付款单',
  67. self::type_four => '施工单',
  68. self::type_five => '采购单',
  69. self::type_six => '退换货单',
  70. self::type_seven => '发货单',
  71. self::type_eight => '进销存报表',
  72. self::type_nine => '虚拟采购单',
  73. self::type_ten => '客户',
  74. self::type_eve => '现存量',
  75. self::type_twl => '库存台账',
  76. ];
  77. public static $filename = "";
  78. //导出的方式 0 选择导出的数据 1 查询后 导出指定的数据(最多每次一千条)
  79. public static $export_type = [
  80. 0,
  81. 1
  82. ];
  83. public function exportAll($data,$user){
  84. if(empty($data['type']) || ! isset(self::$fuc[$data['type']])) return [false,'导出文件类型错误或者不存在'];
  85. self::$filename = self::$fuc_name[$data['type']] ?? "";
  86. $export_type = $data['export_type'] ?? 0;
  87. if(! isset(self::$export_type[$export_type])) return [false,'导出文件方式错误或者不存在'];
  88. if(empty($export_type)){
  89. if(empty($data['id'])) return [false,'请选择导出数据'];
  90. }else{
  91. if(empty($data['order_search'])) return [false,'搜索条件不能为空'];
  92. $search = $data['order_search'];
  93. if(empty($search['page_index'])) return [false,'请选择导出数据的开始页码'];
  94. if(empty($search['page_size'])) return [false,'请选择导出数据的条数'];
  95. if($search['page_size'] > 5000) return [false,'请选择导出数据的条数每次最多5000条'];
  96. $id = $this->getListForSearch($data, $user);
  97. $data['id'] = $id;
  98. }
  99. $function = self::$fuc[$data['type']];
  100. $return = $this->$function($data,$user);
  101. return [true, $return];
  102. }
  103. public function one($ergs,$user){
  104. $id = $ergs['id'];
  105. // 导出数据
  106. $return = [];
  107. DB::table('product')
  108. ->whereIn('id', $id)
  109. ->select(Product::$field)
  110. ->orderBy('id','desc')
  111. ->chunkById(500,function ($data) use(&$return){
  112. $data = Collect($data)->map(function ($object) {
  113. return (array)$object;
  114. })->toArray();
  115. foreach ($data as $value) {
  116. $return[] = [
  117. 0 => $value['code'],
  118. 1 => $value['title'],
  119. 2 => $value['size'],
  120. 3 => $value['category'],
  121. 4 => $value['unit'],
  122. 5 => $value['cost'],
  123. 6 => $value['business_cost'],
  124. 7 => $value['major_client_settlement_price'],
  125. 8 => $value['write_off_price'],
  126. 9 => $value['return_change_price'],
  127. 10 => $value['mark'],
  128. ];
  129. }
  130. });
  131. $header = ['存货编码','存货名称','规格类型','所属类别','计量单位','成本单价','业务成本单价','大客户结算单价','核销单价','退换货损耗单价','备注'];
  132. return $this->saveExportData($return,$header);
  133. }
  134. public function getListForSearch($ergs, $user){
  135. $data = $ergs['order_search'];
  136. $id = [];
  137. if($ergs['type'] == self::type_one){
  138. $service = new ProductService();
  139. $model = $service->productCommon($data, $user, ['id']);
  140. $return = $this->limitData($model,'',$data);
  141. $id = array_column($return,'id');
  142. }
  143. return $id;
  144. }
  145. public function saveExportData($data, $headers, $type = 'default',$file_name = ''){
  146. if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
  147. $filename = $file_name . '.' . 'xlsx';
  148. $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);
  149. return $filename;
  150. }
  151. }