|
@@ -11,13 +11,12 @@ import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
|
import cn.iocoder.yudao.module.bpm.api.listener.dto.BpmResultListenerRespDTO;
|
|
|
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.BpmProcessInstanceResultEnum;
|
|
|
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.CrmContractPageReqVO;
|
|
|
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.CrmContractSaveReqVO;
|
|
|
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.CrmContractTransferReqVO;
|
|
|
-import cn.iocoder.yudao.module.crm.convert.contract.CrmContractConvert;
|
|
|
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.product.CrmProductDO;
|
|
|
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.enums.common.CrmAuditStatusEnum;
|
|
@@ -25,11 +24,11 @@ import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
|
|
|
import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum;
|
|
|
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.business.bo.CrmBusinessUpdateProductReqBO;
|
|
|
+import cn.iocoder.yudao.module.crm.service.contact.CrmContactService;
|
|
|
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
|
|
|
-import cn.iocoder.yudao.module.crm.service.followup.bo.CrmUpdateFollowUpReqBO;
|
|
|
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.product.CrmProductService;
|
|
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
|
|
import com.mzt.logapi.context.LogRecordContext;
|
|
@@ -40,17 +39,15 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
|
|
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;
|
|
|
import static cn.iocoder.yudao.module.crm.enums.LogRecordConstants.*;
|
|
|
-import static cn.iocoder.yudao.module.crm.util.CrmAuditStatusUtils.convertAuditStatus;
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* CRM 合同 Service 实现类
|
|
@@ -79,6 +76,8 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
private CrmCustomerService customerService;
|
|
|
@Resource
|
|
|
private CrmBusinessService businessService;
|
|
|
+ @Resource
|
|
|
+ private CrmContactService contactService;
|
|
|
|
|
|
@Resource
|
|
|
private AdminUserApi adminUserApi;
|
|
@@ -90,30 +89,29 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
@LogRecord(type = CRM_CONTRACT_TYPE, subType = CRM_CONTRACT_CREATE_SUB_TYPE, bizNo = "{{#contract.id}}",
|
|
|
success = CRM_CONTRACT_CREATE_SUCCESS)
|
|
|
public Long createContract(CrmContractSaveReqVO createReqVO, Long userId) {
|
|
|
+ // 1.1 校验产品项的有效性
|
|
|
+ List<CrmContractProductDO> contractProducts = validateContractProducts(createReqVO.getProducts());
|
|
|
+ // 1.2 校验关联字段
|
|
|
validateRelationDataExists(createReqVO);
|
|
|
- // 1.1 插入合同
|
|
|
- CrmContractDO contract = BeanUtils.toBean(createReqVO, CrmContractDO.class).setId(null);
|
|
|
+ // TODO 芋艿:生成 no
|
|
|
+
|
|
|
+ // 2.1 插入合同
|
|
|
+ CrmContractDO contract = BeanUtils.toBean(createReqVO, CrmContractDO.class);
|
|
|
+ contract.setNo(System.currentTimeMillis() + ""); // TODO
|
|
|
+ calculateTotalPrice(contract, contractProducts);
|
|
|
contractMapper.insert(contract);
|
|
|
- // 1.2 插入合同关联商品
|
|
|
- if (CollUtil.isNotEmpty(createReqVO.getProductItems())) { // 如果有的话
|
|
|
- List<CrmContractProductDO> productList = convertContractProductList(createReqVO, contract.getId());
|
|
|
- contractProductMapper.insertBatch(productList);
|
|
|
- // 更新合同商品总金额
|
|
|
- contractMapper.updateById(new CrmContractDO().setId(contract.getId()).setProductPrice(
|
|
|
- getSumValue(productList, CrmContractProductDO::getTotalPrice, Integer::sum)));
|
|
|
- // 如果存在合同关联了商机则更新商机商品关联
|
|
|
- if (contract.getBusinessId() != null) {
|
|
|
- businessService.updateBusinessProduct(new CrmBusinessUpdateProductReqBO().setId(contract.getBusinessId())
|
|
|
- .setItems(BeanUtils.toBean(createReqVO.getProductItems(), CrmBusinessUpdateProductReqBO.Item.class)));
|
|
|
- }
|
|
|
+ // 2.2 插入合同关联商品
|
|
|
+ if (CollUtil.isNotEmpty(contractProducts)) {
|
|
|
+ contractProducts.forEach(item -> item.setContractId(contract.getId()));
|
|
|
+ contractProductMapper.insertBatch(contractProducts);
|
|
|
}
|
|
|
|
|
|
- // 2. 创建数据权限
|
|
|
- crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setUserId(userId)
|
|
|
+ // 3. 创建数据权限
|
|
|
+ crmPermissionService.createPermission(new CrmPermissionCreateReqBO().setUserId(contract.getOwnerUserId())
|
|
|
.setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()).setBizId(contract.getId())
|
|
|
.setLevel(CrmPermissionLevelEnum.OWNER.getLevel()));
|
|
|
|
|
|
- // 3. 记录操作日志上下文
|
|
|
+ // 4. 记录操作日志上下文
|
|
|
LogRecordContext.putVariable("contract", contract);
|
|
|
return contract.getId();
|
|
|
}
|
|
@@ -125,6 +123,7 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
@CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#updateReqVO.id", level = CrmPermissionLevelEnum.WRITE)
|
|
|
public void updateContract(CrmContractSaveReqVO updateReqVO) {
|
|
|
Assert.notNull(updateReqVO.getId(), "合同编号不能为空");
|
|
|
+ updateReqVO.setOwnerUserId(null); // 不允许更新的字段
|
|
|
// 1.1 校验存在
|
|
|
CrmContractDO contract = validateContractExists(updateReqVO.getId());
|
|
|
// 1.2 只有草稿、审批中,可以编辑;
|
|
@@ -132,63 +131,41 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
CrmAuditStatusEnum.PROCESS.getStatus())) {
|
|
|
throw exception(CONTRACT_UPDATE_FAIL_EDITING_PROHIBITED);
|
|
|
}
|
|
|
+ // 1.3 校验产品项的有效性
|
|
|
+ List<CrmContractProductDO> contractProducts = validateContractProducts(updateReqVO.getProducts());
|
|
|
+ // 1.4 校验关联字段
|
|
|
validateRelationDataExists(updateReqVO);
|
|
|
|
|
|
// 2.1 更新合同
|
|
|
CrmContractDO updateObj = BeanUtils.toBean(updateReqVO, CrmContractDO.class);
|
|
|
+ calculateTotalPrice(updateObj, contractProducts);
|
|
|
contractMapper.updateById(updateObj);
|
|
|
// 2.2 更新合同关联商品
|
|
|
- updateContractProduct(updateReqVO, updateObj.getId());
|
|
|
+ updateContractProduct(updateReqVO.getId(), contractProducts);
|
|
|
|
|
|
// 3. 记录操作日志上下文
|
|
|
LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(contract, CrmContractSaveReqVO.class));
|
|
|
LogRecordContext.putVariable("contractName", contract.getName());
|
|
|
}
|
|
|
|
|
|
- private void updateContractProduct(CrmContractSaveReqVO updateReqVO, Long contractId) {
|
|
|
- if (CollUtil.isEmpty(updateReqVO.getProductItems())) {
|
|
|
- return;
|
|
|
- }
|
|
|
- List<CrmContractProductDO> newProductList = convertContractProductList(updateReqVO, contractId);
|
|
|
- List<CrmContractProductDO> oldProductList = contractProductMapper.selectListByContractId(contractId);
|
|
|
- List<List<CrmContractProductDO>> diffList = diffList(oldProductList, newProductList, (oldObj, newObj) -> {
|
|
|
- boolean match = ObjUtil.equal(oldObj.getProductId(), newObj.getProductId());
|
|
|
- if (match) {
|
|
|
- newObj.setId(oldObj.getId()); // 设置一下老的编号更新时需要使用
|
|
|
- }
|
|
|
- return match;
|
|
|
- });
|
|
|
+ private void updateContractProduct(Long id, List<CrmContractProductDO> newList) {
|
|
|
+ List<CrmContractProductDO> oldList = contractProductMapper.selectListByContractId(id);
|
|
|
+ List<List<CrmContractProductDO>> diffList = diffList(oldList, newList, // id 不同,就认为是不同的记录
|
|
|
+ (oldVal, newVal) -> oldVal.getId().equals(newVal.getId()));
|
|
|
if (CollUtil.isNotEmpty(diffList.get(0))) {
|
|
|
+ diffList.get(0).forEach(o -> o.setContractId(id));
|
|
|
contractProductMapper.insertBatch(diffList.get(0));
|
|
|
}
|
|
|
if (CollUtil.isNotEmpty(diffList.get(1))) {
|
|
|
contractProductMapper.updateBatch(diffList.get(1));
|
|
|
}
|
|
|
if (CollUtil.isNotEmpty(diffList.get(2))) {
|
|
|
- contractProductMapper.deleteBatchIds(convertList(diffList.get(2), CrmContractProductDO::getId));
|
|
|
+ contractProductMapper.deleteBatchIds(convertSet(diffList.get(2), CrmContractProductDO::getId));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// TODO @合同待定:缺一个取消合同的接口;只有草稿、审批中可以取消;CrmAuditStatusEnum
|
|
|
|
|
|
- private List<CrmContractProductDO> convertContractProductList(CrmContractSaveReqVO reqVO, Long contractId) {
|
|
|
- // 校验商品存在
|
|
|
- Set<Long> productIds = convertSet(reqVO.getProductItems(), CrmContractSaveReqVO.CrmContractProductItem::getId);
|
|
|
- List<CrmProductDO> productList = productService.getProductList(productIds);
|
|
|
- if (CollUtil.isEmpty(productIds) || productList.size() != productIds.size()) {
|
|
|
- throw exception(PRODUCT_NOT_EXISTS);
|
|
|
- }
|
|
|
- Map<Long, CrmProductDO> productMap = convertMap(productList, CrmProductDO::getId);
|
|
|
- return convertList(reqVO.getProductItems(), productItem -> {
|
|
|
- CrmProductDO product = productMap.get(productItem.getId());
|
|
|
- return BeanUtils.toBean(product, CrmContractProductDO.class)
|
|
|
- .setId(null).setProductId(productItem.getId()).setContractId(contractId)
|
|
|
- .setCount(productItem.getCount()).setDiscountPercent(productItem.getDiscountPercent())
|
|
|
- // TODO 芋艿:这里临时注释掉
|
|
|
- .setTotalPrice(MoneyUtils.calculator(null, productItem.getCount(), productItem.getDiscountPercent()));
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 校验关联数据是否存在
|
|
|
*
|
|
@@ -196,17 +173,38 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
*/
|
|
|
private void validateRelationDataExists(CrmContractSaveReqVO reqVO) {
|
|
|
// 1. 校验客户
|
|
|
- if (reqVO.getCustomerId() != null && customerService.getCustomer(reqVO.getCustomerId()) == null) {
|
|
|
- throw exception(CUSTOMER_NOT_EXISTS);
|
|
|
+ if (reqVO.getCustomerId() != null) {
|
|
|
+ customerService.validateCustomer(reqVO.getCustomerId());
|
|
|
}
|
|
|
// 2. 校验负责人
|
|
|
- if (reqVO.getOwnerUserId() != null && adminUserApi.getUser(reqVO.getOwnerUserId()) == null) {
|
|
|
- throw exception(USER_NOT_EXISTS);
|
|
|
+ if (reqVO.getOwnerUserId() != null) {
|
|
|
+ adminUserApi.validateUser(reqVO.getOwnerUserId());
|
|
|
}
|
|
|
// 3. 如果有关联商机,则需要校验存在
|
|
|
- if (reqVO.getBusinessId() != null && businessService.getBusiness(reqVO.getBusinessId()) == null) {
|
|
|
- throw exception(BUSINESS_NOT_EXISTS);
|
|
|
+ if (reqVO.getBusinessId() != null) {
|
|
|
+ businessService.validateBusiness(reqVO.getBusinessId());
|
|
|
}
|
|
|
+ // 4. 校验签约相关字段
|
|
|
+ if (reqVO.getSignContactId() != null) {
|
|
|
+ contactService.validateContact(reqVO.getSignContactId());
|
|
|
+ }
|
|
|
+ if (reqVO.getSignUserId() != null) {
|
|
|
+ adminUserApi.validateUser(reqVO.getSignUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<CrmContractProductDO> validateContractProducts(List<CrmContractSaveReqVO.Product> list) {
|
|
|
+ // 1. 校验产品存在
|
|
|
+ productService.validProductList(convertSet(list, CrmContractSaveReqVO.Product::getProductId));
|
|
|
+ // 2. 转化为 CrmContractProductDO 列表
|
|
|
+ return convertList(list, o -> BeanUtils.toBean(o, CrmContractProductDO.class,
|
|
|
+ item -> item.setTotalPrice(MoneyUtils.priceMultiply(item.getContractPrice(), item.getCount()))));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void calculateTotalPrice(CrmContractDO contract, List<CrmContractProductDO> contractProducts) {
|
|
|
+ contract.setTotalProductPrice(getSumValue(contractProducts, CrmContractProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO));
|
|
|
+ BigDecimal discountPrice = MoneyUtils.priceMultiplyPercent(contract.getTotalProductPrice(), contract.getDiscountPercent());
|
|
|
+ contract.setTotalPrice(contract.getTotalProductPrice().subtract(discountPrice));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -245,8 +243,8 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
CrmContractDO contract = validateContractExists(reqVO.getId());
|
|
|
|
|
|
// 2.1 数据权限转移
|
|
|
- crmPermissionService.transferPermission(
|
|
|
- CrmContractConvert.INSTANCE.convert(reqVO, userId).setBizType(CrmBizTypeEnum.CRM_CONTRACT.getType()));
|
|
|
+ crmPermissionService.transferPermission(new CrmPermissionTransferReqBO(userId, CrmBizTypeEnum.CRM_CONTRACT.getType(),
|
|
|
+ reqVO.getId(), reqVO.getNewOwnerUserId(), reqVO.getOldOwnerPermissionLevel()));
|
|
|
// 2.2 设置负责人
|
|
|
contractMapper.updateOwnerUserIdById(reqVO.getId(), reqVO.getNewOwnerUserId());
|
|
|
|
|
@@ -255,8 +253,18 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateContractFollowUp(CrmUpdateFollowUpReqBO contractUpdateFollowUpReqBO) {
|
|
|
- contractMapper.updateById(BeanUtils.toBean(contractUpdateFollowUpReqBO, CrmContractDO.class).setId(contractUpdateFollowUpReqBO.getBizId()));
|
|
|
+ @LogRecord(type = CRM_CONTRACT_TYPE, subType = CRM_CONTRACT_FOLLOW_UP_SUB_TYPE, bizNo = "{{#id}",
|
|
|
+ success = CRM_CONTRACT_FOLLOW_UP_SUCCESS)
|
|
|
+ @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTRACT, bizId = "#id", level = CrmPermissionLevelEnum.WRITE)
|
|
|
+ public void updateContractFollowUp(Long id, LocalDateTime contactNextTime, String contactLastContent) {
|
|
|
+ // 1. 校验存在
|
|
|
+ CrmContractDO contract = validateContractExists(id);
|
|
|
+
|
|
|
+ // 2. 更新联系人的跟进信息
|
|
|
+ contractMapper.updateById(new CrmContractDO().setId(id).setContactLastTime(LocalDateTime.now()));
|
|
|
+
|
|
|
+ // 3. 记录操作日志上下文
|
|
|
+ LogRecordContext.putVariable("contractName", contract.getName());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -284,12 +292,36 @@ public class CrmContractServiceImpl implements CrmContractService {
|
|
|
|
|
|
@Override
|
|
|
public void updateContractAuditStatus(BpmResultListenerRespDTO event) {
|
|
|
- convertAuditStatus(event);
|
|
|
+ // 判断下状态是否符合预期
|
|
|
+ if (!isEndResult(event.getResult())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 状态转换
|
|
|
+ if (ObjUtil.equal(event.getResult(), BpmProcessInstanceResultEnum.APPROVE.getResult())) {
|
|
|
+ event.setResult(CrmAuditStatusEnum.APPROVE.getStatus());
|
|
|
+ }
|
|
|
+ if (ObjUtil.equal(event.getResult(), BpmProcessInstanceResultEnum.REJECT.getResult())) {
|
|
|
+ event.setResult(CrmAuditStatusEnum.REJECT.getStatus());
|
|
|
+ }
|
|
|
+ if (ObjUtil.equal(event.getResult(), BpmProcessInstanceResultEnum.CANCEL.getResult())) {
|
|
|
+ event.setResult(CrmAuditStatusEnum.CANCEL.getStatus());
|
|
|
+ }
|
|
|
// 更新合同状态
|
|
|
contractMapper.updateById(new CrmContractDO().setId(Long.parseLong(event.getBusinessKey()))
|
|
|
.setAuditStatus(event.getResult()));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 判断该结果是否处于 End 最终结果
|
|
|
+ *
|
|
|
+ * @param result 结果
|
|
|
+ * @return 是否
|
|
|
+ */
|
|
|
+ public static boolean isEndResult(Integer result) {
|
|
|
+ return ObjectUtils.equalsAny(result, BpmProcessInstanceResultEnum.APPROVE.getResult(),
|
|
|
+ BpmProcessInstanceResultEnum.REJECT.getResult(), BpmProcessInstanceResultEnum.CANCEL.getResult());
|
|
|
+ }
|
|
|
+
|
|
|
//======================= 查询相关 =======================
|
|
|
|
|
|
@Override
|