New file |
| | |
| | | package cn.lili.modules.lmk.service.impl; |
| | | |
| | | import cn.lili.common.security.context.UserContext; |
| | | import cn.lili.modules.lmk.domain.entity.ShareClickRecord; |
| | | import cn.lili.modules.lmk.domain.form.ShareClickRecordForm; |
| | | import cn.lili.modules.lmk.service.ShareClickRecordService; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import cn.lili.modules.lmk.domain.entity.Share; |
| | | import cn.lili.modules.lmk.mapper.ShareMapper; |
| | | import cn.lili.modules.lmk.service.ShareService; |
| | | import cn.lili.base.Result; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import cn.lili.modules.lmk.domain.form.ShareForm; |
| | | import cn.lili.modules.lmk.domain.vo.ShareVO; |
| | | import cn.lili.modules.lmk.domain.query.ShareQuery; |
| | | 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.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 分享记录 服务实现类 |
| | | * |
| | | * @author xp |
| | | * @since 2025-06-16 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class ShareServiceImpl extends ServiceImpl<ShareMapper, Share> implements ShareService { |
| | | |
| | | private final ShareMapper shareMapper; |
| | | private final ShareClickRecordService shareClickRecordService; |
| | | |
| | | /** |
| | | * 添加 |
| | | * @param form |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result add(ShareForm form) { |
| | | // 没分享过才新增 |
| | | boolean exists = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .eq(Share::getShareType, form.getShareType()) |
| | | .eq(Share::getRefId, form.getRefId()) |
| | | .eq(Share::getShareUser, form.getShareUser()) |
| | | .exists(); |
| | | if (! exists) { |
| | | Share entity = ShareForm.getEntityByForm(form, null); |
| | | entity.setShareTime(new Date()); |
| | | baseMapper.insert(entity); |
| | | } |
| | | return Result.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 修改 |
| | | * @param form |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result update(ShareForm form) { |
| | | Share 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(ShareQuery query) { |
| | | IPage<ShareVO> page = PageUtil.getPage(query, ShareVO.class); |
| | | baseMapper.getPage(page, query); |
| | | return Result.ok().data(page.getRecords()).total(page.getTotal()); |
| | | } |
| | | |
| | | /** |
| | | * 根据id查找 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result detail(String id) { |
| | | ShareVO vo = baseMapper.getById(id); |
| | | Assert.notNull(vo, "记录不存在"); |
| | | return Result.ok().data(vo); |
| | | } |
| | | |
| | | /** |
| | | * 列表 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result all() { |
| | | List<Share> entities = baseMapper.selectList(null); |
| | | List<ShareVO> vos = entities.stream() |
| | | .map(entity -> ShareVO.getVoByEntity(entity, null)) |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos); |
| | | } |
| | | |
| | | @Override |
| | | public Result addShareClick(ShareClickRecordForm form) { |
| | | // 查出分享记录 |
| | | Share share = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .eq(Share::getShareUser, form.getShareUserId()) |
| | | .eq(Share::getRefId, form.getRefId()) |
| | | .one(); |
| | | if (Objects.isNull(share)) { |
| | | return Result.ok(); |
| | | } |
| | | // 没有访问过才添加 |
| | | boolean exists = new LambdaQueryChainWrapper<>(shareClickRecordService.getBaseMapper()) |
| | | .eq(ShareClickRecord::getUserId, UserContext.getCurrentUserId()) |
| | | .eq(ShareClickRecord::getShareId, share.getId()) |
| | | .exists(); |
| | | if (! exists) { |
| | | ShareClickRecord record = new ShareClickRecord(); |
| | | record.setShareId(share.getId()); |
| | | record.setUserId(UserContext.getCurrentUserId()); |
| | | record.setClickTime(new Date()); |
| | | shareClickRecordService.save(record); |
| | | } |
| | | return Result.ok(); |
| | | } |
| | | } |