package cn.lili.modules.lmk.service.impl; import cn.lili.common.exception.ServiceException; import cn.lili.common.utils.StringUtils; import cn.lili.modules.lmk.domain.entity.AdImg; import cn.lili.utils.COSUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import cn.lili.modules.lmk.domain.entity.AdCover; import cn.lili.modules.lmk.mapper.AdCoverMapper; import cn.lili.modules.lmk.service.AdCoverService; import cn.lili.base.Result; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import cn.lili.modules.lmk.domain.form.AdCoverForm; import cn.lili.modules.lmk.domain.vo.AdCoverVO; import cn.lili.modules.lmk.domain.query.AdCoverQuery; 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 AdCoverServiceImpl extends ServiceImpl implements AdCoverService { private final AdCoverMapper adCoverMapper; private final COSUtil cosUtil; /** * 添加 * @param form * @return */ @Override public Result add(AdCoverForm form) { AdCover entity = AdCoverForm.getEntityByForm(form, null); List adCovers = baseMapper.selectList(null); if (!adCovers.isEmpty()) { throw new ServiceException("已经有封面存在了"); } baseMapper.insert(entity); return Result.ok("添加成功"); } /** * 修改 * @param form * @return */ @Override public Result update(AdCoverForm form) { AdCover 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 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(AdCoverQuery query) { IPage page = PageUtil.getPage(query, AdCoverVO.class); baseMapper.getPage(page, query); for (AdCoverVO 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) { AdCoverVO vo = baseMapper.getById(id); Assert.notNull(vo, "记录不存在"); return Result.ok().data(vo); } /** * 列表 * @return */ @Override public Result all() { List entities = baseMapper.selectList(null); List vos = entities.stream() .map(entity -> AdCoverVO.getVoByEntity(entity, null)) .collect(Collectors.toList()); return Result.ok().data(vos); } @Override public Result getKitchenCover() { List adCovers = baseMapper.selectList(null); if (adCovers.size()>1) { AdCover adImg = adCovers.get(0); adCovers = new ArrayList<>(); adCovers.add(adImg); } for(AdCover adCover : adCovers) { if (StringUtils.isNotBlank(adCover.getCoverUrl())&&!adCover.getCoverUrl().contains("http")) { adCover.setCoverUrl(cosUtil.getPreviewUrl(adCover.getCoverUrl())); } } return Result.ok().data(adCovers); } }