package com.ycl.platform.service.impl; import com.ycl.platform.domain.entity.CalculateRecord; import com.ycl.platform.mapper.CalculateRecordMapper; import com.ycl.platform.service.CalculateRecordService; import com.ycl.system.Result; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.platform.domain.form.CalculateRecordForm; import com.ycl.platform.domain.vo.CalculateRecordVO; import com.ycl.platform.domain.query.CalculateRecordQuery; import java.util.List; import org.apache.commons.lang3.StringUtils; import com.baomidou.mybatisplus.core.metadata.IPage; import com.ycl.system.page.PageUtil; import org.springframework.stereotype.Service; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import java.util.ArrayList; import java.util.stream.Collectors; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import lombok.RequiredArgsConstructor; /** * 核算记录 服务实现类 * * @author xp * @since 2024-04-23 */ @Service @RequiredArgsConstructor public class CalculateRecordServiceImpl extends ServiceImpl implements CalculateRecordService { private final CalculateRecordMapper calculateRecordMapper; /** * 添加 * @param form * @return */ @Override public Result add(CalculateRecordForm form) { CalculateRecord entity = CalculateRecordForm.getEntityByForm(form, null); if(baseMapper.insert(entity) > 0) { return Result.ok("添加成功"); } return Result.error("添加失败"); } /** * 修改 * @param form * @return */ @Override public Result update(CalculateRecordForm form) { CalculateRecord entity = baseMapper.selectById(form.getId()); // 为空抛IllegalArgumentException,做全局异常处理 Assert.notNull(entity, "记录不存在"); BeanUtils.copyProperties(form, entity); if (baseMapper.updateById(entity) > 0) { return Result.ok("修改成功"); } return Result.error("修改失败"); } /** * 批量删除 * @param ids * @return */ @Override public Result remove(List ids) { if(baseMapper.deleteBatchIds(ids) > 0) { return Result.ok("删除成功"); } return Result.error("删除失败"); } /** * id删除 * @param id * @return */ @Override public Result removeById(String id) { if(baseMapper.deleteById(id) > 0) { return Result.ok("删除成功"); } return Result.error("删除失败"); } /** * 分页查询 * @param query * @return */ @Override public Result page(CalculateRecordQuery query) { IPage page = new LambdaQueryChainWrapper<>(baseMapper) .orderByDesc(CalculateRecord::getCreateTime) .page(PageUtil.getPage(query, CalculateRecord.class)); List vos = page.getRecords().stream() .map( entity -> CalculateRecordVO.getVoByEntity(entity, null) ) .collect(Collectors.toList()); return Result.ok().data(vos).total(page.getTotal()); } /** * 根据id查找 * @param id * @return */ @Override public Result detail(String id) { CalculateRecord entity = baseMapper.selectById(id); Assert.notNull(entity, "记录不存在"); CalculateRecordVO vo = CalculateRecordVO.getVoByEntity(entity, null); return Result.ok().data(vo); } /** * 列表 * @return */ @Override public Result all() { List entities = baseMapper.selectList(null); List vos = entities.stream() .map( entity -> CalculateRecordVO.getVoByEntity(entity, null) ) .collect(Collectors.toList()); return Result.ok().data(vos); } }