SysCustomerDataPublicService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.jtzx.crm.module.sys.service;
  2. import cc.uncarbon.framework.core.context.UserContextHolder;
  3. import cc.uncarbon.framework.core.exception.BusinessException;
  4. import cc.uncarbon.framework.core.page.PageResult;
  5. import cn.hutool.core.bean.BeanUtil;
  6. import cn.hutool.core.collection.CollUtil;
  7. import cn.hutool.core.date.DateUtil;
  8. import cn.hutool.core.text.CharSequenceUtil;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import com.google.common.collect.Lists;
  12. import com.jtzx.crm.module.sys.entity.SysCustomerDataEntity;
  13. import com.jtzx.crm.module.sys.entity.SysParamBaseDataEntity;
  14. import com.jtzx.crm.module.sys.entity.SysUserEntity;
  15. import com.jtzx.crm.module.sys.mapper.SysCustomerDataMapper;
  16. import com.jtzx.crm.module.sys.mapper.SysParamBaseDataMapper;
  17. import com.jtzx.crm.module.sys.mapper.SysUserMapper;
  18. import com.jtzx.crm.module.sys.model.request.*;
  19. import com.jtzx.crm.module.sys.model.response.*;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import java.time.format.DateTimeFormatter;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Objects;
  29. /**
  30. * 公海客户(学校)数据
  31. */
  32. @RequiredArgsConstructor
  33. @Service
  34. @Slf4j
  35. public class SysCustomerDataPublicService {
  36. private final SysCustomerDataMapper sysCustomerDataMapper;
  37. private final SysParamBaseDataMapper sysParamBaseDataMapper;
  38. private final SysCustomerUserCommonService sysCustomerUserCommonService;
  39. private final SysCustomerDataService sysCustomerDataService;
  40. private final SysCustomerPersonService sysCustomerPersonService;
  41. private final SysCustomerDataFollowService sysCustomerDataFollowService;
  42. private final SysCustomerFollowService sysCustomerFollowService;
  43. private final SysCustomerOperLogService sysCustomerOperLogService;
  44. private final SysCustomerDataPermissionService permissionService;
  45. private final SysUserMapper sysUserMapper;
  46. /**
  47. * 查询公海客户页面顶部筛选条件数据
  48. *
  49. * @return
  50. */
  51. public SysCustomerPublicSelectBO queryHeadSelectData() {
  52. permissionService.publicPrivateCustomerPermission();
  53. //返回数据对象
  54. SysCustomerPublicSelectBO resultData = SysCustomerPublicSelectBO.builder().build();
  55. //学段信息
  56. resultData.setStageData(sysCustomerUserCommonService.handleStageData());
  57. //省市信息
  58. SysCustomerSelectBO sysCustomerSelectBO = sysCustomerUserCommonService.querySelectDataByType1(false);
  59. if (sysCustomerSelectBO != null) {
  60. resultData.setRegionData(sysCustomerSelectBO.getRegionData());
  61. }
  62. return resultData;
  63. }
  64. /**
  65. * 后台管理-公海客户(学校)分页列表
  66. */
  67. public PageResult<SysCustomerDataPublicBO> adminList(AdminSysCustomerDataPublicListDTO dto) {
  68. permissionService.publicPrivateCustomerPermission();
  69. Page<SysCustomerDataEntity> entityPage = sysCustomerDataMapper.selectPage(
  70. new Page<>(dto.getPageNum(), dto.getPageSize()),
  71. new QueryWrapper<SysCustomerDataEntity>()
  72. .lambda()
  73. // 名称
  74. .like(CharSequenceUtil.isNotBlank(dto.getQueryStr()), SysCustomerDataEntity::getName, CharSequenceUtil.cleanBlank(dto.getQueryStr()))
  75. // 学段id(param_base_data表主id)
  76. .eq(Objects.nonNull(dto.getStageId()), SysCustomerDataEntity::getStageId, dto.getStageId())
  77. // 省份id(省份表主id)
  78. .eq(Objects.nonNull(dto.getProvinceId()), SysCustomerDataEntity::getProvinceId, dto.getProvinceId())
  79. // 城市id(不为空时)
  80. .eq(Objects.nonNull(dto.getCityId()), SysCustomerDataEntity::getCityId, dto.getCityId())
  81. // 城市id(为空时)
  82. .in(Objects.isNull(dto.getCityId()) && dto.getProvinceId() != null, SysCustomerDataEntity::getCityId, sysCustomerUserCommonService.queryUserCityIds(dto.getProvinceId()))
  83. //认领状态
  84. .eq(SysCustomerDataEntity::getClaimStatus, 0)
  85. // 排序
  86. .orderByDesc(SysCustomerDataEntity::getCreatedAt)
  87. );
  88. return this.entityPage2BOPage(entityPage);
  89. }
  90. /**
  91. * 新增公海客户
  92. *
  93. * @param dto
  94. */
  95. @Transactional(rollbackFor = Exception.class)
  96. public void adminInsertCustomerData(AdminSysCustomerDataInsertOrUpdateDTO dto) {
  97. permissionService.publicPrivateCustomerPermission();
  98. sysCustomerDataService.adminInsert(dto);
  99. }
  100. /**
  101. * 公海客户修改
  102. *
  103. * @param dto
  104. */
  105. @Transactional(rollbackFor = Exception.class)
  106. public void adminUpdateCustomerData(AdminSysCustomerDataInsertOrUpdateDTO dto) {
  107. permissionService.publicPrivateCustomerPermission();
  108. sysCustomerDataService.adminUpdate(dto);
  109. }
  110. /**
  111. * 删除公海客户
  112. *
  113. * @param id
  114. */
  115. @Transactional(rollbackFor = Exception.class)
  116. public void adminDeleteCustomerData(Long id) {
  117. permissionService.publicPrivateCustomerPermission();
  118. sysCustomerDataService.adminDelete(id);
  119. }
  120. /**
  121. * 公海认领客户
  122. *
  123. * @param customerId
  124. */
  125. @Transactional(rollbackFor = Exception.class)
  126. public void claimCustomer(Long customerId) {
  127. permissionService.publicPrivateCustomerPermission();
  128. sysCustomerDataService.claimCustomer(customerId);
  129. }
  130. /**
  131. * 公海添加客户联系人
  132. *
  133. * @param dto
  134. * @return
  135. */
  136. @Transactional(rollbackFor = Exception.class)
  137. public void adminInsertCustomerPerson(AdminSysCustomerPersonInsertOrUpdateDTO dto) {
  138. permissionService.publicPrivateCustomerPermission();
  139. sysCustomerPersonService.adminInsert(dto);
  140. }
  141. /**
  142. * 公海修改客户联系人
  143. *
  144. * @param dto
  145. */
  146. @Transactional(rollbackFor = Exception.class)
  147. public void adminUpdateCustomerPerson(AdminSysCustomerPersonInsertOrUpdateDTO dto) {
  148. permissionService.publicPrivateCustomerPermission();
  149. sysCustomerPersonService.adminUpdate(dto);
  150. }
  151. /**
  152. * 公海删除联系人
  153. *
  154. * @param id
  155. */
  156. @Transactional(rollbackFor = Exception.class)
  157. public void adminDeleteCustomerPerson(Long id) {
  158. permissionService.publicPrivateCustomerPermission();
  159. sysCustomerPersonService.adminDelete(id);
  160. }
  161. /**
  162. * 查询公海客户联系人
  163. */
  164. public List<SysCustomerPersonBO> adminListCustomerPerson(CustomerPersonReqDTO dto) {
  165. permissionService.publicPrivateCustomerPermission();
  166. return sysCustomerPersonService.adminList(dto);
  167. }
  168. /**
  169. * 查询公海客户详情的信息总览
  170. *
  171. * @param dto
  172. */
  173. public SysCustomerDetailBO queryCustomerDetail(AdminSysCustomerFollowReqDTO dto) throws BusinessException {
  174. permissionService.publicPrivateCustomerPermission();
  175. SysCustomerDetailBO detailBO = SysCustomerDetailBO.builder().build();
  176. SysUserEntity sysUserEntity = sysUserMapper.selectById(UserContextHolder.getUserId());
  177. if (sysUserEntity == null) {
  178. throw new BusinessException("用户不存在");
  179. }
  180. dto.setUserId(null);
  181. detailBO.setHistoryFollowStatus(sysUserEntity.getHistoryFollowStatus() == null ? 0 : sysUserEntity.getHistoryFollowStatus());
  182. if(UserContextHolder.getUserContext().getRoles().contains("Admin")){
  183. detailBO.setHistoryFollowStatus(1);
  184. }
  185. //历史跟进总览开关
  186. if (UserContextHolder.getUserContext().getRoles().contains("Admin") ||
  187. sysUserEntity.getHistoryFollowStatus() != null && sysUserEntity.getHistoryFollowStatus() == 1) {
  188. //顶部筛选条件数据
  189. SysCustomerFollowHistorySelectBO publicCustomerSelectData = sysCustomerDataFollowService.queryCustomerFollowSelectCommonData(dto.getCustomerId(), null);
  190. detailBO.setSelectData(publicCustomerSelectData);
  191. //客户详情信息
  192. PageResult<SysCustomerFollowListBO> dataPageList = sysCustomerDataFollowService.queryCustomerFollowDataCommonByPage(dto);
  193. detailBO.setDataPageList(dataPageList);
  194. //信息总览
  195. detailBO.setProductOverviewData(sysCustomerFollowService.queryProductOverViewData(dto.getCustomerId(), null));
  196. //跟进记录总览
  197. detailBO.setFollowOverViewData(sysCustomerFollowService.handleCustomerFollowOverViewData(dto.getCustomerId(), null));
  198. }
  199. return detailBO;
  200. }
  201. /**
  202. * 操作日志列表查询
  203. */
  204. public SysCustomerOperLogDataBO queryOperLogData(AdminSysCustomerOperLogListDTO dto) {
  205. permissionService.publicPrivateCustomerPermission();
  206. List<Long> userIds = Lists.newArrayList();
  207. if (dto.getOperUserId() != null) {
  208. userIds.add(dto.getOperUserId());
  209. }
  210. List<Long> groupIds = Lists.newArrayList();
  211. if (dto.getGroupId() != null) {
  212. groupIds.add(dto.getGroupId());
  213. }
  214. return sysCustomerOperLogService.adminList(dto, userIds, groupIds, false, false);
  215. }
  216. /*
  217. ----------------------------------------------------------------
  218. 私有方法 private methods
  219. ----------------------------------------------------------------
  220. */
  221. /**
  222. * 实体分页转 BO 分页
  223. *
  224. * @param entityPage 实体分页
  225. * @return BO 分页
  226. */
  227. private PageResult<SysCustomerDataPublicBO> entityPage2BOPage(Page<SysCustomerDataEntity> entityPage) {
  228. return new PageResult<SysCustomerDataPublicBO>()
  229. .setCurrent(entityPage.getCurrent())
  230. .setSize(entityPage.getSize())
  231. .setTotal(entityPage.getTotal())
  232. .setRecords(this.entityList2BOs(entityPage.getRecords()));
  233. }
  234. /**
  235. * 实体 List 转 BO List
  236. *
  237. * @param entityList 实体 List
  238. * @return BO List
  239. */
  240. private List<SysCustomerDataPublicBO> entityList2BOs(List<SysCustomerDataEntity> entityList) {
  241. if (CollUtil.isEmpty(entityList)) {
  242. return Collections.emptyList();
  243. }
  244. //查询学段
  245. List<Long> stageIds =
  246. entityList.stream()
  247. .map(SysCustomerDataEntity::getStageId)
  248. .filter(Objects::nonNull).distinct().toList();
  249. List<SysParamBaseDataEntity> stageDataList = sysParamBaseDataMapper.selectByIds(stageIds);
  250. // 深拷贝
  251. List<SysCustomerDataPublicBO> ret = new ArrayList<>(entityList.size());
  252. entityList.forEach(
  253. entity -> {
  254. SysCustomerDataPublicBO bo = this.entity2BO(entity);
  255. if (CollUtil.isNotEmpty(stageDataList)) {
  256. stageDataList.stream()
  257. .filter(x -> x.getId().equals(entity.getStageId()))
  258. .findFirst()
  259. .ifPresent(e -> bo.setStageName(e.getTitle()));
  260. }
  261. ret.add(bo);
  262. });
  263. return ret;
  264. }
  265. /**
  266. * 实体转 BO
  267. *
  268. * @param entity 实体
  269. * @return BO
  270. */
  271. private SysCustomerDataPublicBO entity2BO(SysCustomerDataEntity entity) {
  272. if (entity == null) {
  273. return null;
  274. }
  275. SysCustomerDataPublicBO bo = new SysCustomerDataPublicBO();
  276. BeanUtil.copyProperties(entity, bo);
  277. // 可以在此处为BO填充字段
  278. bo.setUnUsedDayCount("1天");
  279. long days = DateUtil.betweenDay(DateUtil.date(),
  280. DateUtil.parse(entity.getResetDatetime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))),
  281. false);
  282. //向上取整
  283. double ceilDays = Math.ceil(days);
  284. if ((int) ceilDays >= 1) {
  285. bo.setUnUsedDayCount(days + "天");
  286. }
  287. bo.setDelStatus(true);
  288. return bo;
  289. }
  290. }