refactor: banner新增相关分类数据字段
This commit is contained in:
@@ -3,8 +3,11 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\exception\InvalidOperateException;
|
||||
use app\admin\model\v1\SysBannerItemModel;
|
||||
use app\admin\model\v1\SysBannerProdCateMappingModel;
|
||||
use app\admin\validate\v1\SysBannerItemValidate;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 横幅数据项控制器
|
||||
@@ -67,7 +70,8 @@ class BannerItem
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$banner_item = SysBannerItemModel::withoutField([
|
||||
$banner_item = SysBannerItemModel::with('prod_mapping.category')
|
||||
->withoutField([
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
@@ -88,6 +92,7 @@ class BannerItem
|
||||
{
|
||||
$post = request()->post([
|
||||
'banner_id',
|
||||
'rel_prod_cate_id',
|
||||
'title',
|
||||
'title_txt_color',
|
||||
'desc',
|
||||
@@ -106,8 +111,29 @@ class BannerItem
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$banner_item = SysBannerItemModel::create($post);
|
||||
if ($banner_item->isEmpty()) {
|
||||
SysBannerItemModel::startTrans();
|
||||
try {
|
||||
$banner_item = SysBannerItemModel::create($post);
|
||||
if ($banner_item->isEmpty()) {
|
||||
throw new InvalidOperateException('新增横幅失败');
|
||||
}
|
||||
if (!empty($post['rel_prod_cate_id'])) {
|
||||
$mapping = SysBannerProdCateMappingModel::create([
|
||||
'banner_item_id' => $banner_item->id,
|
||||
'product_category_id' => $post['rel_prod_cate_id']
|
||||
]);
|
||||
if ($mapping->isEmpty()) {
|
||||
throw new InvalidOperateException('新增横幅与产品分类关联失败');
|
||||
}
|
||||
}
|
||||
|
||||
SysBannerItemModel::commit();
|
||||
} catch (InvalidOperateException $e) {
|
||||
SysBannerItemModel::rollback();
|
||||
return error($e->getMessage());
|
||||
} catch (\Throwable $th) {
|
||||
SysBannerItemModel::rollback();
|
||||
Log::error(sprintf('%s:%s %s', $th->getFile(), $th->getLine(), $th->getMessage()));
|
||||
return error('操作失败');
|
||||
}
|
||||
|
||||
@@ -120,6 +146,7 @@ class BannerItem
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'banner_id',
|
||||
'rel_prod_cate_id',
|
||||
'title',
|
||||
'title_txt_color',
|
||||
'desc',
|
||||
@@ -138,17 +165,45 @@ class BannerItem
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$banner_item = SysBannerItemModel::bypk($id)->find();
|
||||
if (empty($banner_item)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
if ($put['type'] == 'video') {
|
||||
unset($put['link']);
|
||||
unset($put['link_to']);
|
||||
}
|
||||
if (!$banner_item->save($put)) {
|
||||
SysBannerItemModel::startTrans();
|
||||
try {
|
||||
$banner_item = SysBannerItemModel::bypk($id)->find();
|
||||
if (empty($banner_item)) {
|
||||
throw new InvalidOperateException('请确认操作对象是否存在');
|
||||
}
|
||||
if ($put['type'] == 'video') {
|
||||
unset($put['link']);
|
||||
unset($put['link_to']);
|
||||
}
|
||||
if (!$banner_item->save($put)) {
|
||||
throw new InvalidOperateException('操作失败');
|
||||
}
|
||||
|
||||
// 更新关联的产品分类
|
||||
if (!empty($put['rel_prod_cate_id'])) {
|
||||
$ok = SysBannerProdCateMappingModel::where('banner_item_id', '=', $id)->delete();
|
||||
if (!$ok) {
|
||||
throw new InvalidOperateException('更新横幅与产品分类关联失败');
|
||||
}
|
||||
$mapping = SysBannerProdCateMappingModel::create([
|
||||
'banner_item_id' => $id,
|
||||
'product_category_id' => $put['rel_prod_cate_id']
|
||||
]);
|
||||
if ($mapping->isEmpty()) {
|
||||
throw new InvalidOperateException('更新横幅与产品分类关联失败');
|
||||
}
|
||||
}
|
||||
|
||||
SysBannerItemModel::commit();
|
||||
} catch (InvalidOperateException $e) {
|
||||
SysBannerItemModel::rollback();
|
||||
return error($e->getMessage());
|
||||
} catch (\Throwable $th) {
|
||||
SysBannerItemModel::rollback();
|
||||
Log::error(sprintf('%s:%s %s', $th->getFile(), $th->getLine(), $th->getMessage()));
|
||||
return error('操作失败');
|
||||
}
|
||||
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
|
||||
@@ -17,12 +17,34 @@ class SysBannerItemModel extends SysBannerItemBaseModel
|
||||
// 软删除字段
|
||||
protected $deleteTime = 'deleted_at';
|
||||
|
||||
// 绑定产品分类关联模型中字段到父模型
|
||||
protected $append = ['rel_prod_cate_id', 'rel_prod_cate_name'];
|
||||
// 要隐藏的字段或关联模型数据字段
|
||||
protected $hidden = ['prod_mapping'];
|
||||
|
||||
// 关联分类
|
||||
public function banner()
|
||||
{
|
||||
return $this->belongsTo(SysBannerModel::class, 'banner_id', 'id');
|
||||
}
|
||||
|
||||
// 关联相关产品分类中间表模型
|
||||
public function prodMapping()
|
||||
{
|
||||
return $this->hasOne(SysBannerProdCateMappingModel::class, 'banner_item_id', 'id');
|
||||
}
|
||||
|
||||
// 从产品分类关联模型中获取id字段
|
||||
public function getRelProdCateIdAttr()
|
||||
{
|
||||
return $this->prodMapping?->category?->id;
|
||||
}
|
||||
// 从产品分类关联模型中获取name字段
|
||||
public function getRelProdCateNameAttr()
|
||||
{
|
||||
return $this->prodMapping?->category?->name;
|
||||
}
|
||||
|
||||
// 按横幅标题搜索
|
||||
public function searchTitleAttr($query, $value, $data)
|
||||
{
|
||||
|
||||
19
app/admin/model/v1/SysBannerProdCateMappingModel.php
Normal file
19
app/admin/model/v1/SysBannerProdCateMappingModel.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\SysBannerProdCateMappingBaseModel;
|
||||
|
||||
/**
|
||||
* banner与产品分类关联表模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysBannerProdCateMappingModel extends SysBannerProdCateMappingBaseModel
|
||||
{
|
||||
// 关联产品分类
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(ProductCategoryModel::class, 'product_category_id', 'id');
|
||||
}
|
||||
}
|
||||
22
app/common/model/SysBannerProdCateMappingBaseModel.php
Normal file
22
app/common/model/SysBannerProdCateMappingBaseModel.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* banner与产品分类关联表模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class SysBannerProdCateMappingBaseModel extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'sys_banner_prod_cate_mapping';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'banner_item_id' => 'int',
|
||||
'product_category_id' => 'int',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
|
||||
class CreateSysBannerProdCateMapping extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('sys_banner_prod_cate_mapping', ['id' => false, 'engine' => 'InnoDB', 'comment' => 'banner与产品分类关联表']);
|
||||
$table->addColumn('banner_item_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'comment' => '横幅项id'])
|
||||
->addColumn('product_category_id', 'integer', ['limit' => 11, 'null' => false,'signed' => false, 'comment' => '产品分类id'])
|
||||
->addIndex(['banner_item_id'], ['unique' => false])
|
||||
->addIndex(['product_category_id'], ['unique' => false])
|
||||
->create();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user