完成商品的添加、修改、删除

lihaobo
luoyijiucheng 6 years ago
parent 0e3d393da2
commit 083abbcd45

@ -152,11 +152,6 @@
<artifactId>pinyin4j</artifactId>
<version>${pinyin4j.version}</version>
</dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>

@ -1,7 +1,7 @@
package com.yeqifu.bus.cache;
import com.yeqifu.bus.entity.Customer;
import com.yeqifu.bus.entity.Provider;
import com.yeqifu.bus.entity.Goods;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
@ -147,114 +147,92 @@ public class BusinessCacheAspect {
/**
*
*
*/
private static final String POINTCUT_PROVIDER_ADD="execution(* com.yeqifu.bus.service.impl.ProviderServiceImpl.save(..))";
private static final String POINTCUT_PROVIDER_UPDATE="execution(* com.yeqifu.bus.service.impl.ProviderServiceImpl.updateById(..))";
private static final String POINTCUT_PROVIDER_GET="execution(* com.yeqifu.bus.service.impl.ProviderServiceImpl.getById(..))";
private static final String POINTCUT_PROVIDER_DELETE="execution(* com.yeqifu.bus.service.impl.ProviderServiceImpl.removeById(..))";
private static final String POINTCUT_PROVIDER_BATCHDELETE="execution(* com.yeqifu.bus.service.impl.ProviderServiceImpl.removeByIds(..))";
private static final String POINTCUT_GOODS_ADD="execution(* com.yeqifu.bus.service.impl.GoodsServiceImpl.save(..))";
private static final String POINTCUT_GOODS_UPDATE="execution(* com.yeqifu.bus.service.impl.GoodsServiceImpl.updateById(..))";
private static final String POINTCUT_GOODS_GET="execution(* com.yeqifu.bus.service.impl.GoodsServiceImpl.getById(..))";
private static final String POINTCUT_GOODS_DELETE="execution(* com.yeqifu.bus.service.impl.GoodsServiceImpl.removeById(..))";
private static final String CACHE_PROVIDER_PROFIX="provider:";
private static final String CACHE_GOODS_PROFIX="goods:";
/**
*
*
* @param joinPoint
* @return
*/
@Around(value = POINTCUT_PROVIDER_ADD)
public Object cacheProviderAdd(ProceedingJoinPoint joinPoint) throws Throwable {
@Around(value = POINTCUT_GOODS_ADD)
public Object cacheGoodsAdd(ProceedingJoinPoint joinPoint) throws Throwable {
//取出第一个参数
Provider object = (Provider) joinPoint.getArgs()[0];
Goods object = (Goods) joinPoint.getArgs()[0];
Boolean res = (Boolean) joinPoint.proceed();
if (res){
CACHE_CONTAINER.put(CACHE_PROVIDER_PROFIX + object.getId(),object);
CACHE_CONTAINER.put(CACHE_GOODS_PROFIX + object.getId(),object);
}
return res;
}
/**
*
*
* @param joinPoint
* @return
*/
@Around(value = POINTCUT_PROVIDER_GET)
public Object cacheProviderGet(ProceedingJoinPoint joinPoint) throws Throwable {
@Around(value = POINTCUT_GOODS_GET)
public Object cacheGoodsGet(ProceedingJoinPoint joinPoint) throws Throwable {
//取出第一个参数
Integer object = (Integer) joinPoint.getArgs()[0];
//从缓存里面取
Object res1 = CACHE_CONTAINER.get(CACHE_PROVIDER_PROFIX + object);
Object res1 = CACHE_CONTAINER.get(CACHE_GOODS_PROFIX + object);
if (res1!=null){
log.info("已从缓存里面找到供应商对象"+CACHE_PROVIDER_PROFIX + object);
log.info("已从缓存里面找到商品对象"+CACHE_GOODS_PROFIX + object);
return res1;
}else {
log.info("未从缓存里面找到供应商对象,从数据库中查询并放入缓存");
Provider res2 =(Provider) joinPoint.proceed();
CACHE_CONTAINER.put(CACHE_PROVIDER_PROFIX+res2.getId(),res2);
log.info("未从缓存里面找到对象,从数据库中查询并放入缓存");
Goods res2 =(Goods) joinPoint.proceed();
CACHE_CONTAINER.put(CACHE_GOODS_PROFIX+res2.getId(),res2);
return res2;
}
}
/**
*
*
* @param joinPoint
* @return
*/
@Around(value = POINTCUT_PROVIDER_UPDATE)
public Object cacheProviderUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
@Around(value = POINTCUT_GOODS_UPDATE)
public Object cacheGoodsUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
//取出第一个参数
Provider providerVo = (Provider) joinPoint.getArgs()[0];
Goods goodsVo = (Goods) joinPoint.getArgs()[0];
Boolean isSuccess = (Boolean) joinPoint.proceed();
if (isSuccess){
Provider provider =(Provider) CACHE_CONTAINER.get(CACHE_PROVIDER_PROFIX + providerVo.getId());
if (null==provider){
provider=new Provider();
Goods goods =(Goods) CACHE_CONTAINER.get(CACHE_GOODS_PROFIX + goodsVo.getId());
if (null==goods){
goods=new Goods();
}
BeanUtils.copyProperties(providerVo,provider);
log.info("供应商对象缓存已更新"+CACHE_PROVIDER_PROFIX + providerVo.getId());
CACHE_CONTAINER.put(CACHE_PROVIDER_PROFIX+provider.getId(),provider);
BeanUtils.copyProperties(goodsVo,goods);
log.info("商品对象缓存已更新"+CACHE_GOODS_PROFIX + goodsVo.getId());
CACHE_CONTAINER.put(CACHE_GOODS_PROFIX+goods.getId(),goods);
}
return isSuccess;
}
/**
*
*
* @param joinPoint
* @return
*/
@Around(value = POINTCUT_PROVIDER_DELETE)
public Object cacheProviderDelete(ProceedingJoinPoint joinPoint) throws Throwable {
@Around(value = POINTCUT_GOODS_DELETE)
public Object cacheGoodsDelete(ProceedingJoinPoint joinPoint) throws Throwable {
//取出第一个参数
Integer id = (Integer) joinPoint.getArgs()[0];
Boolean isSuccess = (Boolean) joinPoint.proceed();
if (isSuccess){
//删除缓存
CACHE_CONTAINER.remove(CACHE_PROVIDER_PROFIX+id);
CACHE_CONTAINER.remove(CACHE_GOODS_PROFIX+id);
}
return isSuccess;
}
/**
*
*
* @throws Throwable
*/
@Around(value = POINTCUT_PROVIDER_BATCHDELETE)
public Object cacheProviderBatchDelete(ProceedingJoinPoint joinPoint) throws Throwable {
// 取出第一个参数
@SuppressWarnings("unchecked")
Collection<Serializable> idList = (Collection<Serializable>) joinPoint.getArgs()[0];
Boolean isSuccess = (Boolean) joinPoint.proceed();
if (isSuccess) {
for (Serializable id : idList) {
// 删除缓存
CACHE_CONTAINER.remove(CACHE_PROVIDER_PROFIX + id);
log.info("供应商对象缓存已删除" + CACHE_PROVIDER_PROFIX + id);
}
}
return isSuccess;
}
}

@ -9,11 +9,13 @@ import com.yeqifu.bus.entity.Provider;
import com.yeqifu.bus.service.IGoodsService;
import com.yeqifu.bus.service.IProviderService;
import com.yeqifu.bus.vo.GoodsVo;
import com.yeqifu.sys.common.AppFileUtils;
import com.yeqifu.sys.common.Constast;
import com.yeqifu.sys.common.DataGridView;
import com.yeqifu.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -55,16 +57,80 @@ public class GoodsController {
List<Goods> records = page.getRecords();
for (Goods goods : records) {
Provider provider = providerService.getById(goods.getId());
Provider provider = providerService.getById(goods.getProviderid());
if (null!=provider){
goods.setProvidername(provider.getProvidername());
}
}
return new DataGridView(page.getTotal(),page.getRecords());
}
/**
*
* @param goodsVo
* @return
*/
@RequestMapping("addGoods")
public ResultObj addGoods(GoodsVo goodsVo){
try {
if (goodsVo.getGoodsimg()!=null&&goodsVo.getGoodsimg().endsWith("_temp")){
String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
goodsVo.setGoodsimg(newName);
}
goodsService.save(goodsVo);
return ResultObj.ADD_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.ADD_ERROR;
}
}
/**
*
* @param goodsVo
* @return
*/
@RequestMapping("updateGoods")
public ResultObj updateGoods(GoodsVo goodsVo){
try {
//商品图片不是默认图片
if (!(goodsVo.getGoodsimg()!=null&&goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMG))){
if (goodsVo.getGoodsimg().endsWith("_temp")){
String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
goodsVo.setGoodsimg(newName);
//删除原先的图片
String oldPath = goodsService.getById(goodsVo.getId()).getGoodsimg();
AppFileUtils.removeFileByPath(oldPath);
}
}
goodsService.updateById(goodsVo);
return ResultObj.UPDATE_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.UPDATE_ERROR;
}
}
/**
*
* @param id
* @return
*/
@RequestMapping("deleteGoods")
public ResultObj deleteGoods(Integer id,String goodsimg){
try {
//删除商品的图片
AppFileUtils.removeFileByPath(goodsimg);
goodsService.removeById(id);
return ResultObj.DELETE_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.DELETE_ERROR;
}
}
}

@ -5,6 +5,9 @@ import com.yeqifu.bus.mapper.GoodsMapper;
import com.yeqifu.bus.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
/**
* <p>
@ -15,6 +18,26 @@ import org.springframework.stereotype.Service;
* @since 2019-12-06
*/
@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
@Override
public boolean save(Goods entity) {
return super.save(entity);
}
@Override
public boolean updateById(Goods entity) {
return super.updateById(entity);
}
@Override
public boolean removeById(Serializable id) {
return super.removeById(id);
}
@Override
public Goods getById(Serializable id) {
return super.getById(id);
}
}

@ -18,9 +18,8 @@ import java.util.Properties;
*/
public class AppFileUtils {
//文件上传的保存路径
/**
*
*
*/
public static String UPLOAD_PATH="E:/upload/";
@ -41,11 +40,13 @@ public class AppFileUtils {
/**
*
* @param oldName
* @return
* @param oldName
* @return 32
*/
public static String createNewFileName(String oldName) {
//获取文件名后缀
String stuff=oldName.substring(oldName.lastIndexOf("."), oldName.length());
//将UUID与文件名后缀进行拼接生成新的文件名
return IdUtil.simpleUUID().toUpperCase()+stuff;
}
@ -75,4 +76,34 @@ public class AppFileUtils {
}
return null;
}
/**
* _temp
* @param goodsimg
* @return
*/
public static String renameFile(String goodsimg) {
File file = new File(UPLOAD_PATH,goodsimg);
String replace = goodsimg.replace("_temp","");
if (file.exists()){
file.renameTo(new File(UPLOAD_PATH,replace));
}
return replace;
}
/**
*
* @param oldPath
*/
public static void removeFileByPath(String oldPath) {
//图片的路径不是默认图片的路径
if (!oldPath.equals(Constast.DEFAULT_IMG)){
File file = new File(UPLOAD_PATH,oldPath);
if (file.exists()){
file.delete();
}
}
}
}

@ -40,4 +40,9 @@ public class Constast {
*/
public static final Integer OPEN_TRUE = 1;
public static final Integer OPEN_FALSE = 0;
/**
*
*/
public static final String DEFAULT_IMG= "/images/noDefaultImage.jpg";
}

@ -23,12 +23,14 @@ public class FileController {
/**
*
* @param mf
* @return
*/
@RequestMapping("uploadFile")
public Map<String,Object> uploadFile(MultipartFile mf) {
//1.得到文件名
String oldName = mf.getOriginalFilename();
//2.根据文件名生成新的文件名
//2.根据旧的文件名生成新的文件名
String newName=AppFileUtils.createNewFileName(oldName);
//3.得到当前日期的字符串
String dirName= DateUtil.format(new Date(), "yyyy-MM-dd");
@ -36,23 +38,22 @@ public class FileController {
File dirFile=new File(AppFileUtils.UPLOAD_PATH,dirName);
//5.判断当前文件夹是否存在
if(!dirFile.exists()) {
//创建文件夹
//如果不存在则创建文件夹
dirFile.mkdirs();
}
//6.构造文件对象
File file=new File(dirFile, newName);
//7,把mf里面的图片信息写入file
File file=new File(dirFile, newName+"_temp");
//7.把mf里面的图片信息写入file
try {
mf.transferTo(file);
} catch (IOException e) {
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Map<String,Object> map=new HashMap<String, Object>();
map.put("path",dirFile+"/"+newName);
map.put("path",dirName+"/"+newName+"_temp");
return map;
}
/**
*
*/

@ -148,7 +148,7 @@
</div>
<div class="layui-col-md3 layui-col-xs5">
<div class="layui-upload-list thumbBox mag0 magt3">
<input type="hidden" name="goodsimg" value="/images/noDefaultImage.jpg">
<input type="hidden" name="goodsimg" id="goodsimg" value="images/noDefaultImage.jpg">
<img class="layui-upload-img thumbImg" src="/file/showImageByPath?path=images/noDefaultImage.jpg">
</div>
</div>
@ -157,49 +157,49 @@
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="produceplace" lay-verify="" placeholder="请输入商品产地">
<input type="text" class="layui-input input-radius" name="produceplace" placeholder="请输入商品产地">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="goodspackage" lay-verify="" placeholder="请输入商品包装">
<input type="text" class="layui-input input-radius" name="goodspackage" placeholder="请输入商品包装">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="size" lay-verify="" placeholder="请输入商品规格">
<input type="text" class="layui-input input-radius" name="size" placeholder="请输入商品规格">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="productcode" lay-verify="" placeholder="请输入商品生产批号">
<input type="text" class="layui-input input-radius" name="productcode" placeholder="请输入商品生产批号">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="promitcode" lay-verify="" placeholder="请输入商品批准文号">
<input type="text" class="layui-input input-radius" name="promitcode" placeholder="请输入商品批准文号">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="price" lay-verify="" placeholder="请输入商品销售价格">
<input type="text" class="layui-input input-radius" name="price" placeholder="请输入商品销售价格">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="number" lay-verify="" placeholder="请输入商品库存量">
<input type="text" class="layui-input input-radius" name="number" placeholder="请输入商品库存量">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<input type="text" class="layui-input input-radius" name="dangernum" lay-verify="" placeholder="请输入商品预警值">
<input type="text" class="layui-input input-radius" name="dangernum" placeholder="请输入商品预警值">
</div>
</div>
<div class="layui-inline">
@ -350,6 +350,8 @@
var path=res.path;
$('.thumbImg').attr('src','/file/showImageByPath?path='+path);
$('.thumbBox').css("background","#fff");
//给隐藏域赋值
$("#goodsimg").val(path);
}
});
@ -376,6 +378,9 @@
dom.html(html);
form.render("select");
});
//设置默认为无图片
$(".thumbImg").attr("src",'/file/showImageByPath?path=images/noDefaultImage.jpg');
$("#goodsimg").val('/images/noDefaultImage.jpg');
}
});
}
@ -392,6 +397,23 @@
$("#dataFrm")[0].reset();
//装载新的数据
form.val("dataFrm",data);
//图片进行回显
$(".thumbImg").attr("src",'/file/showImageByPath?path='+data.goodsimg);
//下拉列表的回显
$.get("/provider/loadAllProviderForSelect",function(res){
var redata=res.data;
var dom=$("#providerid");
var html='<option value="0"></option>'
$.each(redata,function(index,item){
if (data.providerid===item.id){
html+='<option value="'+item.id+'" selected>'+item.providername+'</option>'
}else {
html+='<option value="'+item.id+'">'+item.providername+'</option>'
}
});
dom.html(html);
form.render("select");
});
url="/goods/updateGoods";
}
});
@ -411,7 +433,7 @@
//删除
function deleteGoods(data) {
layer.confirm('' + data.goodsname + '', {icon: 3, title: ''}, function (index) {
$.post("/goods/deleteGoods", {id: data.id},function (res) {
$.post("/goods/deleteGoods", {id: data.id,goodsimg:data.goodsimg},function (res) {
if (res.code == 200) {
tableIns.reload();
}

Loading…
Cancel
Save