feat(cate): 更新商品分类逻辑并优化商品分类列表查询
This commit is contained in:
parent
3d55b3f505
commit
48bf661a78
@ -183,26 +183,34 @@ class StoreProductLogic extends BaseLogic
|
||||
/**
|
||||
* 更新商品分类
|
||||
*/
|
||||
public static function updateGoodsclass($id, $type = 0)
|
||||
public static function updateGoodsclass($cate_id, $type = 0)
|
||||
{
|
||||
$pid = StoreCategory::where('id', $id)->value('pid');
|
||||
$pid = StoreCategory::where('id', $cate_id)->value('pid');
|
||||
if ($pid) {
|
||||
$goodsclass = StoreCategory::where('id', $pid)->field('pid,children')->find();
|
||||
if ($goodsclass) {
|
||||
if (count($goodsclass['children']) >= 1) {
|
||||
if (!in_array($id, $goodsclass['children'])) {
|
||||
$arr = $goodsclass['children'];
|
||||
array_push($arr, $id);
|
||||
StoreCategory::where('id', $pid)->update(['children' => $arr]);
|
||||
if ($goodsclass['pid'] != 0 && $type == 0) {
|
||||
self::updateGoodsclass($pid, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
//查二级分类
|
||||
$StoreCategory = StoreCategory::where('id', $pid)->find();
|
||||
if ($StoreCategory) {
|
||||
$StoreCategory = StoreCategory::where('id', $StoreCategory['id'])->find();
|
||||
self::cate_update($cate_id,0,3);
|
||||
self::cate_update($pid,0,2);
|
||||
self::cate_update($StoreCategory['id'],0,1);
|
||||
} else {
|
||||
self::cate_update($cate_id,0,2);
|
||||
self::cate_update($pid,0,1);
|
||||
}
|
||||
} else {
|
||||
self::cate_update($cate_id,0,1);
|
||||
}
|
||||
}
|
||||
|
||||
public static function cate_update($cate_id=0,$product_id=0,$leve=1){
|
||||
$find=Db::name('store_product_cate')->where(['store_id' => 1, 'cate_id' => $cate_id,'leve'=>$leve])->find();
|
||||
if($find){
|
||||
Db::name('store_product_cate')->where('id',$find['id'])->inc('count',1)->update();
|
||||
}else{
|
||||
Db::name('store_product_cate')->insert(['store_id' => 1, 'cate_id' => $cate_id,'count'=>1,'leve'=>$leve,'create_time'=>time(),'update_time'=>time()]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 复制商品到门店
|
||||
*/
|
||||
|
@ -7,6 +7,7 @@ use app\admin\lists\BaseAdminDataLists;
|
||||
use app\common\model\cate\Cate;
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
use app\Request;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品分类列表
|
||||
@ -26,7 +27,7 @@ class CateLists extends BaseAdminDataLists implements ListsSearchInterface
|
||||
public function setSearch(): array
|
||||
{
|
||||
return [
|
||||
'=' => ['pid', 'name', 'data', 'pic', 'sort'],
|
||||
'=' => ['pid', 'name', 'data', 'store_id', 'sort'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -42,36 +43,27 @@ class CateLists extends BaseAdminDataLists implements ListsSearchInterface
|
||||
*/
|
||||
public function lists(): array
|
||||
{
|
||||
$pid=Request()->get('pid',0);
|
||||
if($pid==0){
|
||||
$lists = Cate::where($this->searchWhere)
|
||||
->where('pid', 0)
|
||||
->where('children','<>',null)
|
||||
->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order(['sort' => 'desc','id' => 'desc'])
|
||||
->select()->toArray();
|
||||
}else{
|
||||
$three=Request()->get('three',0);
|
||||
if($three==0){
|
||||
$where[]=['children','<>',''];
|
||||
$this->where=$where;
|
||||
}
|
||||
$lists = Cate::where($this->searchWhere)
|
||||
->where($this->where)
|
||||
->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order(['sort' => 'desc','id' => 'desc'])
|
||||
->select()->toArray();
|
||||
$leve = Request()->get('leve', 1);
|
||||
$this->searchWhere[] = ['leve', '=', $leve];
|
||||
$this->searchWhere[] = ['count', '>', 0];
|
||||
$cate_arr = Db::name('store_product_cate')->where($this->searchWhere)->column('cate_id');
|
||||
$lists = [];
|
||||
if ($cate_arr) {
|
||||
return Cate::where('id', 'in', $cate_arr)
|
||||
->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order(['sort' => 'desc', 'id' => 'desc'])
|
||||
->select()->toArray();
|
||||
}
|
||||
// ->each(function ($item) {
|
||||
// $a = Goodsclass::where('pid', $item['id'])->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])->select();
|
||||
// $item['children'] = $a;
|
||||
// foreach ($a as $k => &$v) {
|
||||
// $b = Goodsclass::where('pid', $v['id'])->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])->select();
|
||||
// $v['children'] = $b;
|
||||
// }
|
||||
// })->toArray();
|
||||
|
||||
// ->each(function ($item) {
|
||||
// $a = Goodsclass::where('pid', $item['id'])->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])->select();
|
||||
// $item['children'] = $a;
|
||||
// foreach ($a as $k => &$v) {
|
||||
// $b = Goodsclass::where('pid', $v['id'])->field(['id', 'pid', 'name', 'data', 'pic', 'sort'])->select();
|
||||
// $v['children'] = $b;
|
||||
// }
|
||||
// })->toArray();
|
||||
|
||||
return $lists;
|
||||
// return linear_to_tree($lists, 'children');
|
||||
@ -87,17 +79,8 @@ class CateLists extends BaseAdminDataLists implements ListsSearchInterface
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
$pid=Request()->get('pid',0);
|
||||
if($pid==0){
|
||||
return Cate::where($this->searchWhere)->where('pid', 0)
|
||||
->where('children','<>',null)
|
||||
->count();
|
||||
}else{
|
||||
return Cate::where($this->searchWhere)
|
||||
->where($this->where)
|
||||
->count();
|
||||
}
|
||||
|
||||
return Db::name('store_product_cate')->where($this->searchWhere)
|
||||
->count();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user