package cn.iocoder.yudao.module.mes.service.wm.productsales; import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesDetailMapper; import org.apache.ibatis.annotations.Select; import org.h2.jdbcx.JdbcDataSource; import org.junit.jupiter.api.*; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import java.util.*; import static org.junit.jupiter.api.Assertions.*; /** 执行生产 Mapper 中的实际占用查询 SQL。 */ class SalesPickingSqlTest { JdbcTemplate db;NamedParameterJdbcTemplate named; @BeforeEach void setup(){ var source=new JdbcDataSource();source.setURL("jdbc:h2:mem:"+UUID.randomUUID()+";MODE=MySQL;DB_CLOSE_DELAY=-1"); db=new JdbcTemplate(source);named=new NamedParameterJdbcTemplate(source); db.execute("CREATE TABLE mes_wm_product_sales(id BIGINT PRIMARY KEY,status INT,deleted INT DEFAULT 0)"); db.execute("CREATE TABLE mes_wm_product_sales_detail(id BIGINT PRIMARY KEY,sales_id BIGINT,bag_code_id BIGINT,pallet_code_id BIGINT,batch_code VARCHAR(64),deleted INT DEFAULT 0)"); db.update("INSERT INTO mes_wm_product_sales(id,status) VALUES(1,2),(2,10),(3,3),(4,4),(5,5)"); db.update("INSERT INTO mes_wm_product_sales_detail(id,sales_id,bag_code_id,pallet_code_id,batch_code) VALUES(1,1,31,200,'B1'),(2,2,32,200,'B1'),(3,3,NULL,201,'B1'),(4,4,34,202,'B1'),(5,5,35,203,'B1'),(6,1,36,204,'B2'),(7,1,37,205,'B1')"); db.update("UPDATE mes_wm_product_sales_detail SET deleted=1 WHERE id=7"); } List run(String method)throws Exception { String sql=String.join(" ",MesWmProductSalesDetailMapper.class.getMethod(method,String.class).getAnnotation(Select.class).value()).replace("#{batchCode}",":batchCode"); return named.query(sql,Map.of("batchCode","B1"),(rs,n)->rs.getLong("id")); } @Test void onlyLivePickingOrdersAndLegacyPalletsOccupyBags()throws Exception{assertEquals(List.of(1L,2L,3L),run("selectPickingByBatchCode"));} @Test void lockedCurrentReadHasSameScopeAndCanceledOrderReleasesBags()throws Exception{ assertEquals(List.of(1L,2L,3L),run("selectPickingByBatchCodeForUpdate"));db.update("UPDATE mes_wm_product_sales SET status=5 WHERE id=1"); assertEquals(List.of(2L,3L),run("selectPickingByBatchCodeForUpdate")); } }