正在显示
7 个修改的文件
包含
120 行增加
和
32 行删除
| @@ -212,4 +212,21 @@ class Arr extends \Illuminate\Support\Arr | @@ -212,4 +212,21 @@ class Arr extends \Illuminate\Support\Arr | ||
| 212 | } | 212 | } |
| 213 | return $data; | 213 | return $data; |
| 214 | } | 214 | } |
| 215 | + | ||
| 216 | + /** | ||
| 217 | + * 根据列表查找指定id下的所有子id | ||
| 218 | + * @param $dataArray | ||
| 219 | + * @param $parentId | ||
| 220 | + * @param $resultArray | ||
| 221 | + * @author zbj | ||
| 222 | + * @date 2023/10/17 | ||
| 223 | + */ | ||
| 224 | + public static function findChildIds($dataArray, $parentId, &$resultArray) { | ||
| 225 | + foreach ($dataArray as $value) { | ||
| 226 | + if ($value['pid'] == $parentId) { | ||
| 227 | + $resultArray[] = $value['id']; | ||
| 228 | + self::findChildIds($dataArray, $value['id'], $resultArray); | ||
| 229 | + } | ||
| 230 | + } | ||
| 231 | + } | ||
| 215 | } | 232 | } |
| @@ -38,7 +38,7 @@ class CategoryController extends BaseController | @@ -38,7 +38,7 @@ class CategoryController extends BaseController | ||
| 38 | if(!empty($list)){ | 38 | if(!empty($list)){ |
| 39 | foreach ($list as $k =>$v){ | 39 | foreach ($list as $k =>$v){ |
| 40 | $v['url'] = $this->user['domain'] . $v['route']; | 40 | $v['url'] = $this->user['domain'] . $v['route']; |
| 41 | - $v['product_num'] = Product::where('category_id','like' ,'%,'.$v['id'].',%')->where(['status'=>1])->count(); | 41 | + $v['product_num'] = Category::getProductNum($v['id']); |
| 42 | $v['image_link'] = getImageUrl($v['image']); | 42 | $v['image_link'] = getImageUrl($v['image']); |
| 43 | $list[$k] = $v; | 43 | $list[$k] = $v; |
| 44 | } | 44 | } |
| @@ -79,9 +79,9 @@ class CategoryController extends BaseController | @@ -79,9 +79,9 @@ class CategoryController extends BaseController | ||
| 79 | $v = (array)$v; | 79 | $v = (array)$v; |
| 80 | if ($v['pid'] == 0) { | 80 | if ($v['pid'] == 0) { |
| 81 | $v['sub'] = _get_child($v['id'], $list); | 81 | $v['sub'] = _get_child($v['id'], $list); |
| 82 | - foreach ($v['sub'] as $sub){ | ||
| 83 | - $v['product_num'] += $sub['product_num']; | ||
| 84 | - } | 82 | +// foreach ($v['sub'] as $sub){ |
| 83 | +// $v['product_num'] += $sub['product_num']; | ||
| 84 | +// } | ||
| 85 | 85 | ||
| 86 | $data[] = $v; | 86 | $data[] = $v; |
| 87 | } | 87 | } |
| @@ -41,9 +41,12 @@ class ProductController extends BaseController | @@ -41,9 +41,12 @@ class ProductController extends BaseController | ||
| 41 | public function index(Product $product) | 41 | public function index(Product $product) |
| 42 | { | 42 | { |
| 43 | $this->map = $this->searchParam(); | 43 | $this->map = $this->searchParam(); |
| 44 | + | ||
| 44 | $filed = ['id', 'project_id', 'title', 'sort' ,'thumb', 'gallery' ,'product_type' , 'route' , | 45 | $filed = ['id', 'project_id', 'title', 'sort' ,'thumb', 'gallery' ,'product_type' , 'route' , |
| 45 | 'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at']; | 46 | 'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at']; |
| 46 | - $lists = $product->lists($this->map,$this->page,$this->row,$this->order = ['sort','id'],$filed); | 47 | + |
| 48 | + $lists = $product->product_lists($this->map,$this->page,$this->row,$this->order = ['sort','id'],$filed); | ||
| 49 | + | ||
| 47 | if(!empty($lists) && !empty($lists['list'])){ | 50 | if(!empty($lists) && !empty($lists['list'])){ |
| 48 | $cate_data = $this->getCategoryList();//分类 | 51 | $cate_data = $this->getCategoryList();//分类 |
| 49 | $key_data = $this->getKeywordsList();//关键字 | 52 | $key_data = $this->getKeywordsList();//关键字 |
| @@ -62,7 +65,6 @@ class ProductController extends BaseController | @@ -62,7 +65,6 @@ class ProductController extends BaseController | ||
| 62 | } | 65 | } |
| 63 | return $this->response('success',Code::SUCCESS,$lists); | 66 | return $this->response('success',Code::SUCCESS,$lists); |
| 64 | } | 67 | } |
| 65 | - | ||
| 66 | /** | 68 | /** |
| 67 | * @remark :搜索参数处理 | 69 | * @remark :搜索参数处理 |
| 68 | * @name :searchParam | 70 | * @name :searchParam |
| @@ -74,9 +76,6 @@ class ProductController extends BaseController | @@ -74,9 +76,6 @@ class ProductController extends BaseController | ||
| 74 | if(isset($this->map['title']) && !empty($this->map['title'])){ | 76 | if(isset($this->map['title']) && !empty($this->map['title'])){ |
| 75 | $this->map['title'] = ['like','%'.$this->map['title'].'%']; | 77 | $this->map['title'] = ['like','%'.$this->map['title'].'%']; |
| 76 | } | 78 | } |
| 77 | - if(isset($this->map['category_id']) && !empty($this->map['category_id'])){ | ||
| 78 | - $this->map['category_id'] = ['like','%'.$this->map['category_id'].'%']; | ||
| 79 | - } | ||
| 80 | $this->map['project_id'] = $this->user['project_id']; | 79 | $this->map['project_id'] = $this->user['project_id']; |
| 81 | return $this->map; | 80 | return $this->map; |
| 82 | } | 81 | } |
| @@ -6,6 +6,7 @@ use App\Helper\Arr; | @@ -6,6 +6,7 @@ use App\Helper\Arr; | ||
| 6 | use App\Helper\Common; | 6 | use App\Helper\Common; |
| 7 | use App\Http\Logic\Bside\BaseLogic; | 7 | use App\Http\Logic\Bside\BaseLogic; |
| 8 | use App\Models\Product\Category; | 8 | use App\Models\Product\Category; |
| 9 | +use App\Models\Product\CategoryRelated; | ||
| 9 | use App\Models\Product\Product; | 10 | use App\Models\Product\Product; |
| 10 | use App\Models\RouteMap\RouteMap; | 11 | use App\Models\RouteMap\RouteMap; |
| 11 | use Illuminate\Support\Facades\DB; | 12 | use Illuminate\Support\Facades\DB; |
| @@ -30,9 +31,10 @@ class CategoryLogic extends BaseLogic | @@ -30,9 +31,10 @@ class CategoryLogic extends BaseLogic | ||
| 30 | $data = parent::getList($map, $sort, $columns, $limit); | 31 | $data = parent::getList($map, $sort, $columns, $limit); |
| 31 | foreach ($data as &$v){ | 32 | foreach ($data as &$v){ |
| 32 | $v['url'] = $this->user['domain'] . $v['route'] ; | 33 | $v['url'] = $this->user['domain'] . $v['route'] ; |
| 33 | - $v['product_num'] = Product::where('category_id','like' ,'%,'.$v['category_id'].',%')->count();; | 34 | + $v['product_num'] = Category::getProductNum($v['id']); |
| 34 | $v['image_link'] = getImageUrl($v['image']); | 35 | $v['image_link'] = getImageUrl($v['image']); |
| 35 | } | 36 | } |
| 37 | + | ||
| 36 | if(!$map){ | 38 | if(!$map){ |
| 37 | $data = Arr::listToTree($data); | 39 | $data = Arr::listToTree($data); |
| 38 | } | 40 | } |
| @@ -64,10 +66,10 @@ class CategoryLogic extends BaseLogic | @@ -64,10 +66,10 @@ class CategoryLogic extends BaseLogic | ||
| 64 | * @time :2023/8/21 17:14 | 66 | * @time :2023/8/21 17:14 |
| 65 | */ | 67 | */ |
| 66 | public function categorySave(){ | 68 | public function categorySave(){ |
| 69 | + $this->handleEditParam($this->param); | ||
| 67 | DB::beginTransaction(); | 70 | DB::beginTransaction(); |
| 68 | try { | 71 | try { |
| 69 | if(isset($this->param['id']) && !empty($this->param['id'])){ | 72 | if(isset($this->param['id']) && !empty($this->param['id'])){ |
| 70 | - $this->handleEditParam($this->param); | ||
| 71 | //是否编辑路由 | 73 | //是否编辑路由 |
| 72 | $id = $this->editCategoryRoute($this->param['id'],$this->param['route']); | 74 | $id = $this->editCategoryRoute($this->param['id'],$this->param['route']); |
| 73 | $this->model->edit($this->param,['id'=>$this->param['id']]); | 75 | $this->model->edit($this->param,['id'=>$this->param['id']]); |
| @@ -119,19 +121,10 @@ class CategoryLogic extends BaseLogic | @@ -119,19 +121,10 @@ class CategoryLogic extends BaseLogic | ||
| 119 | * @time :2023/8/21 17:43 | 121 | * @time :2023/8/21 17:43 |
| 120 | */ | 122 | */ |
| 121 | public function handleEditParam(&$param){ | 123 | public function handleEditParam(&$param){ |
| 122 | - if($param['pid'] == $param['id']){ | ||
| 123 | - $this->fail('上级分类不能是本分类'); | 124 | + $category_ids = Category::getChildIdsArr($param['id']); |
| 125 | + if(in_array($param['pid'], $category_ids)){ | ||
| 126 | + $this->fail('上级分类不能是本分类或子分类'); | ||
| 124 | } | 127 | } |
| 125 | -// $info = $this->model->read(['id'=>$param['id']]); | ||
| 126 | -// $sub_info = $this->model->read(['pid'=>$param['id']]); | ||
| 127 | -// if(($info['pid'] != $param['pid']) && ($sub_info != false)){ | ||
| 128 | -// $this->fail('当前分类拥有子分类,不允许修改上级分类'); | ||
| 129 | -// } | ||
| 130 | -// $productModel = new Product(); | ||
| 131 | -// $product_info = $productModel->read(['category_id'=>['like','%'.$param['id'].'%']]); | ||
| 132 | -// if(($product_info !== false) && ($info['pid'] != $param['pid'])){ | ||
| 133 | -// $this->fail('当前产品分类拥有产品不允许编辑上级分类'); | ||
| 134 | -// } | ||
| 135 | return $this->success(); | 128 | return $this->success(); |
| 136 | } | 129 | } |
| 137 | 130 |
| @@ -26,6 +26,9 @@ class ProductLogic extends BaseLogic | @@ -26,6 +26,9 @@ class ProductLogic extends BaseLogic | ||
| 26 | $this->model = new Product(); | 26 | $this->model = new Product(); |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + | ||
| 29 | /** | 32 | /** |
| 30 | * @remark :保存产品 | 33 | * @remark :保存产品 |
| 31 | * @name :productSave | 34 | * @name :productSave |
| @@ -34,9 +37,10 @@ class ProductLogic extends BaseLogic | @@ -34,9 +37,10 @@ class ProductLogic extends BaseLogic | ||
| 34 | * @time :2023/8/21 18:35 | 37 | * @time :2023/8/21 18:35 |
| 35 | */ | 38 | */ |
| 36 | public function productSave(){ | 39 | public function productSave(){ |
| 40 | + $category_ids = $this->param['category_id']; | ||
| 37 | //参数处理 | 41 | //参数处理 |
| 38 | $this->param = $this->handleSaveParam($this->param); | 42 | $this->param = $this->handleSaveParam($this->param); |
| 39 | - DB::beginTransaction(); | 43 | + DB::connection('custom_mysql')->beginTransaction(); |
| 40 | try { | 44 | try { |
| 41 | if(isset($this->param['id']) && !empty($this->param['id'])){ | 45 | if(isset($this->param['id']) && !empty($this->param['id'])){ |
| 42 | //查看路由是否更新 | 46 | //查看路由是否更新 |
| @@ -51,9 +55,12 @@ class ProductLogic extends BaseLogic | @@ -51,9 +55,12 @@ class ProductLogic extends BaseLogic | ||
| 51 | //路由映射 | 55 | //路由映射 |
| 52 | $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']); | 56 | $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']); |
| 53 | $this->model->edit(['route'=>$route],['id'=>$id]); | 57 | $this->model->edit(['route'=>$route],['id'=>$id]); |
| 54 | - DB::commit(); | 58 | + //产品分类关联 |
| 59 | + CategoryRelated::saveRelated($id, $category_ids); | ||
| 60 | + | ||
| 61 | + DB::connection('custom_mysql')->commit(); | ||
| 55 | }catch (\Exception $e){ | 62 | }catch (\Exception $e){ |
| 56 | - DB::rollBack(); | 63 | + DB::connection('custom_mysql')->rollBack(); |
| 57 | $this->fail('系统错误请联系管理员'); | 64 | $this->fail('系统错误请联系管理员'); |
| 58 | } | 65 | } |
| 59 | //通知更新 | 66 | //通知更新 |
| @@ -128,7 +135,7 @@ class ProductLogic extends BaseLogic | @@ -128,7 +135,7 @@ class ProductLogic extends BaseLogic | ||
| 128 | * @time :2023/8/21 17:11 | 135 | * @time :2023/8/21 17:11 |
| 129 | */ | 136 | */ |
| 130 | public function productDelete(){ | 137 | public function productDelete(){ |
| 131 | - DB::beginTransaction(); | 138 | + DB::connection('custom_mysql')->beginTransaction(); |
| 132 | try { | 139 | try { |
| 133 | foreach ($this->param['ids'] as $id) { | 140 | foreach ($this->param['ids'] as $id) { |
| 134 | $info = $this->model->read(['id'=>$id],['id','status']); | 141 | $info = $this->model->read(['id'=>$id],['id','status']); |
| @@ -142,9 +149,9 @@ class ProductLogic extends BaseLogic | @@ -142,9 +149,9 @@ class ProductLogic extends BaseLogic | ||
| 142 | $this->model->edit(['status'=>Product::STATUS_RECYCLE],['id'=>$id]); | 149 | $this->model->edit(['status'=>Product::STATUS_RECYCLE],['id'=>$id]); |
| 143 | } | 150 | } |
| 144 | } | 151 | } |
| 145 | - DB::commit(); | 152 | + DB::connection('custom_mysql')->commit(); |
| 146 | }catch (\Exception $e){ | 153 | }catch (\Exception $e){ |
| 147 | - DB::rollBack(); | 154 | + DB::connection('custom_mysql')->rollBack(); |
| 148 | $this->fail('删除失败'); | 155 | $this->fail('删除失败'); |
| 149 | } | 156 | } |
| 150 | return $this->success(); | 157 | return $this->success(); |
| @@ -334,7 +341,7 @@ class ProductLogic extends BaseLogic | @@ -334,7 +341,7 @@ class ProductLogic extends BaseLogic | ||
| 334 | * @time :2023/8/15 17:53 | 341 | * @time :2023/8/15 17:53 |
| 335 | */ | 342 | */ |
| 336 | public function batchSetCategory(){ | 343 | public function batchSetCategory(){ |
| 337 | - DB::beginTransaction(); | 344 | + DB::connection('custom_mysql')->beginTransaction(); |
| 338 | try { | 345 | try { |
| 339 | //批量 | 346 | //批量 |
| 340 | $param = [ | 347 | $param = [ |
| @@ -342,10 +349,16 @@ class ProductLogic extends BaseLogic | @@ -342,10 +349,16 @@ class ProductLogic extends BaseLogic | ||
| 342 | 'status'=>$this->param['status'] | 349 | 'status'=>$this->param['status'] |
| 343 | ]; | 350 | ]; |
| 344 | $this->model->edit($param,['id'=>['in',$this->param['id']]]); | 351 | $this->model->edit($param,['id'=>['in',$this->param['id']]]); |
| 345 | - DB::commit(); | 352 | + |
| 353 | + //分类关联 | ||
| 354 | + foreach ($this->param['id'] as $id){ | ||
| 355 | + CategoryRelated::saveRelated($id, $this->param['category_id']); | ||
| 356 | + } | ||
| 357 | + | ||
| 358 | + DB::connection('custom_mysql')->commit(); | ||
| 346 | //对应添加关联表 | 359 | //对应添加关联表 |
| 347 | }catch (\Exception $e){ | 360 | }catch (\Exception $e){ |
| 348 | - DB::rollBack(); | 361 | + DB::connection('custom_mysql')->rollBack(); |
| 349 | $this->fail('系统错误,请联系管理员'); | 362 | $this->fail('系统错误,请联系管理员'); |
| 350 | } | 363 | } |
| 351 | return $this->success(); | 364 | return $this->success(); |
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace App\Models\Product; | 3 | namespace App\Models\Product; |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | +use App\Helper\Arr; | ||
| 6 | use App\Models\Base; | 7 | use App\Models\Base; |
| 7 | use Illuminate\Database\Eloquent\SoftDeletes; | 8 | use Illuminate\Database\Eloquent\SoftDeletes; |
| 8 | 9 | ||
| @@ -21,4 +22,31 @@ class Category extends Base | @@ -21,4 +22,31 @@ class Category extends Base | ||
| 21 | 22 | ||
| 22 | const STATUS_ACTIVE = 1; | 23 | const STATUS_ACTIVE = 1; |
| 23 | 24 | ||
| 25 | + /** | ||
| 26 | + * 获取指定分类的所有子分类IDS(包括自己) | ||
| 27 | + * @param $id | ||
| 28 | + * @return array | ||
| 29 | + * @author zbj | ||
| 30 | + * @date 2023/10/17 | ||
| 31 | + */ | ||
| 32 | + public static function getChildIdsArr($id) | ||
| 33 | + { | ||
| 34 | + $list = self::where('status', self::STATUS_ACTIVE)->select(['id','pid'])->get()->toArray(); | ||
| 35 | + $ids = []; | ||
| 36 | + Arr::findChildIds($list, $id, $ids); | ||
| 37 | + return $ids; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * 关联产品数量 | ||
| 42 | + * @param $cate_id | ||
| 43 | + * @return mixed | ||
| 44 | + * @author zbj | ||
| 45 | + * @date 2023/4/28 | ||
| 46 | + */ | ||
| 47 | + public static function getProductNum($cate_id){ | ||
| 48 | + $cate_ids = self::getChildIdsArr($cate_id); | ||
| 49 | + $product_ids = CategoryRelated::whereIn('cate_id', $cate_ids)->groupBy('product_id')->select(['product_id'])->get(); | ||
| 50 | + return count($product_ids); | ||
| 51 | + } | ||
| 24 | } | 52 | } |
| @@ -40,6 +40,44 @@ class Product extends Base | @@ -40,6 +40,44 @@ class Product extends Base | ||
| 40 | ]; | 40 | ]; |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | + /** | ||
| 44 | + * 产品列表 | ||
| 45 | + * @param $map | ||
| 46 | + * @param $page | ||
| 47 | + * @param $row | ||
| 48 | + * @param string $order | ||
| 49 | + * @param string[] $fields | ||
| 50 | + * @param string $sort | ||
| 51 | + * @return array | ||
| 52 | + * @author zbj | ||
| 53 | + * @date 2023/10/17 | ||
| 54 | + */ | ||
| 55 | + public function product_lists($map, $page, $row, $order = 'id', $fields = ['*'], $sort = 'desc'): array | ||
| 56 | + { | ||
| 57 | + $category_id = $map['category_id'] ?? 0; | ||
| 58 | + $query = $this->where(function ($query) use ($category_id) { | ||
| 59 | + if ($category_id) { | ||
| 60 | + $category_ids = Category::getChildIdsArr($category_id); | ||
| 61 | + $query->whereIn('id', function ($subQuery) use ($category_ids) { | ||
| 62 | + $subQuery->select('product_id') | ||
| 63 | + ->from('gl_product_category_related') | ||
| 64 | + ->whereIn('cate_id', $category_ids) | ||
| 65 | + ->groupBy('product_id'); | ||
| 66 | + }); | ||
| 67 | + } | ||
| 68 | + }); | ||
| 69 | + unset($map['category_id']); | ||
| 70 | + $query = $this->formatQuery($map, $query); | ||
| 71 | + $query = $this->sortOrder($query,$order,$sort); | ||
| 72 | + $lists = $query->select($fields)->paginate($row, ['*'], 'page', $page); | ||
| 73 | + if (empty($lists)) { | ||
| 74 | + return []; | ||
| 75 | + } | ||
| 76 | + $lists = $lists->toArray(); | ||
| 77 | + return $lists; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + | ||
| 43 | public function getThumbAttribute($value){ | 81 | public function getThumbAttribute($value){ |
| 44 | if(!empty($value)){ | 82 | if(!empty($value)){ |
| 45 | $value = json_decode($value,true); | 83 | $value = json_decode($value,true); |
-
请 注册 或 登录 后发表评论