package com.ycl.platform.service.impl; import com.ycl.platform.domain.entity.CalculateMoneyRule; import com.ycl.platform.mapper.CalculateMoneyRuleMapper; import com.ycl.platform.service.CalculateMoneyRuleService; import com.ycl.system.Result; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.platform.domain.form.CalculateMoneyRuleForm; import com.ycl.platform.domain.vo.CalculateMoneyRuleVO; import com.ycl.platform.domain.query.CalculateMoneyRuleQuery; 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-26 */ @Service @RequiredArgsConstructor public class CalculateMoneyRuleServiceImpl extends ServiceImpl implements CalculateMoneyRuleService { private final CalculateMoneyRuleMapper calculateMoneyRuleMapper; /** * 添加 * @param form * @return */ @Override public Result add(CalculateMoneyRuleForm form) { CalculateMoneyRule entity = CalculateMoneyRuleForm.getEntityByForm(form, null); if(baseMapper.insert(entity) > 0) { return Result.ok("添加成功"); } return Result.error("添加失败"); } /** * 修改 * @param form * @return */ @Override public Result update(CalculateMoneyRuleForm form) { CalculateMoneyRule 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(CalculateMoneyRuleQuery query) { IPage page = new LambdaQueryChainWrapper<>(baseMapper) .orderByDesc(CalculateMoneyRule::getCreateTime) .page(PageUtil.getPage(query, CalculateMoneyRule.class)); List vos = page.getRecords().stream() .map( entity -> CalculateMoneyRuleVO.getVoByEntity(entity, null) ) .collect(Collectors.toList()); return Result.ok().data(vos).total(page.getTotal()); } /** * 根据id查找 * @param id * @return */ @Override public Result detail(String id) { CalculateMoneyRule entity = baseMapper.selectById(id); Assert.notNull(entity, "记录不存在"); CalculateMoneyRuleVO vo = CalculateMoneyRuleVO.getVoByEntity(entity, null); return Result.ok().data(vo); } /** * 列表 * @return */ @Override public Result all() { List entities = baseMapper.selectList(null); List vos = entities.stream() .map( entity -> CalculateMoneyRuleVO.getVoByEntity(entity, null) ) .collect(Collectors.toList()); return Result.ok().data(vos); } @Override public List selectMoneyRules(Integer contractId) { return new LambdaQueryChainWrapper<>(baseMapper) .eq(CalculateMoneyRule::getContractId, contractId) .list(); } }