fanli/src/main/java/com/yeshi/fanli/controller/admin/BrandShopAdminController.java
@@ -445,9 +445,15 @@ * @param out */ @RequestMapping(value = "saveBrand") public void saveBrand(String callback, BrandInfo brandInfo, PrintWriter out) { public void saveBrand(String callback, BrandInfo brandInfo, HttpServletRequest request, PrintWriter out) { try { brandInfoService.saveObject(brandInfo); // 1. 先判断httpRequest 是否含有文件类型 if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request; brandInfoService.saveObject(fileRequest.getFile("file"), brandInfo); }else{ brandInfoService.saveObject(null, brandInfo); } JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("保存成功")); } catch (BrandInfoException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg())); @@ -499,7 +505,7 @@ * @param out */ @RequestMapping(value = "queryBrand") public void query(String callback, Integer pageIndex, Integer pageSize, String key, Long cid, Integer state, public void queryBrand(String callback, Integer pageIndex, Integer pageSize, String key, Long cid, Integer state, PrintWriter out) { if (pageIndex == null || pageIndex < 1) { @@ -518,6 +524,16 @@ return; } for (BrandInfo brandInfo: list) { BrandClass brandClass = brandInfo.getBrandClass(); if (brandClass != null && StringUtil.isNullOrEmpty(brandClass.getName())) { BrandClass bclass = brandClassService.selectByPrimaryKey(brandClass.getId()); if (bclass != null) { brandInfo.setBrandClass(bclass); } } } long count = brandInfoService.countQuery(key, cid, state); int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1); PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage); fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandClassServiceImpl.java
@@ -28,6 +28,13 @@ @Resource private BrandClassShopService brandClassShopService; @Override public BrandClass selectByPrimaryKey(Long id) { return brandClassMapper.selectByPrimaryKey(id); } @Override public List<BrandClass> listEffective() { return brandClassMapper.listEffective(); fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
@@ -1,14 +1,18 @@ package com.yeshi.fanli.service.impl.brand; import java.io.InputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.yeshi.utils.tencentcloud.COSManager; import com.yeshi.fanli.dao.brand.BrandShopCaheDao; import com.yeshi.fanli.dao.mybatis.brand.BrandInfoMapper; @@ -27,6 +31,7 @@ import com.yeshi.fanli.service.inter.lable.QualityGoodsService; import com.yeshi.fanli.service.inter.order.config.HongBaoManageService; import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory; import com.yeshi.fanli.vo.brand.BrandInfoVO; @@ -64,7 +69,7 @@ private BrandShopCaheService brandShopCaheService; @Override public void saveObject(BrandInfo record) throws BrandInfoException { public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException { String name = record.getName(); if (name == null || name.trim().length() == 0) throw new BrandInfoException(1, "名称不能为空"); @@ -72,6 +77,16 @@ Integer state = record.getState(); if (state == null) record.setState(0); // 图片上传 String picture = null; if (file != null) { try { picture = uploadPicture(file); } catch (Exception e) { throw new BrandInfoException(1, "图片上传失败"); } } Long id = record.getId(); if (id == null) { @@ -83,12 +98,46 @@ if (resultObj == null) throw new BrandInfoException(1, "修改内容已不存在"); if (picture != null && picture.trim().length() > 0) { // 删除老图 if (picture != null && picture.trim().length() > 0 && !Constant.IS_TEST) { COSManager.getInstance().deleteFile(picture); }; // 存储新图 record.setIcon(picture); } else { record.setIcon(resultObj.getIcon()); } record.setGoodsTotal(resultObj.getGoodsTotal()); record.setCreateTime(resultObj.getCreateTime()); record.setUpdateTime(new Date()); brandInfoMapper.updateByPrimaryKey(record); } } /** * 上传图片 * @param file * @return * @throws Exception */ public String uploadPicture(MultipartFile file) throws Exception { // 文件解析 InputStream inputStream = file.getInputStream(); String contentType = file.getContentType(); String type = contentType.substring(contentType.indexOf("/") + 1); // 文件路径 String filePath="/img/brand/"+UUID.randomUUID().toString().replace("-", "") + "." + type; // 执行上传 String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl(); return fileLink; } @Override public int deleteBatchByPrimaryKey(List<Long> list) { return brandInfoMapper.deleteBatchByPrimaryKey(list); fanli/src/main/java/com/yeshi/fanli/service/inter/brand/BrandClassService.java
@@ -61,5 +61,12 @@ */ public int deleteBatchByPrimaryKey(List<Long> list); /** * * @param id * @return */ public BrandClass selectByPrimaryKey(Long id); } fanli/src/main/java/com/yeshi/fanli/service/inter/brand/BrandInfoService.java
@@ -2,6 +2,8 @@ import java.util.List; import org.springframework.web.multipart.MultipartFile; import com.yeshi.fanli.entity.brand.BrandInfo; import com.yeshi.fanli.exception.brand.BrandClassException; import com.yeshi.fanli.exception.brand.BrandInfoException; @@ -22,7 +24,7 @@ * @throws BrandClassException * @throws Exception */ public void saveObject(BrandInfo record) throws BrandInfoException; public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException; /**