荆轲刺秦王
public function storeGroupImport( Request $request , StoreGroupDao $storeGroupDao )
{
$nodeId = txt_filter($request -> get('node_id'));
$brandId = txt_filter($request -> get('brand_id'));
$file = $request->file('store_group_file');
if( empty( $nodeId ) || empty( $brandId ) || empty( $file ) ) S::error( 40001 );
Log::log_write('门店分组上传文件:'.print_r($file,1));
$fileData = $storeGroupDao -> parsingFile( $file, $brandId );
if ( empty($fileData) ) S::error( 40005 );
$sendParam = [];
foreach( $fileData as $key=>$value ){
$arr = explode(',',$value);
$sendParam[] = [
'group_id' => $arr[0],
'store_id' => $arr[1],
];
}
$modelData = StoreGroupInfoTest::select()->get()->toArray();
//去重
$serializeArrs = array_map('serialize',$sendParam);
$uniqueArrs = array_unique($serializeArrs);
$sendParamUnique = array_map('unserialize',$uniqueArrs);
//数据库没有 列表有
$paramDiffArr = array_filter( $sendParamUnique, function ($i) use ($modelData){
return !in_array( $i['group_id'], array_column( $modelData,'group_id' )) &&
!in_array( $i['store_id'], array_column( $modelData,'store_id' ));
} );
//数据库没有 列表有 插入
if( !empty( $paramDiffArr ) ){
try
{
foreach ( $paramDiffArr as $k=> $v )
{
$sendParamUnique[$k]['store_id'] = strval($v['store_id']);
}
StoreGroupInfoTest::insert( $paramDiffArr );
return S::jsonReturn( ['success'=> '数据修改成功'] );
}
catch ( \Exception $e ){
log_write( '数据修改失败'.print_r( $e-> getMessage() ));
S::error( 50001 );
}
}
return S::jsonReturn( ['success'=> '数据库已存在,未修改'] );
}