SysCustomerDataPublicService.java 11 KB

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