package cn.lili.modules.lmk.service.impl;
|
|
import cn.lili.common.exception.ServiceException;
|
import cn.lili.common.utils.StringUtils;
|
import cn.lili.utils.COSUtil;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import cn.lili.modules.lmk.domain.entity.AdImg;
|
import cn.lili.modules.lmk.mapper.AdImgMapper;
|
import cn.lili.modules.lmk.service.AdImgService;
|
import cn.lili.base.Result;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import cn.lili.modules.lmk.domain.form.AdImgForm;
|
import cn.lili.modules.lmk.domain.vo.AdImgVO;
|
import cn.lili.modules.lmk.domain.query.AdImgQuery;
|
import org.abego.treelayout.internal.util.java.lang.string.StringUtil;
|
import org.springframework.stereotype.Service;
|
import lombok.RequiredArgsConstructor;
|
import cn.lili.utils.PageUtil;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.util.Assert;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* 私厨定制广告图片 服务实现类
|
*
|
* @author peng
|
* @since 2025-12-24
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class AdImgServiceImpl extends ServiceImpl<AdImgMapper, AdImg> implements AdImgService {
|
|
private final AdImgMapper adImgMapper;
|
private final COSUtil cosUtil;
|
|
/**
|
* 添加
|
* @param form
|
* @return
|
*/
|
@Override
|
public Result add(AdImgForm form) {
|
AdImg entity = AdImgForm.getEntityByForm(form, null);
|
List<AdImg> adImgs = baseMapper.selectList(null);
|
if (!adImgs.isEmpty()) {
|
throw new ServiceException("当前封面有图片了");
|
}
|
baseMapper.insert(entity);
|
return Result.ok("添加成功");
|
}
|
|
/**
|
* 修改
|
* @param form
|
* @return
|
*/
|
@Override
|
public Result update(AdImgForm form) {
|
AdImg entity = baseMapper.selectById(form.getId());
|
|
// 为空抛IllegalArgumentException,做全局异常处理
|
Assert.notNull(entity, "记录不存在");
|
BeanUtils.copyProperties(form, entity);
|
baseMapper.updateById(entity);
|
return Result.ok("修改成功");
|
}
|
|
/**
|
* 批量删除
|
* @param ids
|
* @return
|
*/
|
@Override
|
public Result remove(List<String> ids) {
|
baseMapper.deleteBatchIds(ids);
|
return Result.ok("删除成功");
|
}
|
|
/**
|
* id删除
|
* @param id
|
* @return
|
*/
|
@Override
|
public Result removeById(String id) {
|
baseMapper.deleteById(id);
|
return Result.ok("删除成功");
|
}
|
|
/**
|
* 分页查询
|
* @param query
|
* @return
|
*/
|
@Override
|
public Result page(AdImgQuery query) {
|
IPage<AdImgVO> page = PageUtil.getPage(query, AdImgVO.class);
|
baseMapper.getPage(page, query);
|
for (AdImgVO record : page.getRecords()) {
|
if (StringUtils.isNotBlank(record.getCoverUrl())&&!record.getCoverUrl().contains("http")) {
|
record.setCoverUrl(cosUtil.getPreviewUrl(record.getCoverUrl()));
|
}
|
}
|
return Result.ok().data(page.getRecords()).total(page.getTotal());
|
}
|
|
/**
|
* 根据id查找
|
* @param id
|
* @return
|
*/
|
@Override
|
public Result detail(String id) {
|
AdImgVO vo = baseMapper.getById(id);
|
Assert.notNull(vo, "记录不存在");
|
return Result.ok().data(vo);
|
}
|
|
/**
|
* 列表
|
* @return
|
*/
|
@Override
|
public Result all() {
|
List<AdImg> entities = baseMapper.selectList(null);
|
List<AdImgVO> vos = entities.stream()
|
.map(entity -> AdImgVO.getVoByEntity(entity, null))
|
.collect(Collectors.toList());
|
return Result.ok().data(vos);
|
}
|
|
@Override
|
public Result getKitchenBanner() {
|
List<AdImg> adImgs = baseMapper.selectList(null);
|
if (!adImgs.isEmpty()) {
|
AdImg adImg = adImgs.get(0);
|
adImgs = new ArrayList<>();
|
if (StringUtils.isNotBlank(adImg.getCoverUrl())&&!adImg.getCoverUrl().contains("http")) {
|
adImg.setCoverUrl(cosUtil.getPreviewUrl(adImg.getCoverUrl()));
|
}
|
adImgs.add(adImg);
|
}
|
return Result.ok().data(adImgs);
|
}
|
}
|