文章目录
新增 AttrController.java AttrVo.java AttrServiceImpl.java AttrAttrgroupRelationEntity.java AttrEntity.java AttrGroupEntity.java
查询 AttrController.java AttrServiceImpl.java AttrRespVo.java
修改回显 AttrController.java AttrServiceImpl.java
修改提交 AttrController.java AttrServiceImpl.java
baseattr.vue
新增
AttrController.java
@RestController
@RequestMapping ( "product/attr" )
public class AttrController {
@Autowired
private AttrService attrService;
@RequestMapping ( "/save" )
public R save ( @RequestBody AttrVo attrVo) {
attrService. saveAttr ( attrVo) ;
return R . ok ( ) ;
}
}
AttrVo.java
package com. xd. cubemall. product. vo ;
import lombok. Data ;
@Data
public class AttrVo {
private static final long serialVersionUID = 1L ;
private Long id;
private String name;
private Integer searchType;
private String icon;
private String valueSelect;
private Integer attrType;
private Long enable;
private Integer categoryId;
private Integer showDesc;
private Long attrGroupId;
}
AttrServiceImpl.java
@Service ( "attrService" )
public class AttrServiceImpl extends ServiceImpl < AttrDao , AttrEntity > implements AttrService {
@Autowired
private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
@Override
public void saveAttr ( AttrVo attrVo) {
AttrEntity attrEntity = new AttrEntity ( ) ;
BeanUtils . copyProperties ( attrVo, attrEntity) ;
this . save ( attrEntity) ;
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity ( ) ;
attrAttrgroupRelationEntity. setAttrId ( attrEntity. getId ( ) ) ;
attrAttrgroupRelationEntity. setAttrGroupId ( attrVo. getAttrGroupId ( ) ) ;
attrAttrgroupRelationDao. insert ( attrAttrgroupRelationEntity) ;
}
}
AttrAttrgroupRelationEntity.java
package com. xd. cubemall. product. entity ;
import com. baomidou. mybatisplus. annotation. TableId ;
import com. baomidou. mybatisplus. annotation. TableName ;
import java. io. Serializable ;
import java. util. Date ;
import lombok. Data ;
@Data
@TableName ( "tb_attr_attrgroup_relation" )
public class AttrAttrgroupRelationEntity implements Serializable {
private static final long serialVersionUID = 1L ;
@TableId
private Long id;
private Long attrId;
private Long attrGroupId;
private Integer attrSort;
}
AttrEntity.java
package com. xd. cubemall. product. entity ;
import com. baomidou. mybatisplus. annotation. TableId ;
import com. baomidou. mybatisplus. annotation. TableName ;
import java. io. Serializable ;
import java. util. Date ;
import lombok. Data ;
@Data
@TableName ( "tb_attr" )
public class AttrEntity implements Serializable {
private static final long serialVersionUID = 1L ;
@TableId
private Long id;
private String name;
private Integer searchType;
private String icon;
private String valueSelect;
private Integer attrType;
private Long enable;
private Integer categoryId;
private Integer showDesc;
}
AttrGroupEntity.java
package com. xd. cubemall. product. entity ;
import com. baomidou. mybatisplus. annotation. TableField ;
import com. baomidou. mybatisplus. annotation. TableId ;
import com. baomidou. mybatisplus. annotation. TableName ;
import java. io. Serializable ;
import java. util. Date ;
import lombok. Data ;
@Data
@TableName ( "tb_attr_group" )
public class AttrGroupEntity implements Serializable {
private static final long serialVersionUID = 1L ;
@TableId
private Long id;
private String name;
private Integer sort;
private String descript;
private String icon;
private Integer categoryId;
@TableField ( exist = false )
private Long [ ] categoryPath;
}
查询
AttrController.java
@RestController
@RequestMapping ( "product/attr" )
public class AttrController {
@Autowired
private AttrService attrService;
@RequestMapping ( "/base/list/{categoryId}" )
public R list ( @RequestParam Map < String , Object > params, @PathVariable ( "categoryId" ) Long categoryId) {
PageUtils page = attrService. queryBaseAttrPage ( params, categoryId) ;
return R . ok ( ) . put ( "page" , page) ;
}
}
AttrServiceImpl.java
@Service ( "attrService" )
public class AttrServiceImpl extends ServiceImpl < AttrDao , AttrEntity > implements AttrService {
@Autowired
private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
@Autowired
private AttrGroupDao attrGroupDao;
@Autowired
private CategoryDao categoryDao;
@Override
public PageUtils queryBaseAttrPage ( Map < String , Object > params, Long categoryId) {
QueryWrapper < AttrEntity > queryWrapper = new QueryWrapper < > ( ) ;
if ( categoryId != 0 ) {
queryWrapper. eq ( "category_id" , categoryId) ;
}
String key = ( String ) params. get ( "key" ) ;
if ( StringUtils . isEmpty ( key) ) {
queryWrapper. and ( wrapper -> {
wrapper. eq ( "id" , key) . or ( ) . like ( "name" , key) ;
} ) ;
}
IPage < AttrEntity > page = this . page (
new Query < AttrEntity > ( ) . getPage ( params) ,
queryWrapper
) ;
PageUtils pageUtils = new PageUtils ( page) ;
List < AttrEntity > records = page. getRecords ( ) ;
List < AttrRespVo > respVos = records. stream ( ) . map ( attrEntity -> {
AttrRespVo attrRespVo = new AttrRespVo ( ) ;
BeanUtils . copyProperties ( attrEntity, attrRespVo) ;
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao. selectOne (
new QueryWrapper < AttrAttrgroupRelationEntity > ( ) . eq ( "attr_id" , attrEntity. getId ( ) )
) ;
if ( attrAttrgroupRelationEntity != null ) {
AttrGroupEntity attrGroupEntity = attrGroupDao. selectById ( attrAttrgroupRelationEntity. getAttrGroupId ( ) ) ;
if ( attrGroupEntity != null ) {
attrRespVo. setGroupName ( attrGroupEntity. getName ( ) ) ;
}
}
CategoryEntity categoryEntity = categoryDao. selectById ( attrEntity. getCategoryId ( ) ) ;
if ( categoryEntity != null ) {
attrRespVo. setCategoryName ( categoryEntity. getName ( ) ) ;
}
return attrRespVo;
} ) . collect ( Collectors . toList ( ) ) ;
pageUtils. setList ( respVos) ;
return pageUtils;
}
}
AttrRespVo.java
package com. xd. cubemall. product. vo ;
import lombok. Data ;
@Data
public class AttrRespVo extends AttrVo {
private String categoryName;
private String groupName;
private Long [ ] categoryPath;
}
修改回显
AttrController.java
@RequestMapping ( "/info/{id}" )
public R info ( @PathVariable ( "id" ) Long id) {
AttrRespVo attrRespVo = attrService. getAttrInfo ( id) ;
return R . ok ( ) . put ( "attr" , attrRespVo) ;
}
AttrServiceImpl.java
import com. xd. cubemall. common. utils. PageUtils ;
import com. xd. cubemall. common. utils. Query ;
import com. xd. cubemall. product. dao. AttrAttrgroupRelationDao ;
import com. xd. cubemall. product. dao. AttrGroupDao ;
import com. xd. cubemall. product. dao. CategoryDao ;
import com. xd. cubemall. product. entity. AttrAttrgroupRelationEntity ;
import com. xd. cubemall. product. entity. AttrGroupEntity ;
import com. xd. cubemall. product. entity. CategoryEntity ;
import com. xd. cubemall. product. service. CategoryService ;
import com. xd. cubemall. product. vo. AttrRespVo ;
import com. xd. cubemall. product. vo. AttrVo ;
import org. apache. commons. lang3. StringUtils ;
import org. springframework. beans. BeanUtils ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
import java. util. Map ;
import java. util. stream. Collectors ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. baomidou. mybatisplus. core. metadata. IPage ;
import com. baomidou. mybatisplus. extension. service. impl. ServiceImpl ;
import com. xd. cubemall. product. dao. AttrDao ;
import com. xd. cubemall. product. entity. AttrEntity ;
import com. xd. cubemall. product. service. AttrService ;
@Service ( "attrService" )
public class AttrServiceImpl extends ServiceImpl < AttrDao , AttrEntity > implements AttrService {
@Autowired
private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
@Autowired
private AttrGroupDao attrGroupDao;
@Autowired
private CategoryDao categoryDao;
@Autowired
private CategoryService categoryService;
@Override
public AttrRespVo getAttrInfo ( Long id) {
AttrRespVo attrRespVo = new AttrRespVo ( ) ;
AttrEntity attrEntity = this . getById ( id) ;
BeanUtils . copyProperties ( attrEntity, attrRespVo) ;
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao. selectOne (
new QueryWrapper < AttrAttrgroupRelationEntity > ( ) . eq ( "attr_id" , attrEntity. getId ( ) )
) ;
if ( attrAttrgroupRelationEntity != null ) {
attrRespVo. setAttrGroupId ( attrAttrgroupRelationEntity. getAttrGroupId ( ) ) ;
AttrGroupEntity attrGroupEntity = attrGroupDao. selectById ( attrAttrgroupRelationEntity. getAttrGroupId ( ) ) ;
if ( attrGroupEntity != null ) {
attrRespVo. setGroupName ( attrGroupEntity. getName ( ) ) ;
}
}
Long [ ] categoryPath = categoryService. findCategoryPath ( attrEntity. getCategoryId ( ) ) ;
attrRespVo. setCategoryPath ( categoryPath) ;
CategoryEntity categoryEntity = categoryDao. selectById ( attrEntity. getCategoryId ( ) ) ;
if ( categoryEntity != null ) {
attrRespVo. setCategoryName ( categoryEntity. getName ( ) ) ;
}
return attrRespVo;
}
}
修改提交
AttrController.java
@RequestMapping ( "/update" )
public R update ( @RequestBody AttrVo attrVo) {
attrService. updateAttr ( attrVo) ;
return R . ok ( ) ;
}
AttrServiceImpl.java
@Override
public void updateAttr ( AttrVo attrVo) {
AttrEntity attrEntity = new AttrEntity ( ) ;
BeanUtils . copyProperties ( attrVo, attrEntity) ;
this . updateById ( attrEntity) ;
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity ( ) ;
attrAttrgroupRelationEntity. setAttrId ( attrVo. getId ( ) ) ;
attrAttrgroupRelationEntity. setAttrGroupId ( attrVo. getAttrGroupId ( ) ) ;
Integer count = attrAttrgroupRelationDao. selectCount (
new QueryWrapper < AttrAttrgroupRelationEntity > ( ) . eq ( "attr_id" , attrVo. getId ( ) )
) ;
if ( count > 0 ) {
attrAttrgroupRelationDao. update (
attrAttrgroupRelationEntity,
new UpdateWrapper < AttrAttrgroupRelationEntity > ( ) . eq ( "attr_id" , attrVo. getId ( ) )
) ;
} else {
attrAttrgroupRelationDao. insert ( attrAttrgroupRelationEntity) ;
}
}
baseattr.vue
<template>
<el-row :gutter="20">
<el-col :span="6">
<category @tree-node-click="treenodeclick"></category>
</el-col>
<el-col :span="18">
<div class="mod-config">
<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
<el-form-item>
<el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
</el-form-item>
<el-form-item>
<el-button @click="getDataList()">查询</el-button>
<el-button type="success" @click="getAllDataList()">查询全部</el-button>
<el-button
type="primary"
@click="addOrUpdateHandle()"
>新增</el-button>
<el-button
v-if="isAuth('product:attr:delete')"
type="danger"
@click="deleteHandle()"
:disabled="dataListSelections.length <= 0"
>批量删除</el-button>
</el-form-item>
</el-form>
<el-table
:data="dataList"
border
v-loading="dataListLoading"
@selection-change="selectionChangeHandle"
style="width: 100%;"
>
<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
<el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
<el-table-column prop="name" header-align="center" align="center" label="属性名"></el-table-column>
<el-table-column
v-if="attrtype == 1"
prop="searchType"
header-align="center"
align="center"
label="可检索"
>
<template slot-scope="scope">
<i class="el-icon-success" v-if="scope.row.searchType==1"></i>
<i class="el-icon-error" v-else></i>
</template>
</el-table-column>
<el-table-column prop="valueType" header-align="center" align="center" label="值类型">
<template slot-scope="scope">
<el-tag type="success" v-if="scope.row.valueType==0">单选</el-tag>
<el-tag v-else>多选</el-tag>
</template>
</el-table-column>
<el-table-column prop="icon" header-align="center" align="center" label="图标"></el-table-column>
<el-table-column prop="valueSelect" header-align="center" align="center" label="可选值">
<template slot-scope="scope">
<el-tooltip placement="top">
<div slot="content">
<span v-for="(i,index) in scope.row.valueSelect.split(';')" :key="index">{{i}}<br/></span>
</div>
<el-tag>{{scope.row.valueSelect.split(";")[0]+" ..."}}</el-tag>
</el-tooltip>
</template>
</el-table-column>
<el-table-column prop="enable" header-align="center" align="center" label="启用">
<template slot-scope="scope">
<i class="el-icon-success" v-if="scope.row.enable==1"></i>
<i class="el-icon-error" v-else></i>
</template>
</el-table-column>
<el-table-column prop="categoryName" header-align="center" align="center" label="所属分类"></el-table-column>
<el-table-column
v-if="attrtype == 1"
prop="groupName"
header-align="center"
align="center"
label="所属分组"
></el-table-column>
<el-table-column v-if="attrtype == 1" prop="showDesc" header-align="center" align="center" label="快速展示">
<template slot-scope="scope">
<i class="el-icon-success" v-if="scope.row.showDesc==1"></i>
<i class="el-icon-error" v-else></i>
</template>
</el-table-column>
<el-table-column
fixed="right"
header-align="center"
align="center"
width="150"
label="操作"
>
<template slot-scope="scope">
<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
@size-change="sizeChangeHandle"
@current-change="currentChangeHandle"
:current-page="pageIndex"
:page-sizes="[10, 20, 50, 100]"
:page-size="pageSize"
:total="totalPage"
layout="total, sizes, prev, pager, next, jumper"
></el-pagination>
<!-- 弹窗, 新增 / 修改 -->
<add-or-update
:type="attrtype"
v-if="addOrUpdateVisible"
ref="addOrUpdate"
@refreshDataList="getDataList"
></add-or-update>
</div>
</el-col>
</el-row>
</template>
<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';
import Category from "../common/category";
import AddOrUpdate from "./attr-add-or-update";
export default {
//import引入的组件需要注入到对象中才能使用
components: { Category, AddOrUpdate },
props: {
attrtype: {
type: Number,
default: 1
}
},
data() {
return {
catId: 0,
type: 1,
dataForm: {
key: ""
},
dataList: [],
pageIndex: 1,
pageSize: 10,
totalPage: 0,
dataListLoading: false,
dataListSelections: [],
addOrUpdateVisible: false
};
},
activated() {
this.getDataList();
},
methods: {
//感知树节点被点击
treenodeclick(data, node, component) {
if (node.level == 3) {
this.catId = data.catId;
this.getDataList(); //重新查询
}
},
getAllDataList(){
this.catId = 0;
this.getDataList();
},
// 获取数据列表
getDataList() {
this.dataListLoading = true;
let type = this.attrtype == 0 ? "sale" : "base";
this.$http({
url: this.$http.adornUrl(`/product/attr/${type}/list/${this.catId}`),
method: "get",
params: this.$http.adornParams({
page: this.pageIndex,
limit: this.pageSize,
key: this.dataForm.key
})
}).then(({ data }) => {
if (data && data.code === 0) {
this.dataList = data.page.list;
this.totalPage = data.page.totalCount;
} else {
this.dataList = [];
this.totalPage = 0;
}
this.dataListLoading = false;
});
},
// 每页数
sizeChangeHandle(val) {
this.pageSize = val;
this.pageIndex = 1;
this.getDataList();
},
// 当前页
currentChangeHandle(val) {
this.pageIndex = val;
this.getDataList();
},
// 多选
selectionChangeHandle(val) {
this.dataListSelections = val;
},
// 新增 / 修改
addOrUpdateHandle(id) {
this.addOrUpdateVisible = true;
this.$nextTick(() => {
this.$refs.addOrUpdate.init(id);
});
},
// 删除
deleteHandle(id) {
var ids = id
? [id]
: this.dataListSelections.map(item => {
return item.id;
});
this.$confirm(
`确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
).then(() => {
this.$http({
url: this.$http.adornUrl("/product/attr/delete"),
method: "post",
data: this.$http.adornData(ids, false)
}).then(({ data }) => {
if (data && data.code === 0) {
this.$message({
message: "操作成功",
type: "success",
duration: 1500,
onClose: () => {
this.getDataList();
}
});
} else {
this.$message.error(data.msg);
}
});
});
}
}
};
</script>
<style scoped>
</style>