Przeglądaj źródła

【Add】产品增加总库存以及城市筛选

liuxiangxin 6 miesięcy temu
rodzic
commit
f429af3095

+ 46 - 13
app/Http/Controllers/Admin/Product.php

@@ -32,37 +32,62 @@ class Product extends Auth{
 	 * 首页列表
 	 * 
 	 * */
-    public function index(Model $Model,AdminUser $AdminUser,Producer $Producer){
+    public function index(Model $Model,City $City,ProductCity $ProductCity){
 		// 接受参数
 		$code					= request('product_code','');
 		$name					= request('name','');
 		$status					= request('status');
 		$startTime				= request('start_time','');
+		$cityIds				= request('city_ids',[]);
 		// 编码转ID
 		$id						= $code ? $Model->codeToId($code) : 0;
 		// 查询条件
 		$map 					= [];
+		// 是否有城市
+		$wherIn					= empty($custom['city_id']) ? [] : $cityIds;
 		// 编码ID
-		if( $id )				$map[] = ['id','=',$id];
-		if( $name )				$map[] = ['name','like','%'.$name.'%'];
-		if( $startTime )		$map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
-		if( $startTime )		$map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
-		if( !is_null($status) )	$map[] = ['status','=',$status];
+		if( $id )				$map[] = ['product.id','=',$id];
+		if( $name )				$map[] = ['product.name','like','%'.$name.'%'];
+		if( $startTime )		$map[] = ['product.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
+		if( $startTime )		$map[] = ['product.insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
+		if( !is_null($status) )	$map[] = ['product.status','=',$status];
 		// 查询数据
-		$list					= $Model->query()->where($map)->orderBy('sort')->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
+		$list					= $Model->query();
+		if( $wherIn )			$list = $list->join('product_city','product_city.product_id','=','product.id')->whereIn('product_city.city_id',$wherIn);
+		$list 					= $list->groupBy('product.id')
+								->where($map)
+								->orderBy('product.sort')
+								->orderByDesc('product.id')
+								->select(['product.*'])
+								->paginate(request('limit',config('page_num',10)))->appends(request()->all());
 		// 循环处理数据
 		foreach ($list as $key => $value) {
 			// id转编号
 			$value['product_code']	= $Model->idToCode($value['id']);
-			// 创建人
-			$value['admin_name']	= $AdminUser->getOne($value['admin_uid'],'username');
-			// 生产厂家
-			$value['producer_name']	= $Producer->getOne($value['producer_id'],'name');
+			// id转编号
+			$cityName				= [];
+			// 获取城市列表
+			$cityIds				= $ProductCity->query()->where([['product_id','=',$value['id']]])->pluck('city_id');
+			// 如果有的话
+			if( $cityIds )			{
+				// 返回结果
+				foreach ($cityIds as $cityId) {
+					// 城市ID
+					if( $cityId == 1 ) continue;
+					// 获取城市名
+					$cityName[] = $City->getOne($cityId,'name');
+				}
+			}
+			// 城市
+			$value['city_name']		= implode('、',$cityName);
 			// 重组
 			$list[$key]			  	= $value;
 		}
+		// 获取列表
+		$cityList					= $City->getCityList();
 		// 分配数据
 		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
+		$this->assign('cityList',$cityList);
 		$this->assign('list', $list);
 		// 加载模板
 		return $this->fetch();
@@ -116,6 +141,8 @@ class Product extends Auth{
 			}
 			// 总库存
 			if( $skuList )			$data['stock'] = array_sum(array_column($skuList,'stock'));
+			// 总库存
+			$data['stock_total']	= $data['stock'];
 			// 获取规格属性
 			$specAttr				= $this->getSpecAttr($attr,$ProductSpec,true);
 			// 开启事务
@@ -168,9 +195,11 @@ class Product extends Auth{
 						}
 					}
 					// 转成好存储的数据
-					$value['attr_ids']	= implode(',',$value['attr_ids']);
+					$value['attr_ids']	 = implode(',',$value['attr_ids']);
 					// 转成好存储的数据
-					$value['product_id']= $id;
+					$value['product_id'] = $id;
+					// 转成好存储的数据
+					$value['stock_total']= $value['stock'];
 					// 转成好存储的数据
 					$value['insert_time']= time();
 					// 转成好存储的数据
@@ -272,6 +301,8 @@ class Product extends Auth{
 			}
 			// 总库存
 			if( $skuList )			$data['stock'] = array_sum(array_column($skuList,'stock'));
+			// 总库存
+			$data['stock_total']	= $data['stock'];
 			// 获取规格属性
 			$specAttr				= $this->getSpecAttr($attr,$ProductSpec,true);
 			// 开启事务
@@ -328,6 +359,8 @@ class Product extends Auth{
 					// 转成好存储的数据
 					$value['attr_ids']	= implode(',',$value['attr_ids']);
 					// 转成好存储的数据
+					$value['stock_total']= $value['stock'];
+					// 转成好存储的数据
 					$value['product_id']= $id;
 					// 查询
 					$oldSkuId			= $ProductSkus->query()->where([['product_id','=',$id],['attr_ids','=',$value['attr_ids']]])->value('id');

+ 1 - 0
resources/views/admin/orders/index.blade.php

@@ -95,6 +95,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 						<td>{{$a['contact_province']}}/{{$a['contact_city']}}/{{$a['contact_area']}}/{{$a['contact_addr']}} {{$a['contact_shop']}}</td>
 						<td>{{$a['state']}}</td>
 						<td>{{date('Y-m-d H:i:s',$a['insert_time']??0)}}</td>
+						<td>{{date('Y-m-d H:i:s',$a['update_time']??0)}}</td>
 						<td>
 							@if( $a['status'] != 8 && check_auth('admin/orders_transport/add') )
 							<a class="btn btn-sm btn-primary" href="{{url('admin/orders_transport/add?'.http_build_query(['order_code'=>$a['order_code']]))}}" title="查看">

+ 14 - 3
resources/views/admin/product/index.blade.php

@@ -22,6 +22,17 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 			<option value="1" @if (request('status') === '1' ) selected="selected" @endif >已下架</option>
 		</select>
 	</div>
+	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-12">
+		<select name="city_ids[]" class="form-control selectpicker" data-live-search="true" data-live-search-placeholder="搜索城市" data-none-results-text="未搜索到 {0}" title="选择城市" multiple>
+			@foreach ($cityList as $group)
+			<optgroup label="{{$group['name']}}">
+				@foreach ($group['city'] as $city)
+				<option value="{{$city['id']}}" @if ( in_array($city['id'],request('city_ids',[])) ) selected="selected" @endif >{{$city['name']}}</option>
+				@endforeach
+			</optgroup>
+			@endforeach
+		</select>
+	</div>
 	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-2" style="margin-right: 2px;">
 		<input type="date" class="form-control" name="start_time" value="{{request('start_time','')}}" placeholder="请输入创建时间查询" />
 	</div>
@@ -40,7 +51,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 						<th>产品价格</th>
 						<th>产品库存</th>
 						<th>产品状态</th>
-						<th>生产厂家</th>
+						<th>服务城市</th>
 						<th>排序</th>
 						<th>限购数量</th>
 						<th>修改时间</th>
@@ -54,9 +65,9 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 						<td> {{$a['name']}}</td>
 						<td> {{$a['spec']}}</td>
 						<td> ¥<del>{{$a['market_price']}}</del> ¥{{$a['price']}}</td>
-						<td> {{$a['stock']}} </td>
+						<td> {{$a['stock']}}/{{$a['stock_total']}}</td>
 						<td> {{$a['status']?'已下架':'已上架'}} </td>
-						<td> {{$a['producer_name']}}</td>
+						<td> {{$a['city_name']}}</td>
 						<td> 
 							@if(check_auth('admin/product/set_sort'))
 							<input type="text" class="set_sort" id="{{$a['id']}}" value="{{$a['sort']}}">