package com.yeshi.fanli.service.impl.goods;
|
|
import java.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.UUID;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.cache.annotation.Cacheable;
|
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.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.yeshi.fanli.dao.mybatis.GoodsClassMapper;
|
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
|
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass;
|
import com.yeshi.fanli.entity.bus.clazz.TaoBaoClass;
|
import com.yeshi.fanli.exception.goods.GoodsClassException;
|
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
|
import com.yeshi.fanli.service.inter.goods.GoodsSubClassService;
|
import com.yeshi.fanli.service.inter.goods.TaoBaoClassService;
|
import com.yeshi.fanli.service.inter.lable.LabelClassService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.FilePathEnum;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Service
|
public class GoodsClassServiceImpl implements GoodsClassService {
|
|
@Resource
|
private GoodsClassMapper goodsClassMapper;
|
|
@Resource
|
private GoodsSubClassService goodsSubClassService;
|
|
@Resource
|
private LabelClassService labelClassService;
|
|
@Resource
|
private TaoBaoClassService taoBaoClassService;
|
|
public GoodsClass getGoodsClass(long id) {
|
return goodsClassMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public int updateByPrimaryKey(GoodsClass record) {
|
return goodsClassMapper.updateByPrimaryKey(record);
|
}
|
|
@Override
|
public String getKeysById(Long id) {
|
return goodsClassMapper.getKeysById(id);
|
}
|
|
@Override
|
public GoodsClass selectByPrimaryKey(Long gcid) {
|
return goodsClassMapper.selectByPrimaryKey(gcid);
|
}
|
|
@Override
|
public int updateByPrimaryKeySelective(GoodsClass record) {
|
return goodsClassMapper.updateByPrimaryKeySelective(record);
|
}
|
|
@Cacheable(value = "classCache", key = "'getGoodsClass-'+#gcid")
|
@Override
|
public GoodsClass getGoodsClassCache(long gcid) {
|
return getGoodsClass(gcid);
|
}
|
|
@Override
|
public void deleteGoodsClass(long id) {
|
goodsClassMapper.deleteByPrimaryKey(id);
|
}
|
|
@Override
|
public List<GoodsClass> listquery(Integer sex) throws Exception {
|
|
List<GoodsClass> list = goodsClassMapper.getGoodsClassAll(sex);
|
|
if (list != null && list.size() == 0) {
|
return list;
|
}
|
|
for (GoodsClass gclass : list) {
|
Long iosClick = gclass.getIosClick();
|
Long androidClick = gclass.getAndroidClick();
|
|
if (iosClick != null && androidClick != null) {
|
gclass.setCountClick(iosClick + androidClick);
|
} else if (androidClick != null) {
|
gclass.setCountClick(androidClick);
|
} else if (iosClick != null) {
|
gclass.setCountClick(iosClick);
|
} else {
|
gclass.setCountClick(0l);
|
}
|
|
// 参数处理
|
String searchParam = gclass.getSearchParam();
|
if (searchParam == null) {
|
gclass.setSearchParam("");
|
} else {
|
gclass.setSearchParam(searchParam);
|
}
|
|
// 标签
|
int countlabel = labelClassService.getCountQueryByClassId(gclass.getId());
|
gclass.setCountlabel(countlabel);
|
|
List<TaoBaoClass> listTB = taoBaoClassService.listBySystemCid(0, Integer.MAX_VALUE, gclass.getId());
|
if (listTB == null || listTB.size() == 0) {
|
gclass.setTaobaoCids("");
|
} else {
|
String taobaoCids = "";
|
for (TaoBaoClass taoBaoClass : listTB) {
|
taobaoCids = taobaoCids + taoBaoClass.getCategoryName() + "-" + taoBaoClass.getCategoryId() + ",";
|
}
|
|
if (!StringUtil.isNullOrEmpty(taobaoCids)) {
|
taobaoCids = taobaoCids.substring(0, taobaoCids.length() - 1);
|
}
|
gclass.setTaobaoCids(taobaoCids);
|
}
|
|
}
|
return list;
|
}
|
|
public List<GoodsClass> getGoodsClassAll() {
|
return goodsClassMapper.getGoodsClassAll(null);
|
}
|
|
@Override
|
@Transactional(rollbackFor=Exception.class)
|
public void saveObject(MultipartFile file, GoodsClass record) throws GoodsClassException, Exception {
|
|
String name = record.getName();
|
if (name == null || name.trim().length() == 0) {
|
throw new GoodsClassException(1, "分类名称不能为空");
|
}
|
|
Integer sex = record.getSex();
|
if (sex == null) {
|
throw new GoodsClassException(1, "性别设置不能为空");
|
}
|
|
// 图片上传
|
String picture = null;
|
if (file != null) {
|
picture = uploadPicture(file);
|
}
|
|
String params = record.getSearchParam();
|
if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
|
record.setSearchParam(null);
|
} else if (!StringUtil.isJson(params)) {
|
throw new GoodsClassException(1, "筛选条件非JSON格式");
|
}
|
|
Long id = record.getId();
|
if (id == null) {
|
record.setPicture(picture);
|
record.setIosClick(0L);
|
record.setAndroidClick(0L);
|
record.setCreatetime(java.lang.System.currentTimeMillis());
|
|
Integer state = record.getState();
|
if (state == null) {
|
record.setState(0);
|
}
|
|
if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
|
// 搜索条件:有券、在售价20-200、牛皮癣轻微
|
record.setSearchParam(
|
"{\"quan\":1,\"endPrice\":220,\"includeGoodRate\":true,\"baoYou\":true,\"ip\":\"218.72.111.105\"}");
|
}
|
|
// 排序
|
int maxOrder = goodsClassMapper.getMaxOrder();
|
record.setOrderby(maxOrder + 1);
|
|
goodsClassMapper.insert(record);
|
|
} else {
|
// 修改
|
GoodsClass resultObj = goodsClassMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new GoodsClassException(1, "修改内容已不存在");
|
}
|
|
if (picture != null && picture.trim().length() > 0) {
|
// 删除老图
|
removePicture(resultObj.getPicture());
|
// 存储新图
|
record.setPicture(picture);
|
} else {
|
record.setPicture(resultObj.getPicture());
|
}
|
|
record.setIosClick(resultObj.getIosClick());
|
record.setOrderby(resultObj.getOrderby());
|
record.setAndroidClick(resultObj.getAndroidClick());
|
record.setCreatetime(resultObj.getCreatetime());
|
goodsClassMapper.updateByPrimaryKey(record);
|
}
|
|
// 保存淘宝商品分类id
|
String taobaoCids = record.getTaobaoCids();
|
List<Long> tbCidList = new ArrayList<>();
|
if (!StringUtil.isNullOrEmpty(taobaoCids) && !"null".equalsIgnoreCase(taobaoCids)) {
|
|
String[] sts = taobaoCids.split(",");
|
for (String st : sts) {
|
String cid = st.split("-")[st.split("-").length - 1].trim();
|
tbCidList.add(Long.parseLong(cid));
|
}
|
}
|
taoBaoClassService.save(record.getId(), tbCidList);
|
}
|
|
/**
|
* 上传图片
|
*
|
* @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 = FilePathEnum.goodsClass.getPath() + 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 (!Constant.IS_TEST)
|
if (picture != null && picture.trim().length() > 0) {
|
COSManager.getInstance().deleteFile(picture);
|
}
|
}
|
|
@Override
|
public void uploadPicture(GoodsClass record, MultipartFile file) throws Exception {
|
|
InputStream inputStream = file.getInputStream();
|
String contentType = file.getContentType();
|
String type = contentType.substring(contentType.indexOf("/") + 1);
|
// 上传文件相对位置
|
String fileUrl = FilePathEnum.goodsClassOld.getPath()+ UUID.randomUUID().toString().replace("-", "") + "." + type;
|
|
boolean deleteFile = true;
|
|
/* 修改图片时,先删除已存在图片 */
|
String picture = record.getPicture();
|
if (!StringUtil.isNullOrEmpty(picture))
|
deleteFile = COSManager.getInstance().deleteFile(picture);
|
|
String uploadFilePath = null;
|
/* 上传新图片 */
|
if (deleteFile) {
|
uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
|
}
|
|
/* 更新数据库信息 */
|
if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
|
record.setPicture(uploadFilePath);
|
goodsClassMapper.updateByPrimaryKey(record);
|
}
|
|
}
|
|
/**
|
* 删除图片
|
*/
|
@Override
|
public void removePicture(GoodsClass record) throws Exception {
|
|
String fileUrl = record.getPicture();
|
boolean deleteFile = true;
|
|
if (StringUtil.isNullOrEmpty(fileUrl)) {
|
return;
|
}
|
|
deleteFile = COSManager.getInstance().deleteFile(fileUrl);
|
if (deleteFile) {
|
record.setPicture(null);
|
// 更新数据库
|
goodsClassMapper.updateByPrimaryKey(record);
|
}
|
}
|
|
@Override
|
public void switchState(Long id) throws GoodsClassException {
|
if (id == null) {
|
throw new GoodsClassException(1, "请传递正确参数");
|
}
|
GoodsClass resultObj = goodsClassMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new GoodsClassException(1, "此内容已不存在");
|
}
|
|
Integer state = resultObj.getState();
|
if (state == null || state == 0) {
|
state = 1;
|
} else {
|
state = 0;
|
}
|
|
GoodsClass updateObj = new GoodsClass();
|
updateObj.setId(id);
|
updateObj.setState(state);
|
goodsClassMapper.updateByPrimaryKeySelective(updateObj);
|
}
|
|
|
@Override
|
@Cacheable(value = "classCache", key = "'getEffectiveClassCache'")
|
public List<GoodsClass> getEffectiveClassCache() {
|
return goodsClassMapper.getEffectiveClass();
|
}
|
|
@Override
|
@Cacheable(value = "classCache", key = "'getClassListAllCache'+#changePicture")
|
public List<Map<String, Object>> getClassListAllCache(boolean changePicture) throws Exception {
|
|
List<GoodsClass> goodsClassList = goodsClassMapper.getEffectiveClass();
|
if (goodsClassList == null || goodsClassList.size() == 0) {
|
return null;
|
}
|
|
List<Long> listID = new ArrayList<Long>();
|
for (GoodsClass coodsClass : goodsClassList) {
|
listID.add(coodsClass.getId());
|
}
|
|
List<GoodsSubClass> listSub = goodsSubClassService.queryByListCid(listID);
|
|
Gson gson = new GsonBuilder().create();
|
|
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
for (GoodsClass goodsClass : goodsClassList) {
|
|
List<GoodsSubClass> subClassList = new ArrayList<GoodsSubClass>();
|
|
if (listSub != null && listSub.size() > 0) {
|
Iterator<GoodsSubClass> iterator = listSub.iterator();
|
while (iterator.hasNext()) {
|
GoodsSubClass goodsSubClass = iterator.next();
|
GoodsClass goodsClassInner = goodsSubClass.getRootClass();
|
if (goodsClass.getId() == goodsClassInner.getId()) {
|
|
// 1.5.3新版图片替换
|
if (changePicture) {
|
String pictureSecond = goodsSubClass.getPictureSecond();
|
if (pictureSecond != null && pictureSecond.trim().length() > 0) {
|
goodsSubClass.setPicture(pictureSecond);
|
}
|
}
|
|
// 对应下子分类
|
subClassList.add(goodsSubClass);
|
iterator.remove();
|
}
|
|
}
|
}
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
map.put("gclass", goodsClass);
|
map.put("subList", gson.toJson(subClassList));
|
|
list.add(map);
|
}
|
|
return list;
|
}
|
|
@Override
|
@Transactional(rollbackFor=Exception.class)
|
public void updateOrder(Long id, Integer moveType) throws GoodsClassException {
|
|
if (id == null || moveType == null || (!moveType.equals(1) && !moveType.equals(-1))) {
|
throw new GoodsClassException(1, "请传递正确参数");
|
}
|
|
GoodsClass resultObj = goodsClassMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new GoodsClassException(1, "此内容已不存在");
|
}
|
|
Integer order = resultObj.getOrderby();
|
// 获取交换对象
|
GoodsClass exchangeObject = goodsClassMapper.getByAdjoinOrder(order, moveType);
|
if (exchangeObject == null) {
|
if (moveType == 1) {
|
throw new GoodsClassException(1, "在相同使用地方中优先级已经最低了");
|
} else {
|
throw new GoodsClassException(1, "在相同使用地方中优先级已经最高了");
|
}
|
}
|
|
resultObj.setOrderby(exchangeObject.getOrderby());
|
exchangeObject.setOrderby(order);
|
|
goodsClassMapper.updateByPrimaryKey(resultObj);
|
goodsClassMapper.updateByPrimaryKey(exchangeObject);
|
}
|
|
}
|