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());
|
});
|
}
|
|
}
|