1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

分类管理增删改查完成

This commit is contained in:
2023-04-13 22:07:05 +08:00
parent 7dcf15fd79
commit bad6ecb111
19 changed files with 376 additions and 36 deletions

View File

@@ -216,7 +216,7 @@ public class GoodController {
.build();
// 拼装返回结果
JSONObject map = new JSONObject(2);
JSONObject map = new JSONObject(6);
map.put("total", goodPageInfo.getTotal());
map.put("list", voList);
map.put("columns", columns);
@@ -244,12 +244,12 @@ public class GoodController {
if (good.getId() == null || good.getId() < 1) {
// 新增商品
if (existGood != null) {
return Res.error("商品已存在,操作失败");
return Res.error("商品已存在,操作失败");
}
// if (password == null || "".equals(password)) {
// return Res.error("密码不能为空");
// }
if (good.getGoodsName() == null || "".equals(good.getGoodsName())) {
return Res.error("商品名称不能为空");
}
good.setId(null);
goodService.addGood(good);
} else {
@@ -275,7 +275,7 @@ public class GoodController {
if (id == null || id <= 0) {
return Res.error("商品不存在,删除失败");
}
// 先查询商品是否存在
// 先查询商品是否存在
Good existGood = goodService.getGoodById(id);
if (existGood == null) {
return Res.error("商品不存在,删除失败");
@@ -308,6 +308,13 @@ public class GoodController {
return Res.success(map);
}
/**
* 上传商品图片到腾讯云COS桶
* —— 获取上传临时密钥及生成文件名
*
* @param ext
* @return
*/
@GetMapping("/manage/imageUpload/getTmpCosCredential")
@ResponseBody
public Res getTmpCosCredential(@RequestParam(required = true) String ext) {
@@ -319,4 +326,144 @@ public class GoodController {
// 返回结果
return Res.success(credential);
}
/**
* 获取商品分类
*
* @return
*/
@GetMapping("/manage/getCategoryList")
@ResponseBody
public Res getCategoryList(PageQuery pageQuery, GoodCategoryVO goodCategoryVO) {
// 查询分页数据
PageHelper.startPage(pageQuery.getPageIndex(), pageQuery.getPageSize());
List<GoodCategory> goodCategoryList = goodService.getCateListWithQuery(goodCategoryVO);
PageInfo<GoodCategory> goodCategoryPageInfo = new PageInfo<>(goodCategoryList);
List<GoodCategory> list = goodCategoryPageInfo.getList();
List<GoodCategoryVO> voList = GoodCategoryVO.convertFrom(list);
// id列 字段名区分大小写以VO中的变量名为准
// 新增、修改弹窗时,使用该列作为主键列进行操作
String idFieldName = "id";
// 当前管理页面
String pageName = "分类管理";
// 指定前端表格显示列
JSONArray columns = FieldBuilder.create()
.add("categoryName", "categoryName", "商品分类", "",
FieldType.TEXT, SearchType.INPUT, AddType.INPUT, EditType.INPUT,
FieldBuilder.SEARCH_PLACEHOLDER_SAME_AS_FIELDNAME,
"商品分类", FieldBuilder.EDIT_PLACEHOLDER_SAME_AS_ADD_PLACEHOLDER,
FieldRuleListBuilder.create()
.add(FieldRuleBuilder.create("商品分类").required())
.add(FieldRuleBuilder.create("商品分类").minMax(2, 4)),
"DPD @cword(2, 4)"
)
.add("order", "order", "排序", "",
FieldType.TEXT, SearchType.CAN_NOT_SEARCH, AddType.INPUT, EditType.INPUT,
FieldBuilder.SEARCH_PLACEHOLDER_SAME_AS_FIELDNAME,
"排序", FieldBuilder.EDIT_PLACEHOLDER_SAME_AS_ADD_PLACEHOLDER,
FieldRuleListBuilder.create(),
"DTD /^\\d+?$/"
)
.build();
// 指定需要翻译的字段
JSONArray fieldMapper = FieldMapperBuilder.create()
.build();
// 拼装返回结果
JSONObject map = new JSONObject(6);
map.put("total", goodCategoryPageInfo.getTotal());
map.put("list", voList);
map.put("columns", columns);
map.put("fieldMapper", fieldMapper);
map.put("idFieldName", idFieldName);
map.put("pageName", pageName);
// 返回结果
return Res.success(map);
}
/**
* 新增 / 编辑分类
*
* @return
*/
@PostMapping("/manage/editCategory")
@ResponseBody
public Res editCategory(@ModelAttribute GoodCategoryVO goodCategoryVO) {
GoodCategory goodCategory = GoodCategoryVO.convertTo(goodCategoryVO);
// 先查询分类是否存在
GoodCategory existGoodCategory = goodService.getGoodCategoryById(goodCategory.getId());
if (goodCategory.getId() == null || goodCategory.getId() < 1) {
// 新增分类
if (existGoodCategory != null) {
return Res.error("分类已存在,操作失败");
}
if (goodCategory.getCategoryName() == null || "".equals(goodCategory.getCategoryName())) {
return Res.error("分类名称不能为空");
}
goodCategory.setId(null);
goodService.addGoodCategory(goodCategory);
} else {
// 修改分类
if (existGoodCategory == null) {
return Res.error("分类不存在,操作失败");
}
goodService.updateGoodCategory(goodCategory);
}
return Res.success(true);
}
/**
* 删除分类
*
* @param id
* @return
*/
@PostMapping("/manage/deleteCategory")
@ResponseBody
public Res deleteCategory(Long id) {
if (id == null || id <= 0) {
return Res.error("分类不存在,删除失败");
}
// 先查询分类是否存在
GoodCategory existGoodCategory = goodService.getGoodCategoryById(id);
if (existGoodCategory == null) {
return Res.error("分类不存在,删除失败");
}
boolean b = goodService.deleteGoodCategory(existGoodCategory.getId());
return Res.success(b);
}
/**
* 导出分类列表
*
* @return
*/
@GetMapping("/manage/exportCategoryList")
@ResponseBody
public Res exportCategoryList(GoodCategoryVO goodCategoryVO) {
List<GoodCategory> categoryList = goodService.getCateListWithQuery(goodCategoryVO);
List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(categoryList);
// 当前时间
Date now = Calendar.getInstance().getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dateTime = format.format(now);
HashMap<String, Object> map = new HashMap<>();
map.put("list", goodCategoryVOList);
map.put("sheetName", "商品分类表-" + System.currentTimeMillis());
map.put("fileName", "商品分类表_导出时间_" + dateTime);
return Res.success(map);
}
}

View File

@@ -1,6 +1,7 @@
package com.cxyxiaomo.epp.shop.dao;
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@@ -12,5 +13,14 @@ public interface GoodCategoryDao {
List<GoodCategory> list();
GoodCategory getById(Integer id);
GoodCategory getById(Long id);
public boolean addCategory(GoodCategory goodCategory);
public boolean updateCategory(GoodCategory goodCategory);
public List<GoodCategory> getCategoryList(GoodCategoryVO goodCategoryVO);
public boolean deleteCategoryById(Long goodCategoryId);
}

View File

@@ -49,11 +49,27 @@ public class GoodService {
}
/**
* 获取分类列表(不带查询参数)
*
* @return
*/
public List<GoodCategory> getCateList() {
List<GoodCategory> list = goodCategoryDao.list();
return list;
}
/**
* 获取分类列表(带查询参数)
*
* @param goodCategoryVO
* @return
*/
public List<GoodCategory> getCateListWithQuery(GoodCategoryVO goodCategoryVO) {
List<GoodCategory> list = goodCategoryDao.getCategoryList(goodCategoryVO);
return list;
}
public Good getGoodById(Long id) {
if (id == null) {
@@ -79,4 +95,25 @@ public class GoodService {
public boolean deleteGood(Long goodId) {
return goodDao.deleteGoodById(goodId);
}
public boolean addGoodCategory(GoodCategory goodCategory) {
goodCategory.setId(null);
return goodCategoryDao.addCategory(goodCategory);
}
public boolean updateGoodCategory(GoodCategory goodCategory) {
return goodCategoryDao.updateCategory(goodCategory);
}
public boolean deleteGoodCategory(Long goodCategoryId) {
return goodCategoryDao.deleteCategoryById(goodCategoryId);
}
public GoodCategory getGoodCategoryById(Long id) {
if (id == null) {
return null;
}
return goodCategoryDao.getById(id);
}
}

View File

@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodCategoryDao">
<select id="list" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
SELECT * FROM goods_category
WHERE 1 = 1
order by `order` asc
</select>
<select id="getById" parameterType="java.lang.Integer" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
SELECT * FROM goods_category
WHERE id = #{id}
order by `order` asc
</select>
</mapper>

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodCategoryDao">
<select id="list" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
SELECT * FROM goods_category
WHERE 1 = 1
order by `order` asc
</select>
<select id="getById" parameterType="java.lang.Long" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
SELECT * FROM goods_category
WHERE id = #{id}
</select>
<insert id="addCategory" parameterType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
INSERT INTO goods_category ( category_name, `order`)
VALUES (#{categoryName}, #{order})
</insert>
<update id="updateCategory" parameterType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
UPDATE goods_category
<set>
<if test="categoryName != null and categoryName != ''">category_name = #{categoryName,jdbcType=VARCHAR},</if>
<if test="order != null">`order` = #{order},</if>
</set>
WHERE id = #{id}
</update>
<select id="getCategoryList" resultType="com.cxyxiaomo.epp.common.pojo.GoodCategory">
select *
from goods_category
where 1 = 1
<if test="id != null">
AND id = #{id}
</if>
<if test="categoryName != null &amp;&amp; categoryName != ''">
AND category_name LIKE concat('%',#{categoryName,jdbcType=VARCHAR},'%')
</if>
order by `order` asc
</select>
<delete id="deleteCategoryById">
DELETE
FROM goods_category
WHERE id = #{goodCategoryId}
</delete>
</mapper>