Commit 8a4dcdad by mushishixian

导出

parent 2d25db7d
......@@ -4,6 +4,7 @@
namespace App\Http\Services;
//后台用户相关信息服务
use App\Http\Transformers\SupplierContactTransformer;
use App\Http\Transformers\SupplierTransformer;
use App\Model\BrandModel;
use App\Model\IntracodeModel;
......@@ -12,266 +13,107 @@ use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\UserInfoModel;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
class DataService
{
// //修复数据
// public function repairData($limit = 0)
// {
// //先找出和无效采购员(离职或者不是采购员)相关的供应商
// //写死
// $invalidChannelUserName = [
// "葛金菊",
// "姜喆",
// "罗晓民",
// "周亚琳",
// "左佳晨",
// "陈泽彬",
// "李跃",
// "赵佩旋",
// "朱国军",
// "许金荣",
// "余佳兰",
// "李晶晶",
// "朱玉嘉"
// ];
// //找出对应的code_id
// $userModel = new UserInfoModel();
// $invalidChannelUserIds = $userModel->whereIn('name', $invalidChannelUserName)->pluck('userId');
// //找出对应的codeId
// $adminUserService = new AdminUserService();
// $codeIds = $adminUserService->getCodeIdsByUserIds($invalidChannelUserIds);
// $codeIds = $codeIds->toArray();
// $likeSqlRaw = implode('|', $codeIds);
// $model = new SupplierChannelModel();
// //根据code_id去找供应商表里面找到包含该code_id的供应商
// if (!empty($limit)) {
// $suppliers = $model->whereRaw("channel_uid REGEXP '$likeSqlRaw'")->where('is_type',
// 0)->limit($limit)->get();
// } else {
// $suppliers = $model->whereRaw("channel_uid REGEXP '$likeSqlRaw'")->where('is_type', 0)->get();
// }
// if ($suppliers) {
// $suppliers = $suppliers->toArray();
// } else {
//
// echo "已经处理完";
// die;
// }
// $i = 0;
// foreach ($suppliers as $supplier) {
// $channelUidResult = [];
// $channelUid = explode(',', $supplier['channel_uid']);
// foreach ($channelUid as $key => $uid) {
// //如果不属于不合理采购员codeId,则保留
// if (!in_array($uid, $codeIds)) {
// $channelUidResult[] = $uid;
// }
// }
// $channelUidResult = implode(',', $channelUidResult);
// $content = "采购员从" . implode(',', $channelUid) . '修改到' . $channelUidResult;
// //写日志
// $remark = implode(',', $channelUid) . "|" . $channelUidResult;
// $logService = new LogService();
// $logService->AddLog($supplier['supplier_id'], 1, "系统修改采购员", $content, $remark);
// $model->where('supplier_id', $supplier['supplier_id'])->update([
// 'channel_uid' => $channelUidResult,
// ]);
// $i++;
// if (!empty($limit)) {
// print_r($supplier['supplier_id'] . "<br>");
// }
// }
// echo "修改了${i}条记录";
// die;
// }
//
// //补全联系人
// public function completeContact($limit = 0)
// {
// $i = 0;
// //先找出联系人和采购员数量对不上的记录;
// $model = new SupplierChannelModel();
// if (!empty($limit)) {
// $suppliers = $model->where('is_type', 0)->limit($limit)->get()->toArray();
// } else {
// $suppliers = $model->where('is_type', 0)->get()->toArray();
// }
// $contactModel = new SupplierContactModel();
// foreach ($suppliers as $supplier) {
// //找出联系人数量
// if (empty($supplier['channel_uid'])) {
// continue;
// }
// $contactCount = $contactModel->where('supplier_id', $supplier['supplier_id'])->where('can_check_uids', '!=',
// '')->count();
// $channelUidCount = count(explode(',', $supplier['channel_uid']));
// if ($contactCount >= $channelUidCount) {
// continue;
// }
// //直接根据采购员id
//// $needAppendCount = $channelUidCount - $count;
//// $countArray = array_fill(0, $needAppendCount, 1);
// $countArray = array_fill(0, $channelUidCount, 1);
// //补的时候,记得把采购员id写进去
// $channelUids = explode(',', $supplier['channel_uid']);
// foreach ($countArray as $key => $value) {
// $channelUserId = $channelUids[$key];
// $data = [
// 'supplier_id' => $supplier['supplier_id'],
// 'can_check_uids' => $channelUserId,
// 'add_time' => time(),
// 'admin_id' => 1000,
// ];
// $contactModel->insert($data);
// }
// $i++;
// if (!empty($limit)) {
// print_r($supplier['supplier_id'] . "<br>");
// }
//// dd($supplier['supplier_id']);
// }
// echo "已经处理" . $i . "家供应商的联系方式";
// }
//
// //导出不合理的采购员的供应商
// public function exportNotValidChannelUid()
// {
// $supplierModel = new SupplierChannelModel();
// $list = $supplierModel->where('is_type', 0)->whereRaw('length(channel_uid) > 5')->get();
// $result = [];
// foreach ($list as $key => $item) {
// $channelUid = explode(',', $item['channel_uid']);
// $adminService = new AdminUserService();
// $names = [];
// foreach ($channelUid as $codeId) {
// $user = $adminService->getAdminUserInfoByCodeId($codeId);
// $name = array_get($user, 'name');
// $names[] = $name;
// }
// $names = implode('|', $names);
// $result[] = [
// 'name' => '"' . $item['supplier_name'] . '"',
// 'value' => $names,
// 'code' => $item['supplier_code']
// ];
// }
//
// // 需要导出的内容
//
// // 文件名,这里都要将utf-8编码转为gbk,要不可能出现乱码现象
// $filename = $this->utfToGbk('导出csv文件.csv');
//
// // 拼接文件信息,这里注意两点
// // 1、字段与字段之间用逗号分隔开
// // 2、行与行之间需要换行符
// $fileData = $this->utfToGbk('编码, 姓名, 采购') . "\n";
// foreach ($result as $value) {
// $temp = $value['code'] . ',' . $value['name'] . ',' .
// $value['value'];
// $fileData .= $this->utfToGbk($temp) . "\n";
// }
//
// // 头信息设置
// header("Content-type:text/csv");
// header("Content-Disposition:attachment;filename=" . $filename);
// header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
// header('Expires:0');
// header('Pragma:public');
// echo $fileData;
// exit;
// }
//
// function utfToGbk($data)
// {
// return iconv('utf-8', 'GBK', $data);
// }
//供应商导出给业务整理,还要带有是否有上架sku数量的标识
//供应商导出给业务整理
public function exportSuppliers()
{
$model = new SupplierChannelModel();
$suppliers = $model->where('is_type', 0)->get();
if (empty($suppliers)) {
return '供应商空';
}
$redis = new RedisModel();
foreach ($suppliers as $supplier) {
if ($redis->hget('supplier_data_service', $supplier['supplier_id'])) {
continue;
}
var_dump($supplier['supplier_id']);
//先判断是否有sku
$url = env('ES_SKU_URL', '');
$map['canal/condition'] = $supplier['supplier_code'];
$return = curl($url, $map, 1);
$data = json_decode($return, true);
if (!empty($data['data']['total'])) {
$total = $data['data']['total'];
} else {
$redis->hset('supplier_data_service', $supplier['supplier_id'], '1');
continue;
}
$service = new AdminUserService();
$intracodeModel = new IntracodeModel();
$userCodes = $intracodeModel->getSampleEncode();
$channelUids = explode(',', $supplier['channel_uid']);
$channelUids = array_filter($channelUids, function ($codeId) {
return !empty($codeId);
Excel::create('供应商资料导出', function ($excel) {
$excel->sheet('基本资料', function ($sheet) {
$sheet->setAutoSize(true);
$excelHeader = [
'供应商编码',
'供应商名称',
'所在区域(欧美,国内,日韩,港台,其他)',
'法人代表',
'公司税号',
'公司性质(代理商,方案商IDH/IDM,原厂,分销商平台,EMS/OEM/ODM)',
'合作类型(联营,自营,代购,寄售,云芯) 可以多个,逗号隔开',
'渠道开发员',
'采购员',
'注册地址',
];
$model = new SupplierChannelModel();
$suppliers = $model->where('is_type', 0)->get();
if (empty($suppliers)) {
return '供应商空';
}
$suppliers = $suppliers->toArray();
$excelData = [];
$transformer = new SupplierTransformer();
$suppliers = $transformer->transformList($suppliers);
foreach ($suppliers as $supplier) {
$excelData[] = [
$supplier['supplier_code'],
$supplier['supplier_name'],
$supplier['region_name'],
$supplier['legal_representative'],
$supplier['tax_number'],
$supplier['supplier_group'],
str_replace(" | ",',',$supplier['stockup_type']),
trim($supplier['channel_username'],','),
$supplier['purchase_username'] ?: '',
$supplier['supplier_address'],
];
}
array_unshift($excelData, $excelHeader);
$sheet->fromArray($excelData);
});
$channelNames = array_map(function ($codeId) use ($userCodes) {
return array_get($userCodes, $codeId, '');
}, $channelUids);
$channelNames = implode('|', $channelNames);
$userInfo = $service->getAdminUserInfoByCodeId($supplier['purchase_uid']);
$purchaseName = $userInfo['name'] ?: '';
$redis->hset('supplier_data_service', $supplier['supplier_id'], json_encode([
$supplier['supplier_code'],
'"' . $supplier['supplier_name'] . '"',
$channelNames,
$purchaseName,
!empty($supplier['create_time']) ? '"' . date('Y-m-d H:i:s', $supplier['create_time']) . '"' : '',
$total
]));
}
})->export('xlsx');
}
function export()
//联系人数据导出
public function exportSupplierContact()
{
$redis = new RedisModel();
$exportData = $redis->hgetall('supplier_data_service');
$exportData = array_filter($exportData, function ($data) {
return $data != 1;
});
$exportData = array_map(function ($data) {
return json_decode($data);
}, $exportData);
// 文件名,这里都要将utf-8编码转为gbk,要不可能出现乱码现象
$filename = $this->utfToGbk('导出csv文件.csv');
// 拼接文件信息,这里注意两点
// 1、字段与字段之间用逗号分隔开
// 2、行与行之间需要换行符
$fileData = $this->utfToGbk('供应商编码,供应商名称,采购员名称,渠道开发员,创建时间,sku数量') . "\n";
foreach ($exportData as $value) {
$temp = $value[0] . ',' . $value[1] . ',' . $value[2] . ',' . $value[3] . ',' . $value[4] . ',' . $value[5];
$fileData .= $this->utfToGbk($temp) . "\n";
}
// 头信息设置
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=" . $filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $fileData;
exit;
}
function utfToGbk($data)
{
return iconv('utf-8', 'GBK', $data);
Excel::create('供应商联系人导出', function ($excel) {
$excel->sheet('联系人', function ($sheet) {
$sheet->setAutoSize(true);
$excelHeader = [
'绑定ID',
'供应商编码',
'供应商名称',
'采购员',
'职位',
'邮箱',
'手机',
'座机',
'QQ',
'传真',
];
$model = new SupplierContactModel();
$contacts = $model->get();
if (empty($contacts)) {
return '联系人数据为空';
}
$contacts = $contacts->toArray();
$excelData = [];
$transformer = new SupplierContactTransformer();
$contacts = $transformer->transformList($contacts);
foreach ($contacts as $contact) {
$supplierModel = new SupplierChannelModel();
$supplier = $supplierModel->where('supplier_id', $contact['supplier_id'])->first();
if (!$supplier) {
continue;
}
$supplier = $supplier->toArray();
$excelData[] = [
$contact['contact_id'],
$supplier['supplier_code'],
$supplier['supplier_name'],
$contact['channel_name'],
$contact['supplier_position'],
$contact['supplier_email'],
$contact['supplier_mobile'],
$contact['supplier_telephone'],
$contact['supplier_qq'],
$contact['supplier_fax'],
];
}
array_unshift($excelData, $excelHeader);
$sheet->fromArray($excelData);
});
})->export('xlsx');
}
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ use App\Model\LogModel;
use App\Model\RedisModel;
use App\Model\SupplierContactModel;
use App\Model\SupplierExtendModel;
use function Sodium\add;
class SupplierTransformer
{
......@@ -30,6 +29,7 @@ class SupplierTransformer
$supplier['viewed'] = array_get($viewData, $supplier['supplier_id'], false);
$supplier = $this->getStockupType($supplier);
$supplier['supplier_group'] = array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置');
$supplier['region_name'] = array_get(config('fixed.Region'), $supplier['region']);
$supplier['create_time'] = $supplier['create_time']?date('Y-m-d H:i:s', $supplier['create_time']):'';
$supplier['audit_time'] = $supplier['audit_time'] ? date('Y-m-d H:i:s', $supplier['audit_time']) : '';
$supplier['update_time'] = $supplier['update_time'] ? date('Y-m-d H:i:s', $supplier['update_time']) : '';
......
......@@ -45,6 +45,7 @@ Route::group(['middleware' => ['web'], 'namespace' => 'Api'], function () {
Route::match(['get', 'post'], '/test', function () {
$service = new \App\Http\Services\DataService();
$service->exportSuppliers();
// $service->exportSupplierContact();
// $service->export();
// $service->repairData(10);
// $service->completeContact(10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment