9 天以前 8c14b50773a1e582b652c03f5a63bb2233d069b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cn.iocoder.yudao.module.crm.controller.admin.followup;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.business.vo.business.CrmBusinessRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowUpRecordPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowUpRecordRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.followup.vo.CrmFollowUpRecordSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.followup.CrmFollowUpRecordDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum;
import cn.iocoder.yudao.module.crm.service.business.CrmBusinessService;
import cn.iocoder.yudao.module.crm.service.contact.CrmContactService;
import cn.iocoder.yudao.module.crm.service.followup.CrmFollowUpRecordService;
import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService;
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.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
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.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSetByFlatMap;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CRM_PERMISSION_DENIED;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.FOLLOW_UP_RECORD_NOT_EXISTS;
 
 
@Tag(name = "管理后台 - 跟进记录")
@RestController
@RequestMapping("/crm/follow-up-record")
@Validated
public class CrmFollowUpRecordController {
 
    @Resource
    private CrmFollowUpRecordService followUpRecordService;
    @Resource
    private CrmContactService contactService;
    @Resource
    private CrmBusinessService businessService;
    @Resource
    private CrmPermissionService permissionService;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建跟进记录")
    public CommonResult<Long> createFollowUpRecord(@Valid @RequestBody CrmFollowUpRecordSaveReqVO createReqVO) {
        return success(followUpRecordService.createFollowUpRecord(createReqVO));
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除跟进记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFollowUpRecord(@RequestParam("id") Long id) {
        followUpRecordService.deleteFollowUpRecord(id, getLoginUserId());
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得跟进记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmFollowUpRecordRespVO> getFollowUpRecord(@RequestParam("id") Long id) {
        CrmFollowUpRecordDO followUpRecord = followUpRecordService.getFollowUpRecord(id);
        if (followUpRecord == null) {
            throw exception(FOLLOW_UP_RECORD_NOT_EXISTS);
        }
        if (!permissionService.hasPermission(followUpRecord.getBizType(), followUpRecord.getBizId(),
                getLoginUserId(), CrmPermissionLevelEnum.READ)) {
            throw exception(CRM_PERMISSION_DENIED, CrmBizTypeEnum.getNameByType(followUpRecord.getBizType()));
        }
        return success(BeanUtils.toBean(followUpRecord, CrmFollowUpRecordRespVO.class));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得跟进记录分页")
    public CommonResult<PageResult<CrmFollowUpRecordRespVO>> getFollowUpRecordPage(@Valid CrmFollowUpRecordPageReqVO pageReqVO) {
        PageResult<CrmFollowUpRecordDO> pageResult = followUpRecordService.getFollowUpRecordPage(pageReqVO);
        // 1.1 查询联系人和商机
        Map<Long, CrmContactDO> contactMap = contactService.getContactMap(
                convertSetByFlatMap(pageResult.getList(), item -> {
                    List<Long> contactIds = item.getContactIds();
                    return contactIds != null ? contactIds.stream() : Stream.empty();
                }));
        Map<Long, CrmBusinessDO> businessMap = businessService.getBusinessMap(
                convertSetByFlatMap(pageResult.getList(), item -> {
                    List<Long> businessIds = item.getBusinessIds();
                    return businessIds != null ? businessIds.stream() : Stream.empty();
                }));
        // 1.2 查询用户
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertSet(pageResult.getList(), item -> {
                    String creator = item.getCreator();
                    return creator != null && !creator.isEmpty() ? Long.valueOf(creator) : null;
                }));
        // 2. 拼接数据
        PageResult<CrmFollowUpRecordRespVO> voPageResult = BeanUtils.toBean(pageResult, CrmFollowUpRecordRespVO.class, record -> {
            // 2.1 设置联系人和商机信息
            record.setBusinesses(new ArrayList<>()).setContacts(new ArrayList<>());
            List<Long> contactIds = record.getContactIds();
            if (contactIds != null) {
                contactIds.forEach(id -> MapUtils.findAndThen(contactMap, id, contact ->
                        record.getContacts().add(new CrmBusinessRespVO().setId(contact.getId()).setName(contact.getName()))));
            }
            List<Long> businessIds = record.getBusinessIds();
            if (businessIds != null) {
                businessIds.forEach(id -> MapUtils.findAndThen(businessMap, id, business ->
                        record.getBusinesses().add(new CrmBusinessRespVO().setId(business.getId()).setName(business.getName()))));
            }
            // 2.2 设置用户信息
            String creator = record.getCreator();
            if (creator != null && !creator.isEmpty()) {
                MapUtils.findAndThen(userMap, Long.valueOf(creator), user -> record.setCreatorName(user.getNickname()));
            }
        });
        return success(voPageResult);
    }
 
}