|
|
|
@ -2,6 +2,7 @@ package com.luoo.music.service;
|
|
|
|
|
|
|
|
|
|
import api.PageResult;
|
|
|
|
|
import api.Result;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.luoo.music.client.UserClient;
|
|
|
|
|
import com.luoo.music.dao.AdvertisementDao;
|
|
|
|
|
import com.luoo.music.dto.request.cms.AdvertisementReqModel;
|
|
|
|
@ -9,6 +10,7 @@ import com.luoo.music.dto.response.cms.AdvertisementRespDTO;
|
|
|
|
|
import com.luoo.music.pojo.Advertisement;
|
|
|
|
|
import com.luoo.music.pojo.UserInfo;
|
|
|
|
|
import com.luoo.music.util.Constants;
|
|
|
|
|
import net.oschina.j2cache.CacheChannel;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.domain.Example;
|
|
|
|
@ -28,7 +30,6 @@ import java.time.LocalDateTime;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Revers.
|
|
|
|
@ -46,6 +47,9 @@ public class CMSAdvertisementService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserClient userClient;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private CacheChannel cacheChannel;
|
|
|
|
|
|
|
|
|
|
public Result add(AdvertisementReqModel paramAdd) {
|
|
|
|
|
String image = null;
|
|
|
|
|
if (StringUtils.isNotBlank(paramAdd.getImage())) {
|
|
|
|
@ -90,6 +94,7 @@ public class CMSAdvertisementService {
|
|
|
|
|
|
|
|
|
|
if(!adDao.exists(example)) {
|
|
|
|
|
adDao.save(ad);
|
|
|
|
|
cleanAdCache(ad.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.success();
|
|
|
|
@ -109,22 +114,23 @@ public class CMSAdvertisementService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result deleteById(String id){
|
|
|
|
|
Advertisement ad = adDao.findById(id).get();
|
|
|
|
|
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
|
|
|
|
|
|
|
|
|
|
if (!Objects.isNull(ad)) {
|
|
|
|
|
ad.setIsDeleted("1");
|
|
|
|
|
adDao.save(ad);
|
|
|
|
|
cleanAdCache(ad.getId());
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
return Result.failed("广告不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result update(String id,AdvertisementReqModel param){
|
|
|
|
|
Optional<Advertisement> optional = adDao.findById(id);
|
|
|
|
|
if (!optional.isPresent()) {
|
|
|
|
|
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
|
|
|
|
|
if (ad == null) {
|
|
|
|
|
return Result.failed("找不到广告: " + id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Advertisement ad = optional.get();
|
|
|
|
|
|
|
|
|
|
//如果图片路径存在 temp/ 则为新图片
|
|
|
|
|
if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) {
|
|
|
|
@ -151,16 +157,16 @@ public class CMSAdvertisementService {
|
|
|
|
|
ad.setSummary(contentToSummary(param.getContent()));
|
|
|
|
|
|
|
|
|
|
adDao.save(ad);
|
|
|
|
|
cleanAdCache(ad.getId());
|
|
|
|
|
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result<AdvertisementRespDTO> findOne(String id) {
|
|
|
|
|
Optional<Advertisement> optional=adDao.findById(id);
|
|
|
|
|
if(!optional.isPresent()) {
|
|
|
|
|
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
|
|
|
|
|
if(ad == null) {
|
|
|
|
|
return Result.failed("无法找到广告: " + id);
|
|
|
|
|
}
|
|
|
|
|
Advertisement ad = optional.get();
|
|
|
|
|
|
|
|
|
|
if("1".equals(ad.getIsDeleted())){
|
|
|
|
|
return Result.success(null);
|
|
|
|
@ -175,20 +181,8 @@ public class CMSAdvertisementService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result<PageResult<AdvertisementRespDTO>> search(int page, int size,String location) {
|
|
|
|
|
List<AdvertisementRespDTO> result = new ArrayList<>();
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size);
|
|
|
|
|
Specification<Advertisement> AdSpecification = buildSearchSpecification(location);
|
|
|
|
|
Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest);
|
|
|
|
|
|
|
|
|
|
long totalElements = AdPage.getTotalElements();
|
|
|
|
|
List<Advertisement> content = AdPage.getContent();
|
|
|
|
|
if (!CollectionUtils.isEmpty(content)) {
|
|
|
|
|
for (Advertisement item : content) {
|
|
|
|
|
AdvertisementRespDTO response = AdvertisementRespDTO.convertPojo(item);
|
|
|
|
|
result.add(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Result.success(new PageResult<>(totalElements, result));
|
|
|
|
|
List<AdvertisementRespDTO> pageAdFromCache = getPageAdFromCache(page, size, location);
|
|
|
|
|
return Result.success(new PageResult<>(Long.valueOf(pageAdFromCache.size()), pageAdFromCache));
|
|
|
|
|
}
|
|
|
|
|
private Specification<Advertisement> buildSearchSpecification(String location) {
|
|
|
|
|
return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
|
|
|
|
@ -228,13 +222,53 @@ public class CMSAdvertisementService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result visitAdd(String id){
|
|
|
|
|
Advertisement advertisement = adDao.findById(id).get();
|
|
|
|
|
if (!Objects.isNull(advertisement)) {
|
|
|
|
|
advertisement.setVisits(advertisement.getVisits()+1);
|
|
|
|
|
adDao.save(advertisement);
|
|
|
|
|
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
|
|
|
|
|
|
|
|
|
|
if (!Objects.isNull(ad)) {
|
|
|
|
|
ad.setVisits(ad.getVisits()+1);
|
|
|
|
|
adDao.save(ad);
|
|
|
|
|
cleanAdCache(ad.getId());
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
return Result.failed("文章不存在");
|
|
|
|
|
return Result.failed("广告不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<AdvertisementRespDTO> getPageAdFromCache(int page , int size,String location){
|
|
|
|
|
|
|
|
|
|
return (List<AdvertisementRespDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ADVERTISEMENT_PAGE, page+","+size, key -> getPageAd(page,size,location), false).getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<AdvertisementRespDTO> getPageAd(int page , int size,String location){
|
|
|
|
|
|
|
|
|
|
List<AdvertisementRespDTO> result = new ArrayList<>();
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size);
|
|
|
|
|
Specification<Advertisement> AdSpecification = buildSearchSpecification(location);
|
|
|
|
|
Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest);
|
|
|
|
|
|
|
|
|
|
List<Advertisement> content = AdPage.getContent();
|
|
|
|
|
if (!CollectionUtils.isEmpty(content)) {
|
|
|
|
|
for (Advertisement item : content) {
|
|
|
|
|
AdvertisementRespDTO response = AdvertisementRespDTO.convertPojo(item);
|
|
|
|
|
result.add(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getAdFromCache(String id){
|
|
|
|
|
return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ADVERTISEMENT, id, key -> getAdById(id), false).getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getAdById(String id){
|
|
|
|
|
return JSON.toJSONString(adDao.findById(id).get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cleanAdCache(String id){
|
|
|
|
|
cacheChannel.evict(constants.Constants.J2CACHE_REGION_ADVERTISEMENT,id);
|
|
|
|
|
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ADVERTISEMENT_PAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|