package cn.iocoder.yudao.module.crm.service.bidbond; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.module.crm.controller.admin.bidbond.vo.CrmBidBondSaveReqVO; import cn.iocoder.yudao.module.crm.dal.dataobject.bidbond.CrmBidBondDO; 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.mysql.bidbond.CrmBidBondMapper; import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper; import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Proxy; import java.math.BigDecimal; import java.util.concurrent.atomic.AtomicReference; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_ALREADY_BOUND; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_CUSTOMER_MISMATCH; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_STATUS_INVALID; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_WON_QUOTATION_REQUIRED; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; class CrmBidBondQuotationBindingTest { private final CrmBidBondServiceImpl service = new CrmBidBondServiceImpl(); private final AtomicReference inserted = new AtomicReference<>(); private final AtomicReference bound = new AtomicReference<>(); @BeforeEach void setUp() { CrmCustomerService customerService = stub(CrmCustomerService.class, (proxy, method, args) -> { if ("getCustomer".equals(method.getName())) { return new CrmCustomerDO().setId((Long) args[0]).setName("客户1"); } throw new AssertionError(method.getName()); }); CrmBidBondMapper bondMapper = stub(CrmBidBondMapper.class, (proxy, method, args) -> switch (method.getName()) { case "selectByNo" -> null; case "selectById" -> new CrmBidBondDO().setId((Long) args[0]).setNo("BZ-EXIST"); case "insert" -> { CrmBidBondDO bond = (CrmBidBondDO) args[0]; bond.setId(100L); inserted.set(bond); yield 1; } default -> throw new AssertionError(method.getName()); }); ReflectionTestUtils.setField(service, "customerService", customerService); ReflectionTestUtils.setField(service, "bidBondMapper", bondMapper); ReflectionTestUtils.setField(service, "bidProgressMapper", stub(cn.iocoder.yudao.module.crm.dal.mysql.bidbond.CrmBidProgressMapper.class, (proxy, method, args) -> { if ("insert".equals(method.getName())) return 1; throw new AssertionError(method.getName()); })); } private void bindQuotation(CrmSaleQuotationDO quotation) { CrmSaleQuotationMapper quoteMapper = stub(CrmSaleQuotationMapper.class, (proxy, method, args) -> switch (method.getName()) { case "selectById" -> quotation; case "bindBidBond" -> { bound.set(args); yield null; } default -> throw new AssertionError(method.getName()); }); ReflectionTestUtils.setField(service, "saleQuotationMapper", quoteMapper); } @Test void customerMismatchRejected() { bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(2L).setStatus(2)); CrmBidBondSaveReqVO req = baseReq(); req.setQuotationId(5L); ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req)); assertEquals(BID_BOND_QUOTATION_CUSTOMER_MISMATCH.getCode(), ex.getCode()); } @Test void voidQuotationCannotBeBound() { bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(5)); CrmBidBondSaveReqVO req = baseReq(); req.setQuotationId(5L); ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req)); assertEquals(BID_BOND_QUOTATION_STATUS_INVALID.getCode(), ex.getCode()); } @Test void quotationAlreadyBoundRejected() { bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2) .setBidBondId(8L)); CrmBidBondSaveReqVO req = baseReq(); req.setQuotationId(5L); ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req)); assertEquals(BID_BOND_QUOTATION_ALREADY_BOUND.getCode(), ex.getCode()); } @Test void createCannotSetResultDirectly() { bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2)); CrmBidBondSaveReqVO req = baseReq(); req.setBidResult(1); ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req)); assertEquals(400, ex.getCode()); } @Test void createBindsQuotationBack() { bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2) .setRevisionNo(2)); CrmBidBondSaveReqVO req = baseReq(); req.setQuotationId(5L); service.createBidBond(req); assertNotNull(inserted.get()); assertEquals("BJ5", inserted.get().getQuotationNo()); assertEquals(2, inserted.get().getQuotationRound()); assertNotNull(bound.get()); assertEquals(5L, bound.get()[0]); assertEquals(100L, bound.get()[1]); assertEquals(inserted.get().getNo(), bound.get()[2]); } private CrmBidBondSaveReqVO baseReq() { CrmBidBondSaveReqVO req = new CrmBidBondSaveReqVO(); req.setCustomerId(1L); req.setProjectName("某某项目"); req.setBondAmount(new BigDecimal("1000")); req.setBidResult(0); return req; } private static T stub(Class type, java.lang.reflect.InvocationHandler handler) { return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler)); } }