| | |
| | | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractAuditReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractImportExcelVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractImportReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractImportRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractPageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractRespVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractProductDO; |
| | | 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.enums.common.CrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | 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.CrmContractCommandService; |
| | | 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.invoice.CrmInvoiceService; |
| | | 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 java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Stream; |
| | | |
| | | import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; |
| | | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; |
| | | import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | |
| | | @Resource |
| | | private CrmBusinessService businessService; |
| | | @Resource |
| | | private CrmProductService productService; |
| | | @Resource |
| | | private CrmReceivableService receivableService; |
| | | @Resource |
| | | private CrmInvoiceService invoiceService; |
| | | @Resource |
| | | private CrmContractCommandService contractCommandService; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | @GetMapping("/get") |
| | | @Operation(summary = "获得合同") |
| | | @Parameter(name = "id", description = "编号", required = true, example = "1024") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<CrmContractRespVO> getContract(@RequestParam("id") Long id) { |
| | | CrmContractDO contract = contractService.getContract(id); |
| | | return success(buildContractDetail(contract)); |
| | |
| | | CrmContractRespVO contractVO = buildContractDetailList(singletonList(contract)).get(0); |
| | | // 拼接产品项 |
| | | List<CrmContractProductDO> businessProducts = contractService.getContractProductListByContractId(contractVO.getId()); |
| | | Map<Long, CrmProductDO> productMap = productService.getProductMap( |
| | | convertSet(businessProducts, CrmContractProductDO::getProductId)); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap( |
| | | convertSet(businessProducts, CrmContractProductDO::getItemId)); |
| | | contractVO.setProducts(BeanUtils.toBean(businessProducts, CrmContractRespVO.Product.class, businessProductVO -> |
| | | MapUtils.findAndThen(productMap, businessProductVO.getProductId(), |
| | | product -> businessProductVO.setProductName(product.getName()) |
| | | .setProductNo(product.getNo()).setProductUnit(product.getUnit())))); |
| | | MapUtils.findAndThen(itemMap, businessProductVO.getItemId(), |
| | | item -> businessProductVO.setItemCode(item.getCode()) |
| | | .setItemName(item.getName()).setItemBarCode(item.getBarCode())))); |
| | | return contractVO; |
| | | } |
| | | |
| | | @GetMapping("/page") |
| | | @Operation(summary = "获得合同分页") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<PageResult<CrmContractRespVO>> getContractPage(@Valid CrmContractPageReqVO pageVO) { |
| | | PageResult<CrmContractDO> pageResult = contractService.getContractPage(pageVO, getLoginUserId()); |
| | | return success(BeanUtils.toBean(pageResult, CrmContractRespVO.class).setList(buildContractDetailList(pageResult.getList()))); |
| | |
| | | @GetMapping("/page-by-customer") |
| | | @Operation(summary = "获得合同分页,基于指定客户") |
| | | public CommonResult<PageResult<CrmContractRespVO>> getContractPageByCustomer(@Valid CrmContractPageReqVO pageVO) { |
| | | Assert.notNull(pageVO.getCustomerId(), "客户编号不能为空"); |
| | | if (pageVO.getCustomerId() == null) { |
| | | throw exception0(BAD_REQUEST.getCode(), "客户编号不能为空"); |
| | | } |
| | | PageResult<CrmContractDO> pageResult = contractService.getContractPageByCustomerId(pageVO); |
| | | return success(BeanUtils.toBean(pageResult, CrmContractRespVO.class).setList(buildContractDetailList(pageResult.getList()))); |
| | | } |
| | |
| | | @GetMapping("/page-by-business") |
| | | @Operation(summary = "获得合同分页,基于指定商机") |
| | | public CommonResult<PageResult<CrmContractRespVO>> getContractPageByBusiness(@Valid CrmContractPageReqVO pageVO) { |
| | | Assert.notNull(pageVO.getBusinessId(), "商机编号不能为空"); |
| | | if (pageVO.getBusinessId() == null) { |
| | | throw exception0(BAD_REQUEST.getCode(), "商机编号不能为空"); |
| | | } |
| | | PageResult<CrmContractDO> pageResult = contractService.getContractPageByBusinessId(pageVO); |
| | | return success(BeanUtils.toBean(pageResult, CrmContractRespVO.class).setList(buildContractDetailList(pageResult.getList()))); |
| | | } |
| | |
| | | public void exportContractExcel(@Valid CrmContractPageReqVO exportReqVO, |
| | | HttpServletResponse response) throws IOException { |
| | | PageResult<CrmContractDO> pageResult = contractService.getContractPage(exportReqVO, getLoginUserId()); |
| | | List<CrmContractRespVO> list = BeanUtils.toBean(pageResult.getList(), CrmContractRespVO.class); |
| | | // 计算未回款金额、转换审批状态为中文 |
| | | list.forEach(vo -> { |
| | | if (vo.getTotalPrice() != null && vo.getTotalReceivablePrice() != null) { |
| | | vo.setUnReceivablePrice(vo.getTotalPrice().subtract(vo.getTotalReceivablePrice())); |
| | | } |
| | | if (vo.getAuditStatus() != null) { |
| | | vo.setAuditStatusName(CrmAuditStatusEnum.getNameByStatus(vo.getAuditStatus())); |
| | | } |
| | | }); |
| | | // 导出 Excel |
| | | ExcelUtils.write(response, "合同.xls", "数据", CrmContractRespVO.class, |
| | | BeanUtils.toBean(pageResult.getList(), CrmContractRespVO.class)); |
| | | ExcelUtils.write(response, "合同.xls", "数据", CrmContractRespVO.class, list); |
| | | } |
| | | |
| | | @GetMapping("/get-import-template") |
| | | @Operation(summary = "获得导入合同模板") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | // 手动创建导出 demo |
| | | List<CrmContractImportExcelVO> list = Arrays.asList( |
| | | CrmContractImportExcelVO.builder().name("示例合同").customerName("示例客户") |
| | | .orderDate(null).startTime(null).endTime(null).discountPercent(null) |
| | | .totalPrice(null).depositPrice(null).contractType(null).voltageLevel(null) |
| | | .electricityType(null).contractCapacity(null).tariffMode(null).meterMode(null) |
| | | .payCycle(null).remark("").build() |
| | | ); |
| | | // 输出 |
| | | ExcelUtils.writeTemplate(response, "合同导入模板.xls", "合同列表", CrmContractImportExcelVO.class, list); |
| | | } |
| | | |
| | | @PostMapping("/import") |
| | | @Operation(summary = "导入合同") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:import')") |
| | | public CommonResult<CrmContractImportRespVO> importExcel(@Valid CrmContractImportReqVO importReqVO) |
| | | throws Exception { |
| | | List<CrmContractImportExcelVO> list = ExcelUtils.read(importReqVO.getFile(), CrmContractImportExcelVO.class); |
| | | return success(contractService.importContractList(list, importReqVO)); |
| | | } |
| | | |
| | | @PutMapping("/transfer") |
| | |
| | | } |
| | | |
| | | @PutMapping("/submit") |
| | | @Operation(summary = "提交合同审批") |
| | | @Operation(summary = "提交合同审批(未提交/审核不通过→审批中),审批人由审批配置统一指定") |
| | | @Parameter(name = "id", description = "合同编号", required = true) |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:update')") |
| | | public CommonResult<Boolean> submitContract(@RequestParam("id") Long id) { |
| | | contractService.submitContract(id, getLoginUserId()); |
| | | contractService.submitContract(id); |
| | | return success(true); |
| | | } |
| | | |
| | | @PutMapping("/audit") |
| | | @Operation(summary = "审核合同(审批中→审核通过/审核不通过),仅审批人本人可操作") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:audit')") |
| | | public CommonResult<Boolean> auditContract(@Valid @RequestBody CrmContractAuditReqVO auditReqVO) { |
| | | contractService.auditContract(auditReqVO.getId(), auditReqVO.getPass(), auditReqVO.getReviewRemark()); |
| | | return success(true); |
| | | } |
| | | |
| | | @GetMapping("/approver-ids") |
| | | @Operation(summary = "获得合同的审批人编号集合,供前端判断是否展示审核按钮") |
| | | public CommonResult<Set<Long>> getApproverUserIds() { |
| | | return success(contractService.getContractApproverUserIds()); |
| | | } |
| | | |
| | | private List<CrmContractRespVO> buildContractDetailList(List<CrmContractDO> contractList) { |
| | |
| | | // 1.5 获得已回款金额 |
| | | Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId( |
| | | convertSet(contractList, CrmContractDO::getId)); |
| | | // 1.6 获得已开票金额(审批通过 + 审批中,与开票防超校验口径一致) |
| | | Map<Long, BigDecimal> invoicePriceMap = invoiceService.getInvoicePriceMapByContractId( |
| | | convertSet(contractList, CrmContractDO::getId)); |
| | | // 2. 拼接数据 |
| | | return BeanUtils.toBean(contractList, CrmContractRespVO.class, contractVO -> { |
| | | // 2.1 设置客户信息 |
| | |
| | | findAndThen(businessMap, contractVO.getBusinessId(), business -> contractVO.setBusinessName(business.getName())); |
| | | // 2.5 设置已回款金额 |
| | | contractVO.setTotalReceivablePrice(receivablePriceMap.getOrDefault(contractVO.getId(), BigDecimal.ZERO)); |
| | | // 2.6 设置已开票金额 |
| | | contractVO.setTotalInvoicePrice(invoicePriceMap.getOrDefault(contractVO.getId(), BigDecimal.ZERO)); |
| | | // 2.7 设置未开票金额 |
| | | if (contractVO.getTotalPrice() != null && contractVO.getTotalInvoicePrice() != null) { |
| | | contractVO.setUnInvoicePrice(contractVO.getTotalPrice().subtract(contractVO.getTotalInvoicePrice())); |
| | | } |
| | | // 2.8 设置指令下发数量 |
| | | contractVO.setCommandCount(contractCommandService.countByContractId(contractVO.getId())); |
| | | contractVO.setAuditStatusName(CrmAuditStatusEnum.getNameByStatus(contractVO.getAuditStatus())); |
| | | }); |
| | | } |
| | | |
| | | @GetMapping("/audit-count") |
| | | @Operation(summary = "获得待审核合同数量") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<Long> getAuditContractCount() { |
| | | return success(contractService.getAuditContractCount(getLoginUserId())); |
| | | } |
| | | |
| | | @GetMapping("/remind-count") |
| | | @Operation(summary = "获得即将到期(提醒)的合同数量") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<Long> getRemindContractCount() { |
| | | return success(contractService.getRemindContractCount(getLoginUserId())); |
| | | } |
| | |
| | | @GetMapping("/simple-list") |
| | | @Operation(summary = "获得合同精简列表", description = "只包含的合同,主要用于前端的下拉选项") |
| | | @Parameter(name = "customerId", description = "客户编号", required = true) |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<List<CrmContractRespVO>> getContractSimpleList(@RequestParam("customerId") Long customerId) { |
| | | CrmContractPageReqVO pageReqVO = new CrmContractPageReqVO().setCustomerId(customerId); |
| | | pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); // 不分页 |
| | |
| | | // 拼接数据 |
| | | Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId( |
| | | convertSet(pageResult.getList(), CrmContractDO::getId)); |
| | | Map<Long, BigDecimal> invoicePriceMap = invoiceService.getInvoicePriceMapByContractId( |
| | | convertSet(pageResult.getList(), CrmContractDO::getId)); |
| | | return success(convertList(pageResult.getList(), contract -> new CrmContractRespVO() // 只返回 id、name 等精简字段 |
| | | .setId(contract.getId()).setName(contract.getName()).setAuditStatus(contract.getAuditStatus()) |
| | | .setTotalPrice(contract.getTotalPrice()) |
| | | .setTotalReceivablePrice(receivablePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)))); |
| | | .setTotalReceivablePrice(receivablePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)) |
| | | .setTotalInvoicePrice(invoicePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)))); |
| | | } |
| | | |
| | | @PostMapping("/generate-sale-order") |