| | |
| | | import cn.hutool.core.lang.Assert; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.number.MoneyUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| | | 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.CrmContractSaveReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractTransferReqVO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractConfigDO; |
| | | 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.quotation.CrmSaleQuotationDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationProductDO; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractProductMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.redis.no.CrmNoRedisDAO; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum; |
| | |
| | | import cn.iocoder.yudao.module.crm.framework.permission.core.annotations.CrmPermission; |
| | | import cn.iocoder.yudao.module.crm.service.business.CrmBusinessService; |
| | | import cn.iocoder.yudao.module.crm.service.contact.CrmContactService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; |
| | | import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO; |
| | | import cn.iocoder.yudao.module.crm.service.quotation.CrmSaleQuotationService; |
| | | import cn.iocoder.yudao.module.crm.service.receivable.CrmReceivableService; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderCreateReqDTO; |
| | | import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderRespDTO; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mdm.enums.MdmItemTypeEnum; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | |
| | | */ |
| | | private static final String CONTRACT_APPROVE_BIZ_TYPE = "contract_approve"; |
| | | |
| | | /** |
| | | * CRM 合同审批的 BPM 流程定义 Key |
| | | */ |
| | | private static final String CONTRACT_APPROVE_PROCESS_KEY = "contract_approve"; |
| | | |
| | | @Resource |
| | | private CrmContractMapper contractMapper; |
| | | @Resource |
| | | private CrmContractProductMapper contractProductMapper; |
| | | @Resource |
| | | private CrmCustomerMapper customerMapper; |
| | | |
| | | @Resource |
| | | private CrmNoRedisDAO noRedisDAO; |
| | |
| | | private CrmPermissionService crmPermissionService; |
| | | @Resource |
| | | private CrmCustomerService customerService; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | @Resource |
| | | private CrmBusinessService businessService; |
| | | @Resource |
| | |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | @Resource |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | @Resource |
| | |
| | | crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setUserId(contract.getOwnerUserId()) |
| | | .setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId()) |
| | | .setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | // 3.1 客户负责人自动拥有该合同的读写权限 |
| | | crmPermissionService.createPermissionForCustomerOwner(CrmBizTypeEnum.CRM_CONTRACT.getType(), |
| | | contract.getId(), contract.getCustomerId(), contract.getOwnerUserId()); |
| | | |
| | | // 4. 记录操作日志上下文 |
| | | LogRecordContext.putVariable("contract", contract); |
| | |
| | | updateReqVO.setOwnerUserId(null); // 不允许更新的字段 |
| | | // 1.1 校验存在 |
| | | CrmContractDO oldContract = validateContractExists(updateReqVO.getId()); |
| | | // 1.2 只有草稿、审批中,可以编辑; |
| | | // 1.2 只有草稿、审核不通过,可以编辑; |
| | | if (!ObjectUtils.equalsAny(oldContract.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus(), |
| | | CrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | CrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(CONTRACT_UPDATE_FAIL_NOT_DRAFT); |
| | | } |
| | | // 1.3 校验产品项的有效性 |
| | |
| | | public void deleteContract(Long id) { |
| | | // 1.1 校验存在 |
| | | CrmContractDO contract = validateContractExists(id); |
| | | // 1.2 如果被 CrmReceivableDO 所使用,则不允许删除 |
| | | // 1.2 只有草稿、审核不通过,可以删除; |
| | | if (!ObjectUtils.equalsAny(contract.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus(), |
| | | CrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(CONTRACT_DELETE_FAIL_NOT_EDITABLE); |
| | | } |
| | | // 1.3 如果被 CrmReceivableDO 所使用,则不允许删除 |
| | | if (receivableService.getReceivableCountByContractId(contract.getId()) > 0) { |
| | | throw exception(CONTRACT_DELETE_FAIL); |
| | | } |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @LogRecord(type = CRM_CONTRACT_TYPE, subType = CRM_CONTRACT_SUBMIT_SUB_TYPE, bizNo = "{{#id}}", |
| | | success = CRM_CONTRACT_SUBMIT_SUCCESS) |
| | | @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#id", level = CrmPermissionLevelEnum.WRITE) |
| | | public void submitContract(Long id) { |
| | | // 1. 校验存在 + 可提交状态(未提交 / 审核不通过) |
| | | CrmContractDO contract = validateContractExists(id); |
| | |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(CONTRACT_APPROVE_BIZ_TYPE); |
| | | |
| | | // 3. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因 |
| | | contractMapper.submitAndResetAuditInfo(id, CrmAuditStatusEnum.PROCESS.getStatus()); |
| | | if (contractMapper.submitAndResetAuditInfo(id, CrmAuditStatusEnum.PROCESS.getStatus()) != 1) { |
| | | throw exception(CONTRACT_SUBMIT_FAIL_NOT_EDITABLE); |
| | | } |
| | | |
| | | // 4. 记录日志 |
| | | // 4. 创建 BPM 流程实例,使审批出现在「协同办公 - 我的流程」 |
| | | bpmProcessInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(CONTRACT_APPROVE_PROCESS_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 5. 记录日志 |
| | | LogRecordContext.putVariable("contractName", contract.getName()); |
| | | } |
| | | |
| | |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(CONTRACT_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | contractMapper.auditContract(id, CrmAuditStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | if (contractMapper.auditContract(id, CrmAuditStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(CONTRACT_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | return; |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为审核通过 |
| | | contractMapper.auditContract(id, CrmAuditStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | if (contractMapper.auditContract(id, CrmAuditStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(CONTRACT_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateContractAuditStatus(Long id, Integer bpmStatus, String reason) { |
| | | // BPM 审批结果映射为合同审核状态:通过=20 / 不通过=30 / 取消=40 |
| | | Integer auditStatus; |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmStatus)) { |
| | | auditStatus = CrmAuditStatusEnum.APPROVE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmStatus)) { |
| | | auditStatus = CrmAuditStatusEnum.REJECT.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmStatus)) { |
| | | auditStatus = CrmAuditStatusEnum.CANCEL.getStatus(); |
| | | } else { |
| | | return; |
| | | } |
| | | if (contractMapper.updateAuditStatusByBpm(id, auditStatus, reason) != 1) { |
| | | log.warn("[updateContractAuditStatus] 合同({}) 不处于审批中状态,忽略 BPM 结果({})", id, bpmStatus); |
| | | } |
| | | } |
| | | |
| | | // ======================= 查询相关 ======================= |
| | |
| | | return PageResult.empty(); |
| | | } |
| | | } |
| | | // 2. 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedCustomerIds = customerApi.getPermittedCustomerIds(); |
| | | // 3. 查询分页 |
| | | return contractMapper.selectPage(pageReqVO, userId, config, permittedCustomerIds); |
| | | // 2. 查询分页 |
| | | // 合同可见性完全由 appendPermissionCondition 控制(合同团队 ∪ 客户团队), |
| | | // 不再额外按「有权限的客户」过滤,否则不在客户团队、但属于合同负责人/合同团队的用户会被错误过滤掉 |
| | | return contractMapper.selectPage(pageReqVO, userId, config); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (config == null || Boolean.FALSE.equals(config.getNotifyEnabled())) { |
| | | return 0L; |
| | | } |
| | | // 提醒天数未配置时按默认 7 天处理,避免 config.getNotifyDays() 为 null 时拆箱抛 NPE 导致接口 500 |
| | | if (config.getNotifyDays() == null) { |
| | | config = BeanUtils.toBean(config, CrmContractConfigDO.class); |
| | | config.setNotifyDays(DEFAULT_NOTIFY_DAYS); |
| | | } |
| | | return contractMapper.selectCountByRemind(userId, config); |
| | | } |
| | | |
| | | /** |
| | | * 合同到期提醒的默认提前天数:合同提醒未配置 notifyDays 时使用 |
| | | */ |
| | | private static final Integer DEFAULT_NOTIFY_DAYS = 7; |
| | | |
| | | @Override |
| | | public List<CrmContractDO> getContractListByCustomerIdOwnerUserId(Long customerId, Long ownerUserId) { |
| | |
| | | @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#id", level = CrmPermissionLevelEnum.WRITE) |
| | | public Long generateSaleOrder(Long id, Long userId) { |
| | | // 1. 校验合同存在且已审批通过 |
| | | CrmContractDO contract = validateContractExists(id); |
| | | CrmContractDO contract = contractMapper.selectByIdForUpdate(id); |
| | | if (contract == null) throw exception(CONTRACT_NOT_EXISTS); |
| | | if (!CrmAuditStatusEnum.APPROVE.getStatus().equals(contract.getAuditStatus())) { |
| | | throw exception(CONTRACT_GENERATE_ORDER_FAIL_NOT_APPROVE); |
| | | } |
| | |
| | | createReqDTO.setRemark("由合同【" + contract.getNo() + "】自动生成"); |
| | | |
| | | // 5. 构建订单项 |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap( |
| | | convertSet(contractProducts, CrmContractProductDO::getItemId)); |
| | | List<ErpSaleOrderCreateReqDTO.Item> items = contractProducts.stream() |
| | | .map(cp -> { |
| | | ErpSaleOrderCreateReqDTO.Item item = new ErpSaleOrderCreateReqDTO.Item(); |
| | | item.setProductId(cp.getItemId()); |
| | | item.setProductPrice(cp.getContractPrice()); |
| | | item.setCount(cp.getCount()); |
| | | // 只有产品才进入生产计划;温控等外购物料可随合同进入销售订单, |
| | | // 但不能创建 MPS,否则会导致整笔销售订单审批回滚。 |
| | | MdmItemRespDTO mdmItem = itemMap.get(cp.getItemId()); |
| | | item.setNeedProduction(mdmItem != null && MdmItemTypeEnum.isProduct(mdmItem.getItemType()) ? 1 : 0); |
| | | return item; |
| | | }) |
| | | .toList(); |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public CrmContractImportRespVO importContractList(List<CrmContractImportExcelVO> rows, CrmContractImportReqVO req) { |
| | | if (CollUtil.isEmpty(rows)) throw exception(CONTRACT_IMPORT_LIST_IS_EMPTY); |
| | | Long ownerId = req.getOwnerUserId() != null ? req.getOwnerUserId() : getLoginUserId(); |
| | | adminUserApi.validateUser(ownerId); |
| | | CrmContractImportRespVO result = CrmContractImportRespVO.builder() |
| | | .createContractNames(new ArrayList<>()).updateContractNames(new ArrayList<>()) |
| | | .failureContractNames(new LinkedHashMap<>()).build(); |
| | | Map<String, List<CrmContractImportExcelVO>> groups = new LinkedHashMap<>(); |
| | | int rowNo = 0; |
| | | for (CrmContractImportExcelVO row : rows) { |
| | | rowNo++; |
| | | if (row == null) continue; |
| | | if (StrUtil.isBlank(row.getName()) && StrUtil.isBlank(row.getCustomerName()) && StrUtil.isBlank(row.getProductCode())) continue; |
| | | if (StrUtil.isBlank(row.getName()) || StrUtil.isBlank(row.getCustomerName())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "第" + rowNo + "行合同名称和客户名称不能为空"); |
| | | } |
| | | String key = row.getCustomerName().trim() + " / " + row.getName().trim(); |
| | | groups.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row); |
| | | } |
| | | if (groups.isEmpty() && result.getFailureContractNames().isEmpty()) throw exception(CONTRACT_IMPORT_LIST_IS_EMPTY); |
| | | groups.forEach((key, group) -> { |
| | | try { |
| | | boolean created = getSelf().importContractGroup(group, ownerId, Boolean.TRUE.equals(req.getUpdateSupport())); |
| | | (created ? result.getCreateContractNames() : result.getUpdateContractNames()).add(key); |
| | | } catch (ServiceException ex) { |
| | | result.getFailureContractNames().put(key, ex.getMessage()); |
| | | } |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | /** 一张合同及全部明细在同一事务中保存。 */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean importContractGroup(List<CrmContractImportExcelVO> rows, Long ownerId, boolean updateSupport) { |
| | | CrmContractImportExcelVO first = rows.get(0); |
| | | CrmCustomerDO customer = customerMapper.selectByCustomerName(first.getCustomerName().trim()); |
| | | if (customer == null) throw exception(CONTRACT_IMPORT_CUSTOMER_NOT_EXISTS, first.getCustomerName()); |
| | | CrmContractDO existing = contractMapper.selectByNameAndCustomerId(first.getName().trim(), customer.getId()); |
| | | if (existing != null && !updateSupport) throw exception(CONTRACT_NAME_EXISTS, first.getName()); |
| | | if (existing != null && !ObjectUtils.equalsAny(existing.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus(), CrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(CONTRACT_UPDATE_FAIL_NOT_DRAFT); |
| | | } |
| | | if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "折扣必须在 0 到 100 之间"); |
| | | } |
| | | if (first.getStartTime() != null && first.getEndTime() != null && first.getEndTime().isBefore(first.getStartTime())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "结束时间不能早于开始时间"); |
| | | } |
| | | List<CrmContractSaveReqVO.Product> products = new ArrayList<>(); |
| | | for (CrmContractImportExcelVO row : rows) { |
| | | if (row.getOrderDate() != null && !row.getOrderDate().equals(first.getOrderDate()) |
| | | || row.getDiscountPercent() != null && (first.getDiscountPercent() == null || row.getDiscountPercent().compareTo(first.getDiscountPercent()) != 0)) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "同一合同的下单日期和折扣必须一致"); |
| | | } |
| | | if (row.getStartTime() != null && !row.getStartTime().equals(first.getStartTime()) |
| | | || row.getEndTime() != null && !row.getEndTime().equals(first.getEndTime()) |
| | | || row.getTotalPrice() != null && (first.getTotalPrice() == null || row.getTotalPrice().compareTo(first.getTotalPrice()) != 0) |
| | | || row.getDepositPrice() != null && (first.getDepositPrice() == null || row.getDepositPrice().compareTo(first.getDepositPrice()) != 0) |
| | | || StrUtil.isNotBlank(row.getContractType()) && !StrUtil.equals(row.getContractType(), first.getContractType()) |
| | | || StrUtil.isNotBlank(row.getVoltageLevel()) && !StrUtil.equals(row.getVoltageLevel(), first.getVoltageLevel()) |
| | | || StrUtil.isNotBlank(row.getElectricityType()) && !StrUtil.equals(row.getElectricityType(), first.getElectricityType()) |
| | | || row.getContractCapacity() != null && (first.getContractCapacity() == null || row.getContractCapacity().compareTo(first.getContractCapacity()) != 0) |
| | | || StrUtil.isNotBlank(row.getTariffMode()) && !StrUtil.equals(row.getTariffMode(), first.getTariffMode()) |
| | | || StrUtil.isNotBlank(row.getMeterMode()) && !StrUtil.equals(row.getMeterMode(), first.getMeterMode()) |
| | | || row.getPayCycle() != null && !row.getPayCycle().equals(first.getPayCycle()) |
| | | || StrUtil.isNotBlank(row.getRemark()) && !StrUtil.equals(row.getRemark(), first.getRemark())) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "同一合同的主表信息必须一致"); |
| | | } |
| | | boolean hasDetail = StrUtil.isNotBlank(row.getProductCode()) || row.getProductPrice() != null || row.getProductCount() != null; |
| | | if (!hasDetail) continue; |
| | | if (StrUtil.isBlank(row.getProductCode()) || row.getProductPrice() == null || row.getProductPrice().signum() < 0 || row.getProductCount() == null || row.getProductCount() <= 0) { |
| | | throw exception(CONTRACT_IMPORT_INVALID, "物料编码、非负合同单价和大于零的整数数量必须同时填写"); |
| | | } |
| | | MdmItemRespDTO item = mdmItemApi.getItemByCode(row.getProductCode().trim()).getCheckedData(); |
| | | if (item == null) throw exception(CONTRACT_IMPORT_INVALID, "物料不存在:" + row.getProductCode()); |
| | | CrmContractSaveReqVO.Product product = new CrmContractSaveReqVO.Product(); |
| | | product.setItemId(item.getId()); |
| | | product.setItemPrice(item.getSalesPrice()); |
| | | product.setContractPrice(row.getProductPrice()); |
| | | product.setCount(row.getProductCount()); |
| | | products.add(product); |
| | | } |
| | | if (!products.isEmpty() && products.size() != rows.size()) throw exception(CONTRACT_IMPORT_INVALID, "同一合同的每一行都必须填写产品明细"); |
| | | CrmContractDO contract = BeanUtils.toBean(first, CrmContractDO.class).setCustomerId(customer.getId()); |
| | | contract.setName(first.getName().trim()); |
| | | if (existing == null) { |
| | | String no = noRedisDAO.generate(CrmNoRedisDAO.CONTRACT_NO_PREFIX); |
| | | if (contractMapper.selectByNo(no) != null) throw exception(CONTRACT_NO_EXISTS); |
| | | contract.setNo(no).setOwnerUserId(ownerId).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); |
| | | if (contract.getOrderDate() == null) contract.setOrderDate(LocalDateTime.now()); |
| | | if (contract.getDiscountPercent() == null) contract.setDiscountPercent(BigDecimal.ZERO); |
| | | if (!products.isEmpty() || contract.getTotalPrice() == null) calculateTotalPrice(contract, validateContractProducts(products)); |
| | | contractMapper.insert(contract); |
| | | crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId()).setUserId(ownerId).setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | // 客户负责人自动拥有该合同的读写权限 |
| | | crmPermissionService.createPermissionForCustomerOwner(CrmBizTypeEnum.CRM_CONTRACT.getType(), |
| | | contract.getId(), customer.getId(), ownerId); |
| | | } else { |
| | | contract.setId(existing.getId()); |
| | | List<CrmContractProductDO> amountDetails = products.isEmpty() |
| | | ? contractProductMapper.selectListByContractId(existing.getId()) : validateContractProducts(products); |
| | | if (CollUtil.isNotEmpty(amountDetails)) { |
| | | if (contract.getDiscountPercent() == null) contract.setDiscountPercent( |
| | | existing.getDiscountPercent() == null ? BigDecimal.ZERO : existing.getDiscountPercent()); |
| | | calculateTotalPrice(contract, amountDetails); |
| | | } |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if (!products.isEmpty()) { |
| | | List<CrmContractProductDO> details = validateContractProducts(products); |
| | | if (existing != null) contractProductMapper.delete(CrmContractProductDO::getContractId, existing.getId()); |
| | | details.forEach(detail -> detail.setContractId(contract.getId())); |
| | | contractProductMapper.insertBatch(details); |
| | | } |
| | | getSelf().importContractLog(contract, existing != null); |
| | | return existing == null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 记录导入合同时的操作日志 |
| | | * |
| | | * @param contract 合同信息 |
| | | * @param isUpdate 是否更新;true - 更新,false - 新增 |
| | | */ |
| | | @LogRecord(type = CRM_CONTRACT_TYPE, subType = CRM_CONTRACT_IMPORT_SUB_TYPE, bizNo = "{{#contract.id}}", |
| | | success = CRM_CONTRACT_IMPORT_SUCCESS) |
| | | public void importContractLog(CrmContractDO contract, boolean isUpdate) { |
| | | LogRecordContext.putVariable("contract", contract); |
| | | LogRecordContext.putVariable("isUpdate", isUpdate); |
| | | } |
| | | |
| | | /** |
| | | * 获得自身的代理对象,解决 AOP 生效问题 |
| | | * |
| | | * @return 自己 |
| | | */ |
| | | private CrmContractServiceImpl getSelf() { |
| | | return SpringUtil.getBean(getClass()); |
| | | } |
| | | |
| | | } |