From 2e53c542b4dfac6d1e62c7a2e7d21a3984e797c2 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期一, 11 十一月 2019 17:24:44 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/div' into div

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java |   90 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java
index 78f072b..ca5fce5 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java
@@ -1,12 +1,16 @@
 package com.yeshi.fanli.service.impl.shop;
 
+import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
+import java.util.UUID;
 
 import javax.annotation.Resource;
 
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+import org.yeshi.utils.tencentcloud.COSManager;
 
 import com.yeshi.fanli.dao.mybatis.shop.BanLiShopGoodsMapper;
 import com.yeshi.fanli.entity.shop.BanLiShopGoods;
@@ -127,6 +131,92 @@
 
 	}
 
+	
+	@Override
+	public void saveObject(MultipartFile file, MultipartFile file2, BanLiShopGoods record) throws BanLiShopGoodsException, Exception{
+		if (record.getGoodsClass() == null || record.getGoodsClass().getId() == null)
+			throw new BanLiShopGoodsException(1, "璇锋寚瀹氬晢鍝佸垎绫�");
+
+		if (StringUtil.isNullOrEmpty(record.getTitle()))
+			throw new BanLiShopGoodsException(1, "缂哄皯鏍囬");
+
+		if (file != null)
+			record.setPicture(uploadPicture(file));
+		
+		if (file2 != null)
+			record.setSquarePicture(uploadPicture(file2));
+		
+		if (record.getState() == null)
+			record.setState(BanLiShopGoods.STATE_ONLINE);
+		
+		if (record.getSalesCount() == null)
+			record.setSalesCount(0L);
+		
+		record.setUpdateTime(new Date());
+		
+		if (record.getId() == null) {
+			if (StringUtil.isNullOrEmpty(record.getPicture()))
+				throw new BanLiShopGoodsException(1, "缂哄皯灏侀潰鍥�");
+			
+			record.setCreateTime(new Date());
+			banLiShopGoodsMapper.insertSelective(record);
+		} else {
+			BanLiShopGoods resultObj = banLiShopGoodsMapper.selectDetailByPrimaryKey(record.getId());
+			if (resultObj == null)
+				throw new BanLiShopGoodsException(1, "淇敼鍐呭宸蹭笉瀛樺湪");
+			
+			if (StringUtil.isNullOrEmpty(record.getPicture())) {
+				record.setPicture(resultObj.getPicture());
+			} else {
+				removePicture(resultObj.getPicture());
+			}
+			
+			if (StringUtil.isNullOrEmpty(record.getSquarePicture())) {
+				record.setSquarePicture(resultObj.getSquarePicture());
+			} else {
+				removePicture(resultObj.getSquarePicture());
+			}
+			
+			if (StringUtil.isNullOrEmpty(record.getPicture()))
+				throw new BanLiShopGoodsException(1, "缂哄皯灏侀潰鍥�");
+			
+			record.setCreateTime(resultObj.getCreateTime());
+			banLiShopGoodsMapper.updateByPrimaryKey(record);
+		}
+	}
+	
+
+	/**
+	 * 涓婁紶鍥剧墖
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
+	public String uploadPicture(MultipartFile file) throws Exception {
+		// 鏂囦欢瑙f瀽 
+		InputStream inputStream = file.getInputStream();
+		String contentType = file.getContentType();
+		String type = contentType.substring(contentType.indexOf("/") + 1);
+		// 鏂囦欢璺緞
+		String filePath="/img/BanLiShopGoods/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
+		// 鎵ц涓婁紶
+		String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
+		return fileLink;
+	}
+
+	/**
+	 * 鍒犻櫎鍥剧墖-涓嶆洿鏂版暟鎹簱
+	 * @param record
+	 * @throws Exception
+	 */
+	public void removePicture(String picture) throws Exception {
+		if (picture != null && picture.trim().length() > 0) {
+			COSManager.getInstance().deleteFile(picture);
+		}
+	}
+	
+	
+	
 	@Override
 	public void updateSelectiveByPrimaryKey(BanLiShopGoods goods) {
 		if (goods == null || goods.getId() == null)

--
Gitblit v1.8.0