package com.yeshi.fanli.service.impl.homemodule;
|
|
import java.io.InputStream;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
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.homemodule.FloatADMapper;
|
import com.yeshi.fanli.entity.bus.homemodule.FloatAD;
|
import com.yeshi.fanli.entity.common.JumpDetailV2;
|
import com.yeshi.fanli.exception.homemodule.FloatADException;
|
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
|
import com.yeshi.fanli.service.inter.homemodule.FloatADService;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Service
|
public class FloatADServiceImpl implements FloatADService {
|
|
@Resource
|
private FloatADMapper floatADMapper;
|
|
@Resource
|
private JumpDetailV2Service jumpDetailV2Service;
|
|
|
@Override
|
public void saveObject(MultipartFile file, FloatAD record, String jumpType) throws FloatADException, Exception{
|
|
String position = record.getPosition();
|
if (position == null || position.trim().length() == 0) {
|
throw new FloatADException(1, "使用位置不能为空");
|
}
|
|
String showMode = record.getShowMode();
|
if (showMode == null || showMode.trim().length() == 0) {
|
throw new FloatADException(1, "显示方式不能为空");
|
}
|
|
String params = record.getParams();
|
if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params) ) {
|
record.setParams(null);
|
} else if (!StringUtil.isJson(params)) {
|
throw new FloatADException(1, "跳转参数非JSON格式");
|
}
|
|
if (!StringUtil.isNullOrEmpty(jumpType)) {
|
List<JumpDetailV2> listByType = jumpDetailV2Service.listByType(jumpType);
|
if (listByType !=null && listByType.size() > 0) {
|
record.setJumpDetail(listByType.get(0));
|
}
|
}
|
|
try {
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
String startTime_str = record.getStartTime_str();
|
if (startTime_str != null && startTime_str.trim().length() > 0) {
|
startTime_str = startTime_str.replaceAll("T", " ");
|
record.setStartTime(format.parse(startTime_str));
|
}
|
|
String endTime_str = record.getEndTime_str();
|
if (endTime_str != null && endTime_str.trim().length() > 0) {
|
endTime_str = endTime_str.replaceAll("T", " ");
|
record.setEndTime(format.parse(endTime_str));
|
}
|
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
|
// 图片上传
|
String picture = null;
|
if (file != null) {
|
picture = uploadPicture(file);
|
}
|
|
// 适用类型 : 0通用 1新人
|
Integer type = record.getType();
|
if (type == null) {
|
record.setType(0);
|
}
|
|
Long id = record.getId();
|
if (id == null) {
|
int maxOrder = floatADMapper.getMaxOrderByPosition(position);
|
record.setPicture(picture);
|
Integer state = record.getState();
|
if (state == null) {
|
record.setState(0);
|
}
|
|
record.setOrder(maxOrder + 1);
|
record.setCreateTime(new Date());
|
record.setUpdateTime(new Date());
|
floatADMapper.insert(record);
|
} else {
|
// 修改
|
FloatAD resultObj = floatADMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new FloatADException(1, "修改内容已不存在");
|
}
|
|
if (picture != null && picture.trim().length() > 0) {
|
// 删除老图
|
removePicture(resultObj.getPicture());
|
// 存储新图
|
record.setPicture(picture);
|
} else {
|
record.setPicture(resultObj.getPicture());
|
}
|
|
record.setOrder(resultObj.getOrder());
|
record.setCreateTime(resultObj.getCreateTime());
|
record.setUpdateTime(new Date());
|
floatADMapper.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/FloatAD/"+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
|
@Transactional
|
public void updateOrder(Long id, Integer moveType) throws FloatADException {
|
|
if (id == null || moveType == null || (!moveType.equals(1) && !moveType.equals(-1))) {
|
throw new FloatADException(1, "请传递正确参数");
|
}
|
|
FloatAD resultObj = floatADMapper.selectByPrimaryKey(id);
|
if (resultObj == null) {
|
throw new FloatADException(1, "此内容已不存在");
|
}
|
|
Integer order = resultObj.getOrder();
|
String position = resultObj.getPosition();
|
|
// 获取交换对象
|
FloatAD exchangeObject = floatADMapper.getByAdjoinOrder(position, order, moveType);
|
if (exchangeObject == null) {
|
if (moveType == 1) {
|
throw new FloatADException(1, "在相同使用地方中优先级已经最低了");
|
} else {
|
throw new FloatADException(1, "在相同使用地方中优先级已经最高了");
|
}
|
}
|
|
resultObj.setOrder(exchangeObject.getOrder());
|
exchangeObject.setOrder(order);
|
|
floatADMapper.updateByPrimaryKey(resultObj);
|
floatADMapper.updateByPrimaryKey(exchangeObject);
|
}
|
|
@Override
|
@Transactional
|
public int deleteByPrimaryKeyList(List<Long> list) throws Exception{
|
|
List<FloatAD> listSwiper = floatADMapper.ListByPrimaryKey(list);
|
if (listSwiper == null || listSwiper.size() == 0) {
|
return 0;
|
}
|
|
// 删除已存在图片
|
for (FloatAD floatAD: listSwiper) {
|
removePicture(floatAD.getPicture());
|
}
|
|
return floatADMapper.deleteByPrimaryKeyList(list);
|
}
|
|
|
|
@Override
|
public List<FloatAD> listQuery(long start, int count, String key, Integer state) throws FloatADException {
|
|
List<FloatAD> listQuery = floatADMapper.listQuery(start, count, key, state);
|
if (listQuery == null || listQuery.size() == 0) {
|
return listQuery;
|
}
|
|
for (FloatAD floatAD : listQuery) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
|
Date startTime = floatAD.getStartTime();
|
if (startTime == null) {
|
floatAD.setStartTime_str("");
|
} else {
|
floatAD.setStartTime_str(sdf.format(startTime));
|
}
|
|
Date endTime = floatAD.getEndTime();
|
if (endTime == null) {
|
floatAD.setEndTime_str("");
|
} else {
|
floatAD.setEndTime_str(sdf.format(endTime));
|
}
|
|
String params = floatAD.getParams();
|
if (params == null) {
|
floatAD.setParams("");
|
}
|
|
JumpDetailV2 jumpDetail = floatAD.getJumpDetail();
|
if (jumpDetail == null) {
|
// 默认未选择
|
jumpDetail = new JumpDetailV2();
|
jumpDetail.setName("-未选择-");
|
jumpDetail.setType("default");
|
} else {
|
jumpDetail = jumpDetailV2Service.selectByPrimaryKey(jumpDetail.getId());
|
}
|
|
floatAD.setJumpDetail(jumpDetail);
|
}
|
|
return listQuery;
|
}
|
|
|
@Override
|
public long countQuery(String key, Integer state) {
|
return floatADMapper.countQuery(key, state);
|
}
|
|
@Override
|
public FloatAD getEffectiveFloatAD(String position, Integer type) {
|
FloatAD floatAD = floatADMapper.getEffectiveFloatAD(position, type);
|
if (floatAD != null) {
|
JumpDetailV2 jumpDetail = floatAD.getJumpDetail();
|
|
// 查询JumpDetailV2
|
if (jumpDetail != null) {
|
jumpDetail = jumpDetailV2Service.selectByPrimaryKey(jumpDetail.getId());
|
if (jumpDetail != null) {
|
// 跳转界面是否需要登录
|
jumpDetail.setNeedLogin(floatAD.isJumpNeedLogin());
|
}
|
}
|
|
floatAD.setJumpDetail(jumpDetail);
|
}
|
return floatAD;
|
}
|
|
}
|
|