作者 liyuhang

gx

... ... @@ -4,6 +4,8 @@ namespace App\Http\Controllers\Bside\News;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsCategoryLogic;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Requests\Bside\News\NewsCategoryRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
... ... @@ -20,7 +22,7 @@ class NewsCategoryController extends BaseController
*/
public function lists(NewsCategoryModel $newsCategory){
//搜索条件
$lists = $newsCategory->lists($this->map,$this->page,$this->row,$this->order);
$lists = $newsCategory->lists($this->map,$this->page,$this->row,'sort');
$this->response('success',Code::SUCCESS,$lists);
}
... ... @@ -48,37 +50,12 @@ class NewsCategoryController extends BaseController
* @author :liyuhang
* @method
*/
public function add(NewsCategoryRequest $request,NewsCategoryModel $newsCategoryModel,NewsModel $newsModel){
public function add(NewsCategoryRequest $request,NewsCategoryLogic $newsCategoryLogic){
$request->validated();
$this->param['project_id'] = $this->user['project_id'];
$this->param['operator_id'] = $this->uid;
$this->param['create_id'] = $this->uid;
DB::beginTransaction();
$rs = $newsCategoryModel->add($this->param);
if($rs === false){
DB::rollBack();
$this->response('error',Code::USER_ERROR);
}
//判断当前分内是否为一级分类
if(isset($this->param['pid']) && !empty($this->param['pid'])){
//查看当前上级分类下是否有其他分类
$cate_info = $newsCategoryModel->read(['pid'=>$this->param['pid'],'id'=>['!=',$newsCategoryModel->id]]);
if($cate_info === false){
//查看当前上一级分类下是否有商品
$news_info = $newsModel->read(['category_id'=>$this->param['pid'],'pid'=>0]);
if($news_info !== false){
//更新所有商品到当前分类
$rs = $newsModel->edit(['category_id'=>$newsCategoryModel->id],['category_id'=>$this->param['pid']]);
if($rs === false){
DB::rollBack();
$this->response('error',Code::USER_ERROR);
}
}
}
}
DB::commit();
//添加商品时,验证分类上级分类是否有商品,有则更新到当前分类中,没有时直接添加
$newsCategoryLogic->add_news_category();
//TODO::写入日志
$this->response('success',Code::SUCCESS);
$this->response('success',Code::SUCCESS,[]);
}
/**
... ...
... ... @@ -79,6 +79,8 @@ class NewsController extends BaseController
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['project_id'] = $this->user['project_id'];
//多个分类按逗号隔开
$this->param['category_id'] = ','.$this->param['category_id'].',';
//TODO::路由映射
$rs = $news->add($this->param);
if($rs === false){
... ... @@ -101,6 +103,8 @@ class NewsController extends BaseController
'id.required' => 'ID不能为空',
]);
$this->param['operator_id'] = $this->uid;
//多个分类按逗号隔开
$this->param['category_id'] = ','.$this->param['category_id'].',';
$rs = $news->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->response('error',Code::USER_ERROR);
... ...
<?php
namespace App\Http\Logic\Bside\News;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use Illuminate\Support\Facades\DB;
class NewsCategoryLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new NewsCategoryModel();
}
/**
* @name :添加时验证上级分类是否有商品,有则替换带当前分类下
* @return void
* @author :liyuhang
* @method
*/
public function add_news_category(){
DB::beginTransaction();
$this->param = $this->requestAll;
$this->param['project_id'] = $this->user['project_id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['create_id'] = $this->user['id'];
$this->param['created_at'] = date('Y-m-d H:i:s');
$this->param['updated_at'] = date('Y-m-d H:i:s');
$cate_id = $this->model->insertGetId($this->param);
if($cate_id === false){
DB::rollBack();
$this->fail('error',Code::USER_ERROR);
}
//判断为子分类时
if(isset($this->param['pid']) && !empty($this->param['pid'])) {
//查看当前上级分类下是否有其他分类
$cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
if ($cate_info === false) {
//查看当前上一级分类下是否有新闻
$newsModel = new NewsModel();
$news_info = $newsModel->read(['category_id' => ['like', ',' . $this->param['pid'] . ',']]);
if ($news_info !== false) {
$replacement = ',' . $cate_id . ',';
$old = ',' . $this->param['pid'] . ',';
//更新所有商品到当前分类
$rs = DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')
->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
if ($rs === false) {
DB::rollBack();
$this->fail('error', Code::USER_ERROR);
}
}
}
}
DB::commit();
$this->success();
}
}
... ...
<?php
namespace App\Http\Logic\Bside\News;
use App\Http\Logic\Bside\BaseLogic;
class NewsLogic extends BaseLogic
{
}
... ...
... ... @@ -24,7 +24,7 @@ class NewsCategoryRequest extends FormRequest
public function rules()
{
return [
'name'=>'required|max:100',
'name'=>'required|max:100||unique:gl_news_category',
];
}
... ...
... ... @@ -19,7 +19,7 @@ class ProjectGroupRequest extends FormRequest
public function rules()
{
return [
'name' => 'required|max:255',
'name' => 'required|max:255||unique:gl_project_group',
];
}
... ...
... ... @@ -19,7 +19,7 @@ class ProjectRoleRequest extends FormRequest
public function rules()
{
return [
'name'=>'required|max:11',
'name'=>'required|max:11||unique:gl_project_role',
'role_menu'=>'required|string',
];
}
... ...
... ... @@ -24,7 +24,7 @@ class UserRequest extends FormRequest
public function rules()
{
return [
'mobile'=>'required|string|max:11',
'mobile'=>'required|string|max:11||unique:gl_project_user',
'password'=>'required|string|min:5',
'name'=>'required|max:20',
'role_id'=>'required'
... ...