| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- 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.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.google.common.collect.Lists;
- import com.jtzx.crm.module.sys.entity.SysCustomerOperLogEntity;
- import com.jtzx.crm.module.sys.entity.SysGroupEntity;
- import com.jtzx.crm.module.sys.mapper.SysCustomerOperLogMapper;
- import com.jtzx.crm.module.sys.mapper.SysGroupMapper;
- import com.jtzx.crm.module.sys.model.request.AdminSysCustomerOperLogInsertOrUpdateDTO;
- import com.jtzx.crm.module.sys.model.request.AdminSysCustomerOperLogListDTO;
- import com.jtzx.crm.module.sys.model.response.BindUserBO;
- import com.jtzx.crm.module.sys.model.response.SelectBO;
- import com.jtzx.crm.module.sys.model.response.SysCustomerOperLogBO;
- import com.jtzx.crm.module.sys.model.response.SysCustomerOperLogDataBO;
- import com.jtzx.crm.module.sys.util.DateSelectUtil;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.*;
- /**
- * 客户操作日志信息数据
- */
- @RequiredArgsConstructor
- @Service
- @Slf4j
- public class SysCustomerOperLogService {
- private final SysCustomerOperLogMapper sysCustomerOperLogMapper;
- private final SysGroupMapper sysGroupMapper;
- /**
- * 操作日志列表查询
- */
- public SysCustomerOperLogDataBO adminList(AdminSysCustomerOperLogListDTO dto, Boolean dateSelectStatus, Boolean groupSelectStatus) {
- Page<SysCustomerOperLogEntity> entityPage = sysCustomerOperLogMapper.selectPage(
- new Page<>(dto.getPageNum(), dto.getPageSize()),
- new QueryWrapper<SysCustomerOperLogEntity>()
- .lambda()
- // 客户表主id
- .eq(Objects.nonNull(dto.getCustomerId()), SysCustomerOperLogEntity::getCustomerId, dto.getCustomerId())
- // 操作人id
- .eq(Objects.nonNull(dto.getOperUserId()), SysCustomerOperLogEntity::getOperUserId, dto.getOperUserId())
- .ge(dto.getStartDate() != null, SysCustomerOperLogEntity::getCreatedAt, dto.getStartDate())
- .le(dto.getEndDate() != null, SysCustomerOperLogEntity::getCreatedAt, dto.getEndDate())
- // 排序
- .orderByDesc(SysCustomerOperLogEntity::getCreatedAt)
- );
- SysCustomerOperLogDataBO resultData = SysCustomerOperLogDataBO.builder().build();
- PageResult<SysCustomerOperLogBO> pageResult = this.entityPage2BOPage(entityPage);
- //数据
- resultData.setData(pageResult);
- // 时间
- resultData.setDateData(Lists.newArrayList());
- if (dateSelectStatus != null && dateSelectStatus) {
- resultData.setDateData(DateSelectUtil.getDateData());
- }
- // 操作人
- List<BindUserBO> memberData = Lists.newArrayList();
- // 小组
- List<SelectBO> groupData = Lists.newArrayList();
- List<SysCustomerOperLogEntity> logEntityList =
- sysCustomerOperLogMapper.selectList(
- new LambdaQueryWrapper<>(SysCustomerOperLogEntity.class)
- .select(
- SysCustomerOperLogEntity::getId,
- SysCustomerOperLogEntity::getCustomerId,
- SysCustomerOperLogEntity::getOperUserId,
- SysCustomerOperLogEntity::getOperName,
- SysCustomerOperLogEntity::getOperUserGroupId
- )
- .eq(SysCustomerOperLogEntity::getCustomerId, dto.getCustomerId())
- .orderByDesc(SysCustomerOperLogEntity::getCreatedAt)
- );
- if (CollUtil.isNotEmpty(logEntityList)) {
- logEntityList.forEach(
- entity -> {
- if (memberData.stream().map(BindUserBO::getId).noneMatch(entity.getOperUserId()::equals)) {
- BindUserBO member = BindUserBO.builder()
- .id(entity.getOperUserId())
- .username(entity.getOperName())
- .groupId(entity.getOperUserGroupId())
- .build();
- memberData.add(member);
- }
- }
- );
- // 小组
- if (groupSelectStatus != null && groupSelectStatus) {
- List<Long> operUserGroupIdList =
- logEntityList.stream()
- .map(SysCustomerOperLogEntity::getOperUserGroupId)
- .distinct().toList();
- List<SysGroupEntity> groupEntityList =
- sysGroupMapper.selectList(
- new LambdaQueryWrapper<>(SysGroupEntity.class)
- .select(
- SysGroupEntity::getId,
- SysGroupEntity::getTitle
- )
- .in(SysGroupEntity::getId, operUserGroupIdList)
- .orderByAsc(SysGroupEntity::getSortCode)
- );
- if (CollUtil.isNotEmpty(groupEntityList)) {
- groupEntityList.forEach(
- entity ->
- groupData.add(SelectBO.builder()
- .id(entity.getId())
- .name(entity.getTitle())
- .build()));
- }
- }
- }
- // 操作人
- resultData.setMemberData(memberData);
- // 小组
- resultData.setGroupData(groupData);
- return resultData;
- }
- /**
- * 根据 ID 取详情
- *
- * @param id 主键ID
- * @return null or BO
- */
- public SysCustomerOperLogBO getOneById(Long id) {
- return this.getOneById(id, false);
- }
- /**
- * 根据 ID 取详情
- *
- * @param id 主键ID
- * @param throwIfInvalidId 是否在 ID 无效时抛出异常
- * @return null or BO
- */
- public SysCustomerOperLogBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
- SysCustomerOperLogEntity entity = sysCustomerOperLogMapper.selectById(id);
- if (throwIfInvalidId && entity == null) {
- throw new BusinessException(400, "无效客户操作日志信息数据ID");
- }
- return this.entity2BO(entity);
- }
- /**
- * 后台管理-新增
- */
- @Transactional(rollbackFor = Exception.class)
- public Long adminInsert(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
- log.info("[后台管理-客户操作日志信息数据-新增] >> DTO={}", dto);
- this.checkExistence(dto);
- dto.setId(null);
- SysCustomerOperLogEntity entity = new SysCustomerOperLogEntity();
- BeanUtil.copyProperties(dto, entity);
- sysCustomerOperLogMapper.insert(entity);
- return entity.getId();
- }
- /**
- * 后台管理-编辑
- */
- @Transactional(rollbackFor = Exception.class)
- public void adminUpdate(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
- log.info("[后台管理-客户操作日志信息数据-编辑] >> DTO={}", dto);
- this.checkExistence(dto);
- SysCustomerOperLogEntity entity = new SysCustomerOperLogEntity();
- BeanUtil.copyProperties(dto, entity);
- sysCustomerOperLogMapper.updateById(entity);
- }
- /**
- * 后台管理-删除
- */
- @Transactional(rollbackFor = Exception.class)
- public void adminDelete(Collection<Long> ids) {
- log.info("[后台管理-客户操作日志信息数据-删除] >> ids={}", ids);
- sysCustomerOperLogMapper.deleteByIds(ids);
- }
- /*
- ----------------------------------------------------------------
- 私有方法 private methods
- ----------------------------------------------------------------
- */
- /**
- * 实体转 BO
- *
- * @param entity 实体
- * @return BO
- */
- private SysCustomerOperLogBO entity2BO(SysCustomerOperLogEntity entity) {
- if (entity == null) {
- return null;
- }
- SysCustomerOperLogBO bo = new SysCustomerOperLogBO();
- BeanUtil.copyProperties(entity, bo);
- // 可以在此处为BO填充字段
- return bo;
- }
- /**
- * 实体 List 转 BO List
- *
- * @param entityList 实体 List
- * @return BO List
- */
- private List<SysCustomerOperLogBO> entityList2BOs(List<SysCustomerOperLogEntity> entityList) {
- if (CollUtil.isEmpty(entityList)) {
- return Collections.emptyList();
- }
- // 深拷贝
- List<SysCustomerOperLogBO> ret = new ArrayList<>(entityList.size());
- entityList.forEach(
- entity -> ret.add(this.entity2BO(entity))
- );
- return ret;
- }
- /**
- * 实体分页转 BO 分页
- *
- * @param entityPage 实体分页
- * @return BO 分页
- */
- private PageResult<SysCustomerOperLogBO> entityPage2BOPage(Page<SysCustomerOperLogEntity> entityPage) {
- return new PageResult<SysCustomerOperLogBO>()
- .setCurrent(entityPage.getCurrent())
- .setSize(entityPage.getSize())
- .setTotal(entityPage.getTotal())
- .setRecords(this.entityList2BOs(entityPage.getRecords()));
- }
- /**
- * 检查是否已存在同名数据
- *
- * @param dto DTO
- */
- private void checkExistence(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
- /*
- 可以根据自己业务需要,解禁这段代码,修改判断条件和文案
- SysCustomerOperLogEntity existingEntity = sysCustomerOperLogMapper.selectOne(
- new QueryWrapper<SysCustomerOperLogEntity>()
- .lambda()
- .select(SysCustomerOperLogEntity::getId)
- .eq(SysCustomerOperLogEntity::getTitle, dto.getTitle())
- .last(HelioConstant.CRUD.SQL_LIMIT_1)
- );
- if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
- throw new BusinessException(400, "已存在相同客户操作日志信息数据,请重新输入");
- }
- */
- }
- }
|