Commit a41773a2 by xingfeizhou

搜索接口和详情接口

parent ad703c08
<?php
namespace App\Http\Controllers\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Excel;
use App\Model\ExtSample;
use App\Model\ExtSampleAttachment;
use App\Model\ExtSampleAttributeValue;
use App\Model\ExtSampleLocation;
use DB;
class IndexController extends Controller
{
public function __construct(){
}
//首页
// public function index(){
// return response()->json("ok",200);
// }
//搜索结果页
public function search(Request $request){
$search = $request->get('search');
$page = $request->get('page',1);
$perPage = $request->get('perPage',12);
if(empty($search)){
return response()->json("参数错误",404);
}
//$subQuery = ExtSampleLocation::where('sample_location', 'like', '%'.$search.'%')->offset(0)->limit(999)->pluck("SampleID");
$total = DB::select("select count(a.ID) as total from ext_sample a where
a.SampleID in (select SampleID from ext_sample_location where sample_location like '%{$search}%')
or a.SupplierName like '%{$search}%'
or a.SampleName like '%{$search}%'
or a.SampleBrand like '%{$search}%'
or a.SampleVarietyName like '%{$search}%'");
$data = DB::select("select a.SampleID, a.SupplierName, a.SampleName, a.SampleBrand,
a.SampleVarietyName, b.FileCode, b.FileName, b.FileSize, c.sample_location from ext_sample a left
join (select * from ext_sample_attachment where FileState = 1 limit 1) b
on a.SampleID = b.SampleID left join ext_sample_location c on a.SampleID = c.SampleID where
a.SampleID in (select SampleID from ext_sample_location where sample_location like '%{$search}%')
or a.SupplierName like '%{$search}%'
or a.SampleName like '%{$search}%'
or a.SampleBrand like '%{$search}%'
or a.SampleVarietyName like '%{$search}%' limit ".($page-1)*$perPage.",".$perPage);
// $data = ExtSample::with('attribute','attachment','location')
// ->whereIn('ext_sample.SampleID', $subQuery)
// ->orWhere('ext_sample.SupplierName', 'like', '%'.$search.'%')
// ->orWhere('ext_sample.SampleName', 'like', '%'.$search.'%')
// ->orWhere('ext_sample.SampleBrand', 'like', '%'.$search.'%')
// ->orWhere('ext_sample.SampleVarietyName', 'like', '%'.$search.'%')
// ->offset(($page-1)*$perPage)
// ->limit($perPage)
// ->get();
return response()->json(['data'=>$data,'total'=>$total[0]->total], 200);
}
//详情页
public function detail(Request $request){
$sampleId = $request->get('sampleId');
if(empty($sampleId)){
return response()->json("参数错误", 404);
}
$data = ExtSample::with('attribute','attachment','location')->where('SampleID',$sampleId)->first();
return response()->json(['data'=>$data],200);
}
//导入
public function importLocation(){
return view('import');
}
public function doImport(Request $request){
if($request->hasFile('excel')){
$file = $request->file('excel');
Excel::load($file, function($reader) {
$reader = $reader->getSheet(0);
$excel = $reader->toArray();
$data = [];
foreach($excel as $k=>$v){
if($k != 0){
$data[$k]['SampleID'] = $v[0];
$data[$k]['number'] = $v[1];
}
}
if($data){
DB::table('ext_sample_location')->insert($data);
}
});
return "上传成功";
}
return "上传失败";
}
}
...@@ -57,5 +57,6 @@ class Kernel extends HttpKernel ...@@ -57,5 +57,6 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'crossHttp' => \App\Http\Middleware\CrossHttp::class
]; ];
} }
<?php
namespace App\Http\Middleware;
use Closure;
//处理跨域
class CrossHttp
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
$response->header('Access-Control-Allow-Credentials', 'false');
return $response;
// return $next($request);
}
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ExtSample extends Model
{
protected $table = 'ext_sample';
public $timestamps = false;
//与ext_SampleAttributeValue表一对多
public function attribute(){
return $this->hasMany('App\Model\ExtSampleAttributeValue', 'SampleID', 'SampleID');
}
//与Ext_SampleAttachment一对多
public function attachment(){
return $this->hasMany('App\Model\ExtSampleAttachment', 'SampleID', 'SampleID');
}
//与ext_loaction一对一
public function location(){
return $this->hasOne('App\Model\ExtSampleLocation', 'SampleID', 'SampleID');
}
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ExtSampleAttachment extends Model
{
protected $table = 'ext_sample_attachment';
public $timestamps = false;
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ExtSampleAttributeValue extends Model
{
protected $table = 'ext_sample_attribute_value';
public $timestamps = false;
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ExtSampleLocation extends Model
{
protected $table = 'ext_sample_location';
public $timestamps = false;
}
<?php
return [
];
\ No newline at end of file
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExtSamplesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ext_sample', function (Blueprint $table) {
$table->uuid('ID')->nullable()->comment('样品id');
$table->uuid('SampleID')->nullable()->comment('样品ID');
$table->uuid('SupplierCategoryID')->nullable()->comment('供方分类ID');
$table->string('SupplierCategoryName', 200)->nullable()->comment('供方分类名称');
$table->uuid('SupplierID')->nullable()->comment('供方ID');
$table->string('SupplierName', 100)->nullable()->comment('供方名称');
$table->string('SampleVarietyCode', 200)->nullable()->comment('样品品种编码');
$table->string('SampleVarietyName', 200)->nullable()->comment('样品品种名称');
$table->string('SampleName', 200)->nullable()->comment('样品名称');
$table->string('SampleBrand', 200)->nullable()->comment('样品品牌');
$table->dateTime('SampleReportDate')->nullable()->comment('上传时间');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ext_samples');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExtSampleAttributeValuesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ext_sample_attribute_value', function (Blueprint $table) {
$table->uuid('ID')->nullable()->comment('属性ID');
$table->uuid('SampleID')->nullable()->comment('样品ID');
$table->string('SampleName', 100)->nullable()->comment('样品名称');
$table->string('SampleVarietyAttributeName', 200)->nullable()->comment('属性名称');
$table->string('SampleVarietyAttributeValue', 1000)->nullable()->comment('属性值');
$table->string('Unit', 100)->nullable()->comment('属性单位');
$table->integer('Sort')->nullable()->comment('排序号');
$table->tinyInteger('IsMain')->nullable()->comment('是否主要属性');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ext_sample_attribute_values');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExtSampleAttachmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ext_sample_attachment', function (Blueprint $table) {
$table->uuid('ID')->nullable()->comment('属性ID');
$table->uuid('SampleID')->nullable()->comment('样品ID');
$table->string('FileCode', 100)->nullable()->comment('文件编码');
$table->string('FileName', 200)->nullable()->comment('文件名称');
$table->string('FileSize', 100)->nullable()->comment('文件大小');
$table->integer('FileState')->nullable()->comment('文件类别');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ext_sample_attachments');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExtSampleLocationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ext_sample_location', function (Blueprint $table) {
// $table->increments("ID");
$table->uuid('SampleID')->nullable()->comment('样品ID');
$table->string('sample_location', 100)->nullable()->comment('编号');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ext_sample_locations');
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>导入</title>
</head>
<body>
<form action = "{{url('/import/do')}}" method = "post" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="excel"/>
<button>提交</button>
</form>
</body>
</html>
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