package com.sandu.ximon.admin.service;
|
|
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.lang.Snowflake;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.sandu.common.execption.BusinessException;
|
import com.sandu.common.service.impl.BaseServiceImpl;
|
import com.sandu.common.util.ResUtils;
|
import com.sandu.ximon.admin.dto.BannerDto;
|
import com.sandu.ximon.admin.dto.BannerPositionDto;
|
import com.sandu.ximon.admin.param.BannerParam;
|
import com.sandu.ximon.admin.security.SecurityUtils;
|
import com.sandu.ximon.dao.domain.Banner;
|
import com.sandu.ximon.dao.mapper.BannerMapper;
|
import lombok.AllArgsConstructor;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
|
@Service
|
@AllArgsConstructor
|
public class BannerService extends BaseServiceImpl<BannerMapper, Banner> {
|
|
private final Snowflake snowflake;
|
|
/**
|
* 获取banner跳转列表
|
*/
|
public List<BannerPositionDto> listPosition() {
|
return ListUtil.of(new BannerPositionDto("首页1", 0), new BannerPositionDto("首页2", 1),
|
new BannerPositionDto("首页3", 2), new BannerPositionDto("首页4", 3));
|
}
|
|
/**
|
* 添加banner
|
*/
|
public boolean addBanner(BannerParam bannerParam) {
|
Banner banner = new Banner();
|
banner.setId(snowflake.nextId());
|
banner.setBannerUrl(ResUtils.removePrefix(bannerParam.getUrl()));
|
banner.setAuthor(SecurityUtils.getUsername());
|
banner.setPosition(bannerParam.getPosition());
|
return save(banner);
|
}
|
|
/**
|
* 修改banner
|
*
|
* @param bannerId
|
* @param bannerParam
|
* @return
|
*/
|
public boolean updateBanner(Long bannerId, BannerParam bannerParam) {
|
Banner banner = getById(bannerId);
|
if (banner == null) {
|
throw new BusinessException("找不到banner");
|
}
|
Banner update = new Banner();
|
// BeanUtils.copyProperties(bannerParam, update);
|
update.setId(bannerId);
|
update.setPosition(bannerParam.getPosition());
|
update.setBannerUrl(bannerParam.getUrl());
|
update.setId(bannerId);
|
return updateById(update);
|
}
|
|
/**
|
* 删除banner
|
* @param bannerId
|
* @return
|
*/
|
public boolean deleteBanner(Long bannerId) {
|
Banner banner = getById(bannerId);
|
if (banner == null) {
|
throw new BusinessException("找不到banner");
|
}
|
return removeById(bannerId);
|
}
|
|
/**
|
* 查询banner
|
* @return
|
*/
|
public List<Banner> listBanner() {
|
LambdaQueryWrapper<Banner> wrapper = Wrappers.lambdaQuery(Banner.class);
|
List<Banner> list = list(wrapper);
|
for (Banner banner : list) {
|
banner.setBannerUrl(ResUtils.addPrefix(banner.getBannerUrl()));
|
}
|
return list;
|
}
|
|
/**
|
* 获取详情
|
*
|
* @param bannerId
|
* @return
|
*/
|
public BannerDto detailBanner(Long bannerId) {
|
Banner banner = getById(bannerId);
|
if (banner == null) {
|
throw new BusinessException("找不到banner");
|
}
|
BannerDto bannerDto = new BannerDto();
|
BeanUtils.copyProperties(banner, bannerDto);
|
bannerDto.setAUrl(ResUtils.addPrefix(banner.getBannerUrl()));
|
return bannerDto;
|
}
|
}
|