package cn.lili.modules.lmk.service.impl; import cn.lili.common.utils.StringUtils; import cn.lili.modules.lmk.domain.vo.ActivityRefPrizeInfoVO; import cn.lili.modules.lmk.enums.general.PrizeActivityStatusEnum; import cn.lili.modules.lmk.service.ActivityRefPrizeService; import cn.lili.utils.COSUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import cn.lili.modules.lmk.domain.entity.PrizeActivity; import cn.lili.modules.lmk.mapper.PrizeActivityMapper; import cn.lili.modules.lmk.service.PrizeActivityService; import cn.lili.base.Result; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import cn.lili.modules.lmk.domain.form.PrizeActivityForm; import cn.lili.modules.lmk.domain.vo.PrizeActivityVO; import cn.lili.modules.lmk.domain.query.PrizeActivityQuery; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; import cn.lili.utils.PageUtil; import org.springframework.beans.BeanUtils; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * 抽奖活动 服务实现类 * * @author peng * @since 2025-08-14 */ @Service @RequiredArgsConstructor public class PrizeActivityServiceImpl extends ServiceImpl implements PrizeActivityService { private final ActivityRefPrizeService activityRefPrizeService; private final PrizeActivityMapper prizeActivityMapper; private final COSUtil cOSUtil; @Override public Result getPopup() { //获得弹窗活动 PrizeActivity prizeActivity = new LambdaQueryChainWrapper<>(baseMapper) .eq(PrizeActivity::getDeleteFlag,Boolean.FALSE) .eq(PrizeActivity::getPopup,Boolean.TRUE) .one(); PrizeActivityVO prizeActivityVO = new PrizeActivityVO(); PrizeActivityVO vo = PrizeActivityVO.getVoByEntity(prizeActivity, prizeActivityVO); if (StringUtils.isNotBlank(vo.getActivityCover())){ vo.setActivityCoverUrl(cOSUtil.getPreviewUrl(vo.getActivityCover())); } return Result.ok().data(prizeActivityVO); } @Override public Result popup(String id){ //设置id的活动为弹窗活动,将其他活动的弹窗状态改为false. PrizeActivity prizeActivity = baseMapper.selectById(id); if (prizeActivity.getPopup()){ prizeActivity.setPopup(Boolean.FALSE); prizeActivityMapper.updateById(prizeActivity); return Result.ok("取消弹窗活动成功!"); }else { prizeActivity.setPopup(Boolean.TRUE); //修改其他活动的推荐状态后,修改该对象的修改状态 prizeActivityMapper.updateById(prizeActivity); new LambdaUpdateChainWrapper<>(baseMapper).eq(PrizeActivity::getPopup,Boolean.TRUE) .eq(PrizeActivity::getDeleteFlag,Boolean.FALSE) .set(PrizeActivity::getPopup, Boolean.FALSE) .ne(PrizeActivity::getId, prizeActivity.getId()) .set(PrizeActivity::getUpdateTime, new Date()) .update(); return Result.ok("弹窗活动开启成功"); } } @Override public Result publishPrizeActivity(String id) { PrizeActivity prizeActivity = baseMapper.selectById(id); //判断活动是否存在奖品 存在允许开启 Object data = activityRefPrizeService.getActivityRefPrizeByActivityId(id).get("data"); if (data instanceof List) { List rawList = (List) data; List list = new ArrayList<>(); for (Object item : rawList) { if (item instanceof ActivityRefPrizeInfoVO) { list.add((ActivityRefPrizeInfoVO) item); } } if (CollectionUtils.isEmpty(list)) { return Result.error("开启抽奖活动前请设置奖品"); } // 使用 list } if (PrizeActivityStatusEnum.OFF.name().equals(prizeActivity.getEnableStatus())) { prizeActivity.setEnableStatus(PrizeActivityStatusEnum.ON.name()); baseMapper.updateById(prizeActivity); return Result.ok("启动成功"); }else { prizeActivity.setEnableStatus(PrizeActivityStatusEnum.OFF.name()); baseMapper.updateById(prizeActivity); return Result.ok("关闭成功"); } } /** * 添加 * @param form * @return */ @Override public Result add(PrizeActivityForm form) { PrizeActivity entity = PrizeActivityForm.getEntityByForm(form, null); entity.setEnableStatus(PrizeActivityStatusEnum.OFF.name()); entity.setPopup(Boolean.FALSE); baseMapper.insert(entity); return Result.ok("添加成功"); } /** * 修改 * @param form * @return */ @Override public Result update(PrizeActivityForm form) { PrizeActivity entity = baseMapper.selectById(form.getId()); // 为空抛IllegalArgumentException,做全局异常处理 Assert.notNull(entity, "记录不存在"); BeanUtils.copyProperties(form, entity); entity.setEnableStatus(PrizeActivityStatusEnum.OFF.name()); 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(PrizeActivityQuery query) { IPage page = PageUtil.getPage(query, PrizeActivityVO.class); baseMapper.getPage(page, query); // for (PrizeActivityVO vo : page.getRecords()) { Object data = activityRefPrizeService.getActivityRefPrizeByActivityId(vo.getId()).get("data"); if (data instanceof List) { List rawList = (List) data; List list = new ArrayList<>(); for (Object item : rawList) { if (item instanceof ActivityRefPrizeInfoVO) { list.add((ActivityRefPrizeInfoVO) item); } } vo.setCanSet(CollectionUtils.isEmpty(list)); // 使用 list } if (StringUtils.isNotBlank(vo.getActivityCover())){ vo.setActivityCoverUrl(cOSUtil.getPreviewUrl(vo.getActivityCover())); } if (StringUtils.isNotBlank(vo.getActivityImg())){ vo.setActivityImgUrl(cOSUtil.getPreviewUrl(vo.getActivityImg())); } } return Result.ok().data(page.getRecords()).total(page.getTotal()); } /** * 根据id查找 * @param id * @return */ @Override public Result detail(String id) { PrizeActivityVO 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 -> PrizeActivityVO.getVoByEntity(entity, null)) .collect(Collectors.toList()); return Result.ok().data(vos); } }