|
@@ -2,48 +2,55 @@ package cn.iocoder.yudao.module.crm.controller.admin.customer;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
-import cn.hutool.core.util.ObjUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.core.KeyValue;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.framework.excel.core.enums.ExcelColumn;
|
|
|
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
|
|
+import cn.iocoder.yudao.framework.ip.core.Area;
|
|
|
+import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
|
|
|
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
|
|
|
-import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*;
|
|
|
-import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert;
|
|
|
+import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer.*;
|
|
|
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
|
|
|
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerPoolConfigDO;
|
|
|
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerPoolConfigService;
|
|
|
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
|
|
|
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
|
|
|
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
|
|
|
+import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
|
|
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
|
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
+import io.swagger.v3.oas.annotations.Parameters;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import jakarta.validation.Valid;
|
|
|
-import org.mapstruct.ap.internal.util.Collections;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
|
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
|
|
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
|
|
|
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
-import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_POOL_CONFIG_NOT_EXISTS_OR_DISABLED;
|
|
|
+import static cn.iocoder.yudao.module.crm.enums.DictTypeConstants.*;
|
|
|
+import static java.util.Collections.singletonList;
|
|
|
|
|
|
@Tag(name = "管理后台 - CRM 客户")
|
|
|
@RestController
|
|
@@ -55,10 +62,13 @@ public class CrmCustomerController {
|
|
|
private CrmCustomerService customerService;
|
|
|
@Resource
|
|
|
private CrmCustomerPoolConfigService customerPoolConfigService;
|
|
|
+
|
|
|
@Resource
|
|
|
private DeptApi deptApi;
|
|
|
@Resource
|
|
|
private AdminUserApi adminUserApi;
|
|
|
+ @Resource
|
|
|
+ private DictDataApi dictDataApi;
|
|
|
|
|
|
@PostMapping("/create")
|
|
|
@Operation(summary = "创建客户")
|
|
@@ -75,6 +85,18 @@ public class CrmCustomerController {
|
|
|
return success(true);
|
|
|
}
|
|
|
|
|
|
+ @PutMapping("/update-deal-status")
|
|
|
+ @Operation(summary = "更新客户的成交状态")
|
|
|
+ @Parameters({
|
|
|
+ @Parameter(name = "id", description = "客户编号", required = true),
|
|
|
+ @Parameter(name = "dealStatus", description = "成交状态", required = true)
|
|
|
+ })
|
|
|
+ public CommonResult<Boolean> updateCustomerDealStatus(@RequestParam("id") Long id,
|
|
|
+ @RequestParam("dealStatus") Boolean dealStatus) {
|
|
|
+ customerService.updateCustomerDealStatus(id, dealStatus);
|
|
|
+ return success(true);
|
|
|
+ }
|
|
|
+
|
|
|
@DeleteMapping("/delete")
|
|
|
@Operation(summary = "删除客户")
|
|
|
@Parameter(name = "id", description = "客户编号", required = true)
|
|
@@ -91,94 +113,126 @@ public class CrmCustomerController {
|
|
|
public CommonResult<CrmCustomerRespVO> getCustomer(@RequestParam("id") Long id) {
|
|
|
// 1. 获取客户
|
|
|
CrmCustomerDO customer = customerService.getCustomer(id);
|
|
|
+ // 2. 拼接数据
|
|
|
+ return success(buildCustomerDetail(customer));
|
|
|
+ }
|
|
|
+
|
|
|
+ public CrmCustomerRespVO buildCustomerDetail(CrmCustomerDO customer) {
|
|
|
if (customer == null) {
|
|
|
- return success(null);
|
|
|
+ return null;
|
|
|
}
|
|
|
- // 2. 拼接数据
|
|
|
- Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
|
|
|
- Collections.asSet(Long.valueOf(customer.getCreator()), customer.getOwnerUserId()));
|
|
|
- Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
|
|
|
- return success(CrmCustomerConvert.INSTANCE.convert(customer, userMap, deptMap));
|
|
|
+ return buildCustomerDetailList(singletonList(customer)).get(0);
|
|
|
}
|
|
|
|
|
|
@GetMapping("/page")
|
|
|
@Operation(summary = "获得客户分页")
|
|
|
@PreAuthorize("@ss.hasPermission('crm:customer:query')")
|
|
|
public CommonResult<PageResult<CrmCustomerRespVO>> getCustomerPage(@Valid CrmCustomerPageReqVO pageVO) {
|
|
|
+ customerService.autoPutCustomerPool();
|
|
|
// 1. 查询客户分页
|
|
|
PageResult<CrmCustomerDO> pageResult = customerService.getCustomerPage(pageVO, getLoginUserId());
|
|
|
if (CollUtil.isEmpty(pageResult.getList())) {
|
|
|
return success(PageResult.empty(pageResult.getTotal()));
|
|
|
}
|
|
|
-
|
|
|
// 2. 拼接数据
|
|
|
- Map<Long, Long> poolDayMap = Boolean.TRUE.equals(pageVO.getPool()) ? null :
|
|
|
- getPoolDayMap(pageResult.getList()); // 客户界面,需要查看距离进入公海的时间
|
|
|
- Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
|
|
|
- convertSetByFlatMap(pageResult.getList(), user -> Stream.of(Long.parseLong(user.getCreator()), user.getOwnerUserId())));
|
|
|
+ return success(new PageResult<>(buildCustomerDetailList(pageResult.getList()), pageResult.getTotal()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<CrmCustomerRespVO> buildCustomerDetailList(List<CrmCustomerDO> list) {
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ return java.util.Collections.emptyList();
|
|
|
+ }
|
|
|
+ // 1.1 获取创建人、负责人列表
|
|
|
+ Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(list,
|
|
|
+ contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));
|
|
|
Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
|
|
|
- return success(CrmCustomerConvert.INSTANCE.convertPage(pageResult, userMap, deptMap, poolDayMap));
|
|
|
+ // 1.2 获取距离进入公海的时间
|
|
|
+ Map<Long, Long> poolDayMap = getPoolDayMap(list);
|
|
|
+ // 2. 转换成 VO
|
|
|
+ return BeanUtils.toBean(list, CrmCustomerRespVO.class, customerVO -> {
|
|
|
+ customerVO.setAreaName(AreaUtils.format(customerVO.getAreaId()));
|
|
|
+ // 2.1 设置创建人、负责人名称
|
|
|
+ MapUtils.findAndThen(userMap, NumberUtils.parseLong(customerVO.getCreator()),
|
|
|
+ user -> customerVO.setCreatorName(user.getNickname()));
|
|
|
+ MapUtils.findAndThen(userMap, customerVO.getOwnerUserId(), user -> {
|
|
|
+ customerVO.setOwnerUserName(user.getNickname());
|
|
|
+ MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> customerVO.setOwnerUserDeptName(dept.getName()));
|
|
|
+ });
|
|
|
+ // 2.2 设置距离进入公海的时间
|
|
|
+ if (customerVO.getOwnerUserId() != null) {
|
|
|
+ customerVO.setPoolDay(poolDayMap.get(customerVO.getId()));
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/put-in-pool-remind-page")
|
|
|
+ @GetMapping("/put-pool-remind-page")
|
|
|
@Operation(summary = "获得待进入公海客户分页")
|
|
|
@PreAuthorize("@ss.hasPermission('crm:customer:query')")
|
|
|
- public CommonResult<PageResult<CrmCustomerRespVO>> getPutInPoolRemindCustomerPage(@Valid CrmCustomerPageReqVO pageVO) {
|
|
|
- // 获取公海配置 TODO @dbh52:合并到 getPutInPoolRemindCustomerPage 会更合适哈;
|
|
|
- CrmCustomerPoolConfigDO poolConfigDO = customerPoolConfigService.getCustomerPoolConfig();
|
|
|
- if (ObjUtil.isNull(poolConfigDO)
|
|
|
- || Boolean.FALSE.equals(poolConfigDO.getEnabled())
|
|
|
- || Boolean.FALSE.equals(poolConfigDO.getNotifyEnabled())
|
|
|
- ) { // TODO @dbh52:这个括号,一般不换行,在 java 这里;
|
|
|
- throw exception(CUSTOMER_POOL_CONFIG_NOT_EXISTS_OR_DISABLED);
|
|
|
- }
|
|
|
-
|
|
|
+ public CommonResult<PageResult<CrmCustomerRespVO>> getPutPoolRemindCustomerPage(@Valid CrmCustomerPageReqVO pageVO) {
|
|
|
// 1. 查询客户分页
|
|
|
- PageResult<CrmCustomerDO> pageResult = customerService.getPutInPoolRemindCustomerPage(pageVO, poolConfigDO, getLoginUserId());
|
|
|
- if (CollUtil.isEmpty(pageResult.getList())) {
|
|
|
- return success(PageResult.empty(pageResult.getTotal()));
|
|
|
- }
|
|
|
-
|
|
|
+ PageResult<CrmCustomerDO> pageResult = customerService.getPutPoolRemindCustomerPage(pageVO, getLoginUserId());
|
|
|
// 2. 拼接数据
|
|
|
- // TODO @芋艿:合并 getCustomerPage 和 getPutInPoolRemindCustomerPage 的后置处理;
|
|
|
- Map<Long, Long> poolDayMap = getPoolDayMap(pageResult.getList()); // 客户界面,需要查看距离进入公海的时间
|
|
|
- Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
|
|
|
- convertSetByFlatMap(pageResult.getList(), user -> Stream.of(Long.parseLong(user.getCreator()), user.getOwnerUserId())));
|
|
|
- Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
|
|
|
- return success(CrmCustomerConvert.INSTANCE.convertPage(pageResult, userMap, deptMap, poolDayMap));
|
|
|
+ return success(new PageResult<>(buildCustomerDetailList(pageResult.getList()), pageResult.getTotal()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/put-pool-remind-count")
|
|
|
+ @Operation(summary = "获得待进入公海客户数量")
|
|
|
+ @PreAuthorize("@ss.hasPermission('crm:customer:query')")
|
|
|
+ public CommonResult<Long> getPutPoolRemindCustomerCount() {
|
|
|
+ return success(customerService.getPutPoolRemindCustomerCount(getLoginUserId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/today-contact-count")
|
|
|
+ @Operation(summary = "获得今日需联系客户数量")
|
|
|
+ @PreAuthorize("@ss.hasPermission('crm:customer:query')")
|
|
|
+ public CommonResult<Long> getTodayContactCustomerCount() {
|
|
|
+ return success(customerService.getTodayContactCustomerCount(getLoginUserId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/follow-count")
|
|
|
+ @Operation(summary = "获得分配给我、待跟进的线索数量的客户数量")
|
|
|
+ @PreAuthorize("@ss.hasPermission('crm:customer:query')")
|
|
|
+ public CommonResult<Long> getFollowCustomerCount() {
|
|
|
+ return success(customerService.getFollowCustomerCount(getLoginUserId()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取距离进入公海的时间
|
|
|
+ * 获取距离进入公海的时间 Map
|
|
|
*
|
|
|
- * @param customerList 客户列表
|
|
|
- * @return Map<key 客户编号, value 距离进入公海的时间>
|
|
|
+ * @param list 客户列表
|
|
|
+ * @return key 客户编号, value 距离进入公海的时间
|
|
|
*/
|
|
|
- private Map<Long, Long> getPoolDayMap(List<CrmCustomerDO> customerList) {
|
|
|
+ private Map<Long, Long> getPoolDayMap(List<CrmCustomerDO> list) {
|
|
|
CrmCustomerPoolConfigDO poolConfig = customerPoolConfigService.getCustomerPoolConfig();
|
|
|
if (poolConfig == null || !poolConfig.getEnabled()) {
|
|
|
return MapUtil.empty();
|
|
|
}
|
|
|
- return convertMap(customerList, CrmCustomerDO::getId, customer -> {
|
|
|
- // 1.1 未成交放入公海天数
|
|
|
- long dealExpireDay = 0;
|
|
|
- if (!customer.getDealStatus()) {
|
|
|
- dealExpireDay = poolConfig.getDealExpireDays() - LocalDateTimeUtils.between(customer.getCreateTime());
|
|
|
+ list = CollectionUtils.filterList(list, customer -> {
|
|
|
+ // 特殊:如果没负责人,则说明已经在公海,不用计算
|
|
|
+ if (customer.getOwnerUserId() == null) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+ // 已成交 or 已锁定,不进入公海
|
|
|
+ return !customer.getDealStatus() && !customer.getLockStatus();
|
|
|
+ });
|
|
|
+ return convertMap(list, CrmCustomerDO::getId, customer -> {
|
|
|
+ // 1.1 未成交放入公海天数
|
|
|
+ long dealExpireDay = poolConfig.getDealExpireDays() - LocalDateTimeUtils.between(customer.getOwnerTime());
|
|
|
// 1.2 未跟进放入公海天数
|
|
|
- LocalDateTime lastTime = ObjUtil.defaultIfNull(customer.getContactLastTime(), customer.getCreateTime());
|
|
|
- long contactExpireDay = poolConfig.getContactExpireDays() - LocalDateTimeUtils.between(lastTime);
|
|
|
- if (contactExpireDay < 0) {
|
|
|
- contactExpireDay = 0;
|
|
|
+ LocalDateTime lastTime = customer.getOwnerTime();
|
|
|
+ if (customer.getContactLastTime() != null && customer.getContactLastTime().isAfter(lastTime)) {
|
|
|
+ lastTime = customer.getContactLastTime();
|
|
|
}
|
|
|
+ long contactExpireDay = poolConfig.getContactExpireDays() - LocalDateTimeUtils.between(lastTime);
|
|
|
// 2. 返回最小的天数
|
|
|
- return Math.min(dealExpireDay, contactExpireDay);
|
|
|
+ long poolDay = Math.min(dealExpireDay, contactExpireDay);
|
|
|
+ return poolDay > 0 ? poolDay : 0;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- @GetMapping(value = "/list-all-simple")
|
|
|
+ @GetMapping(value = "/simple-list")
|
|
|
@Operation(summary = "获取客户精简信息列表", description = "只包含有读权限的客户,主要用于前端的下拉选项")
|
|
|
- public CommonResult<List<CrmCustomerRespVO>> getSimpleDeptList() {
|
|
|
+ public CommonResult<List<CrmCustomerRespVO>> getCustomerSimpleList() {
|
|
|
CrmCustomerPageReqVO reqVO = new CrmCustomerPageReqVO();
|
|
|
reqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
|
|
|
List<CrmCustomerDO> list = customerService.getCustomerPage(reqVO, getLoginUserId()).getList();
|
|
@@ -186,7 +240,6 @@ public class CrmCustomerController {
|
|
|
new CrmCustomerRespVO().setId(customer.getId()).setName(customer.getName())));
|
|
|
}
|
|
|
|
|
|
- // TODO @puhui999:公海的导出,前端可以接下
|
|
|
@GetMapping("/export-excel")
|
|
|
@Operation(summary = "导出客户 Excel")
|
|
|
@PreAuthorize("@ss.hasPermission('crm:customer:export')")
|
|
@@ -197,7 +250,7 @@ public class CrmCustomerController {
|
|
|
List<CrmCustomerDO> list = customerService.getCustomerPage(pageVO, getLoginUserId()).getList();
|
|
|
// 导出 Excel
|
|
|
ExcelUtils.write(response, "客户.xls", "数据", CrmCustomerRespVO.class,
|
|
|
- BeanUtils.toBean(list, CrmCustomerRespVO.class));
|
|
|
+ buildCustomerDetailList(list));
|
|
|
}
|
|
|
|
|
|
@GetMapping("/get-import-template")
|
|
@@ -205,15 +258,33 @@ public class CrmCustomerController {
|
|
|
public void importTemplate(HttpServletResponse response) throws IOException {
|
|
|
// 手动创建导出 demo
|
|
|
List<CrmCustomerImportExcelVO> list = Arrays.asList(
|
|
|
- CrmCustomerImportExcelVO.builder().name("芋道").industryId(1).level(1).source(1).mobile("15601691300").telephone("")
|
|
|
- .website("https://doc.iocoder.cn/").qq("").wechat("").email("yunai@iocoder.cn").description("").remark("")
|
|
|
- .areaId(null).detailAddress("").build(),
|
|
|
- CrmCustomerImportExcelVO.builder().name("源码").industryId(1).level(1).source(1).mobile("15601691300").telephone("")
|
|
|
- .website("https://doc.iocoder.cn/").qq("").wechat("").email("yunai@iocoder.cn").description("").remark("")
|
|
|
- .areaId(null).detailAddress("").build()
|
|
|
+ CrmCustomerImportExcelVO.builder().name("芋道").industryId(1).level(1).source(1)
|
|
|
+ .mobile("15601691300").telephone("").qq("").wechat("").email("yunai@iocoder.cn")
|
|
|
+ .areaId(null).detailAddress("").remark("").build(),
|
|
|
+ CrmCustomerImportExcelVO.builder().name("源码").industryId(1).level(1).source(1)
|
|
|
+ .mobile("15601691300").telephone("").qq("").wechat("").email("yunai@iocoder.cn")
|
|
|
+ .areaId(null).detailAddress("").remark("").build()
|
|
|
);
|
|
|
// 输出
|
|
|
- ExcelUtils.write(response, "客户导入模板.xls", "客户列表", CrmCustomerImportExcelVO.class, list);
|
|
|
+ ExcelUtils.write(response, "客户导入模板.xls", "客户列表", CrmCustomerImportExcelVO.class, list, builderSelectMap());
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<KeyValue<ExcelColumn, List<String>>> builderSelectMap() {
|
|
|
+ List<KeyValue<ExcelColumn, List<String>>> selectMap = new ArrayList<>();
|
|
|
+ // 获取地区下拉数据
|
|
|
+ // TODO @puhui999:嘿嘿,这里改成省份、城市、区域,三个选项,难度大么?
|
|
|
+ Area area = AreaUtils.parseArea(Area.ID_CHINA);
|
|
|
+ selectMap.add(new KeyValue<>(ExcelColumn.G, AreaUtils.getAreaNodePathList(area.getChildren())));
|
|
|
+ // 获取客户所属行业
|
|
|
+ List<String> customerIndustries = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_INDUSTRY);
|
|
|
+ selectMap.add(new KeyValue<>(ExcelColumn.I, customerIndustries));
|
|
|
+ // 获取客户等级
|
|
|
+ List<String> customerLevels = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_LEVEL);
|
|
|
+ selectMap.add(new KeyValue<>(ExcelColumn.J, customerLevels));
|
|
|
+ // 获取客户来源
|
|
|
+ List<String> customerSources = dictDataApi.getDictDataLabelList(CRM_CUSTOMER_SOURCE);
|
|
|
+ selectMap.add(new KeyValue<>(ExcelColumn.K, customerSources));
|
|
|
+ return selectMap;
|
|
|
}
|
|
|
|
|
|
@PostMapping("/import")
|