package com.yeshi.fanli.service.impl.config;
|
|
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.web.multipart.MultipartFile;
|
import org.yeshi.utils.tencentcloud.COSManager;
|
|
import com.yeshi.fanli.dao.mybatis.user.SpreadImgMapper;
|
import com.yeshi.fanli.entity.bus.invite.SpreadImg;
|
import com.yeshi.fanli.service.inter.config.SpreadImgService;
|
|
@Service
|
public class SpreadImgServiceImpl implements SpreadImgService {
|
|
@Resource
|
private SpreadImgMapper spreadImgMapper;
|
|
|
@Override
|
public void deleteInviteFriendImg(long id) {
|
spreadImgMapper.deleteByPrimaryKey(id);
|
}
|
|
@Override
|
public SpreadImg getInviteSpreadImg(long id) {
|
return spreadImgMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public void saveObject(MultipartFile file, SpreadImg record) throws Exception{
|
|
String picture = null;
|
if (file != null) {
|
picture = uploadPicture(file);
|
}
|
|
Long id = record.getId();
|
if (id == null) {
|
record.setUrl(picture);
|
record.setCreatetime(new Date());
|
spreadImgMapper.insert(record);
|
} else {
|
// 修改
|
SpreadImg resultObj = spreadImgMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new Exception("修改内容已不存在");
|
}
|
|
if (picture != null && picture.trim().length() > 0) {
|
// 删除已存在图片
|
removePicture(resultObj.getUrl());
|
record.setUrl(picture);
|
} else {
|
record.setUrl(resultObj.getUrl());
|
}
|
|
record.setCreatetime(resultObj.getCreatetime());
|
spreadImgMapper.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/invite/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
|
// 执行上传
|
return COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
|
}
|
|
/**
|
* 删除图片
|
* @param record
|
* @throws Exception
|
*/
|
public void removePicture(String picture) throws Exception {
|
if (picture != null && picture.trim().length() > 0) {
|
COSManager.getInstance().deleteFile(picture);
|
}
|
}
|
|
@Override
|
public List<SpreadImg> listQuery(int start, int pageSize, String key) {
|
return spreadImgMapper.listQuery(start, pageSize, key);
|
}
|
|
@Override
|
public long countQuery(String key) {
|
return spreadImgMapper.countQuery(key);
|
}
|
|
}
|