提交便利店商品分类代码
This commit is contained in:
parent
c828a46a79
commit
1331e02754
@ -0,0 +1,62 @@
|
||||
package com.fuint.business.convenienceSore.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuint.business.convenienceSore.entity.CvsGoods;
|
||||
import com.fuint.business.convenienceSore.service.CvsGoodsService;
|
||||
import com.fuint.business.convenienceSore.vo.CvsGoodsVo;
|
||||
import com.fuint.framework.web.BaseController;
|
||||
import com.fuint.framework.web.ResponseObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/16
|
||||
* 便利店商品管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/business/cvsGoods")
|
||||
public class CvsGoodsController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private CvsGoodsService cvsGoodsService;
|
||||
|
||||
/**新增商品类别*/
|
||||
@PostMapping("/addCvsGoods")
|
||||
public ResponseObject insertCvsGoods( @RequestBody CvsGoods goods){
|
||||
int i = cvsGoodsService.insertCvsGoods(goods);
|
||||
if(i == -1){
|
||||
return getFailureResult("新增失败,数据已存在");
|
||||
}else {
|
||||
return getSuccessResult(i);
|
||||
}
|
||||
}
|
||||
|
||||
/**分页查询*/
|
||||
@PostMapping("/selectTree")
|
||||
private ResponseObject selectTree(@RequestBody CvsGoods goods){
|
||||
Page page =new Page<>(goods.getPageNum(),goods.getPageSize());
|
||||
IPage<CvsGoodsVo> list = cvsGoodsService.selectTree(page,goods);
|
||||
return getSuccessResult(list);
|
||||
}
|
||||
|
||||
//查询一级菜单
|
||||
@PostMapping("/list")
|
||||
public ResponseObject list(CvsGoods goods)
|
||||
{
|
||||
List<CvsGoods> list = cvsGoodsService.selectCvsGoodsList(goods);
|
||||
return getSuccessResult(list);
|
||||
}
|
||||
|
||||
//根据id查询一级菜单
|
||||
@GetMapping("/{id}")
|
||||
public ResponseObject selectParentById(@PathVariable Integer id)
|
||||
{
|
||||
CvsGoods cvsGoods = cvsGoodsService.selectParentById(id);
|
||||
return getSuccessResult(cvsGoods);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.fuint.business.convenienceSore.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fuint.framework.entity.BaseEntity;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/16
|
||||
* 便利店商品分类表
|
||||
*/
|
||||
@Data
|
||||
@TableName("cvs_goods")
|
||||
public class CvsGoods extends BaseEntity {
|
||||
|
||||
//主键
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
//父分类id
|
||||
private Integer pid;
|
||||
|
||||
//编码
|
||||
private String code;
|
||||
|
||||
//商品分类
|
||||
private String categoryName;
|
||||
|
||||
//商品排序
|
||||
private Integer sorted;
|
||||
|
||||
//商品状态
|
||||
private String status;
|
||||
|
||||
@TableField(exist = false)
|
||||
private Integer pageNum;
|
||||
|
||||
@TableField(exist = false)
|
||||
private Integer pageSize;
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.fuint.business.convenienceSore.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuint.business.convenienceSore.entity.CvsGoods;
|
||||
import com.fuint.business.convenienceSore.vo.CvsGoodsVo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/16
|
||||
* 便利店商品管理接口
|
||||
*/
|
||||
@Mapper
|
||||
public interface CvsGoodsMapper extends BaseMapper<CvsGoods> {
|
||||
//查找一级菜单
|
||||
IPage<CvsGoodsVo> selectCvsGoodsList(Page page , @Param("goods") CvsGoods goods);
|
||||
|
||||
//查找二级菜单
|
||||
List<CvsGoodsVo> selectCvsGoodsTreeList(CvsGoods goods);
|
||||
|
||||
//根据id和name查找
|
||||
List<CvsGoods> selectGoodsList(CvsGoods goods);
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.fuint.business.convenienceSore.mapper.CvsGoodsMapper">
|
||||
|
||||
<sql id="selectCvsGoods">
|
||||
select id,pid,category_name,code,sorted, status, create_time, create_by, update_time, update_by from cvs_goods
|
||||
</sql>
|
||||
|
||||
<!--判断新增是否重复 -->
|
||||
<select id="selectGoodsList" parameterType="com.fuint.business.convenienceSore.entity.CvsGoods" resultType="com.fuint.business.convenienceSore.entity.CvsGoods" >
|
||||
<include refid="selectCvsGoods"></include>
|
||||
<where>
|
||||
<if test="pid != null">
|
||||
and pid = #{pid}
|
||||
</if>
|
||||
<if test="categoryName != null and categoryName != ''">
|
||||
and category_name = #{categoryName}
|
||||
</if>
|
||||
<if test="sorted != null and sorted != ''">
|
||||
and sorted = #{sorted}
|
||||
</if>
|
||||
</where>
|
||||
order by sorted
|
||||
</select>
|
||||
|
||||
<!--查询一级分类列表 -->
|
||||
<select id="selectCvsGoodsList" parameterType="com.fuint.business.convenienceSore.entity.CvsGoods" resultType="com.fuint.business.convenienceSore.vo.CvsGoodsVo">
|
||||
SELECT
|
||||
DISTINCT
|
||||
pTable.id,
|
||||
pTable.pid,
|
||||
pTable.category_name,
|
||||
pTable.`CODE`,
|
||||
pTable.sorted,
|
||||
pTable.`STATUS`,
|
||||
pTable.create_time,
|
||||
pTable.create_by,
|
||||
pTable.update_time,
|
||||
pTable.update_by
|
||||
FROM
|
||||
(SELECT * from cvs_goods WHERE pid = 0) pTable
|
||||
left join
|
||||
(SELECT * from cvs_goods WHERE pid != 0) cTable on pTable.id = cTable.pid
|
||||
<where>
|
||||
<if test="goods.status != null and goods.status != ''">
|
||||
and pTable.status = #{goods.status}
|
||||
</if>
|
||||
<if test="goods.categoryName != null and goods.categoryName!= ''">
|
||||
and pTable.category_name like concat('%',#{goods.categoryName},'%')
|
||||
or cTable.category_name like concat('%',#{goods.categoryName},'%')
|
||||
</if>
|
||||
</where>
|
||||
group by pTable.id
|
||||
order by sorted
|
||||
</select>
|
||||
|
||||
<!--查找二级分类-->
|
||||
<select id="selectCvsGoodsTreeList" parameterType="com.fuint.business.convenienceSore.entity.CvsGoods" resultType="com.fuint.business.convenienceSore.vo.CvsGoodsVo">
|
||||
SELECT
|
||||
id,
|
||||
pid,
|
||||
category_name,
|
||||
`CODE`,
|
||||
sorted,
|
||||
`STATUS`,
|
||||
create_time,
|
||||
create_by,
|
||||
update_time,
|
||||
update_by
|
||||
FROM
|
||||
cvs_goods
|
||||
<where >
|
||||
<if test="pid != null ">
|
||||
and pid = #{pid}
|
||||
</if>
|
||||
<if test="categoryName != null and categoryName != ''">
|
||||
and category_name like CONCAT('%',#{categoryName},'%')
|
||||
</if>
|
||||
<if test="status != null and status != ''">
|
||||
and status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
order by sorted
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,24 @@
|
||||
package com.fuint.business.convenienceSore.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuint.business.convenienceSore.entity.CvsGoods;
|
||||
import com.fuint.business.convenienceSore.vo.CvsGoodsVo;
|
||||
import io.swagger.models.auth.In;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/16
|
||||
*/
|
||||
public interface CvsGoodsService {
|
||||
|
||||
int insertCvsGoods(CvsGoods cvsGoods);
|
||||
|
||||
IPage<CvsGoodsVo> selectTree(Page page, CvsGoods goods);
|
||||
|
||||
List<CvsGoods> selectCvsGoodsList(CvsGoods goods);
|
||||
|
||||
CvsGoods selectParentById(Integer id);
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package com.fuint.business.convenienceSore.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fuint.business.convenienceSore.entity.CvsGoods;
|
||||
import com.fuint.business.convenienceSore.mapper.CvsGoodsMapper;
|
||||
import com.fuint.business.convenienceSore.service.CvsGoodsService;
|
||||
import com.fuint.business.convenienceSore.vo.CvsGoodsVo;
|
||||
import com.fuint.common.dto.AccountInfo;
|
||||
import com.fuint.common.util.TokenUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/16
|
||||
*/
|
||||
@Service
|
||||
public class CvsGoodsServiceImpl extends ServiceImpl<CvsGoodsMapper,CvsGoods> implements CvsGoodsService {
|
||||
|
||||
/**添加商品信息*/
|
||||
@Transactional
|
||||
public int insertCvsGoods( CvsGoods cvsGoods){
|
||||
//查找分类名称是否存在
|
||||
List<CvsGoods> cvsGoodsList = baseMapper.selectGoodsList(cvsGoods);
|
||||
if(CollectionUtil.isNotEmpty(cvsGoodsList)){
|
||||
return -1;
|
||||
}else {
|
||||
int insertGoods = baseMapper.insert(cvsGoods);
|
||||
cvsGoods.setCode(cvsGoods.getPid()+","+cvsGoods.getId()+",");
|
||||
baseMapper.updateById(cvsGoods);
|
||||
return insertGoods;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IPage<CvsGoodsVo> selectTree(Page page, CvsGoods g){
|
||||
// 查询全部数据
|
||||
IPage<CvsGoodsVo> cvsGoodsPage= baseMapper.selectCvsGoodsList(page,g);//一节点
|
||||
List<CvsGoodsVo> cvsGoodsList = cvsGoodsPage.getRecords();
|
||||
if(CollectionUtil.isNotEmpty(cvsGoodsList)){
|
||||
List<CvsGoodsVo> resList = new ArrayList<>();
|
||||
resList.addAll(cvsGoodsList);
|
||||
for (CvsGoodsVo cvsGoods : cvsGoodsList) {
|
||||
g.setPid(cvsGoods.getId());
|
||||
List<CvsGoodsVo> treeList = baseMapper.selectCvsGoodsTreeList(g);//二级节点
|
||||
resList.addAll(treeList);//一级和二级合并
|
||||
}
|
||||
// cvsGoodsList转换为cvsGoodsVoList
|
||||
List<CvsGoodsVo> CvsGoodsVoList = resList.stream().map(goods -> {
|
||||
CvsGoodsVo cvsGoodsVo = new CvsGoodsVo();
|
||||
cvsGoodsVo.setId(goods.getId());
|
||||
cvsGoodsVo.setCategoryName(goods.getCategoryName());
|
||||
cvsGoodsVo.setPid(goods.getPid());
|
||||
cvsGoodsVo.setStatus(goods.getStatus());
|
||||
cvsGoodsVo.setCreateTime(new Date());
|
||||
AccountInfo nowAccountInfo = TokenUtil.getNowAccountInfo();
|
||||
cvsGoodsVo.setCreateBy(nowAccountInfo.getId().toString());
|
||||
cvsGoodsVo.setSorted(goods.getSorted());
|
||||
return cvsGoodsVo;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
//获取得所有parentId为0的数据,也就是一级目录
|
||||
//用于封装数据,取得他的孩子(也就是下级目录)的数据
|
||||
List<CvsGoodsVo> CvsGoodsVoTree = CvsGoodsVoList.stream()
|
||||
.filter(categoryVo -> categoryVo.getPid().equals(0))
|
||||
.map((cvsVoParent) -> {
|
||||
cvsVoParent.setChildren(getChildrenData(cvsVoParent, CvsGoodsVoList));
|
||||
return cvsVoParent;
|
||||
}).collect(Collectors.toList());
|
||||
cvsGoodsPage.setRecords(CvsGoodsVoTree);
|
||||
return cvsGoodsPage;
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取孩子(下级目录)的方法,递归实现
|
||||
* @return
|
||||
*/
|
||||
private List<CvsGoodsVo> getChildrenData(CvsGoodsVo root, List<CvsGoodsVo> all) {
|
||||
List<CvsGoodsVo> children = all.stream().filter(cvsVo ->
|
||||
cvsVo.getPid().equals(root.getId())
|
||||
).map(categoryVo -> {
|
||||
categoryVo.setChildren(getChildrenData(categoryVo, all));
|
||||
return categoryVo;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
//查询一级节点
|
||||
public List<CvsGoods> selectCvsGoodsList(CvsGoods goods){
|
||||
LambdaQueryWrapper<CvsGoods> queryWrapper =new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CvsGoods::getPid,"0").orderByAsc(CvsGoods::getSorted);
|
||||
return baseMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
//根据id查找父级
|
||||
public CvsGoods selectParentById(Integer id){
|
||||
return baseMapper.selectById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.fuint.business.convenienceSore.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.fuint.business.convenienceSore.entity.CvsGoods;
|
||||
import com.fuint.repository.model.base.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.parsing.BeanEntry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author :admin
|
||||
* @date : 2023/10/17
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CvsGoodsVo extends BaseEntity {
|
||||
//主键
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
//父分类id
|
||||
private Integer pid;
|
||||
|
||||
//编码
|
||||
private String code;
|
||||
|
||||
//商品分类
|
||||
private String categoryName;
|
||||
|
||||
//商品排序
|
||||
private Integer sorted;
|
||||
|
||||
//商品状态
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<CvsGoodsVo> children;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user