ExportFileService.php 5.0 KB

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