123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\Product as Request;
- use App\Models\AdminUser;
- use App\Models\Business;
- use App\Models\City;
- use App\Models\Producer;
- use App\Models\Product as Model;
- use Illuminate\Support\Carbon;
- use App\Models\Product\Spec as ProductSpec;
- use App\Models\Product\Type as ProductType;
- use App\Models\Product\Attr as ProductAttr;
- use App\Models\Product\Skus as ProductSkus;
- use App\Models\Product\City as ProductCity;
- use Illuminate\Support\Facades\DB;
- /**
- * 产品管理
- *
- * @author 刘相欣
- *
- */
- class Product extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','基础信息');
- $this->assign('breadcrumb2','产品管理');
- }
-
- /**
- * 首页列表
- *
- * */
- 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[] = ['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();
- 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']);
- // 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();
- }
- /**
- * 添加
- *
- * */
- public function add( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 组合数据
- $data['name'] = request('name','');
- $data['thumb'] = request('thumb','');
- $data['poster'] = request('poster','');
- $data['spec'] = request('spec','');
- $data['price'] = request('price',0);
- $data['market_price'] = request('market_price',0);
- $data['producer_id'] = request('producer_id',0);
- $data['business_id'] = request('business_id',0);
- $data['quota'] = request('quota',0);
- $data['quota_start'] = request('quota_start','');
- $data['quota_end'] = request('quota_end','');
- $data['puton_time'] = request('puton_time','');
- $data['putoff_time'] = request('putoff_time','');
- $data['stock'] = request('stock',0);
- $data['status'] = 1;
- $data['admin_uid'] = admin('uid');
- $description = request('description','');
- $attr = request('attr',[]);
- $skuList = request('sku',[]);
- $cityIds = request('city_ids',[]);
- // 如果没有选择,则意味着全部
- $cityIds = $cityIds ? $cityIds : [1];
- $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
- $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
- $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
- $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
- // 限购提示
- if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
- // 限购提示
- if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
- // 上下架
- if( $data['puton_time'] ) {
- // 下架时间必填
- if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
- // 下架时间必填
- if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
- }
- // 总库存
- if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
- // 总库存
- $data['stock_total'] = $data['stock'];
- // 获取规格属性
- $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
- // 开启事务
- DB::beginTransaction();
- try {
- // 写入
- $id = $Model->add($data);
- // 提示新增失败
- if( !$id ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'新增失败']);
- }
- // 组合数据
- foreach ($specAttr as $key => $value) {
- // 查询结果
- $value['id'] = $ProductAttr->upsertByName($value['name'],$id,$value['spec_id']);
- // 提示新增失败
- if( !$value['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
- }
- // 重组
- $specAttr[$key] = $value;
- }
- // 循环SKU
- foreach ($skuList as $attrNames => $value) {
- // 属性ID值
- $value['attr_ids'] = [];
- // 规格属性值组合
- $value['attr_names']= $attrNames;
- // 切割成数据
- $names = explode(',',$attrNames);
- // 循环处理
- foreach ($names as $name) {
- foreach ($specAttr as $vv) {
- if( $name == $vv['name'] ) {
- // 提示新增失败
- if( !$vv['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
- }
- $value['attr_ids'][] = $vv['id'];
- }
- }
- }
- // 转成好存储的数据
- $value['attr_ids'] = implode(',',$value['attr_ids']);
- // 转成好存储的数据
- $value['product_id'] = $id;
- // 转成好存储的数据
- $value['stock_total']= $value['stock'];
- // 转成好存储的数据
- $value['insert_time']= time();
- // 转成好存储的数据
- $value['update_time']= time();
- // SKU结果
- $skuList[$attrNames] = $value;
- }
- // 循环城市范围
- foreach ($cityIds as $key => $value) {
- // 重组数据
- $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
- }
- // 写入城市范围
- $ProductCity->query()->insert($cityIds);
- // 返回结果
- $result = $ProductSkus->insert($skuList);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'SKU插入失败']);
- }
- // 更新内容
- $Model->updateDesc($id,$description);
- // 提交
- DB::commit();
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
- } catch (\Throwable $th) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
- }
- }
- // 获取类型数据
- $typeList = $ProductType->getList();
- $cityList = $City->getCityList();
- $businessList = $Business->getList();
- $producerList = $Producer->getList();
- // 分配数据
- $this->assign('crumbs','新增');
- $this->assign('typeList',$typeList);
- $this->assign('cityList',$cityList);
- $this->assign('businessList',$businessList);
- $this->assign('producerList',$producerList);
- // 加载模板
- return $this->fetch();
- }
-
- /**
- * 编辑
- *
- * */
- public function edit( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
- if(request()->isMethod('post')){
- // 验证参数
- $request->scene('edit')->validate();
- // 组合数据
- $id = request('id',0);
- $data['name'] = request('name','');
- $data['thumb'] = request('thumb','');
- $data['poster'] = request('poster','');
- $data['spec'] = request('spec','');
- $data['price'] = request('price',0);
- $data['market_price'] = request('market_price',0);
- $data['quota'] = request('quota',0);
- $data['quota_start'] = request('quota_start','');
- $data['quota_end'] = request('quota_end','');
- $data['puton_time'] = request('puton_time','');
- $data['putoff_time'] = request('putoff_time','');
- $data['producer_id'] = request('producer_id',0);
- $data['business_id'] = request('business_id',0);
- $data['stock'] = request('stock',0);
- $data['status'] = 1;
- $description = request('description','');
- $attr = request('attr',[]);
- $skuList = request('sku',[]);
- $cityIds = request('city_ids',[]);
- // 如果没有选择,则意味着全部
- $cityIds = $cityIds ? $cityIds : [1];
- $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
- $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
- $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
- $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
- // 限购提示
- if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
- // 限购提示
- if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
- // 上下架
- if( $data['puton_time'] ) {
- // 下架时间必填
- if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
- // 下架时间必填
- if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
- }
- // 总库存
- if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
- // 总库存
- $data['stock_total'] = $data['stock'];
- // 获取规格属性
- $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
- // 开启事务
- DB::beginTransaction();
- try {
- // 写入
- $result = $Model->edit($id,$data);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'修改失败']);
- }
- // 组合数据
- foreach ($specAttr as $key => $value) {
- // 查询结果
- $value['id'] = $ProductAttr->upsertByName($value['name'],$id,$value['spec_id']);
- // 提示新增失败
- if( !$value['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
- }
- // 重组
- $specAttr[$key] = $value;
- }
- // 如果不在这个属性列表中的数据删除
- $ProductAttr->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($specAttr,'id'))->delete();
- // 循环SKU
- foreach ($skuList as $attrNames => $value) {
- // 属性ID值
- $value['attr_ids'] = [];
- // 规格属性值组合
- $value['attr_names']= $attrNames;
- // 切割成数据
- $names = explode(',',$attrNames);
- // 循环处理
- foreach ($names as $name) {
- foreach ($specAttr as $vv) {
- if( $name == $vv['name'] ) {
- // 提示新增失败
- if( !$vv['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
- }
- $value['attr_ids'][] = $vv['id'];
- }
- }
- }
- // 转成好存储的数据
- $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');
- // 判断是否存在ID
- $value['id'] = $oldSkuId ? $ProductSkus->edit($oldSkuId,$value) : $ProductSkus->add($value);
- // SKU结果
- $skuList[$attrNames]= $value;
- }
- // 如果不在这个属性列表中的数据删除
- $ProductSkus->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($skuList,'id'))->delete();
- // 获取之前的城市ID
- $oldCityIds = $ProductCity->getListByProductId($id);
- // 循环城市范围
- foreach ($cityIds as $key => $value) {
- // 不存在则新增
- if( !in_array($value,$oldCityIds) ) $ProductCity->add(['city_id'=>$value,'product_id'=>$id]);
- }
- // 不存在的城市删除
- $ProductCity->query()->where([['product_id','=',$id]])->whereNotIn('city_id',$cityIds)->delete();
- // 更新内容
- $Model->updateDesc($id,$description);
- // 提交
- DB::commit();
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
- } catch (\Throwable $th) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
- }
- }
- // 接收参数
- $id = request('id',0);
- // 查询数据
- $oldData = $Model->where(['id'=>$id])->first();
- // 如果是没有数据
- if( !$oldData ) return $this->error('查无数据');
- // 产品信息转格式
- $oldData = $oldData->toArray();
- $oldData['description'] = $Model->getDesc($id);
- $oldData['city_ids'] = $ProductCity->getListByProductId($id);
- // 获取产品属性
- $attrList = $ProductAttr->getListByProductId($id);
- // 规格属性
- $specList = [];
- // 获取数据
- foreach ($attrList as $value) {
- $value['active'] = 0;
- if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
- $specList[$value['spec_id']]['attr_list'][] = $value;
- }
- // 产品类型
- $skuList = $ProductSkus->getListByProductId($id);
- // 循环处理
- foreach ($skuList as $key => $value) {
- // 数据解析
- $value['attr_ids'] = explode(',',$value['attr_ids']);
- // 循环处理
- foreach ( $value['attr_ids'] as $k=>$attrId ) {
- // 获取ids对应的name
- $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
- }
- // 数据合并
- $value['attr_ids'] = implode(',',$value['attr_ids']);
- // 重组
- $skuList[$key] = $value;
- }
- // 获取类型数据
- $typeList = $ProductType->getList();
- $cityList = $City->getCityList();
- $businessList = $Business->getList();
- $producerList = $Producer->getList();
- // 分配数据
- $this->assign('typeList',$typeList);
- $this->assign('cityList',$cityList);
- $this->assign('businessList',$businessList);
- $this->assign('producerList',$producerList);
- $this->assign('oldData',$oldData);
- $this->assign('skuList',$skuList);
- $this->assign('specList',$specList);
- $this->assign('crumbs','修改');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 编辑
- *
- * */
- public function copy( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 组合数据
- $data['name'] = request('name','');
- $data['thumb'] = request('thumb','');
- $data['poster'] = request('poster','');
- $data['spec'] = request('spec','');
- $data['price'] = request('price',0);
- $data['market_price'] = request('market_price',0);
- $data['producer_id'] = request('producer_id',0);
- $data['business_id'] = request('business_id',0);
- $data['quota'] = request('quota',0);
- $data['quota_start'] = request('quota_start','');
- $data['quota_end'] = request('quota_end','');
- $data['stock'] = request('stock',0);
- $data['status'] = 1;
- $data['admin_uid'] = admin('uid');
- $description = request('description','');
- $attr = request('attr',[]);
- $skuList = request('sku',[]);
- $cityIds = request('city_ids',[]);
- // 如果没有选择,则意味着全部
- $cityIds = $cityIds ? $cityIds : [1];
- $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
- $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
- // 限购提示
- if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
- // 限购提示
- if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
- // 总库存
- if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
- // 获取规格属性
- $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
- // 开启事务
- DB::beginTransaction();
- try {
- // 写入
- $id = $Model->add($data);
- // 提示新增失败
- if( !$id ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'新增失败']);
- }
- // 组合数据
- foreach ($specAttr as $key => $value) {
- // 查询结果
- $value['id'] = $ProductAttr->upsertByName($value['name'],$id,$value['spec_id']);
- // 提示新增失败
- if( !$value['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
- }
- // 重组
- $specAttr[$key] = $value;
- }
- // 循环SKU
- foreach ($skuList as $attrNames => $value) {
- // 属性ID值
- $value['attr_ids'] = [];
- // 规格属性值组合
- $value['attr_names']= $attrNames;
- // 切割成数据
- $names = explode(',',$attrNames);
- // 循环处理
- foreach ($names as $name) {
- foreach ($specAttr as $vv) {
- if( $name == $vv['name'] ) {
- // 提示新增失败
- if( !$vv['id'] ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
- }
- $value['attr_ids'][] = $vv['id'];
- }
- }
- }
- // 转成好存储的数据
- $value['attr_ids'] = implode(',',$value['attr_ids']);
- // 转成好存储的数据
- $value['product_id']= $id;
- // 转成好存储的数据
- $value['insert_time']= time();
- // 转成好存储的数据
- $value['update_time']= time();
- // SKU结果
- $skuList[$attrNames] = $value;
- }
- // 循环城市范围
- foreach ($cityIds as $key => $value) {
- // 重组数据
- $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
- }
- // 写入城市范围
- $ProductCity->query()->insert($cityIds);
- // 返回结果
- $result = $ProductSkus->insert($skuList);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'SKU插入失败']);
- }
- // 更新内容
- $Model->updateDesc($id,$description);
- // 提交
- DB::commit();
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
- } catch (\Throwable $th) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
- }
- }
- // 接收参数
- $id = request('id',0);
- // 查询数据
- $oldData = $Model->where(['id'=>$id])->first();
- // 如果是没有数据
- if( !$oldData ) return $this->error('查无数据');
- // 产品信息转格式
- $oldData = $oldData->toArray();
- $oldData['description'] = $Model->getDesc($id);
- $oldData['city_ids'] = $ProductCity->getListByProductId($id);
- // 获取产品属性
- $attrList = $ProductAttr->getListByProductId($id);
- // 规格属性
- $specList = [];
- // 获取数据
- foreach ($attrList as $value) {
- $value['active'] = 0;
- if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
- $specList[$value['spec_id']]['attr_list'][] = $value;
- }
- // 产品类型
- $skuList = $ProductSkus->getListByProductId($id);
- // 循环处理
- foreach ($skuList as $key => $value) {
- // 数据解析
- $value['attr_ids'] = explode(',',$value['attr_ids']);
- // 循环处理
- foreach ( $value['attr_ids'] as $k=>$attrId ) {
- // 获取ids对应的name
- $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
- }
- // 数据合并
- $value['attr_ids'] = implode(',',$value['attr_ids']);
- // 重组
- $skuList[$key] = $value;
- }
- // 获取类型数据
- $typeList = $ProductType->getList();
- $cityList = $City->getCityList();
- $businessList = $Business->getList();
- $producerList = $Producer->getList();
- // 分配数据
- $this->assign('typeList',$typeList);
- $this->assign('cityList',$cityList);
- $this->assign('businessList',$businessList);
- $this->assign('producerList',$producerList);
- $this->assign('oldData',$oldData);
- $this->assign('skuList',$skuList);
- $this->assign('specList',$specList);
- $this->assign('crumbs','复制');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 状态
- *
- * */
- public function set_status( Request $request, Model $Model ){
- // 验证参数
- $request->scene('set_status')->validate();
- // 接收参数
- $id = request('id',0);
- $status = request('status',0);
- // 查询数据
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- /**
- * 获取某个类目下的规格
- *
- */
- public function get_spec_html( ProductSpec $ProductSpec){
- // 接收参数
- $typeId = request('type_id',0);
- // 查询数据
- $specList = $ProductSpec->getList();
- // 循环处理
- foreach ($specList as $key => $value) {
- if( $value['type_id'] != $typeId ) unset($specList[$key]);
- }
- // 分配数据
- $this->assign('specList',$specList);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 获取某个类目下的规格
- *
- */
- public function get_sku_html( ProductSpec $ProductSpec,ProductSkus $ProductSkus){
- // 接收参数
- $attr = request('attr',[]);
- $id = request('product_id',0);
- // 产品类型
- $oldSkus = $id ? $ProductSkus->getListByProductId($id) : [];
- // 获取规格属性
- $specAttr = $this->getSpecAttr($attr,$ProductSpec);
- // 组合SKU
- $brushList = [];
- // 循环规格属性
- foreach ($specAttr as $specId => $value) {
- // 获取SKU组合
- $brushList[$specId] = array_column($value['attr_list'],'name');
- }
- // sku列表
- $skuList = $this->brush([],$brushList);
- // 循环规格属性
- foreach ($skuList as $newKey => $new) {
- // 获取新数据
- $new = ['attr_names'=>$new,'price'=>0,'stock'=>0,'status'=>0];
- // 循环旧的sku
- foreach ($oldSkus as $oldKey => $old) {
- // 如果有相等的规格
- if( $old['attr_names'] == $new['attr_names']) {
- $new['price'] = $old['price'];
- $new['stock'] = $old['stock'];
- $new['status'] = $old['status'];
- }
- }
- $skuList[$newKey] = $new;
- }
- // 分配数据
- $this->assign('specAttr',$specAttr);
- $this->assign('skuList',$skuList);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 获取规格属性
- * @param ProductSpec $ProductSpec
- *
- */
- private function getSpecAttr($attr,$ProductSpec,$onlyList=false){
- // 组合参数
- $specAttr = [];
- // 循环处理数据
- foreach ($attr as $specId => $value) {
- $specAttr[$specId]['spec_id'] = $specId;
- $specAttr[$specId]['spec_name'] = $ProductSpec->getOne($specId,'name');
- $specAttr[$specId]['attr_list'] = [];
- foreach ($value['name'] as $key => $name) {
- // 如果没有名称的,跳过
- if( !$name ) continue;
- // 结果
- $temp = ['id'=>0,'spec_id'=>$specId,'thumb'=>'','name'=>'','remark'=>''];
- // 获取名称
- $temp['name'] = $name;
- // 获取对应的备注信息
- if( !empty($value['remark'][$key]) ) $temp['remark'] = $value['remark'][$key];
- if( !empty($value['thumb'][$key]) ) $temp['thumb'] = $value['thumb'][$key];
- // 获取对应的数据
- $specAttr[$specId]['attr_list'][]= $temp;
- }
- // 没有属性的规格,移除
- if( !$specAttr[$specId]['attr_list'] ) unset($specAttr[$specId]);
- }
- // 仅获取属性列表
- if( $onlyList ) {
- // 属性列表
- $attrList = [];
- // 判断结果
- foreach ($specAttr as $value) {
- // 获取数据
- array_push($attrList,...$value['attr_list']);
- }
- // 返回结果
- $specAttr = $attrList;
- }
- // 返回结果
- return $specAttr;
- }
- /**
- * 组合SKU
- *
- */
- private function brush($res = [], $arr = []){
- // 获取第一个规格的属性
- if (empty($res)) $res = (array) array_shift($arr);
- // 如果只有一个规格,返回该规格的属性即可
- if (empty($arr)) return $res;
- // 接下来要参与计算的一组属性
- $current = array_shift($arr);
- // 计算的列表
- $last = [];
- // 循环上一次已经算出的集合
- foreach ($res as $attr) {
- foreach ($current as $col_val) {
- $last[] = $attr . ',' . $col_val;
- }
- }
- return $this->brush($last,$arr); # 递归处理, 直到$arr滚到最后一组属性
- }
- /**
- * 排序
- *
- * */
- public function set_sort( Request $request, Model $Model ){
- // 验证参数
- $request->scene('set_sort')->validate();
- // 接收参数
- $id = request('id',0);
- $sort = request('sort',0);
- // 查询数据
- $result = $Model->edit($id,['sort'=>$sort]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['sort'=>$sort]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- }
|