shopX数据csv导出

这次的需求有个文件导出,我整了好几天,有点晕晕乎乎的,记录下来,下次就方便了。在shopX中文件导出的类型是csv类型,走的是redis队列,刚开始导出数据一直不对应,发现在测试站上配置文件有点问题。

  • 定时任务的配置
crontab -e

照这下面的搬一个

* * * * *  sudo -E -u www php /data/httpd/www/ylf-espier-bloated/artisan schedule:run >> /var/log/espier-cront.log 2>&1
* * * * *  sudo -E -u www php /data/httpd/www/jhfx-espier-bloated/artisan schedule:run >> /var/log/espier-cront.log 2>&1
* * * * *  sudo -E -u www php /data/httpd/www/gn-espier-bloated/artisan schedule:run >> /var/log/espier-cront.log 2>&1
* * * * *  sudo -E -u www php /data/httpd/www/ysg-espier-bloated/artisan schedule:run >> /var/log/espier-cront.log 2>&1
  • 配置队列服务
vim /etc/supervisord.d/super-queue.ini

照这下面的搬一个,记住下面的名字要改

[program:ysg-bloated-queue-default]
command=/data/httpd/www/ysg-espier-bloated/artisan queue:work --queue=default --delay=3 --memory=128 --timeout=30 --sleep=1 --tries=3
stdout_logfile=/data/httpd/www/ysg-espier-bloated/storage/logs/supervisor-bloated-queue-default.log
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
numprocs=1
user=www
startsecs=3
startretries=100000

[program:ysg-bloated-queue-slow]
command=/data/httpd/www/ysg-espier-bloated/artisan queue:work --queue=slow --delay=3 --memory=128 --timeout=1800 --sleep=1 --tries=3
stdout_logfile=/data/httpd/www/ysg-espier-bloated/storage/logs/supervisor-bloated-queue-slow.log
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
redirect_stderr=true
autostart=true
autorestart=true
numprocs=1
user=www
startsecs=3
startretries=100000

[program:ysg-bloated-queue-sms]
command=/data/httpd/www/ysg-espier-bloated/artisan queue:work --queue=sms --delay=3 --memory=128 --timeout=1800 --sleep=1 --tries=3
stdout_logfile=/data/httpd/www/ysg-espier-bloated/storage/logs/supervisor-bloated-queue-sms.log
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
redirect_stderr=true
autostart=true
autorestart=true
numprocs=1
user=www
startsecs=3
TX_STORE_URL=https://bd.automall.qq.com/apiastartretries=100000
  • 配置websocket
vim /etc/supervisord.d/super-websocket.ini

继续搬,记得改名

[program:ysg-websocket]
command=/data/httpd/www/ysg-espier-bloated/artisan websocket:start
stdout_logfile=/data/httpd/www/ysg-espier-bloated/storage/logs/supervisor-websocket.log
redirect_stderr=true
autostart=true
autorestart=true
numprocs=1
user=www
startsecs=3
startretries=100000
  • 启动服务
###启动队列任务, websocket服务
systemctl start supervisord.service

###重启队列任务, websocket服务
###每次修改队列都需要重启一下服务,要不然不生效
systemctl restart supervisord.service
  • 路由
//会员电子表单信息导出csv
$api->post('/members/csv/export', ['name'=>'导出csv文件','middleware'=>'activated', 'as'=>'member.csv.export','uses'=>'ExportData@exportCsvFile']);
  • 路由对应的方法

    /**
     * /members/csv/export
     * 会员电子表单信息csv导出
     * type:post
     */
    public function exportCsvFile(Request $request)
    {
        $type = 'input';
        $postdata = $request->all();
        $authdata = app('auth')->user()->get();
        if (!$authdata) {
        }
        $companyId = $authdata['company_id'];
        $postdata['distributor_id'] = $request->get('distributor_id', 0);
        $filter = $this->dataFilter($postdata, $authdata);
        if (!$filter) {
        }
        $memberService = new MemberService();
        $count = $memberService->getMemberCount($filter);
        //存储导出操作账号者
        $operator_id = app('auth')->user()->get('operator_id');

        // if ($count < 500) {
            $gotoJob = (new ExportFileJob($type, $companyId, $filter, $operator_id))->onQueue('slow');
            app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($gotoJob);
            $result['status'] = true;
            return response()->json($result);
        // } else {
        //     $exportService = $this->getService($type);
        //     $result = $exportService->exportData($filter);
        //     return response()->json($result);
        // }
    }

注意!注意!注意!

一定要重新加载一下队列,要不然怎么都不会生效

###随便在那个目录下,执行一下
systemctl restart supervisord.service

###查看当前的队列服务
supervisorctl status
  • 创建导出文件的队列
    文件路径
/Users/mymac/data/httpd/www/ysg/espier-bloated/src/EspierBundle/Services/Export/MemberInputExportService.php
<?php
namespace EspierBundle\Services\Export;

use EspierBundle\Interfaces\ExportFileInterface;
use MembersBundle\Services\MemberRegSettingService;
use MembersBundle\Services\MemberService;
use MembersBundle\Services\MembersInputService;
use EspierBundle\Services\ExportFileService;
use MembersBundle\Traits\MemberSearchFilter;
use KaquanBundle\Services\MemberCardService;
use KaquanBundle\Services\VipGradeOrderService;
use DistributionBundle\Services\DistributorUserService;

class MemberInputExportService implements ExportFileInterface
{
    use MemberSearchFilter;
    public function exportData($filter)
    {
        $fileName = date('YmdHis')."input";
        $memberList = $this->getLists($filter);
        $exportService = new ExportFileService();
        $result = $exportService->exportCsv($fileName,$this->title,$memberList);
        app('log')->info(var_export($result,true));
        return $result;
    }
    private  $title = [
            'id'=> '序号',
            'username'=> '姓名',
            'sex'=> ' 性别',
            'age'=> '年龄',
            'address'=> '地址',
            'message'=> '信息',
            'message_1'=> '信息一',
            'message_2'=> '信息二',
            'message_3'=> '信息三',
            'wx_name'=> '微信名称',
            'create'=> '创建时间',
        ];

    private function getLists($filter)
    {
        $title = $this->title;
        $memberData = [];
        $MembersInputService = new MembersInputService();
        $getInput = $MembersInputService->getInfo(['company_id'=>$filter['company_id']]);
        foreach ($getInput['list'] as $key => $value) {
                ###这个地方数据类型已经格式要和这个一样
                $memberData[$key]['i']['id'] = $value['id'];
                $memberData[$key]['i']['username'] = $value['username'];
                $memberData[$key]['i']['sex'] = $value['sex'];
                $memberData[$key]['i']['age'] = $value['age'];
                $memberData[$key]['i']['address'] = $value['address'];
                $memberData[$key]['i']['message'] = $value['message'];
                $memberData[$key]['i']['message_1'] = $value['message_1'];
                $memberData[$key]['i']['message_2'] = $value['message_2'];
                $memberData[$key]['i']['message_3'] = $value['message_3'];
                $memberData[$key]['i']['wx_name'] = $value['wx_name'];
                $memberData[$key]['i']['create'] = date( 'Y-m-d H:i:s' ,$value['create']);
            }
        return $memberData;
    }
}
  • 增加刚刚设置的导出类型
    文件路径
/Users/mymac/data/httpd/www/ysg/espier-bloated/src/EspierBundle/Traits
<?php
namespace EspierBundle\Traits;

use EspierBundle\Services\Export\CashWithdrawalExportService;
use EspierBundle\Services\Export\InvoicesExportService;
use EspierBundle\Services\Export\Items;
use Exception;
use EspierBundle\Services\Export\MemberExportService;
###这个地方需要引入自己创建的文件队列
use EspierBundle\Services\Export\MemberInputExportService;
use EspierBundle\Services\Export\ServiceOrderExportService;
use EspierBundle\Services\Export\NormalOrderExportService;
use EspierBundle\Services\Export\RightExportService;
use EspierBundle\Services\Export\TradeExportService;
use EspierBundle\Services\Export\RightConsumeExportService;
use EspierBundle\Services\Export\NormalMasterOrderExportService;
use EspierBundle\Services\Export\RegistrationRecordExportService;
use EspierBundle\Services\Export\TaskBrokerageExportService;
use EspierBundle\Services\Export\AftersalesRecordExportService;
use EspierBundle\Services\Export\RefundRecordExportService;

trait GetExportServiceTraits
{
    public function getService($exportType)
    {
        $exportType = strtolower($exportType);
        switch ($exportType) {
            ###这个地方是我增加的导出类型
            case 'input':
                $exportService = new MemberInputExportService();
                break;
            case 'member':
                $exportService = new MemberExportService();
                break;
            case 'service_order':
                $exportService = new ServiceOrderExportService();
                break;
            case 'normal_order':
                $exportService = new NormalOrderExportService();
                break;
            case 'right':
                $exportService = new RightExportService();
                break;
            case 'right_consume':
                $exportService = new RightConsumeExportService();
                break;
            case 'tradedata':
                $exportService = new TradeExportService();
                break;
            case 'normal_master_order':
                $exportService = new NormalMasterOrderExportService();
                break;
            case 'community_withdraw':
                $exportService = new CashWithdrawalExportService();
                break;
            case 'selform_registration_record':
                $exportService = new RegistrationRecordExportService();
                break;
            case 'task_brokerage_count':
                $exportService = new TaskBrokerageExportService();
                break;
            case 'aftersale_record_count':
                $exportService = new AftersalesRecordExportService();
                break;
            case 'refund_record_count':
                $exportService = new RefundRecordExportService();
                break;
            case 'items':
                $exportService = new Items();
                break;
            case 'invoice':
                $exportService = new InvoicesExportService();
                break;
            default:
                throw new Exception("无此导出类型");
        }
        return $exportService;
    }
}

ishow.bind-time.com Copyright © 2020 小肥青王二狗

添加新评论

有种脾气叫,不放弃。

梦想是注定孤独的旅行,路上少不了质疑和嘲笑,但那又怎样,哪怕遍体鳞伤也要活的漂亮。

不管现在有多么艰辛,我们也要做个生活的舞者。

命运从来不会同情弱者。

不怕万人阻挡在前方,只怕自己先行投降。