| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package com.jtzx.crm.module.sys.service;
- import cc.uncarbon.framework.core.exception.BusinessException;
- import cc.uncarbon.framework.core.page.PageParam;
- import cc.uncarbon.framework.core.page.PageResult;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.collection.CollUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.jtzx.crm.module.sys.entity.SysGroupEntity;
- import com.jtzx.crm.module.sys.mapper.SysGroupMapper;
- import com.jtzx.crm.module.sys.model.request.AdminSysGroupInsertOrUpdateDTO;
- import com.jtzx.crm.module.sys.model.response.SysGroupBO;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.List;
- /**
- * 后台分组数据
- */
- @RequiredArgsConstructor
- @Service
- @Slf4j
- public class SysGroupService {
- private final SysGroupMapper sysGroupMapper;
- /**
- * 后台管理-分页列表
- */
- public PageResult<SysGroupBO> adminList(PageParam pageParam) {
- Page<SysGroupEntity> entityPage = sysGroupMapper.selectPage(
- new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
- new QueryWrapper<SysGroupEntity>()
- .lambda()
- // 排序
- .orderByDesc(SysGroupEntity::getCreatedAt)
- );
- return this.entityPage2BOPage(entityPage);
- }
- /**
- * 根据 ID 取详情
- *
- * @param id 主键ID
- * @return null or BO
- */
- public SysGroupBO getOneById(Long id) {
- return this.getOneById(id, false);
- }
- /**
- * 根据 ID 取详情
- *
- * @param id 主键ID
- * @param throwIfInvalidId 是否在 ID 无效时抛出异常
- * @return null or BO
- */
- public SysGroupBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
- SysGroupEntity entity = sysGroupMapper.selectById(id);
- if (throwIfInvalidId && entity == null) {
- throw new BusinessException(400, "无效后台分组数据ID");
- }
- return this.entity2BO(entity);
- }
- /**
- * 后台管理-新增
- */
- @Transactional(rollbackFor = Exception.class)
- public Long adminInsert(AdminSysGroupInsertOrUpdateDTO dto) {
- log.info("[后台管理-后台分组数据-新增] >> DTO={}", dto);
- this.checkExistence(dto);
- dto.setId(null);
- SysGroupEntity entity = new SysGroupEntity();
- BeanUtil.copyProperties(dto, entity);
- sysGroupMapper.insert(entity);
- return entity.getId();
- }
- /**
- * 后台管理-编辑
- */
- @Transactional(rollbackFor = Exception.class)
- public void adminUpdate(AdminSysGroupInsertOrUpdateDTO dto) {
- log.info("[后台管理-后台分组数据-编辑] >> DTO={}", dto);
- this.checkExistence(dto);
- SysGroupEntity entity = new SysGroupEntity();
- BeanUtil.copyProperties(dto, entity);
- sysGroupMapper.updateById(entity);
- }
- /**
- * 后台管理-删除
- */
- @Transactional(rollbackFor = Exception.class)
- public void adminDelete(Collection<Long> ids) {
- log.info("[后台管理-后台分组数据-删除] >> ids={}", ids);
- sysGroupMapper.deleteByIds(ids);
- }
- /*
- ----------------------------------------------------------------
- 私有方法 private methods
- ----------------------------------------------------------------
- */
- /**
- * 实体转 BO
- *
- * @param entity 实体
- * @return BO
- */
- private SysGroupBO entity2BO(SysGroupEntity entity) {
- if (entity == null) {
- return null;
- }
- SysGroupBO bo = new SysGroupBO();
- BeanUtil.copyProperties(entity, bo);
- // 可以在此处为BO填充字段
- return bo;
- }
- /**
- * 实体 List 转 BO List
- *
- * @param entityList 实体 List
- * @return BO List
- */
- private List<SysGroupBO> entityList2BOs(List<SysGroupEntity> entityList) {
- if (CollUtil.isEmpty(entityList)) {
- return Collections.emptyList();
- }
- // 深拷贝
- List<SysGroupBO> ret = new ArrayList<>(entityList.size());
- entityList.forEach(
- entity -> ret.add(this.entity2BO(entity))
- );
- return ret;
- }
- /**
- * 实体分页转 BO 分页
- *
- * @param entityPage 实体分页
- * @return BO 分页
- */
- private PageResult<SysGroupBO> entityPage2BOPage(Page<SysGroupEntity> entityPage) {
- return new PageResult<SysGroupBO>()
- .setCurrent(entityPage.getCurrent())
- .setSize(entityPage.getSize())
- .setTotal(entityPage.getTotal())
- .setRecords(this.entityList2BOs(entityPage.getRecords()));
- }
- /**
- * 检查是否已存在同名数据
- *
- * @param dto DTO
- */
- private void checkExistence(AdminSysGroupInsertOrUpdateDTO dto) {
- /*
- 可以根据自己业务需要,解禁这段代码,修改判断条件和文案
- SysGroupEntity existingEntity = sysGroupMapper.selectOne(
- new QueryWrapper<SysGroupEntity>()
- .lambda()
- .select(SysGroupEntity::getId)
- .eq(SysGroupEntity::getTitle, dto.getTitle())
- .last(HelioConstant.CRUD.SQL_LIMIT_1)
- );
- if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
- throw new BusinessException(400, "已存在相同后台分组数据,请重新输入");
- }
- */
- }
- }
|