From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java | 740 ++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 445 insertions(+), 295 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java index dc499f9..22610aa 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java @@ -1,295 +1,445 @@ -package com.yeshi.fanli.service.impl.goods; - -import java.io.InputStream; -import java.util.List; -import java.util.UUID; - -import javax.annotation.Resource; -import javax.transaction.Transactional; - -import org.springframework.cache.annotation.Cacheable; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import com.yeshi.fanli.dao.mybatis.GoodsSubClassMapper; -import com.yeshi.fanli.entity.accept.AcceptData; -import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass; -import com.yeshi.fanli.service.inter.goods.GoodsSubClassService; -import com.yeshi.fanli.service.inter.lable.LabelClassService; -import com.yeshi.fanli.util.StringUtil; -import org.yeshi.utils.tencentcloud.COSManager; - -@Service -public class GoodsSubClassServiceImpl implements GoodsSubClassService { - - @Resource - private GoodsSubClassMapper goodsSubClassMapper; - @Resource - private GoodsSubClassService goodsSubClassService; - @Resource - private LabelClassService labelClassService; - - - @Override - public int deleteByPrimaryKey(Long id) { - return goodsSubClassMapper.deleteByPrimaryKey(id); - } - - @Override - public int insert(GoodsSubClass record) { - return goodsSubClassMapper.insert(record); - } - - @Override - public int insertSelective(GoodsSubClass record) { - return goodsSubClassMapper.insertSelective(record); - } - - @Override - public GoodsSubClass selectByPrimaryKey(Long id) { - return goodsSubClassMapper.selectByPrimaryKey(id); - } - - @Override - public int updateByPrimaryKeySelective(GoodsSubClass record) { - return goodsSubClassMapper.updateByPrimaryKeySelective(record); - } - - @Override - public int updateByPrimaryKey(GoodsSubClass record) { - return goodsSubClassMapper.updateByPrimaryKey(record); - } - - @Override - @Transactional - public void deleteByRootId(Long id) throws Exception { - - List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null); - if (subClassList != null && subClassList.size() > 0) { - for (GoodsSubClass goodsSubClass : subClassList) { - deleteSub(goodsSubClass.getId()); - } - } - } - - @Override - @Transactional - public void deleteByPrimaryKeyBatch(List<String> recordIds) throws Exception { - if (recordIds != null && recordIds.size() > 0) { - for (String recordId : recordIds) { - deleteSub(Long.parseLong(recordId)); - } - } - } - - - @Override - @Transactional - public void deleteSub(Long recordId) throws Exception { - // TODO Auto-generated method stub - - GoodsSubClass goodsSubClass = goodsSubClassMapper.selectByPrimaryKey(recordId); - - if (goodsSubClass == null) - return; - - /* 鍒犻櫎缃戠粶鍥剧墖 */ - String picture = goodsSubClass.getPicture(); - if (!StringUtil.isNullOrEmpty(picture)) { - COSManager.getInstance().deleteFile(picture); - } - - /* 鍒犻櫎鎵�鏈夊叧鑱斿瓙绫� */ - List<GoodsSubClass> subList = goodsSubClassMapper.queryByPid(recordId, null); - if (subList != null && subList.size() > 0) { - for (GoodsSubClass subClass : subList) { - Long id = subClass.getId(); - // 缁х画鍒犻櫎涓嬬骇 - deleteSub(id); - } - } - - /* 鍒犻櫎鍏宠仈鏍囩 */ - labelClassService.deleteBySubClassId(recordId); - - /* 鍒犻櫎鏁版嵁 */ - goodsSubClassMapper.deleteByPrimaryKey(recordId); - } - - @Override - public int save(GoodsSubClass record, MultipartFile file) throws Exception { - - int result = 0; - - result = goodsSubClassMapper.insertSelective(record); - - - // 涓婁紶鍥剧墖 - if (file != null) { - result = goodsSubClassService.uploadPicture(record, file); - } - - return result; - } - - @Override - public int uploadPicture(GoodsSubClass record, MultipartFile file) throws Exception { - - InputStream inputStream = file.getInputStream(); - String contentType = file.getContentType(); - String type = contentType.substring(contentType.indexOf("/") + 1); - // 涓婁紶鏂囦欢鐩稿浣嶇疆 - String fileUrl = "ClassImg/" + 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(); - } - - /* 鏇存柊鏁版嵁搴撲俊鎭� */ - int result = 0; - if (!StringUtil.isNullOrEmpty(uploadFilePath)) { - record.setPicture(uploadFilePath); - result = goodsSubClassMapper.updateByPrimaryKeySelective(record); - } - return result; - } - - /** - * 鍒犻櫎鍥剧墖 - */ - @Override - public int removePicture(GoodsSubClass record) throws Exception { - - String fileUrl = record.getPicture(); - - int result = -2; - boolean deleteFile = true; - - if (StringUtil.isNullOrEmpty(fileUrl)) { - return result; - } - - deleteFile = COSManager.getInstance().deleteFile(fileUrl); - - if (deleteFile) { - record.setPicture(null); - // 鏇存柊鏁版嵁搴� - result = goodsSubClassMapper.updateByPrimaryKey(record); - } - - return result; - } - - @Override - public List<GoodsSubClass> queryByRootId(Long rootId, Integer state) throws Exception { - return goodsSubClassMapper.queryByRootId(rootId, state); - } - - @Override - public List<GoodsSubClass> queryByPid(Long pid, Integer state) throws Exception { - return goodsSubClassMapper.queryByPid(pid, state); - } - - - @Override - public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId,int type, int weight) throws Exception { - return goodsSubClassMapper.queryByRootIdAndWeight(rootId, type, weight); - } - - @Override - public List<GoodsSubClass> queryByPidAndWeight(Long pid,int type, int weight) throws Exception { - return goodsSubClassMapper.queryByPidAndWeight(pid, type, weight); - } - - - @Override - public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state) throws Exception { - return goodsSubClassMapper.queryByRootId(rootId, state); - } - - - @Override - @Cacheable(value="classCache",key="'getSubClassCache-'+#rootId +'-'+#state") - public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state) throws Exception { - return getGoodsSecondClass(rootId, state); - } - - - @Override - @Cacheable(value="classCache",key="'getSubClassByPrimaryKeyCache-'+#id") - public GoodsSubClass getSubClassByPrimaryKeyCache(Long id) throws Exception { - return selectByPrimaryKey(id); - } - - - - /** - * 缁熻涓�绾т箣涓嬬殑鎵�鏈変簩绾у垎绫� - * @param rootId 涓�绾d - * @returnL - */ - @Override - public int countByRootId(Long rootId) { - return goodsSubClassMapper.countByRootId(rootId); - } - - /** - * 缁熻浜岀骇鍒嗙被涔嬩笅鍏朵粬鍒嗙被 - * @param rootId 涓�绾d - * @return - */ - @Override - public int countByPid(Long pid){ - return goodsSubClassMapper.countByPid(pid); - } - - /** - * 缁熻涓�绾т箣涓嬬殑鎵�鏈変簩绾у垎绫绘渶澶ф潈閲� - * @param rootId 涓�绾d - * @returnL - */ - @Override - public int getMaxWeightByRootId(Long rootId) { - return goodsSubClassMapper.getMaxWeightByRootId(rootId); - } - - /** - * 缁熻浜岀骇鍒嗙被涔嬩笅鍏朵粬鍒嗙被鏈�澶ф潈閲� - * @param rootId 涓�绾d - * @return - */ - @Override - public int getMaxWeightByPid(Long pid){ - return goodsSubClassMapper.getMaxWeightByPid(pid); - } - - - @Override - public void countClick(AcceptData acceptData, GoodsSubClass record) { - if ("android".equalsIgnoreCase(acceptData.getPlatform())) { - Long androidClick = record.getAndroidClick(); - if (androidClick != null) { - record.setAndroidClick(androidClick + 1); - } else { - record.setAndroidClick(1L); - } - } else if ("ios".equalsIgnoreCase(acceptData.getPlatform())) { - Long iosClick = record.getIosClick(); - if (iosClick != null) { - record.setIosClick(iosClick + 1); - } else { - record.setIosClick(1L); - } - } - goodsSubClassService.updateByPrimaryKeySelective(record); - } -} +package com.yeshi.fanli.service.impl.goods; + +import java.io.InputStream; +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.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import org.yeshi.utils.tencentcloud.COSManager; + +import com.yeshi.fanli.dao.mybatis.GoodsSubClassMapper; +import com.yeshi.fanli.entity.accept.AcceptData; +import com.yeshi.fanli.entity.bus.clazz.GoodsClass; +import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass; +import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabel; +import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabelMap; +import com.yeshi.fanli.exception.goods.GoodsSubClassException; +import com.yeshi.fanli.service.inter.clazz.GoodsSubClassLabelService; +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 GoodsSubClassServiceImpl implements GoodsSubClassService { + + @Resource + private GoodsSubClassMapper goodsSubClassMapper; + @Resource + private GoodsSubClassService goodsSubClassService; + @Resource + private LabelClassService labelClassService; + + @Resource + private TaoBaoClassService taoBaoClassService; + + @Resource + private GoodsSubClassLabelService goodsSubClassLabelService; + + @Override + public int deleteByPrimaryKey(Long id) { + return goodsSubClassMapper.deleteByPrimaryKey(id); + } + + @Override + public int insert(GoodsSubClass record) { + return goodsSubClassMapper.insert(record); + } + + @Override + public int insertSelective(GoodsSubClass record) { + return goodsSubClassMapper.insertSelective(record); + } + + @Override + public GoodsSubClass selectByPrimaryKey(Long id) { + return goodsSubClassMapper.selectByPrimaryKey(id); + } + + @Override + public int updateByPrimaryKeySelective(GoodsSubClass record) { + return goodsSubClassMapper.updateByPrimaryKeySelective(record); + } + + @Override + @Transactional(rollbackFor=Exception.class) + public void deleteByRootId(Long id) throws Exception { + + List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null, null); + if (subClassList != null && subClassList.size() > 0) { + for (GoodsSubClass goodsSubClass : subClassList) { + deleteSub(goodsSubClass.getId()); + } + } + } + + @Override + @Transactional(rollbackFor=Exception.class) + public void deleteByPrimaryKeyBatch(List<String> recordIds) throws Exception { + if (recordIds != null && recordIds.size() > 0) { + for (String recordId : recordIds) { + deleteSub(Long.parseLong(recordId)); + } + } + } + + @Override + @Transactional(rollbackFor=Exception.class) + public void deleteSub(Long recordId) throws Exception { + GoodsSubClass goodsSubClass = goodsSubClassMapper.selectByPrimaryKey(recordId); + + if (goodsSubClass == null) + return; + + /* 鍒犻櫎缃戠粶鍥剧墖 */ + String picture = goodsSubClass.getPicture(); + if (!StringUtil.isNullOrEmpty(picture)) { + COSManager.getInstance().deleteFile(picture); + } + + /* 鍒犻櫎缃戠粶鍥剧墖 */ + String pictureSecond = goodsSubClass.getPictureSecond(); + if (!StringUtil.isNullOrEmpty(pictureSecond)) { + COSManager.getInstance().deleteFile(pictureSecond); + } + + /* 鍒犻櫎鎵�鏈夊叧鑱斿瓙绫� */ + List<GoodsSubClass> subList = goodsSubClassMapper.queryByPid(recordId, null); + if (subList != null && subList.size() > 0) { + for (GoodsSubClass subClass : subList) { + Long id = subClass.getId(); + // 缁х画鍒犻櫎涓嬬骇 + deleteSub(id); + } + } + + /* 鍒犻櫎鍏宠仈鏍囩 */ + labelClassService.deleteBySubClassId(recordId); + + /* 鍒犻櫎鏁版嵁 */ + goodsSubClassMapper.deleteByPrimaryKey(recordId); + } + + @Override + public void saveObject(MultipartFile file, MultipartFile file2, GoodsSubClass record, Integer type, Long pid, + Long labelId) throws GoodsSubClassException, Exception { + + String name = record.getName(); + if (name == null || name.trim().length() == 0) { + throw new GoodsSubClassException(1, "鍒嗙被鍚嶇О涓嶈兘涓虹┖"); + } + + if (StringUtil.isNullOrEmpty(record.getMonth())) + record.setMonth(null); + + // 鍥剧墖涓婁紶 + String picture = null; + if (file != null) { + picture = uploadPicture(file); + } + + // 鍥剧墖涓婁紶 + String pictureSecond = null; + if (file2 != null) { + pictureSecond = uploadPicture(file2); + } + + String params = record.getSearchParam(); + if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) { + record.setSearchJson(null); + } else if (!StringUtil.isJson(params)) { + throw new GoodsSubClassException(1, "绛涢�夋潯浠堕潪JSON鏍煎紡"); + } else { + record.setSearchJson(params); + } + + Long id = record.getId(); + if (id == null) { + if (type == null) { + throw new GoodsSubClassException(1, "绛夌骇涓嶈兘涓虹┖"); + } + + if (type > 5) { + throw new GoodsSubClassException(1, "绛夌骇涓嶈兘瓒呰繃浜旂骇"); + } + + if (pid == null) { + throw new GoodsSubClassException(1, "涓婄骇id涓虹┖"); + } + + if (type == 2) { + record.setRootClass(new GoodsClass(pid)); + int weight = goodsSubClassMapper.getMaxWeightByRootId(pid); + record.setWeight(weight + 1); + } else { + record.setParent(new GoodsSubClass(pid)); + int weight = goodsSubClassMapper.getMaxWeightByPid(pid); + record.setWeight(weight + 1); + } + + String key = record.getKey(); + if (StringUtil.isNullOrEmpty(key)) { + record.setKey(name.trim()); + } + + record.setLevel(type); + record.setPicture(picture); + record.setPictureSecond(pictureSecond); + record.setState(0); + record.setAndroidClick(0L); + record.setIosClick(0L); + record.setCreatetime(new Date()); + record.setUpdatetime(new Date()); + if (labelId != null) + record.setClassLabel(new GoodsSubClassLabel(labelId)); + + if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) { + // 鎼滅储鏉′欢:鏈夊埜銆佸湪鍞环20-200銆佺墰鐨櫍杞诲井 + record.setSearchJson("{\"quan\":1,\"endPrice\":220,\"includeGoodRate\":true}"); + } + + goodsSubClassMapper.insert(record); + + if (labelId != null) { + GoodsSubClassLabelMap map = new GoodsSubClassLabelMap(); + map.setGoodsSubClass(record); + map.setLabel(new GoodsSubClassLabel(labelId)); + goodsSubClassLabelService.addSubClassLabelMap(map); + } + + } else { + // 淇敼 + GoodsSubClass resultObj = goodsSubClassMapper.selectByPrimaryKey(id); + if (resultObj == null) { + throw new GoodsSubClassException(1, "淇敼鍐呭宸蹭笉瀛樺湪"); + } + + if (picture != null && picture.trim().length() > 0) { + // 鍒犻櫎鑰佸浘 + if (!Constant.IS_TEST) + removePicture(resultObj.getPicture()); + // 瀛樺偍鏂板浘 + record.setPicture(picture); + } else { + record.setPicture(resultObj.getPicture()); + } + + if (pictureSecond != null && pictureSecond.trim().length() > 0) { + // 鍒犻櫎鑰佸浘 + if (!Constant.IS_TEST) + removePicture(resultObj.getPictureSecond()); + // 瀛樺偍鏂板浘 + record.setPictureSecond(pictureSecond); + } else { + record.setPictureSecond(resultObj.getPictureSecond()); + } + + record.setLevel(resultObj.getLevel()); + record.setRootClass(resultObj.getRootClass()); + record.setWeight(resultObj.getWeight()); + record.setIosClick(resultObj.getIosClick()); + record.setAndroidClick(resultObj.getAndroidClick()); + record.setCreatetime(resultObj.getCreatetime()); + record.setUpdatetime(new Date()); + if (labelId != null) { + GoodsSubClassLabelMap map = new GoodsSubClassLabelMap(); + map.setGoodsSubClass(record); + map.setLabel(new GoodsSubClassLabel(labelId)); + try { + goodsSubClassLabelService.addSubClassLabelMap(map); + } catch (Exception e) { + } + } + + goodsSubClassMapper.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]; + tbCidList.add(Long.parseLong(cid)); + } + } + taoBaoClassService.saveSub(record.getId(), tbCidList); + } + + /** + * 涓婁紶鍥剧墖 + * + * @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 =FilePathEnum.goodsSubClass.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 (picture != null && picture.trim().length() > 0) { + COSManager.getInstance().deleteFile(picture); + } + } + + /** + * 鍒犻櫎鍥剧墖 + */ + @Override + public int removePicture(GoodsSubClass record) throws Exception { + int result = -2; + String fileUrl = record.getPicture(); + if (StringUtil.isNullOrEmpty(fileUrl)) { + return result; + } + + boolean deleteFile = COSManager.getInstance().deleteFile(fileUrl);; + if (deleteFile) { + record.setPicture(null); + // 鏇存柊鏁版嵁搴� + result = goodsSubClassMapper.updateByPrimaryKey(record); + } + return result; + } + + + @Override + public void switchState(Long id) throws GoodsSubClassException { + if (id == null) { + throw new GoodsSubClassException(1, "璇蜂紶閫掓纭弬鏁�"); + } + GoodsSubClass resultObj = goodsSubClassMapper.selectByPrimaryKey(id); + if (resultObj == null) { + throw new GoodsSubClassException(1, "姝ゅ唴瀹瑰凡涓嶅瓨鍦�"); + } + + Integer state = resultObj.getState(); + if (state == null || state == 0) { + state = 1; + } else { + state = 0; + } + + GoodsSubClass updateObj = new GoodsSubClass(); + updateObj.setId(id); + updateObj.setState(state); + goodsSubClassMapper.updateByPrimaryKeySelective(updateObj); + } + + + @Override + public List<GoodsSubClass> queryByRootId(Long rootId, Integer state) throws Exception { + return goodsSubClassMapper.queryByRootId(rootId, state, null); + } + + @Override + public List<GoodsSubClass> queryByPid(Long pid, Integer state) throws Exception { + return goodsSubClassMapper.queryByPid(pid, state); + } + + @Override + public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId, int type, int weight) throws Exception { + return goodsSubClassMapper.queryByRootIdAndWeight(rootId, type, weight); + } + + @Override + public List<GoodsSubClass> queryByPidAndWeight(Long pid, int type, int weight) throws Exception { + return goodsSubClassMapper.queryByPidAndWeight(pid, type, weight); + } + + @Override + public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state, Integer month) throws Exception { + return goodsSubClassMapper.queryByRootId(rootId, state, month); + } + + @Override + @Cacheable(value = "classCache", key = "'getSubClassCache-'+#rootId +'-'+#state") + public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state, Integer month) throws Exception { + return getGoodsSecondClass(rootId, state, month); + } + + @Override + @Cacheable(value = "classCache", key = "'getSubClassByPrimaryKeyCache-'+#id") + public GoodsSubClass getSubClassByPrimaryKeyCache(Long id) throws Exception { + return selectByPrimaryKey(id); + } + + /** + * 缁熻涓�绾т箣涓嬬殑鎵�鏈変簩绾у垎绫� + * + * @param rootId + * 涓�绾d + * @returnL + */ + @Override + public int countByRootId(Long rootId) { + return goodsSubClassMapper.countByRootId(rootId); + } + + /** + * 缁熻浜岀骇鍒嗙被涔嬩笅鍏朵粬鍒嗙被 + * + * @param rootId + * 涓�绾d + * @return + */ + @Override + public int countByPid(Long pid) { + return goodsSubClassMapper.countByPid(pid); + } + + @Override + public void countClick(AcceptData acceptData, GoodsSubClass record) { + if ("android".equalsIgnoreCase(acceptData.getPlatform())) { + Long androidClick = record.getAndroidClick(); + if (androidClick != null) { + record.setAndroidClick(androidClick + 1); + } else { + record.setAndroidClick(1L); + } + } else if ("ios".equalsIgnoreCase(acceptData.getPlatform())) { + Long iosClick = record.getIosClick(); + if (iosClick != null) { + record.setIosClick(iosClick + 1); + } else { + record.setIosClick(1L); + } + } + goodsSubClassService.updateByPrimaryKeySelective(record); + } + + @Override + public List<GoodsSubClass> queryByListCid(List<Long> list) { + return goodsSubClassMapper.queryByListCid(list); + } + +} -- Gitblit v1.8.0