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 一级id
|
* @returnL
|
*/
|
@Override
|
public int countByRootId(Long rootId) {
|
return goodsSubClassMapper.countByRootId(rootId);
|
}
|
|
/**
|
* 统计二级分类之下其他分类
|
* @param rootId 一级id
|
* @return
|
*/
|
@Override
|
public int countByPid(Long pid){
|
return goodsSubClassMapper.countByPid(pid);
|
}
|
|
/**
|
* 统计一级之下的所有二级分类最大权重
|
* @param rootId 一级id
|
* @returnL
|
*/
|
@Override
|
public int getMaxWeightByRootId(Long rootId) {
|
return goodsSubClassMapper.getMaxWeightByRootId(rootId);
|
}
|
|
/**
|
* 统计二级分类之下其他分类最大权重
|
* @param rootId 一级id
|
* @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);
|
}
|
}
|