新增商品模块所有可能用到的接口

This commit is contained in:
clunt 2022-03-14 13:55:40 +08:00
parent bd22152ef4
commit cbd2cd2f54
8 changed files with 359 additions and 0 deletions

View File

@ -67,6 +67,12 @@
<artifactId>ghy-generator</artifactId>
</dependency>
<!-- 商品模块 -->
<dependency>
<groupId>com.ghy</groupId>
<artifactId>ghy-goods</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,156 @@
package com.ghy.web.controller.goods;
import com.ghy.common.annotation.Log;
import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/goods/goods")
public class GoodsController extends BaseController {
private String prefix = "goods/goods";
@Autowired
private GoodsService goodsService;
@RequiresPermissions("goods:goods:view")
@GetMapping()
public String operlog()
{
return prefix + "/goods";
}
@RequiresPermissions("goods:goods:list")
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(Goods goods)
{
startPage();
List<Goods> list = goodsService.selectGoodsList(goods);
return getDataTable(list);
}
@Log(title = "商品管理", businessType = BusinessType.EXPORT)
@RequiresPermissions("goods:goods:export")
@PostMapping("/export")
@ResponseBody
public AjaxResult export(Goods goods)
{
List<Goods> list = goodsService.selectGoodsList(goods);
ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
return util.exportExcel(list, "商品数据");
}
@RequiresPermissions("goods:goods:remove")
@Log(title = "商品管理", businessType = BusinessType.DELETE)
@PostMapping("/remove")
@ResponseBody
public AjaxResult remove(String ids)
{
try
{
return toAjax(goodsService.deleteGoodsByIds(ids));
}
catch (Exception e)
{
return error(e.getMessage());
}
}
/**
* 新增商品
*/
@GetMapping("/add")
public String add()
{
return prefix + "/add";
}
/**
* 新增保存商品
*/
@RequiresPermissions("goods:goods:add")
@Log(title = "商品管理", businessType = BusinessType.INSERT)
@PostMapping("/add")
@ResponseBody
public AjaxResult addSave(@Validated Goods goods)
{
if (UserConstants.GOODS_CODE_NOT_UNIQUE.equals(goodsService.checkGoodsNameUnique(goods)))
{
return error("新增商品'" + goods.getGoodsName() + "'失败,商品名称已存在");
}
else if (UserConstants.GOODS_CODE_NOT_UNIQUE.equals(goodsService.checkGoodsCodeUnique(goods)))
{
return error("新增商品'" + goods.getGoodsName() + "'失败,商品编码已存在");
}
goods.setCreateBy(getLoginName());
return toAjax(goodsService.insertGoods(goods));
}
/**
* 修改商品
*/
@RequiresPermissions("goods:goods:edit")
@GetMapping("/edit/{goodsId}")
public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap)
{
mmap.put("goods", goodsService.selectById(goodsId));
return prefix + "/edit";
}
/**
* 修改保存商品
*/
@RequiresPermissions("goods:goods:edit")
@Log(title = "商品管理", businessType = BusinessType.UPDATE)
@PostMapping("/edit")
@ResponseBody
public AjaxResult editSave(@Validated Goods goods)
{
if (UserConstants.GOODS_NAME_NOT_UNIQUE.equals(goodsService.checkGoodsNameUnique(goods)))
{
return error("修改商品'" + goods.getGoodsName() + "'失败,商品名称已存在");
}
else if (UserConstants.GOODS_CODE_NOT_UNIQUE.equals(goodsService.checkGoodsCodeUnique(goods)))
{
return error("修改商品'" + goods.getGoodsCode() + "'失败,商品编码已存在");
}
goods.setUpdateBy(getLoginName());
return toAjax(goodsService.updateGoods(goods));
}
/**
* 校验商品名称
*/
@PostMapping("/checkGoodsNameUnique")
@ResponseBody
public String checkGoodsNameUnique(Goods goods)
{
return goodsService.checkGoodsNameUnique(goods);
}
/**
* 校验商品编码
*/
@PostMapping("/checkGoodsCodeUnique")
@ResponseBody
public String checkGoodsCodeUnique(Goods goods)
{
return goodsService.checkGoodsCodeUnique(goods);
}
}

View File

@ -74,6 +74,14 @@ public class UserConstants
public final static String POST_CODE_UNIQUE = "0";
public final static String POST_CODE_NOT_UNIQUE = "1";
/** 商品编码是否唯一的返回结果 */
public final static String GOODS_CODE_UNIQUE = "0";
public final static String GOODS_CODE_NOT_UNIQUE = "1";
/** 商品名称是否唯一的返回结果 */
public final static String GOODS_NAME_UNIQUE = "0";
public final static String GOODS_NAME_NOT_UNIQUE = "1";
/** 菜单名称是否唯一的返回结果码 */
public final static String MENU_NAME_UNIQUE = "0";
public final static String MENU_NAME_NOT_UNIQUE = "1";

View File

@ -35,5 +35,24 @@ public interface GoodsMapper {
*/
public Goods selectById(Long goodsId);
/**
* 批量删除商品信息
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteGoodsByIds(Long[] ids);
/**
* @param goodsName 商品名称
* @return 商品信息
*/
public Goods checkGoodsNameUnique(String goodsName);
/**
* @param goodsCode 商品编码
* @return 商品信息
*/
public Goods checkGoodsCodeUnique(String goodsCode);
}

View File

@ -0,0 +1,58 @@
package com.ghy.goods.service;
import com.ghy.goods.domain.Goods;
import java.util.List;
/**
* 商品模块接口
* @author clunt
*/
public interface GoodsService {
/**
* @param goods 商品属性
* @return 成功条数
*/
public int insertGoods(Goods goods);
/**
* @param goods 商品属性
* @return 成功条数
*/
public int updateGoods(Goods goods);
/**
* @param goods 商品入参
* @return 商品集合
*/
public List<Goods> selectGoodsList(Goods goods);
/**
* @param goodsId 商品id
* @return 商品
*/
public Goods selectById(Long goodsId);
/**
* @param ids 商品ids
* @return 删除结果
*/
public int deleteGoodsByIds(String ids);
/**
* 校验商品名称是否重复
* @param goods 商品属性
* @return 校验结果 1存在 0不存在
*/
public String checkGoodsNameUnique(Goods goods);
/**
* 校验商品编码是否重复
* @param goods 商品属性
* @return 校验结果 1存在 0不存在
*/
public String checkGoodsCodeUnique(Goods goods);
}

View File

@ -0,0 +1,87 @@
package com.ghy.goods.service.impl;
import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.text.Convert;
import com.ghy.common.exception.ServiceException;
import com.ghy.common.utils.StringUtils;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.mapper.GoodsMapper;
import com.ghy.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 商品模块实现类
* @author clunt
*/
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Override
public int insertGoods(Goods goods) {
return goodsMapper.insertGoods(goods);
}
@Override
public int updateGoods(Goods goods) {
return goodsMapper.updateGoods(goods);
}
@Override
public List<Goods> selectGoodsList(Goods goods) {
return goodsMapper.selectGoodsList(goods);
}
@Override
public Goods selectById(Long goodsId) {
return goodsMapper.selectById(goodsId);
}
@Override
public int deleteGoodsByIds(String ids) {
Long[] goodsIds = Convert.toLongArray(ids);
for (Long goodsId : goodsIds)
{
Goods goods = selectById(goodsId);
if (countUserGoodsById(goods) > 0)
{
throw new ServiceException(String.format("%1$s正在使用,不能删除", goods.getGoodsName()));
}
}
return goodsMapper.deleteGoodsByIds(goodsIds);
}
@Override
public String checkGoodsNameUnique(Goods goods) {
Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
Goods info = goodsMapper.checkGoodsNameUnique(goods.getGoodsName());
if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
{
return UserConstants.GOODS_NAME_NOT_UNIQUE;
}
return UserConstants.GOODS_NAME_UNIQUE;
}
@Override
public String checkGoodsCodeUnique(Goods goods) {
Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
Goods info = goodsMapper.checkGoodsCodeUnique(goods.getGoodsCode());
if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
{
return UserConstants.GOODS_CODE_NOT_UNIQUE;
}
return UserConstants.GOODS_CODE_UNIQUE;
}
public int countUserGoodsById(Goods goods){
//TODO 校验商品是否上架
return 0;
}
}

View File

@ -45,6 +45,13 @@
</where>
</select>
<delete id="deleteGoodsByIds" parameterType="Long">
delete from goods where goods_id in
<foreach collection="array" item="goodsId" open="(" separator="," close=")">
#{goodsId}
</foreach>
</delete>
<update id="updateGoods" parameterType="com.ghy.goods.domain.Goods">
update goods
<set>
@ -81,4 +88,14 @@
)
</insert>
<select id="checkGoodsNameUnique" parameterType="String" resultMap="GoodsResult">
<include refid="selectGoods"/>
where goods_name=#{goodsName} limit 1
</select>
<select id="checkGoodsCodeUnique" parameterType="String" resultMap="GoodsResult">
<include refid="selectGoods"/>
where goods_code=#{goodsCode} limit 1
</select>
</mapper>

View File

@ -224,6 +224,14 @@
<version>${ghy.version}</version>
</dependency>
<!-- 商品模块-->
<dependency>
<groupId>com.ghy</groupId>
<artifactId>ghy-goods</artifactId>
<version>${ghy.version}</version>
</dependency>
<!-- 通用工具-->
<dependency>
<groupId>com.ghy</groupId>