|
@@ -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');
|