SysCustomerDataPublicService.java 12 KB

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