云
19 小时以前 a87fcc60346dfc799bd3c2cddc7acfffb105cb61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cn.iocoder.yudao.module.crm.service.quotation;
 
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.quotation.CrmSaleQuotationDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationProductDO;
import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationProductMapper;
import cn.iocoder.yudao.module.crm.service.contract.CrmContractService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.math.BigDecimal;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
 
class CrmSaleQuotationQuantityTest {
    @Test
    void contractRequestAcceptsFractionalQuantity() throws Exception {
        var product = new ObjectMapper().readValue("{\"itemId\":1103,\"count\":22304.8}",
                CrmContractSaveReqVO.Product.class);
        assertEquals(new BigDecimal("22304.8"), product.getCount());
    }
 
    @Test
    void conversionKeepsFractionalQuantity() {
        var service = new CrmSaleQuotationServiceImpl();
        var request = new AtomicReference<CrmContractSaveReqVO>();
        var quote = new CrmSaleQuotationDO().setId(1L).setNo("BJ-1").setName("台账报价").setStatus(0);
        var quotes = stub(CrmSaleQuotationMapper.class, (proxy, method, args) -> switch (method.getName()) {
            case "selectById" -> quote;
            case "updateById" -> 1;
            default -> throw new AssertionError(method.getName());
        });
        var products = stub(CrmSaleQuotationProductMapper.class, (proxy, method, args) -> {
            assertEquals("selectListByQuotationId", method.getName());
            return List.of(new CrmSaleQuotationProductDO().setItemId(1103L).setCount(new BigDecimal("22304.8")));
        });
        var contracts = stub(CrmContractService.class, (proxy, method, args) -> switch (method.getName()) {
            case "createContract" -> {
                assertEquals(291L, args[1]);
                request.set((CrmContractSaveReqVO) args[0]);
                yield 2L;
            }
            case "getContract" -> new CrmContractDO().setNo("HT-2");
            default -> throw new AssertionError(method.getName());
        });
        ReflectionTestUtils.setField(service, "saleQuotationMapper", quotes);
        ReflectionTestUtils.setField(service, "saleQuotationProductMapper", products);
        ReflectionTestUtils.setField(service, "contractService", contracts);
        assertEquals(2L, service.convertToContract(1L, 291L));
        assertEquals(new BigDecimal("22304.8"), request.get().getProducts().getFirst().getCount());
    }
 
    private static <T> T stub(Class<T> type, java.lang.reflect.InvocationHandler handler) {
        return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
    }
}