作者 lyh

Merge branch 'master' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

... ... @@ -212,4 +212,21 @@ class Arr extends \Illuminate\Support\Arr
}
return $data;
}
/**
* 根据列表查找指定id下的所有子id
* @param $dataArray
* @param $parentId
* @param $resultArray
* @author zbj
* @date 2023/10/17
*/
public static function findChildIds($dataArray, $parentId, &$resultArray) {
foreach ($dataArray as $value) {
if ($value['pid'] == $parentId) {
$resultArray[] = $value['id'];
self::findChildIds($dataArray, $value['id'], $resultArray);
}
}
}
}
... ...
... ... @@ -38,7 +38,7 @@ class CategoryController extends BaseController
if(!empty($list)){
foreach ($list as $k =>$v){
$v['url'] = $this->user['domain'] . $v['route'];
$v['product_num'] = Product::where('category_id','like' ,'%,'.$v['id'].',%')->where(['status'=>1])->count();
$v['product_num'] = Category::getProductNum($v['id']);
$v['image_link'] = getImageUrl($v['image']);
$list[$k] = $v;
}
... ... @@ -79,9 +79,9 @@ class CategoryController extends BaseController
$v = (array)$v;
if ($v['pid'] == 0) {
$v['sub'] = _get_child($v['id'], $list);
foreach ($v['sub'] as $sub){
$v['product_num'] += $sub['product_num'];
}
// foreach ($v['sub'] as $sub){
// $v['product_num'] += $sub['product_num'];
// }
$data[] = $v;
}
... ...
... ... @@ -41,9 +41,12 @@ class ProductController extends BaseController
public function index(Product $product)
{
$this->map = $this->searchParam();
$filed = ['id', 'project_id', 'title', 'sort' ,'thumb', 'gallery' ,'product_type' , 'route' ,
'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at'];
$lists = $product->lists($this->map,$this->page,$this->row,$this->order = ['sort','id'],$filed);
$lists = $product->product_lists($this->map,$this->page,$this->row,$this->order = ['sort','id'],$filed);
if(!empty($lists) && !empty($lists['list'])){
$cate_data = $this->getCategoryList();//分类
$key_data = $this->getKeywordsList();//关键字
... ... @@ -62,7 +65,6 @@ class ProductController extends BaseController
}
return $this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :搜索参数处理
* @name :searchParam
... ... @@ -74,9 +76,6 @@ class ProductController extends BaseController
if(isset($this->map['title']) && !empty($this->map['title'])){
$this->map['title'] = ['like','%'.$this->map['title'].'%'];
}
if(isset($this->map['category_id']) && !empty($this->map['category_id'])){
$this->map['category_id'] = ['like','%'.$this->map['category_id'].'%'];
}
$this->map['project_id'] = $this->user['project_id'];
return $this->map;
}
... ...
... ... @@ -6,6 +6,7 @@ use App\Helper\Arr;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;
... ... @@ -30,9 +31,10 @@ class CategoryLogic extends BaseLogic
$data = parent::getList($map, $sort, $columns, $limit);
foreach ($data as &$v){
$v['url'] = $this->user['domain'] . $v['route'] ;
$v['product_num'] = Product::where('category_id','like' ,'%,'.$v['category_id'].',%')->count();;
$v['product_num'] = Category::getProductNum($v['id']);
$v['image_link'] = getImageUrl($v['image']);
}
if(!$map){
$data = Arr::listToTree($data);
}
... ... @@ -64,10 +66,10 @@ class CategoryLogic extends BaseLogic
* @time :2023/8/21 17:14
*/
public function categorySave(){
$this->handleEditParam($this->param);
DB::beginTransaction();
try {
if(isset($this->param['id']) && !empty($this->param['id'])){
$this->handleEditParam($this->param);
//是否编辑路由
$id = $this->editCategoryRoute($this->param['id'],$this->param['route']);
$this->model->edit($this->param,['id'=>$this->param['id']]);
... ... @@ -119,19 +121,10 @@ class CategoryLogic extends BaseLogic
* @time :2023/8/21 17:43
*/
public function handleEditParam(&$param){
if($param['pid'] == $param['id']){
$this->fail('上级分类不能是本分类');
}
// $info = $this->model->read(['id'=>$param['id']]);
// $sub_info = $this->model->read(['pid'=>$param['id']]);
// if(($info['pid'] != $param['pid']) && ($sub_info != false)){
// $this->fail('当前分类拥有子分类,不允许修改上级分类');
// }
// $productModel = new Product();
// $product_info = $productModel->read(['category_id'=>['like','%'.$param['id'].'%']]);
// if(($product_info !== false) && ($info['pid'] != $param['pid'])){
// $this->fail('当前产品分类拥有产品不允许编辑上级分类');
// }
$category_ids = Category::getChildIdsArr($param['id']);
if(in_array($param['pid'], $category_ids)){
$this->fail('上级分类不能是本分类或子分类');
}
return $this->success();
}
... ...
... ... @@ -26,6 +26,9 @@ class ProductLogic extends BaseLogic
$this->model = new Product();
}
/**
* @remark :保存产品
* @name :productSave
... ... @@ -34,9 +37,10 @@ class ProductLogic extends BaseLogic
* @time :2023/8/21 18:35
*/
public function productSave(){
$category_ids = $this->param['category_id'] ?? [];
//参数处理
$this->param = $this->handleSaveParam($this->param);
DB::beginTransaction();
DB::connection('custom_mysql')->beginTransaction();
try {
if(isset($this->param['id']) && !empty($this->param['id'])){
//查看路由是否更新
... ... @@ -51,9 +55,12 @@ class ProductLogic extends BaseLogic
//路由映射
$route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$id]);
DB::commit();
//产品分类关联
CategoryRelated::saveRelated($id, $category_ids);
DB::connection('custom_mysql')->commit();
}catch (\Exception $e){
DB::rollBack();
DB::connection('custom_mysql')->rollBack();
$this->fail('系统错误请联系管理员');
}
//通知更新
... ... @@ -128,7 +135,7 @@ class ProductLogic extends BaseLogic
* @time :2023/8/21 17:11
*/
public function productDelete(){
DB::beginTransaction();
DB::connection('custom_mysql')->beginTransaction();
try {
foreach ($this->param['ids'] as $id) {
$info = $this->model->read(['id'=>$id],['id','status']);
... ... @@ -142,9 +149,9 @@ class ProductLogic extends BaseLogic
$this->model->edit(['status'=>Product::STATUS_RECYCLE],['id'=>$id]);
}
}
DB::commit();
DB::connection('custom_mysql')->commit();
}catch (\Exception $e){
DB::rollBack();
DB::connection('custom_mysql')->rollBack();
$this->fail('删除失败');
}
return $this->success();
... ... @@ -334,7 +341,7 @@ class ProductLogic extends BaseLogic
* @time :2023/8/15 17:53
*/
public function batchSetCategory(){
DB::beginTransaction();
DB::connection('custom_mysql')->beginTransaction();
try {
//批量
$param = [
... ... @@ -342,10 +349,16 @@ class ProductLogic extends BaseLogic
'status'=>$this->param['status']
];
$this->model->edit($param,['id'=>['in',$this->param['id']]]);
DB::commit();
//分类关联
foreach ($this->param['id'] as $id){
CategoryRelated::saveRelated($id, $this->param['category_id']);
}
DB::connection('custom_mysql')->commit();
//对应添加关联表
}catch (\Exception $e){
DB::rollBack();
DB::connection('custom_mysql')->rollBack();
$this->fail('系统错误,请联系管理员');
}
return $this->success();
... ...
... ... @@ -3,6 +3,7 @@
namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
... ... @@ -21,4 +22,32 @@ class Category extends Base
const STATUS_ACTIVE = 1;
/**
* 获取指定分类的所有子分类IDS(包括自己)
* @param $id
* @return array
* @author zbj
* @date 2023/10/17
*/
public static function getChildIdsArr($id)
{
$list = self::where('status', self::STATUS_ACTIVE)->select(['id','pid'])->get()->toArray();
$ids = [];
Arr::findChildIds($list, $id, $ids);
$ids[] = $id; //包含自己
return $ids;
}
/**
* 关联产品数量
* @param $cate_id
* @return mixed
* @author zbj
* @date 2023/4/28
*/
public static function getProductNum($cate_id){
$cate_ids = self::getChildIdsArr($cate_id);
$product_ids = CategoryRelated::whereIn('cate_id', $cate_ids)->groupBy('product_id')->select(['product_id'])->get();
return count($product_ids);
}
}
... ...
... ... @@ -40,6 +40,44 @@ class Product extends Base
];
/**
* 产品列表
* @param $map
* @param $page
* @param $row
* @param string $order
* @param string[] $fields
* @param string $sort
* @return array
* @author zbj
* @date 2023/10/17
*/
public function product_lists($map, $page, $row, $order = 'id', $fields = ['*'], $sort = 'desc'): array
{
$category_id = $map['category_id'] ?? 0;
$query = $this->where(function ($query) use ($category_id) {
if ($category_id) {
$category_ids = Category::getChildIdsArr($category_id);
$query->whereIn('id', function ($subQuery) use ($category_ids) {
$subQuery->select('product_id')
->from('gl_product_category_related')
->whereIn('cate_id', $category_ids)
->groupBy('product_id');
});
}
});
unset($map['category_id']);
$query = $this->formatQuery($map, $query);
$query = $this->sortOrder($query,$order,$sort);
$lists = $query->select($fields)->paginate($row, ['*'], 'page', $page);
if (empty($lists)) {
return [];
}
$lists = $lists->toArray();
return $lists;
}
public function getThumbAttribute($value){
if(!empty($value)){
$value = json_decode($value,true);
... ...