Commit daad3722 by 杨树贤

数据导入脚本

parent 96a132f1
...@@ -35,7 +35,9 @@ class SetSupplierFollowUp extends Command ...@@ -35,7 +35,9 @@ class SetSupplierFollowUp extends Command
// $service->importSupplierLevel(); // $service->importSupplierLevel();
// $service->changeSupplierTypeByIsType(); // $service->changeSupplierTypeByIsType();
// $service->transferPayType(); // $service->transferPayType();
$service->makeTempTagForSupplier(); $service->importSupplierGroup(true);
// $service->changeSupplierType();
// $service->generateYunxinAccount(false);
// $service->makeSupplierSystemTagAbnormal(); // $service->makeSupplierSystemTagAbnormal();
} }
} }
...@@ -68,7 +68,7 @@ class SupplierAccountController extends Controller ...@@ -68,7 +68,7 @@ class SupplierAccountController extends Controller
{ {
$id = $request->get('id'); $id = $request->get('id');
$model = new SupplierAccountModel(); $model = new SupplierAccountModel();
$account =$model->where('id',$id)->first()->toArray(); $account = $model->where('id', $id)->first()->toArray();
//获取所有启用的供应商编码,而且类型是包括云芯 //获取所有启用的供应商编码,而且类型是包括云芯
$model = new SupplierChannelModel(); $model = new SupplierChannelModel();
$suppliers = $model->where('is_type', 0) $suppliers = $model->where('is_type', 0)
......
...@@ -20,6 +20,13 @@ class AdminUserService ...@@ -20,6 +20,13 @@ class AdminUserService
return $userInfoModel->where('userId', $adminId)->first(); return $userInfoModel->where('userId', $adminId)->first();
} }
public function getCodeIdByUserName($userName)
{
$userId = UserInfoModel::where('name', $userName)->value('userId');
$codeId = IntracodeModel::where('admin_id', $userId)->value('code_id');
return $codeId;
}
public function getCodeIdsByUserIds($userIds) public function getCodeIdsByUserIds($userIds)
{ {
$intraCodeModel = new IntracodeModel(); $intraCodeModel = new IntracodeModel();
......
...@@ -9,6 +9,7 @@ use App\Model\IntracodeModel; ...@@ -9,6 +9,7 @@ use App\Model\IntracodeModel;
use App\Model\LogModel; use App\Model\LogModel;
use App\Model\RedisModel; use App\Model\RedisModel;
use App\Model\StandardBrandModel; use App\Model\StandardBrandModel;
use App\Model\SupplierAccountModel;
use App\Model\SupplierAttachmentModel; use App\Model\SupplierAttachmentModel;
use App\Model\SupplierAttachmentsModel; use App\Model\SupplierAttachmentsModel;
use App\Model\SupplierChannelModel; use App\Model\SupplierChannelModel;
...@@ -17,6 +18,7 @@ use App\Model\SupplierPayTypeModel; ...@@ -17,6 +18,7 @@ use App\Model\SupplierPayTypeModel;
use Carbon\Carbon; use Carbon\Carbon;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
...@@ -63,598 +65,277 @@ class DataService ...@@ -63,598 +65,277 @@ class DataService
} }
} }
//20210722
//转移文件数据(之前是只存到一个字段,现在要存到一张表里面去)
public function transferFileData()
{
//先找出所有上传文件非空的供应商
$model = new SupplierChannelModel();
$suppliers = $model->where('qualification_photos', '!=', '')->get()->toArray();
$attachmentModel = new SupplierAttachmentModel();
foreach ($suppliers as $key => $supplier) {
$supplierId = $supplier['supplier_id'];
$files = json_decode($supplier['qualification_photos'], true);
$count = $attachmentModel->where('supplier_id', $supplierId)->count();
if (!$count) {
foreach ($files as $key => &$file) {
if (empty($file['url']) && empty($file['file_name'])) {
$file = '';
}
}
unset($file);
$business_license = array_get($files, 'business_license');
$billing_information = array_get($files, 'billing_information');
$registration_certificate = array_get($files, 'registration_certificate');
$incorporation_certificate = array_get($files, 'incorporation_certificate');
$certification_notice = array_get($files, 'certification_notice');
$supplier_survey = array_get($files, 'supplier_survey');
$proxy_certificate = array_get($files, 'proxy_certificate');
$quality_assurance_agreement = array_get($files, 'quality_assurance_agreement');
$confidentiality_agreement = array_get($files, 'confidentiality_agreement');
$cooperation_agreement = array_get($files, 'cooperation_agreement');
$other_attachment = array_get($files, 'other_attachment');
$attachment = [
'business_license' => $business_license ? [$business_license] : '',
'billing_information' => $billing_information ? [$billing_information] : '',
'registration_certificate' => $registration_certificate ? [$registration_certificate] : '',
'incorporation_certificate' => $incorporation_certificate ? [$incorporation_certificate] : '',
'certification_notice' => $certification_notice ? [$certification_notice] : '',
'supplier_survey' => $supplier_survey ? [$supplier_survey] : '',
'proxy_certificate' => $proxy_certificate ? [$proxy_certificate] : '',
'quality_assurance_agreement' => $quality_assurance_agreement ? [$quality_assurance_agreement] : '',
'confidentiality_agreement' => $confidentiality_agreement ? [$confidentiality_agreement] : '',
'cooperation_agreement' => $cooperation_agreement ? [$cooperation_agreement] : '',
'other_attachment' => $other_attachment ? [$other_attachment] : '',
'create_time' => time(),
'update_time' => time(),
'supplier_id' => $supplierId,
];
$attachment = array_filter($attachment, function ($item) {
return !empty($item);
});
$attachment = array_map(function ($item) {
return json_encode($item);
}, $attachment);
$attachmentModel->insert($attachment);
}
}
}
//历史数据处理 const TYPE_YC = 4;
public function changeSupplierIsTypeByCheckChannelUidOrPurchaseUid() const TYPE_DLS = 1;
const TYPE_MYS = 2;
//供应商类型修改
public function changeSupplierType()
{ {
$model = new SupplierChannelModel(); ini_set('memory_limit', -1);
$suppliers = $model->where(function ($q) { //统一修改掉状态先
$q->where('channel_uid', '!=', '')->orWhere('purchase_uid', '!=', ''); (new SupplierChannelModel)->where('supplier_id', '>', 0)->update([
})->where('is_type', 1)->get()->toArray(); 'outside_contact_type' => 0
// dd(count($suppliers));
if (count($suppliers) > 1670) {
dd("数量有问题");
}
$redis = new RedisModel();
//非正式供应商中存在渠道员/采购员,将供应商改为正式供应商
foreach ($suppliers as $supplier) {
//先查询是否有重复的供应商
$count = $model->where('supplier_name', $supplier['supplier_name'])->count();
if ($count > 1) {
dd($supplier['supplier_name']);
continue;
}
//同时记录被修改的supplier_id列表到redis以防万一
$redis->hset('lie_change_is_type_suppliers', $supplier['supplier_id'], json_encode($supplier));
//没有的话直接修改成待审核,并且转正
$model->where('supplier_id', $supplier['supplier_id'])
->update([
// 'update_time' => time(),
'is_type' => 0,
'status' => 0
]); ]);
}
}
//替换老的品牌选择成为新的标准品牌id //先将真实对接的供应商修改状态
public function replaceStandardBrandId() $realApiSupplierCodes = [
{ 'L0000266',
$supplierModel = new SupplierChannelModel(); 'L0000198',
$suppliers = $supplierModel->where('main_brands', '!=', '')->get()->toArray(); 'L0000273',
$redis = new RedisModel(); 'L0000135',
foreach ($suppliers as $supplier) { 'L0000135',
$mainBrands = explode(',', $supplier['main_brands']); 'L0005966',
$standardBrandIds = []; 'L0000004',
foreach ($mainBrands as $brandId) { 'L0006149',
//找标准品牌ID,没有直接跳过 'L0006150',
$standardBrandId = $redis->hget('standard_brand_mapping', $brandId); 'L0006155',
if (empty($standardBrandId) or empty($brandId)) { 'L0007637',
continue; 'L0006148',
} 'L0006151',
$standardBrandIds[] = $standardBrandId; 'L0006147',
} 'L0007639',
print_r($mainBrands); 'L0007640',
print_r($standardBrandIds); 'L0007646',
echo PHP_EOL; 'L0007648',
if (!empty($standardBrandIds)) { 'L0007650',
$standardBrandIdsStr = implode(',', $standardBrandIds); 'L0007653',
} else { 'L0000089',
$standardBrandIdsStr = ''; 'L0007633',
} 'L0007663',
$supplierModel->where('supplier_id', $supplier['supplier_id'])->update([ 'L0010826',
'main_brands' => $standardBrandIdsStr 'L0010856',
'L0010937',
'L0007595',
'L0011054',
'L0004938',
'L0011040',
'L0001175',
'L0007243',
'L0011042',
];
SupplierChannelModel::whereIn('supplier_code', $realApiSupplierCodes)
->update([
'outside_contact_type' => 2
]); ]);
} $suppliers = $this->getNeedDealSuppliers();
if (empty($suppliers)) {
return '供应商上传类型已经全部处理';
} }
//导入黑名单供应商(前提是不存在) //api对接类型的供应商数量(706)
public function importBlockSupplier() $ycApiTypeCount = 172;
{ $dlsApiTypeCount = 207;
$supplierNames = [ $mysApiTypeCount = 327;
'Electronics Depot', //要先找出原厂类型的API对接类型的供应商数量,然后要从库里面随机找出原厂的供应商打上类型
'Assured Electronics Corp.', $this->dealApiSupplier(self::TYPE_YC, $ycApiTypeCount);
'深圳市力特电子有限公司', //要先找出代理商类型的API对接类型的供应商数量,然后要从库里面随机找出代理商的供应商打上类型
'深圳市铭盛微电子科技有限公司', $this->dealApiSupplier(self::TYPE_DLS, $dlsApiTypeCount);
'Aegis Components Inc', //要先找出贸易类型的API对接类型的供应商数量,然后要从库里面随机找出贸易的供应商打上类型
'深圳市亿博创电子公司', $this->dealApiSupplier(self::TYPE_MYS, $mysApiTypeCount);
];
//构建插入数据
$supplierModel = new SupplierChannelModel();
foreach ($supplierNames as $supplierName) {
$supplierName = trim($supplierName);
$exist = $supplierModel->where('supplier_name', $supplierName)->count();
if (!$exist) {
$insertData = [
'supplier_name' => $supplierName,
'create_uid' => 1000,
'create_time' => time(),
'status' => $supplierModel::STATUS_BLOCK,
'is_type' => 0,
'create_name' => 'admin',
'block_reason' => '系统拉黑',
];
$supplierId = $supplierModel->insertGetId($insertData);
$supplierService = new SupplierService();
$supplierService->saveSupplierCode($supplierId);
}
}
}
//给特定类型的供应商打上标签 echo "-------------------------" . PHP_EOL;
//找出 国内/港台 + 现货商性质的,而且没有上传品质协议的供应商,自动打上临时供应商标签
public function makeSupplierSystemTag()
{
$channelModel = new SupplierChannelModel();
$suppliers = $channelModel->leftjoin('supplier_attachment', 'supplier_channel.supplier_id', '=',
'supplier_attachment.supplier_id')
->whereIn('region', [2, 4])
->where('supplier_group', 2)
->where('quality_assurance_agreement', '')->where('system_tags', 'not like', '%临时供应商%')
->get()->toArray();
$tagService = new SupplierTagService();
//找到后打上标签
foreach ($suppliers as $supplier) {
$supplierId = $supplier['supplier_id'];
$oldTags = $supplier['system_tags'];
$newTags = $supplier['system_tags'] ? rtrim($supplier['system_tags'], ',') . ',临时供应商' : '临时供应商';
if ($tagService->saveTags($supplierId, 14, $newTags, $oldTags)) {
$channelModel->where('supplier_id', $supplierId)->update([
'system_tags' => $newTags
]);
}
}
}
//打上历史检测异常标签 //然后随机取出云芯的供应商(1139)
public function makeSupplierSystemTagAbnormal($change = false) //先剔除api对接的供应商
{ $ycYunxinTypeCount = 113;
ini_set('memory_limit', '-1'); $dlsYunxinTypeCount = 302;
$channelModel = new SupplierChannelModel(); $mysYunxinTypeCount = 724;
//读取excel $this->dealYunxinSupplier(self::TYPE_YC, $ycYunxinTypeCount);
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'abnormal_supplier.xlsx'; $this->dealYunxinSupplier(self::TYPE_DLS, $dlsYunxinTypeCount);
try { $this->dealYunxinSupplier(self::TYPE_MYS, $mysYunxinTypeCount);
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($change) {
$reader->sheet('QC不良记录', function () use ($reader, $change) {
$supplierModel = new SupplierChannelModel();
$suppliers = [];
foreach ($reader->all()->toArray() as $key => $item) {
if ($key == 0 || $key == 1) {
continue;
}
$supplierName = trim($item[2]);
if (empty($supplierName)) {
continue;
}
$supplier = $supplierModel->where('supplier_name', $supplierName)->first();
if (!empty($supplier)) {
$suppliers[] = $supplier->toArray();
} else {
echo "供应商 : " . $supplierName . "不存在,跳过" . PHP_EOL;
}
}
$tagService = new SupplierTagService();
//找到后打上标签
foreach ($suppliers as $supplier) {
$supplierId = $supplier['supplier_id'];
$oldTags = $supplier['system_tags'];
echo "供应商 : " . $supplier['supplier_name'] . "检测异常,需要打上标签" . PHP_EOL;
if (strpos($supplier['system_tags'], '历史检测异常') !== false) {
continue;
}
$newTags = $supplier['system_tags'] ? rtrim($supplier['system_tags'],
',') . ',历史检测异常' : '历史检测异常';
if ($change) {
if ($tagService->saveTags($supplierId, 14, $newTags, $oldTags)) {
$supplierModel->where('supplier_id', $supplierId)->update([
'system_tags' => $newTags
]);
}
}
}
});
});
} catch (\Exception $exception) {
dd($exception);
}
} echo "-------------------------" . PHP_EOL;
//初始化供应商对应的搜索标签到redis //剩下的都是人工上传的,也有一定占比(2658)
public function initSupplierSearchTags() $ycManualTypeCount = 20;
{ $dlsManualTypeCount = 146;
//初始化所有正式服务器的标签情况到redis $mysManualTypeCount = 2492;
$supplierModel = new SupplierChannelModel(); $this->dealManualSupplier(self::TYPE_YC, $ycManualTypeCount);
$suppliers = $supplierModel->where('is_type', 0)->get()->toArray(); $this->dealManualSupplier(self::TYPE_DLS, $dlsManualTypeCount);
$redis = new RedisModel(); $this->dealManualSupplier(self::TYPE_MYS, $mysManualTypeCount);
foreach ($suppliers as $supplier) {
//1精选,2原厂直供,3认证,4猎芯自营,100无标签
$tagFlags = [];
//先去判断供应商性质是否为原厂,如果是的话,写入标签2
if ($supplier['supplier_group'] == 4) {
$tagFlags[] = 2;
}
if ($supplier['has_certification'] == 1) {
$tagFlags[] = 3;
}
$redis->hset('supplier_search_tags', $supplier['supplier_code'], json_encode([
'supplier_code' => $supplier['supplier_code'],
'tags' => $tagFlags,
]));
}
}
//推送更新供应商的队列
public function pushSupplierSKu()
{
$supplierModel = new SupplierChannelModel();
$suppliers = $supplierModel->where('is_type', 0)->where('supplier_group', 4)->orderBy('supplier_id', 'desc')
->limit(130)->get()->toArray();
$redis = new RedisModel();
foreach ($suppliers as $supplier) {
$supplierTag = $redis->hget('supplier_search_tags', $supplier['supplier_code']);
if (!$supplierTag) {
continue;
}
$message = json_encode([
'supplier_code' => $supplier['supplier_code'],
]);
$conn = new AMQPStreamConnection(config('database.connections.rabbitmq.host'),
config('database.connections.rabbitmq.port'),
config('database.connections.rabbitmq.login'),
config('database.connections.rabbitmq.password'));
$channel = $conn->channel();
$channel->queue_declare('supplier_zhuanmai_update', false, true, false, false);
$msg = new AMQPMessage($message,
array('content_type' => 'text/plain'));
$channel->basic_publish($msg, '', 'supplier_zhuanmai_update');
}
} }
//导入主营品牌数据 public function getNeedDealSuppliers($supplierGroup = 0, $otherSupplierGroup = [])
public function importMainBrandData()
{ {
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_main_brand.xlsx'; $query = SupplierChannelModel::where('outside_contact_type', 0)
try { ->where('channel_uid', '!=', '')
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) { ->where('status', '!=', -3)
$reader->sheet('产出', function () use ($reader) { ->whereRaw('supplier_name NOT LIKE "%-1"')
$supplierMainBrandData = []; ->where('is_type', 0);
$standardBrandModel = new StandardBrandModel();
foreach ($reader->all()->toArray() as $key => $item) {
$supplierName = trim($item[0]);
$brandName = $item[1];
$brandId = $standardBrandModel->where('brand_name',
trim($brandName))->value('standard_brand_id');
if (empty($brandId)) {
continue;
}
if (!isset($supplierMainBrandData[$supplierName])) {
$supplierMainBrandData[$supplierName][] = $brandId;
} else {
$supplierMainBrandData[$supplierName][] = $brandId;
}
}
$supplierModel = new SupplierChannelModel();
foreach ($supplierMainBrandData as $supplierName => $mainBrandIds) {
$result = $supplierModel->where('supplier_name', $supplierName)->update([
'main_brands' => implode(',', $mainBrandIds),
]);
if ($result) {
var_dump('更新供应商主营品牌成功,供应商 : ' . $supplierName);
}
}
});
});
} catch (\Exception $exception) {
var_dump($exception);
}
}
//从excel导入供应商
public function importSupplier() if (!empty($otherSupplierGroup)) {
{ $query->whereIn('supplier_group', $otherSupplierGroup);
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_import.xls';
try {
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) {
$supplierChannelModel = new SupplierChannelModel();
$supplierService = new SupplierService();
$reader->sheet('供应商清单', function ($sheet) use ($reader, $supplierChannelModel, $supplierService) {
$number = 0;
foreach ($reader->all()->toArray() as $key => $item) {
$supplierName = trim($item[1]);
//先去判断是否存在
$exist = $supplierChannelModel->where('supplier_name', $supplierName)
->exists();
if (!$exist) {
$number++;
//往库里面插入
$data = [
'supplier_name' => $supplierName,
'create_uid' => 1000,
'create_time' => time(),
'status' => 2,
'is_type' => 1,
'create_name' => 'admin',
];
$supplierId = $supplierChannelModel->insertGetId($data);
$supplierService->saveSupplierCode($supplierId);
//然后修改excel
$sheet->cell('E' . ($key + 2), function ($cell) {
$cell->setValue('供应商不存在,已经新增');
});
} else { } else {
$sheet->cell('E' . ($key + 2), function ($cell) { if (!empty($supplierGroup)) {
$cell->setValue('供应商已经存在,不做新增'); $query->where('supplier_group', $supplierGroup);
});
}
} }
var_dump("一共新增了${number}个不存在的供应商");
});
})->store('xls');
} catch (\Exception $exception) {
var_dump($exception);
} }
$data = $query->get()->toArray();
return $data;
} }
/* //处理API接入的不同类型的数据
* 供应商0.5版本下面需要跑的数据方法 public function dealApiSupplier($supplierGroup, $totalApiTypeCount)
* */ {
//先找出已经存在的api对接类型的供应商数量,方便下面算出补全数量
$existApiTypeCount = SupplierChannelModel::where('outside_contact_type', 2)
->where('supplier_group', $supplierGroup)->count();
echo "API对接类型的 $supplierGroup 供应商数量一共要有数量 : " . $totalApiTypeCount . PHP_EOL;
//临时供应商标记以及等级切换 echo "已经存在的API对接类型的 $supplierGroup 供应商数量 : " . $existApiTypeCount . PHP_EOL;
public function makeTempTagForSupplier($change = false)
{
/*
创建时间为2021-06-01开始截至现在,供应商类别为正式/临时,供应商性质为现货商,且没有上传品质协议,系统标签需要贴【临时供应商标签】,等级标记为E
创建时间为空,不在上述供应商集内
*/
$time = strtotime("2021-06-01");
$suppliers = SupplierChannelModel::where('create_time', '>', $time)->where('is_type', 0)
->where('create_time', '!=', 0)->get();
foreach ($suppliers as $supplier) {
$supplierId = $supplier['supplier_id'];
//判断是否有上传品质协议
$hasQualityAssuranceAgreement = SupplierAttachmentModel::where('supplier_id',
$supplierId)->value('quality_assurance_agreement');
//供应商性质为现货商,且没有上传品质协议,系统标签需要贴【临时供应商标签】,等级标记为E
if (!$hasQualityAssuranceAgreement && $supplier['supplier_group'] == 2) {
// if ($supplier['level'] != '' && $supplier['level'] != 'E') {
// echo "供应商编码 ${supplier['supplier_code']} 已经有等级${supplier['level']},但是不为等级E,跳过" . PHP_EOL;
// continue;
// }
echo "供应商编码 ${supplier['supplier_code']} 转换为临时供应商" . PHP_EOL;
if ($change) {
SupplierChannelModel::where('supplier_id', $supplierId)->update([
'supplier_type' => 2
]);
}
}
}
}
//将非正式供应商(is_type=1)的类别设置为待转正 //剩下就要补全
public function changeSupplierTypeByIsType() $needAddApiTypeCount = $totalApiTypeCount - $existApiTypeCount;
{
SupplierChannelModel::where('supplier_id', 13038)->update([
'supplier_type' => 1
]);
}
//转移附件数据到新的表 echo "需要补全API对接类型的 $supplierGroup 供应商数量 :" . $needAddApiTypeCount . PHP_EOL;
public function transferAttachmentToNewTable()
{
$supplierAttachments = SupplierAttachmentModel::get()->toArray();
foreach ($supplierAttachments as $supplierAttachment) {
$supplierId = $supplierAttachment['supplier_id'];
if (SupplierAttachmentsModel::where('supplier_id', $supplierId)->exists()) {
continue;
}
$fields = [
'business_license',
'billing_information',
'registration_certificate',
'incorporation_certificate',
'certification_notice',
'supplier_survey',
'proxy_certificate',
'quality_assurance_agreement',
'confidentiality_agreement',
'cooperation_agreement',
'other_attachment',
];
$attachmentData = [];
foreach ($fields as $field) {
if (isset($supplierAttachment[$field]) && !empty($supplierAttachment[$field])) {
$supplierAttachment[$field] = json_decode($supplierAttachment[$field], true);
foreach ($supplierAttachment[$field] as $item) {
/*
* [{"name":"business_license","url":"http:\/\/img.ichunt.com\/test\/images\/ichunt\/202106\/25\/fc9c8179aa327f878932dd6b62b4d88c.png","file_name":"38.png"}]
* */
$attachmentData[] = [
'supplier_id' => $supplierId,
'type_name' => array_get(config('fixed.FileNameMapping'), $item['name']),
'field_name' => $item['name'],
'file_url' => $item['url'],
'file_name' => array_get($item, 'file_name', ''),
];
}
}
if ($needAddApiTypeCount <= 0) {
return;
} }
SupplierAttachmentsModel::insert($attachmentData); $suppliers = $this->getNeedDealSuppliers($supplierGroup);
echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
if (!count($suppliers)) {
return;
} }
$rand = array_rand($suppliers, $needAddApiTypeCount);
$apiSuppliers = array_intersect_key($suppliers,
array_flip(is_array($rand) ? $rand : [$rand]));
$apiSupplierIds = array_column($apiSuppliers, 'supplier_id');
echo "供应商性质为 : $supplierGroup 的供应商修改API上传类型数量 : " . count($apiSupplierIds) . PHP_EOL . PHP_EOL;
SupplierChannelModel::whereIn('supplier_id', $apiSupplierIds)
->where('outside_contact_type', 0)->update([
'outside_contact_type' => 2
]);
} }
//转换交易方式和付款方式数据统一到付款方式 //处理云芯接入的不同类型的数据
public function transferPayType($change = false) public function dealYunxinSupplier($supplierGroup, $totalYunxinCount)
{ {
$suppliersA = SupplierChannelModel::where('trading_method', '!=', 0)->where('pay_type', '!=', 0)
->get()->toArray(); $suppliers = $this->getNeedDealSuppliers($supplierGroup);
$suppliersB = SupplierChannelModel::where('trading_method', '!=', 0)->where('pay_type', 0) echo "供应商性质 $supplierGroup 需要修改数量 :" . $totalYunxinCount . PHP_EOL;
->get()->toArray(); echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
$suppliersC = SupplierChannelModel::where('trading_method', 0)->where('pay_type', '!=', 0) if (!count($suppliers)) {
->get()->toArray(); return;
$handledSupplierIds = [];
//如果供应商交易方式与付款方式都有数据,则付款周期从交易方式中映射,映射规则:先款后货映射成预付款;月结映射成账期;
if ($suppliersA) {
foreach ($suppliersA as $supplier) {
if ($supplier['trading_method'] == 1) {
$payType = 3;
} else {
$payType = 1;
}
$tradeMethod = array_get(config('fixed.TradingMethod'), $supplier['trading_method']);
$payTypeName = array_get(config('fixed.SupplierPayType'), $payType);
dump("供应商编码 ${supplier['supplier_code']} 原来的交易方式为 $tradeMethod 转换成付款周期 $payTypeName");
if ($change) {
$handledSupplierIds[] = $supplier['supplier_id'];
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'pay_type' => $payType,
]);
}
} }
if (count($suppliers) < $totalYunxinCount) {
$totalYunxinCount = count($suppliers);
} }
$rand = array_rand($suppliers, $totalYunxinCount);
$yunxinSuppliers = array_intersect_key($suppliers,
array_flip(is_array($rand) ? $rand : [$rand]));
$yunxinSupplierIds = array_column($yunxinSuppliers, 'supplier_id');
dump('--------------------------------'); echo "供应商性质为 : $supplierGroup 的供应商修改云芯上传类型数量 : " . count(array_unique($yunxinSupplierIds)) . PHP_EOL;
//如果供应商交易方式有数据,付款方式无数据,则付款周期从交易方式中映射,映射规则:先款后货映射成预付款;月结映射成账期;
if ($suppliersB) {
foreach ($suppliersB as $supplier) { // foreach ($yunxinSupplierIds as $supplierId) {
if ($supplier['trading_method'] == 1) { // SupplierChannelModel::where('supplier_id', $supplierId)->update([
$payType = 3; // 'outside_contact_type' => 3
} else { // ]);
$payType = 1; // }
} SupplierChannelModel::whereIn('supplier_id', $yunxinSupplierIds)
$tradeMethod = array_get(config('fixed.TradingMethod'), $supplier['trading_method']); ->where('outside_contact_type', 0)->update([
$payTypeName = array_get(config('fixed.SupplierPayType'), $payType); 'outside_contact_type' => 3
dump("供应商编码 ${supplier['supplier_code']} 原来的交易方式为 $tradeMethod 转换成付款周期 $payTypeName");
if ($change) {
$handledSupplierIds[] = $supplier['supplier_id'];
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'pay_type' => $payType,
]); ]);
} }
}
}
dump('--------------------------------');
//如果供应商交易方式无数据,付款方式有数据,则付款周期从付款方式中映射,映射规则:账期-周期结算映射成账期,账期-定期结算映射成账期,全款映射成预付款,定金映射成预付款; //处理人工上传
if ($suppliersC) { public function dealManualSupplier($supplierGroup, $totalManualCount)
foreach ($suppliersC as $supplier) { {
$payType = 0; $otherSupplierGroup = [];
if (!trim($supplier['pay_type'])) { if ($supplierGroup == 2) {
continue; $otherSupplierGroup = [0, 2, 3, 5, 6];
} // $otherSupplierGroup = [2];
switch (trim($supplier['pay_type'])) { }
case 1: //人工上传这个类别,供应商性质如果是贸易商,那就要加上方案商和分销平台和代工厂
$payType = 1; $suppliers = $this->getNeedDealSuppliers($supplierGroup, $otherSupplierGroup);
break; echo "供应商性质 $supplierGroup 需要修改数量 :" . $totalManualCount . PHP_EOL;
case 2: echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
$payType = 1; if (!count($suppliers)) {
break; return;
case 3: }
$payType = 3; if (count($suppliers) <= $totalManualCount) {
break; $totalManualCount = count($suppliers);
case 4: }
$payType = 3; $rand = array_rand($suppliers, $totalManualCount);
break; $manualSuppliers = array_intersect_key($suppliers,
} array_flip(is_array($rand) ? $rand : [$rand]));
$originPayTypeNameMap = [ $manualSupplierIds = array_column($manualSuppliers, 'supplier_id');
1 => '账期-周期结算',
2 => '账期-定期结算', echo "供应商性质为 : $supplierGroup 的供应商修改人工上传类型数量 : " . count($manualSupplierIds) . PHP_EOL;
3 => '全款',
4 => '定金', // foreach ($manualSupplierIds as $supplierId) {
// SupplierChannelModel::where('supplier_id', $supplierId)->update([
// 'outside_contact_type' => 1
// ]);
// }
]; foreach (collect($manualSupplierIds)->chunk(500) as $key => $ids) {
$originPayTypeName = array_get($originPayTypeNameMap, $supplier['pay_type']); SupplierChannelModel::whereIn('supplier_id', $ids)
$payTypeName = array_get(config('fixed.SupplierPayType'), $payType); ->where('outside_contact_type', 0)->update([
dump("供应商编码 ${supplier['supplier_code']} 原来的付款方式为 $originPayTypeName 转换成付款周期 $payTypeName"); 'outside_contact_type' => 1
if ($change && $payType) {
$handledSupplierIds[] = $supplier['supplier_id'];
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'pay_type' => $payType,
]); ]);
} }
}
}
} }
//导入等级修改供应商并且找出没有等级的供应商(针对正式的) //导入公司性质
public function importSupplierLevel($change = false) public function importSupplierGroup($isUpdate = false)
{ {
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_level.xlsx'; $filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_group.xlsx';
try { try {
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($change) { Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($isUpdate) {
$supplierChannelModel = new SupplierChannelModel(); $reader->sheet('Sheet2', function () use ($reader, $isUpdate) {
$reader->sheet('2021年评估', function ($sheet) use ($reader, $supplierChannelModel, $change) { $num = $ycNum = 0;
$number = 0; $redis = new RedisModel();
foreach ($reader->all()->toArray() as $key => $item) { foreach ($reader->all()->toArray() as $key => $item) {
$supplierName = trim($item[1]); $supplierCode = trim($item[0]);
$level = trim($item[2]); $purchaseName = trim($item[3]);
//先去判断是否存在 $channelUid = (new AdminUserService())->getCodeIdByUserName($purchaseName);
$exist = $supplierChannelModel->where('supplier_name', $supplierName) //如果采购员id不为空,那么就要写进去
->exists(); $supplierId = SupplierChannelModel::where('supplier_code', $supplierCode)
$levelExist = $supplierChannelModel->where('supplier_name', $supplierName) ->value('supplier_id');
->value('level'); if (!$supplierId) {
if ($levelExist) { echo "供应商不存在 : " . $supplierCode . PHP_EOL;
echo "供应商 ${supplierName} 已有,等级为 : " . $level . PHP_EOL; }
if ($channelUid) {
echo "添加采购员${purchaseName}到 : " . $supplierCode . PHP_EOL;
if ($isUpdate) {
(new SupplierService())->allocateChannelUser($supplierId, $channelUid, false);
}
}
$isYc = trim($item[4]);
if ($isYc == '原厂') {
//判断是否是原厂,如果是原厂,就跳过
$supplierGroup = SupplierChannelModel::where('supplier_code', $supplierCode)
->value('supplier_group');
if ($supplierGroup == self::TYPE_YC) {
echo "该供应商已经是原厂,跳过 : $supplierCode" . PHP_EOL;
continue; continue;
} }
if (!$exist) { echo "修改供应商性质为原厂 : " . $supplierCode . PHP_EOL;
$number++; if ($isUpdate) {
} else { //写入redis,方便恢复
echo "修改供应商 ${supplierName} 等级为 : " . $level . PHP_EOL; $redis->hset('lie_supplier_group_change', $supplierGroup, self::TYPE_YC);
if ($change) { SupplierChannelModel::where('supplier_code', $supplierCode)
$supplierChannelModel->where('supplier_name', $supplierName)->update([ ->update([
'level' => $level, 'supplier_group' => self::TYPE_YC,
]); ]);
} }
$ycNum++;
} }
$num++;
} }
var_dump("一共有${number}个不存在的供应商");
echo "一共处理 $num 家供应商采购员,处理 $ycNum 家供应商性质为原厂" . PHP_EOL;
}); });
}); });
} catch (\Exception $exception) { } catch (\Exception $exception) {
...@@ -662,7 +343,31 @@ class DataService ...@@ -662,7 +343,31 @@ class DataService
} }
} }
//生成云芯账号
public function generateYunxinAccount($isUpdate = false)
{
$suppliers = SupplierChannelModel::where('outside_contact_type', 3)->get()->toArray();
// $suppliers = SupplierChannelModel::where('is_type', 0)->limit(100)->get()->toArray();
foreach ($suppliers as $supplier) {
if (SupplierAccountModel::where('supplier_id', $supplier['supplier_id'])->exists()) {
continue;
}
$mobile = generateMobile(1)[0];
$password = makePassword(mt_rand(8, 12));
$account = [
'supplier_id' => $supplier['supplier_id'],
'supplier_code' => $supplier['supplier_code'],
'mobile' => $mobile,
'password_raw' => $password,
'password' => Hash::make($password),
'create_uid' => 1000,
'create_time' => time(),
];
if ($isUpdate) {
SupplierAccountModel::insert($account);
}
}
}
} }
...@@ -418,9 +418,9 @@ class SupplierService ...@@ -418,9 +418,9 @@ class SupplierService
} }
//分配采购员 //分配采购员
public function allocateChannelUser($supplierId, $channelUid) public function allocateChannelUser($supplierId, $channelUid, $logFlag = true)
{ {
$result = DB::connection('web')->transaction(function () use ($supplierId, $channelUid) { $result = DB::connection('web')->transaction(function () use ($supplierId, $channelUid, $logFlag) {
$model = new SupplierChannelModel(); $model = new SupplierChannelModel();
$supplier = $model->where('supplier_id', $supplierId)->first(); $supplier = $model->where('supplier_id', $supplierId)->first();
$supplier = $supplier ? $supplier->toArray() : []; $supplier = $supplier ? $supplier->toArray() : [];
...@@ -429,10 +429,18 @@ class SupplierService ...@@ -429,10 +429,18 @@ class SupplierService
$preChannelUid[] = $channelUid; $preChannelUid[] = $channelUid;
$preChannelUid = array_unique($preChannelUid); $preChannelUid = array_unique($preChannelUid);
$preChannelUid = implode(',', $preChannelUid); $preChannelUid = implode(',', $preChannelUid);
$preChannelUid = trim($preChannelUid, ',');
if ($logFlag) {
$result = $model->where('supplier_id', $supplierId)->update([ $result = $model->where('supplier_id', $supplierId)->update([
'update_time' => time(), 'update_time' => time(),
'channel_uid' => $preChannelUid, 'channel_uid' => $preChannelUid,
]); ]);
}else{
$result = $model->where('supplier_id', $supplierId)->update([
'channel_uid' => $preChannelUid,
]);
}
$contactResult = false; $contactResult = false;
if ($result) { if ($result) {
$contactModel = new SupplierContactModel(); $contactModel = new SupplierContactModel();
...@@ -440,11 +448,11 @@ class SupplierService ...@@ -440,11 +448,11 @@ class SupplierService
'supplier_id' => $supplierId, 'supplier_id' => $supplierId,
'can_check_uids' => $channelUid, 'can_check_uids' => $channelUid,
'add_time' => time(), 'add_time' => time(),
'admin_id' => request()->user->userId, 'admin_id' => !empty(request()->user->userId) ? request()->user->userId : 1000,
]; ];
$contactResult = $contactModel->insert($contact); $contactResult = $contactModel->insert($contact);
} }
if ($contactResult) { if ($contactResult && $logFlag) {
//判断是否是非正式供应商,如果是,自动转正,并且修改为待审核状态 //判断是否是非正式供应商,如果是,自动转正,并且修改为待审核状态
$this->autoChangeIsType($supplier); $this->autoChangeIsType($supplier);
//记录日志 //记录日志
...@@ -511,7 +519,7 @@ class SupplierService ...@@ -511,7 +519,7 @@ class SupplierService
$printData['contact'] = $contact; $printData['contact'] = $contact;
$attachmentModel = new SupplierAttachmentsModel(); $attachmentModel = new SupplierAttachmentsModel();
$hasAgreement = $attachmentModel->where('supplier_id', $supplierId) $hasAgreement = $attachmentModel->where('supplier_id', $supplierId)
->where('field_name','quality_assurance_agreement')->exists() ? '已签' : '未签'; ->where('field_name', 'quality_assurance_agreement')->exists() ? '已签' : '未签';
$printData['has_agreement'] = $hasAgreement; $printData['has_agreement'] = $hasAgreement;
$adminUserService = new AdminUserService(); $adminUserService = new AdminUserService();
$user = $adminUserService->getAdminUserInfo(request()->user->userId); $user = $adminUserService->getAdminUserInfo(request()->user->userId);
...@@ -580,8 +588,8 @@ class SupplierService ...@@ -580,8 +588,8 @@ class SupplierService
'supplier_type', 'supplier_type',
'settlement_type', 'settlement_type',
]; ];
foreach ($supplier as $key=>&$value) { foreach ($supplier as $key => &$value) {
if (in_array($key,$needChangeField) && empty($value)) { if (in_array($key, $needChangeField) && empty($value)) {
$value = ''; $value = '';
} }
} }
......
...@@ -204,3 +204,43 @@ function checkArrayAllValueNull($array, $excludeField = []) ...@@ -204,3 +204,43 @@ function checkArrayAllValueNull($array, $excludeField = [])
return true; return true;
} }
function generateMobile($count)
{
$arr = [
130,131,132,133,134,135,136,137,138,139,
144,147,
150,151,152,153,155,156,157,158,159,
176,177,178,
180,181,182,183,184,185,186,187,188,189,
];
$phone = [];
for($i = 0; $i < $count; $i++) {
$phone[] = $arr[array_rand($arr)].''.mt_rand(1000,9999).''.mt_rand(1000,9999);
}
return array_unique($phone);
}
function makePassword($length)
{
// 密码字符集,可任意添加你需要的字符
// $str = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
// 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
// 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
// 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
// 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$str = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y','z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
// 在 $str 中随机取 $length 个数组元素键名
$keys = array_rand($str, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$password .= $str[$keys[$i]];
}
return $password;
}
\ No newline at end of file
...@@ -53,11 +53,5 @@ Route::group(['middleware' => ['external'],'namespace' => 'Api'], function () { ...@@ -53,11 +53,5 @@ Route::group(['middleware' => ['external'],'namespace' => 'Api'], function () {
}); });
Route::match(['get', 'post'], '/test', function () { Route::match(['get', 'post'], '/test', function () {
$data = [ // (new \App\Http\Services\DataService())->importSupplierGroup();
'sku_ids'=> '1163833791883548099,1163833787833091377',
'is_long_term' => -1,
'operate_type' => 1,
'cp_time' => '2022-05-31 0:0:0',
];
(new SkuService())->batchUpdateSkuStatus($data);
}); });
No preview for this file type
[ZoneTransfer]
[ZoneTransfer]
ZoneId=3
ReferrerUrl=https://www.tapd.cn/
HostUrl=https://file.tapd.cn/56056445/attachments/download/1156056445001001663/story
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