ExportFileService.php 6.5 KB

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