|  | @@ -10,11 +10,15 @@ import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.*;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.convert.contract.CrmContractConvert;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessProductDO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.dataobject.product.CrmProductDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.service.business.CrmBusinessProductService;
 | 
	
		
			
				|  |  | +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.contract.CrmContractService;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.service.product.CrmProductService;
 | 
	
	
		
			
				|  | @@ -26,11 +30,13 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 | 
	
		
			
				|  |  |  import jakarta.annotation.Resource;
 | 
	
		
			
				|  |  |  import jakarta.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  |  import jakarta.validation.Valid;
 | 
	
		
			
				|  |  | +import org.springframework.context.annotation.Lazy;
 | 
	
		
			
				|  |  |  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.util.Collections;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  |  import java.util.stream.Stream;
 | 
	
	
		
			
				|  | @@ -52,6 +58,11 @@ public class CrmContractController {
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private CrmCustomerService customerService;
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  | +    private CrmContactService contactService;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private CrmBusinessService businessService;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    @Lazy
 | 
	
		
			
				|  |  |      private CrmBusinessProductService businessProductService;
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private CrmProductService productService;
 | 
	
	
		
			
				|  | @@ -89,7 +100,8 @@ public class CrmContractController {
 | 
	
		
			
				|  |  |      @PreAuthorize("@ss.hasPermission('crm:contract:query')")
 | 
	
		
			
				|  |  |      public CommonResult<CrmContractRespVO> getContract(@RequestParam("id") Long id) {
 | 
	
		
			
				|  |  |          CrmContractDO contract = contractService.getContract(id);
 | 
	
		
			
				|  |  | -        CrmContractRespVO respVO = BeanUtils.toBean(contract, CrmContractRespVO.class);
 | 
	
		
			
				|  |  | +        List<CrmContractRespVO> respVOList = buildContractDetail(Collections.singletonList(contract));
 | 
	
		
			
				|  |  | +        CrmContractRespVO respVO = respVOList.getFirst();
 | 
	
		
			
				|  |  |          List<CrmBusinessProductDO> businessProductList = businessProductService.getBusinessProductListByContractId(id);
 | 
	
		
			
				|  |  |          Map<Long, CrmBusinessProductDO> businessProductMap = convertMap(businessProductList, CrmBusinessProductDO::getProductId);
 | 
	
		
			
				|  |  |          List<CrmProductDO> productList = productService.getProductListByIds(convertSet(businessProductList, CrmBusinessProductDO::getProductId));
 | 
	
	
		
			
				|  | @@ -108,15 +120,15 @@ public class CrmContractController {
 | 
	
		
			
				|  |  |      @PreAuthorize("@ss.hasPermission('crm:contract:query')")
 | 
	
		
			
				|  |  |      public CommonResult<PageResult<CrmContractRespVO>> getContractPage(@Valid CrmContractPageReqVO pageVO) {
 | 
	
		
			
				|  |  |          PageResult<CrmContractDO> pageResult = contractService.getContractPage(pageVO, getLoginUserId());
 | 
	
		
			
				|  |  | -        return success(buildContractDetailPage(pageResult));
 | 
	
		
			
				|  |  | +        return success(BeanUtils.toBean(pageResult, CrmContractRespVO.class).setList(buildContractDetail(pageResult.getList())));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @GetMapping("/page-by-customer")
 | 
	
		
			
				|  |  | -    @Operation(summary = "获得联系人分页,基于指定客户")
 | 
	
		
			
				|  |  | +    @Operation(summary = "获得合同分页,基于指定客户")
 | 
	
		
			
				|  |  |      public CommonResult<PageResult<CrmContractRespVO>> getContractPageByCustomer(@Valid CrmContractPageReqVO pageVO) {
 | 
	
		
			
				|  |  |          Assert.notNull(pageVO.getCustomerId(), "客户编号不能为空");
 | 
	
		
			
				|  |  |          PageResult<CrmContractDO> pageResult = contractService.getContractPageByCustomerId(pageVO);
 | 
	
		
			
				|  |  | -        return success(buildContractDetailPage(pageResult));
 | 
	
		
			
				|  |  | +        return success(BeanUtils.toBean(pageResult, CrmContractRespVO.class).setList(buildContractDetail(pageResult.getList())));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @GetMapping("/export-excel")
 | 
	
	
		
			
				|  | @@ -132,23 +144,28 @@ public class CrmContractController {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * 构建详细的合同分页结果
 | 
	
		
			
				|  |  | +     * 构建详细的合同结果
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  | -     * @param pageResult 简单的合同分页结果
 | 
	
		
			
				|  |  | -     * @return 详细的合同分页结果
 | 
	
		
			
				|  |  | +     * @param contractList 原始合同信息
 | 
	
		
			
				|  |  | +     * @return 细的合同结果
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    private PageResult<CrmContractRespVO> buildContractDetailPage(PageResult<CrmContractDO> pageResult) {
 | 
	
		
			
				|  |  | -        List<CrmContractDO> contactList = pageResult.getList();
 | 
	
		
			
				|  |  | -        if (CollUtil.isEmpty(contactList)) {
 | 
	
		
			
				|  |  | -            return PageResult.empty(pageResult.getTotal());
 | 
	
		
			
				|  |  | +    private List<CrmContractRespVO> buildContractDetail(List<CrmContractDO> contractList) {
 | 
	
		
			
				|  |  | +        if (CollUtil.isEmpty(contractList)) {
 | 
	
		
			
				|  |  | +            return Collections.emptyList();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          // 1. 获取客户列表
 | 
	
		
			
				|  |  |          List<CrmCustomerDO> customerList = customerService.getCustomerList(
 | 
	
		
			
				|  |  | -                convertSet(contactList, CrmContractDO::getCustomerId));
 | 
	
		
			
				|  |  | +                convertSet(contractList, CrmContractDO::getCustomerId));
 | 
	
		
			
				|  |  |          // 2. 获取创建人、负责人列表
 | 
	
		
			
				|  |  | -        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(contactList,
 | 
	
		
			
				|  |  | +        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(contractList,
 | 
	
		
			
				|  |  |                  contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));
 | 
	
		
			
				|  |  | -        return CrmContractConvert.INSTANCE.convertPage(pageResult, userMap, customerList);
 | 
	
		
			
				|  |  | +        // 3. 获取联系人
 | 
	
		
			
				|  |  | +        Map<Long, CrmContactDO> contactMap = convertMap(contactService.getContactListByIds(convertSet(contractList,
 | 
	
		
			
				|  |  | +                CrmContractDO::getContactId)), CrmContactDO::getId);
 | 
	
		
			
				|  |  | +        // 4. 获取商机
 | 
	
		
			
				|  |  | +        Map<Long, CrmBusinessDO> businessMap = convertMap(businessService.getBusinessList(convertSet(contractList,
 | 
	
		
			
				|  |  | +                CrmContractDO::getBusinessId)), CrmBusinessDO::getId);
 | 
	
		
			
				|  |  | +        return CrmContractConvert.INSTANCE.convertList(contractList, userMap, customerList, contactMap, businessMap);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @PutMapping("/transfer-contract")
 |