CsvExportHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @Desc:导出
  4. * @Author: hua
  5. * @DateTime: 2021/10/11 10:56
  6. * @Copyright: copyright (c) 2021 广东七件事集团
  7. */
  8. namespace common\helpers\csv;
  9. use common\components\export\CsvTool;
  10. use common\enums\DownloadEnum;
  11. use common\enums\RabbitMqEnum;
  12. use common\helpers\TaskHelper;
  13. use common\helpers\UploadHelper;
  14. use common\models\common\Download;
  15. use common\traits\ErrorTrait;
  16. use Exception;
  17. use PhpAmqpLib\Message\AMQPMessage;
  18. use Yii;
  19. class CsvExportHelper
  20. {
  21. use ErrorTrait;
  22. /**
  23. * 导出数据到下载中心
  24. * @Author bing
  25. * @DateTime 2021-10-23 16:22:58
  26. * @copyright: Copyright (c) 2020 广东七件事集团
  27. * @return boolean
  28. */
  29. public static function exportDownLoadCenter($mall_id,$filename,$type,$handler_class,$method,$params = [], $mch_id = 0, $store_id = 0){
  30. try{
  31. $name = $filename;
  32. $model = Download::setData(compact('mall_id','mch_id','store_id','name','type','handler_class','method','params'));
  33. $queue_data['download_id'] = $model->id;
  34. $queue_data['mall_id'] = $mall_id;
  35. if($model === false) throw new Exception(Download::getStaticError());
  36. // // 添加异步队列
  37. TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_TASK,RabbitMqEnum::TASK_QUEUE,$queue_data,$handler_class,$method,function(AMQPMessage $message){
  38. // 生产错误
  39. Yii::error('发送异步任务失败');
  40. });
  41. return true;
  42. }catch(Exception $e){
  43. Yii::error('添加任务到下载中心失败:'.$e->getMessage());
  44. self::setStaticError($e->getMessage());
  45. return false;
  46. }
  47. }
  48. /**
  49. * 生产数据文件
  50. * @Author bing
  51. * @DateTime 2021-10-23 16:22:58
  52. * @copyright: Copyright (c) 2020 广东七件事集团
  53. * @return boolean
  54. */
  55. public static function prodDownloadFile($download_id,$head,$data,&$csv_obj){
  56. try{
  57. if($csv_obj instanceof CsvTool){
  58. $csv = $csv_obj;
  59. $csv->data = $data;
  60. }else{
  61. $download = Download::findOne($download_id);
  62. $csv = new CsvTool();
  63. $csv->filename = $download->name.$download_id;
  64. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($download->type ?? '');
  65. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  66. empty($csv->header) && $csv->header = $head;
  67. $csv->data = $data;
  68. $csv_obj = $csv;
  69. }
  70. $csv->export();
  71. }catch(Exception $e){
  72. Yii::error('下载文件失败:'.$e->getMessage());
  73. return false;
  74. }
  75. }
  76. /**
  77. * 导出订单数据到下载中心(使用长队列查询)
  78. * @param $mall_id
  79. * @param $filename
  80. * @param $type
  81. * @param $handler_class
  82. * @param $method
  83. * @param $params
  84. * @param $mch_id
  85. * @param $store_id
  86. * @return bool
  87. */
  88. public static function exportDownLoadOrder($mall_id,$filename,$type,$handler_class,$method,$params = [], $mch_id = 0, $store_id = 0){
  89. try{
  90. $name = $filename;
  91. $model = Download::setData(compact('mall_id','mch_id','store_id','name','type','handler_class','method','params'));
  92. $queue_data['download_id'] = $model->id;
  93. $queue_data['mall_id'] = $mall_id;
  94. if($model === false) throw new Exception(Download::getStaticError());
  95. // 添加异步队列
  96. TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_LONG_ORDER,RabbitMqEnum::LONG_ORDER_QUEUE,$queue_data,$handler_class,$method,function(AMQPMessage $message){
  97. // 生产错误
  98. Yii::error('发送异步任务失败');
  99. });
  100. return true;
  101. }catch(Exception $e){
  102. Yii::error('添加任务到下载中心失败:'.$e->getMessage());
  103. self::setStaticError($e->getMessage());
  104. return false;
  105. }
  106. }
  107. }