| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- /**
- * @Desc:导出
- * @Author: hua
- * @DateTime: 2021/10/11 10:56
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- namespace common\helpers\csv;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\RabbitMqEnum;
- use common\helpers\TaskHelper;
- use common\helpers\UploadHelper;
- use common\models\common\Download;
- use common\traits\ErrorTrait;
- use Exception;
- use PhpAmqpLib\Message\AMQPMessage;
- use Yii;
- class CsvExportHelper
- {
- use ErrorTrait;
- /**
- * 导出数据到下载中心
- * @Author bing
- * @DateTime 2021-10-23 16:22:58
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return boolean
- */
- public static function exportDownLoadCenter($mall_id,$filename,$type,$handler_class,$method,$params = [], $mch_id = 0, $store_id = 0){
- try{
- $name = $filename;
- $model = Download::setData(compact('mall_id','mch_id','store_id','name','type','handler_class','method','params'));
- $queue_data['download_id'] = $model->id;
- $queue_data['mall_id'] = $mall_id;
- if($model === false) throw new Exception(Download::getStaticError());
- // // 添加异步队列
- TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_TASK,RabbitMqEnum::TASK_QUEUE,$queue_data,$handler_class,$method,function(AMQPMessage $message){
- // 生产错误
- Yii::error('发送异步任务失败');
- });
- return true;
- }catch(Exception $e){
- Yii::error('添加任务到下载中心失败:'.$e->getMessage());
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 生产数据文件
- * @Author bing
- * @DateTime 2021-10-23 16:22:58
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return boolean
- */
- public static function prodDownloadFile($download_id,$head,$data,&$csv_obj){
- try{
- if($csv_obj instanceof CsvTool){
- $csv = $csv_obj;
- $csv->data = $data;
- }else{
- $download = Download::findOne($download_id);
- $csv = new CsvTool();
- $csv->filename = $download->name.$download_id;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($download->type ?? '');
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- empty($csv->header) && $csv->header = $head;
- $csv->data = $data;
- $csv_obj = $csv;
- }
- $csv->export();
- }catch(Exception $e){
- Yii::error('下载文件失败:'.$e->getMessage());
- return false;
- }
- }
- /**
- * 导出订单数据到下载中心(使用长队列查询)
- * @param $mall_id
- * @param $filename
- * @param $type
- * @param $handler_class
- * @param $method
- * @param $params
- * @param $mch_id
- * @param $store_id
- * @return bool
- */
- public static function exportDownLoadOrder($mall_id,$filename,$type,$handler_class,$method,$params = [], $mch_id = 0, $store_id = 0){
- try{
- $name = $filename;
- $model = Download::setData(compact('mall_id','mch_id','store_id','name','type','handler_class','method','params'));
- $queue_data['download_id'] = $model->id;
- $queue_data['mall_id'] = $mall_id;
- if($model === false) throw new Exception(Download::getStaticError());
- // 添加异步队列
- TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_LONG_ORDER,RabbitMqEnum::LONG_ORDER_QUEUE,$queue_data,$handler_class,$method,function(AMQPMessage $message){
- // 生产错误
- Yii::error('发送异步任务失败');
- });
- return true;
- }catch(Exception $e){
- Yii::error('添加任务到下载中心失败:'.$e->getMessage());
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|