liu
22 小时以前 46287520465b5664a1ddb0256537857cfd1cfc15
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
136
137
138
139
140
141
package cn.iocoder.yudao.module.aftersales.controller.admin.problem;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.aftersales.controller.admin.problem.vo.AfterSaleProblemPageReqVO;
import cn.iocoder.yudao.module.aftersales.controller.admin.problem.vo.AfterSaleProblemRespVO;
import cn.iocoder.yudao.module.aftersales.controller.admin.problem.vo.AfterSaleProblemSaveReqVO;
import cn.iocoder.yudao.module.aftersales.dal.dataobject.problem.AfterSaleProblemDO;
import cn.iocoder.yudao.module.aftersales.service.problem.AfterSaleProblemService;
import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
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.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 
@Tag(name = "管理后台 - 售后问题库")
@RestController
@RequestMapping("/aftersales/problem")
@Validated
public class AfterSaleProblemController {
 
    @Resource
    private AfterSaleProblemService problemService;
 
    @Resource
    private CrmCustomerApi customerApi;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建售后问题")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:create')")
    public CommonResult<Long> createProblem(@Valid @RequestBody AfterSaleProblemSaveReqVO createReqVO) {
        return success(problemService.createProblem(createReqVO, getLoginUserId()));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新售后问题")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:update')")
    public CommonResult<Boolean> updateProblem(@Valid @RequestBody AfterSaleProblemSaveReqVO updateReqVO) {
        problemService.updateProblem(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除售后问题")
    @Parameter(name = "id", description = "问题编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:delete')")
    public CommonResult<Boolean> deleteProblem(@RequestParam("id") Long id) {
        problemService.deleteProblem(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得售后问题")
    @Parameter(name = "id", description = "问题编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:query')")
    public CommonResult<AfterSaleProblemRespVO> getProblem(@RequestParam("id") Long id) {
        AfterSaleProblemDO problemDO = problemService.getProblem(id);
        AfterSaleProblemRespVO respVO = buildProblemDetailList(List.of(problemDO)).get(0);
        respVO.setAttachmentList(storageAttachmentApi.listAttachments("aftersales_after_sale_problem", id));
        return success(respVO);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得售后问题分页")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:query')")
    public CommonResult<PageResult<AfterSaleProblemRespVO>> getProblemPage(@Valid AfterSaleProblemPageReqVO pageReqVO) {
        PageResult<AfterSaleProblemDO> pageResult = problemService.getProblemPage(pageReqVO);
        return success(new PageResult<>(buildProblemDetailList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @PutMapping("/publish")
    @Operation(summary = "发布售后问题")
    @Parameter(name = "id", description = "问题编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:publish')")
    public CommonResult<Boolean> publishProblem(@RequestParam("id") Long id) {
        problemService.publishProblem(id);
        return success(true);
    }
 
    @PutMapping("/disable")
    @Operation(summary = "停用售后问题")
    @Parameter(name = "id", description = "问题编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:disable')")
    public CommonResult<Boolean> disableProblem(@RequestParam("id") Long id) {
        problemService.disableProblem(id);
        return success(true);
    }
 
    @GetMapping("/reference")
    @Operation(summary = "新工单受理参考检索(已发布问题)")
    @PreAuthorize("@ss.hasPermission('aftersales:problem:query')")
    public CommonResult<List<AfterSaleProblemRespVO>> getReferenceList(@Valid AfterSaleProblemPageReqVO pageReqVO) {
        return success(buildProblemDetailList(problemService.getReferenceList(pageReqVO)));
    }
 
    // ========== 内部方法:批量拼装关联名称 ==========
 
    private List<AfterSaleProblemRespVO> buildProblemDetailList(List<AfterSaleProblemDO> problemList) {
        if (CollUtil.isEmpty(problemList)) {
            return Collections.emptyList();
        }
        // 1. 批量加载客户名称
        Set<Long> customerIds = convertSet(problemList, AfterSaleProblemDO::getCustomerId);
        Map<Long, CrmCustomerRespDTO> customerMap = customerApi.getCustomerMap(customerIds);
        // 2. 批量加载沉淀人名称
        Set<Long> userIds = convertSet(problemList, AfterSaleProblemDO::getOwnerUserId);
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
        // 3. 拼装名称字段
        return BeanUtils.toBean(problemList, AfterSaleProblemRespVO.class, vo -> {
            CrmCustomerRespDTO customer = customerMap.get(vo.getCustomerId());
            if (customer != null) {
                vo.setCustomerName(customer.getName());
            }
            AdminUserRespDTO ownerUser = userMap.get(vo.getOwnerUserId());
            if (ownerUser != null) {
                vo.setOwnerUserName(ownerUser.getNickname());
            }
        });
    }
 
}