package com.sandu.ximon.admin.service; import cn.hutool.core.lang.Snowflake; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.param.BannerParam; import com.sandu.ximon.admin.security.SecurityUtils; import com.sandu.ximon.dao.domain.Admin; import com.sandu.ximon.dao.domain.Banner; import com.sandu.ximon.dao.domain.Client; 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 { private final Snowflake snowflake; // /** // * 获取banner跳转列表 // */ // public List 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, Client client) { Banner banner = new Banner(); banner.setId(snowflake.nextId()); banner.setBannerUrl(ResUtils.removePrefix(bannerParam.getUrl())); banner.setBindingAuthor(bannerParam.getBinding_author()); if (client != null && client.getSuperiorId() != null) { banner.setClientId(client.getSuperiorId()); } 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.setBannerUrl(bannerParam.getUrl()); 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 listBanner() { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(Banner.class).eq(Banner::getBindingAuthor, SecurityUtils.getUserId()) .or(wrapper1 -> { wrapper1.eq(Banner::getClientId, SecurityUtils.getUserId()); }); List 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; } }