CRM:完善合同的待回款金额

This commit is contained in:
YunaiV 2024-02-25 20:50:37 +08:00
parent 33b056e6c4
commit 5fbb423621
5 changed files with 58 additions and 1 deletions

View File

@ -25,6 +25,7 @@ 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;
import cn.iocoder.yudao.module.crm.service.receivable.CrmReceivableService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
@ -40,6 +41,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -68,6 +70,8 @@ public class CrmContractController {
private CrmBusinessService businessService;
@Resource
private CrmProductService productService;
@Resource
private CrmReceivableService receivableService;
@Resource
private AdminUserApi adminUserApi;
@ -192,6 +196,9 @@ public class CrmContractController {
// 1.4 获取商机
Map<Long, CrmBusinessDO> businessMap = businessService.getBusinessMap(
convertSet(contractList, CrmContractDO::getBusinessId));
// 1.5 获得已回款金额
Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId(
convertSet(contractList, CrmContractDO::getId));
// 2. 拼接数据
return BeanUtils.toBean(contractList, CrmContractRespVO.class, contractVO -> {
// 2.1 设置客户信息
@ -207,6 +214,8 @@ public class CrmContractController {
findAndThen(contactMap, contractVO.getSignContactId(), contact -> contractVO.setSignContactName(contact.getName()));
// 2.4 设置商机信息
findAndThen(businessMap, contractVO.getBusinessId(), business -> contractVO.setBusinessName(business.getName()));
// 2.5 设置已回款金额
contractVO.setTotalReceivablePrice(receivablePriceMap.getOrDefault(contractVO.getId(), BigDecimal.ZERO));
});
}
@ -232,9 +241,16 @@ public class CrmContractController {
CrmContractPageReqVO pageReqVO = new CrmContractPageReqVO().setCustomerId(customerId);
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); // 不分页
PageResult<CrmContractDO> pageResult = contractService.getContractPageByCustomerId(pageReqVO);
if (CollUtil.isEmpty(pageResult.getList())) {
return success(Collections.emptyList());
}
// 拼接数据
Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId(
convertSet(pageResult.getList(), CrmContractDO::getId));
return success(convertList(pageResult.getList(), contract -> new CrmContractRespVO() // 只返回 idname 等精简字段
.setId(contract.getId()).setName(contract.getName()).setAuditStatus(contract.getAuditStatus())
.setTotalPrice(contract.getTotalPrice()))); // TODO @芋艿未回款金额
.setTotalPrice(contract.getTotalPrice())
.setTotalReceivablePrice(receivablePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO))));
}
}

View File

@ -88,6 +88,10 @@ public class CrmContractRespVO {
@ExcelProperty("合同金额")
private BigDecimal totalPrice;
@Schema(description = "已回款金额", example = "5617")
@ExcelProperty("已回款金额")
private BigDecimal totalReceivablePrice;
@Schema(description = "客户签约人编号", example = "18546")
private Long signContactId;
@Schema(description = "客户签约人", example = "小豆")

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.crm.dal.mysql.receivable;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@ -11,10 +12,16 @@ import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum;
import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/**
* 回款 Mapper
@ -71,4 +78,18 @@ public interface CrmReceivableMapper extends BaseMapperX<CrmReceivableDO> {
return selectCount(query);
}
default Map<Long, BigDecimal> selectReceivablePriceMapByContractId(Collection<Long> contractIds) {
if (CollUtil.isEmpty(contractIds)) {
return Collections.emptyMap();
}
// SQL sum 查询
List<Map<String, Object>> result = selectMaps(new QueryWrapper<CrmReceivableDO>()
.select("contract_id, SUM(price) AS total_price")
.eq("audit_status", CrmAuditStatusEnum.APPROVE.getStatus())
.groupBy("contract_id")
.in("contract_id", contractIds));
// 获得金额
return convertMap(result, obj -> (Long) obj.get("contract_id"), obj -> (BigDecimal) obj.get("total_price"));
}
}

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.receivable.CrmReceivableDO;
import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -113,4 +114,12 @@ public interface CrmReceivableService {
*/
Long getCheckReceivablesCount(Long userId);
/**
* 获得合同已回款金额 Map
*
* @param contractIds 合同编号
* @return 回款金额 Map
*/
Map<Long, BigDecimal> getReceivablePriceMapByContractId(Collection<Long> contractIds);
}

View File

@ -35,8 +35,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;
@ -258,4 +260,9 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
return receivableMapper.selectCheckReceivablesCount(userId);
}
@Override
public Map<Long, BigDecimal> getReceivablePriceMapByContractId(Collection<Long> contractIds) {
return receivableMapper.selectReceivablePriceMapByContractId(contractIds);
}
}