作者 Your Name

gx

  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside\News;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Http\Logic\Bside\BaseLogic;
  7 +use App\Models\News\News as NewsModel;
  8 +use App\Models\News\NewsCategory as NewsCategoryModel;
  9 +use Illuminate\Support\Facades\DB;
  10 +
  11 +class NewsCategoryLogic extends BaseLogic
  12 +{
  13 + public function __construct()
  14 + {
  15 + parent::__construct();
  16 +
  17 + $this->model = new NewsCategoryModel();
  18 + }
  19 + /**
  20 + * @name :添加时验证上级分类是否有商品,有则替换带当前分类下
  21 + * @return void
  22 + * @author :liyuhang
  23 + * @method
  24 + */
  25 + public function add_news_category(){
  26 + DB::beginTransaction();
  27 + $this->param = $this->requestAll;
  28 + $this->param['project_id'] = $this->user['project_id'];
  29 + $this->param['operator_id'] = $this->user['id'];
  30 + $this->param['create_id'] = $this->user['id'];
  31 + $this->param['created_at'] = date('Y-m-d H:i:s');
  32 + $this->param['updated_at'] = date('Y-m-d H:i:s');
  33 + $cate_id = $this->model->insertGetId($this->param);
  34 + if($cate_id === false){
  35 + DB::rollBack();
  36 + $this->fail('error',Code::USER_ERROR);
  37 + }
  38 + //判断为子分类时
  39 + if(isset($this->param['pid']) && !empty($this->param['pid'])) {
  40 + //查看当前上级分类下是否有其他分类
  41 + $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
  42 + if ($cate_info === false) {
  43 + //查看当前上一级分类下是否有新闻
  44 + $newsModel = new NewsModel();
  45 + $news_info = $newsModel->read(['category_id' => ['like', ',' . $this->param['pid'] . ',']]);
  46 + if ($news_info !== false) {
  47 + $replacement = ',' . $cate_id . ',';
  48 + $old = ',' . $this->param['pid'] . ',';
  49 + //更新所有商品到当前分类
  50 + $rs = DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')
  51 + ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
  52 + if ($rs === false) {
  53 + DB::rollBack();
  54 + $this->fail('error', Code::USER_ERROR);
  55 + }
  56 + }
  57 + }
  58 + }
  59 + DB::commit();
  60 + $this->success();
  61 + }
  62 +}
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside\News;
  4 +
  5 +use App\Http\Logic\Bside\BaseLogic;
  6 +
  7 +class NewsLogic extends BaseLogic
  8 +{
  9 +
  10 +}