SysCustomerOperLogService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.jtzx.crm.module.sys.service;
  2. import cc.uncarbon.framework.core.exception.BusinessException;
  3. import cc.uncarbon.framework.core.page.PageParam;
  4. import cc.uncarbon.framework.core.page.PageResult;
  5. import cn.hutool.core.bean.BeanUtil;
  6. import cn.hutool.core.collection.CollUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import com.google.common.collect.Lists;
  11. import com.jtzx.crm.module.sys.entity.SysCustomerOperLogEntity;
  12. import com.jtzx.crm.module.sys.entity.SysGroupEntity;
  13. import com.jtzx.crm.module.sys.mapper.SysCustomerOperLogMapper;
  14. import com.jtzx.crm.module.sys.mapper.SysGroupMapper;
  15. import com.jtzx.crm.module.sys.model.request.AdminSysCustomerOperLogInsertOrUpdateDTO;
  16. import com.jtzx.crm.module.sys.model.request.AdminSysCustomerOperLogListDTO;
  17. import com.jtzx.crm.module.sys.model.response.BindUserBO;
  18. import com.jtzx.crm.module.sys.model.response.SelectBO;
  19. import com.jtzx.crm.module.sys.model.response.SysCustomerOperLogBO;
  20. import com.jtzx.crm.module.sys.model.response.SysCustomerOperLogDataBO;
  21. import com.jtzx.crm.module.sys.util.DateSelectUtil;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.*;
  27. /**
  28. * 客户操作日志信息数据
  29. */
  30. @RequiredArgsConstructor
  31. @Service
  32. @Slf4j
  33. public class SysCustomerOperLogService {
  34. private final SysCustomerOperLogMapper sysCustomerOperLogMapper;
  35. private final SysGroupMapper sysGroupMapper;
  36. /**
  37. * 操作日志列表查询
  38. */
  39. public SysCustomerOperLogDataBO adminList(AdminSysCustomerOperLogListDTO dto, Boolean dateSelectStatus, Boolean groupSelectStatus) {
  40. Page<SysCustomerOperLogEntity> entityPage = sysCustomerOperLogMapper.selectPage(
  41. new Page<>(dto.getPageNum(), dto.getPageSize()),
  42. new QueryWrapper<SysCustomerOperLogEntity>()
  43. .lambda()
  44. // 客户表主id
  45. .eq(Objects.nonNull(dto.getCustomerId()), SysCustomerOperLogEntity::getCustomerId, dto.getCustomerId())
  46. // 操作人id
  47. .eq(Objects.nonNull(dto.getOperUserId()), SysCustomerOperLogEntity::getOperUserId, dto.getOperUserId())
  48. .ge(dto.getStartDate() != null, SysCustomerOperLogEntity::getCreatedAt, dto.getStartDate())
  49. .le(dto.getEndDate() != null, SysCustomerOperLogEntity::getCreatedAt, dto.getEndDate())
  50. // 排序
  51. .orderByDesc(SysCustomerOperLogEntity::getCreatedAt)
  52. );
  53. SysCustomerOperLogDataBO resultData = SysCustomerOperLogDataBO.builder().build();
  54. PageResult<SysCustomerOperLogBO> pageResult = this.entityPage2BOPage(entityPage);
  55. //数据
  56. resultData.setData(pageResult);
  57. // 时间
  58. resultData.setDateData(Lists.newArrayList());
  59. if (dateSelectStatus != null && dateSelectStatus) {
  60. resultData.setDateData(DateSelectUtil.getDateData());
  61. }
  62. // 操作人
  63. List<BindUserBO> memberData = Lists.newArrayList();
  64. // 小组
  65. List<SelectBO> groupData = Lists.newArrayList();
  66. List<SysCustomerOperLogEntity> logEntityList =
  67. sysCustomerOperLogMapper.selectList(
  68. new LambdaQueryWrapper<>(SysCustomerOperLogEntity.class)
  69. .select(
  70. SysCustomerOperLogEntity::getId,
  71. SysCustomerOperLogEntity::getCustomerId,
  72. SysCustomerOperLogEntity::getOperUserId,
  73. SysCustomerOperLogEntity::getOperName,
  74. SysCustomerOperLogEntity::getOperUserGroupId
  75. )
  76. .eq(SysCustomerOperLogEntity::getCustomerId, dto.getCustomerId())
  77. .orderByDesc(SysCustomerOperLogEntity::getCreatedAt)
  78. );
  79. if (CollUtil.isNotEmpty(logEntityList)) {
  80. logEntityList.forEach(
  81. entity -> {
  82. if (memberData.stream().map(BindUserBO::getId).noneMatch(entity.getOperUserId()::equals)) {
  83. BindUserBO member = BindUserBO.builder()
  84. .id(entity.getOperUserId())
  85. .username(entity.getOperName())
  86. .groupId(entity.getOperUserGroupId())
  87. .build();
  88. memberData.add(member);
  89. }
  90. }
  91. );
  92. // 小组
  93. if (groupSelectStatus != null && groupSelectStatus) {
  94. List<Long> operUserGroupIdList =
  95. logEntityList.stream()
  96. .map(SysCustomerOperLogEntity::getOperUserGroupId)
  97. .distinct().toList();
  98. List<SysGroupEntity> groupEntityList =
  99. sysGroupMapper.selectList(
  100. new LambdaQueryWrapper<>(SysGroupEntity.class)
  101. .select(
  102. SysGroupEntity::getId,
  103. SysGroupEntity::getTitle
  104. )
  105. .in(SysGroupEntity::getId, operUserGroupIdList)
  106. .orderByAsc(SysGroupEntity::getSortCode)
  107. );
  108. if (CollUtil.isNotEmpty(groupEntityList)) {
  109. groupEntityList.forEach(
  110. entity ->
  111. groupData.add(SelectBO.builder()
  112. .id(entity.getId())
  113. .name(entity.getTitle())
  114. .build()));
  115. }
  116. }
  117. }
  118. // 操作人
  119. resultData.setMemberData(memberData);
  120. // 小组
  121. resultData.setGroupData(groupData);
  122. return resultData;
  123. }
  124. /**
  125. * 根据 ID 取详情
  126. *
  127. * @param id 主键ID
  128. * @return null or BO
  129. */
  130. public SysCustomerOperLogBO getOneById(Long id) {
  131. return this.getOneById(id, false);
  132. }
  133. /**
  134. * 根据 ID 取详情
  135. *
  136. * @param id 主键ID
  137. * @param throwIfInvalidId 是否在 ID 无效时抛出异常
  138. * @return null or BO
  139. */
  140. public SysCustomerOperLogBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
  141. SysCustomerOperLogEntity entity = sysCustomerOperLogMapper.selectById(id);
  142. if (throwIfInvalidId && entity == null) {
  143. throw new BusinessException(400, "无效客户操作日志信息数据ID");
  144. }
  145. return this.entity2BO(entity);
  146. }
  147. /**
  148. * 后台管理-新增
  149. */
  150. @Transactional(rollbackFor = Exception.class)
  151. public Long adminInsert(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
  152. log.info("[后台管理-客户操作日志信息数据-新增] >> DTO={}", dto);
  153. this.checkExistence(dto);
  154. dto.setId(null);
  155. SysCustomerOperLogEntity entity = new SysCustomerOperLogEntity();
  156. BeanUtil.copyProperties(dto, entity);
  157. sysCustomerOperLogMapper.insert(entity);
  158. return entity.getId();
  159. }
  160. /**
  161. * 后台管理-编辑
  162. */
  163. @Transactional(rollbackFor = Exception.class)
  164. public void adminUpdate(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
  165. log.info("[后台管理-客户操作日志信息数据-编辑] >> DTO={}", dto);
  166. this.checkExistence(dto);
  167. SysCustomerOperLogEntity entity = new SysCustomerOperLogEntity();
  168. BeanUtil.copyProperties(dto, entity);
  169. sysCustomerOperLogMapper.updateById(entity);
  170. }
  171. /**
  172. * 后台管理-删除
  173. */
  174. @Transactional(rollbackFor = Exception.class)
  175. public void adminDelete(Collection<Long> ids) {
  176. log.info("[后台管理-客户操作日志信息数据-删除] >> ids={}", ids);
  177. sysCustomerOperLogMapper.deleteByIds(ids);
  178. }
  179. /*
  180. ----------------------------------------------------------------
  181. 私有方法 private methods
  182. ----------------------------------------------------------------
  183. */
  184. /**
  185. * 实体转 BO
  186. *
  187. * @param entity 实体
  188. * @return BO
  189. */
  190. private SysCustomerOperLogBO entity2BO(SysCustomerOperLogEntity entity) {
  191. if (entity == null) {
  192. return null;
  193. }
  194. SysCustomerOperLogBO bo = new SysCustomerOperLogBO();
  195. BeanUtil.copyProperties(entity, bo);
  196. // 可以在此处为BO填充字段
  197. return bo;
  198. }
  199. /**
  200. * 实体 List 转 BO List
  201. *
  202. * @param entityList 实体 List
  203. * @return BO List
  204. */
  205. private List<SysCustomerOperLogBO> entityList2BOs(List<SysCustomerOperLogEntity> entityList) {
  206. if (CollUtil.isEmpty(entityList)) {
  207. return Collections.emptyList();
  208. }
  209. // 深拷贝
  210. List<SysCustomerOperLogBO> ret = new ArrayList<>(entityList.size());
  211. entityList.forEach(
  212. entity -> ret.add(this.entity2BO(entity))
  213. );
  214. return ret;
  215. }
  216. /**
  217. * 实体分页转 BO 分页
  218. *
  219. * @param entityPage 实体分页
  220. * @return BO 分页
  221. */
  222. private PageResult<SysCustomerOperLogBO> entityPage2BOPage(Page<SysCustomerOperLogEntity> entityPage) {
  223. return new PageResult<SysCustomerOperLogBO>()
  224. .setCurrent(entityPage.getCurrent())
  225. .setSize(entityPage.getSize())
  226. .setTotal(entityPage.getTotal())
  227. .setRecords(this.entityList2BOs(entityPage.getRecords()));
  228. }
  229. /**
  230. * 检查是否已存在同名数据
  231. *
  232. * @param dto DTO
  233. */
  234. private void checkExistence(AdminSysCustomerOperLogInsertOrUpdateDTO dto) {
  235. /*
  236. 可以根据自己业务需要,解禁这段代码,修改判断条件和文案
  237. SysCustomerOperLogEntity existingEntity = sysCustomerOperLogMapper.selectOne(
  238. new QueryWrapper<SysCustomerOperLogEntity>()
  239. .lambda()
  240. .select(SysCustomerOperLogEntity::getId)
  241. .eq(SysCustomerOperLogEntity::getTitle, dto.getTitle())
  242. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  243. );
  244. if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
  245. throw new BusinessException(400, "已存在相同客户操作日志信息数据,请重新输入");
  246. }
  247. */
  248. }
  249. }