5 天以前 8f7c42bb0bced4697755299f67483be11da3e44d
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
package cn.iocoder.yudao.module.aftersales.controller.admin.returnobj;
 
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.returnobj.vo.AfterSaleReturnPageReqVO;
import cn.iocoder.yudao.module.aftersales.controller.admin.returnobj.vo.AfterSaleReturnRespVO;
import cn.iocoder.yudao.module.aftersales.controller.admin.returnobj.vo.AfterSaleReturnSaveReqVO;
import cn.iocoder.yudao.module.aftersales.dal.dataobject.returnobj.AfterSaleReturnDO;
import cn.iocoder.yudao.module.aftersales.dal.dataobject.returnobj.AfterSaleReturnItemDO;
import cn.iocoder.yudao.module.aftersales.service.returnobj.AfterSaleReturnService;
import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO;
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/return")
@Validated
public class AfterSaleReturnController {
 
    @Resource
    private AfterSaleReturnService returnService;
 
    @Resource
    private CrmCustomerApi customerApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建退货申请")
    @PreAuthorize("@ss.hasPermission('aftersales:return:create')")
    public CommonResult<Long> createReturn(@Valid @RequestBody AfterSaleReturnSaveReqVO createReqVO) {
        return success(returnService.createReturn(createReqVO, getLoginUserId()));
    }
 
    @PutMapping("/approve")
    @Operation(summary = "审核通过退货申请")
    @PreAuthorize("@ss.hasPermission('aftersales:return:approve')")
    public CommonResult<Boolean> approveReturn(@RequestParam("id") Long id) {
        returnService.approveReturn(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得退货申请")
    @Parameter(name = "id", description = "退货申请编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('aftersales:return:query')")
    public CommonResult<AfterSaleReturnRespVO> getReturn(@RequestParam("id") Long id) {
        AfterSaleReturnDO returnDO = returnService.getReturn(id);
        List<AfterSaleReturnItemDO> items = returnService.getReturnItemListByReturnId(id);
        List<AfterSaleReturnRespVO> enrichedList = buildReturnDetailList(List.of(returnDO));
        AfterSaleReturnRespVO respVO = enrichedList.get(0);
        respVO.setItems(BeanUtils.toBean(items, AfterSaleReturnRespVO.Item.class));
        return success(respVO);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得退货申请分页")
    @PreAuthorize("@ss.hasPermission('aftersales:return:query')")
    public CommonResult<PageResult<AfterSaleReturnRespVO>> getReturnPage(@Valid AfterSaleReturnPageReqVO pageReqVO) {
        PageResult<AfterSaleReturnDO> pageResult = returnService.getReturnPage(pageReqVO, getLoginUserId());
        List<AfterSaleReturnRespVO> list = buildReturnDetailList(pageResult.getList());
        return success(new PageResult<>(list, pageResult.getTotal()));
    }
 
    // ========== 内部方法:批量拼装关联名称 ==========
 
    private List<AfterSaleReturnRespVO> buildReturnDetailList(List<AfterSaleReturnDO> returnList) {
        if (CollUtil.isEmpty(returnList)) {
            return Collections.emptyList();
        }
        // 批量加载客户名称
        Set<Long> customerIds = convertSet(returnList, AfterSaleReturnDO::getCustomerId);
        Map<Long, CrmCustomerRespDTO> customerMap = customerApi.getCustomerMap(customerIds);
        // 拼装名称字段
        return BeanUtils.toBean(returnList, AfterSaleReturnRespVO.class, vo -> {
            CrmCustomerRespDTO customer = customerMap.get(vo.getCustomerId());
            if (customer != null) vo.setCustomerName(customer.getName());
        });
    }
 
}