Commit 9fe13f32 by wutong

自动周转修改分片分页,计算时不漏SKU.

parent e89eca99
...@@ -43,51 +43,20 @@ public class AutoTurnoverJob extends PointJob { ...@@ -43,51 +43,20 @@ public class AutoTurnoverJob extends PointJob {
@Override @Override
public void executeJob(ShardingContext shardingContext, JobPointLog jobPointLog) { public void executeJob(ShardingContext shardingContext, JobPointLog jobPointLog) {
long count = 0; long count = getTotalCount();
try {
DcBaseStockMapper baseStockMapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
count = baseStockMapper.countByExample(DcBaseStockExample.newAndCreateCriteria().example());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Mybatis操作DB查询总页数失败", e);
} finally {
SessionUtil.closeSession();
}
int totalPage = getTotalPage(count, jobPointLog.getPageSize()); int totalPage = getTotalPage(count, jobPointLog.getPageSize());
/*
switch (shardingContext.getShardingItem()) { * 根据不同分片
case 0: * 再分页去处理数据
if (shardingContext.getShardingItem() + 1 < shardingContext.getShardingTotalCount()) { */
totalPage = totalPage / shardingContext.getShardingTotalCount() * (shardingContext.getShardingItem() + 1); int shardingSize = totalPage % shardingContext.getShardingTotalCount() == 0? totalPage / shardingContext.getShardingTotalCount() : totalPage / shardingContext.getShardingTotalCount() + 1;
} if (shardingContext.getShardingItem() + 1 < shardingContext.getShardingTotalCount()) {
break; totalPage = shardingSize * (shardingContext.getShardingItem() + 1);
case 1:
if (shardingContext.getShardingItem() + 1 < shardingContext.getShardingTotalCount()) {
totalPage = (totalPage / shardingContext.getShardingTotalCount()) * (shardingContext.getShardingItem() + 1);
}
break;
case 2:
if (shardingContext.getShardingItem() + 1 < shardingContext.getShardingTotalCount()) {
totalPage = (totalPage / shardingContext.getShardingTotalCount()) * (shardingContext.getShardingItem() + 1);
}
break;
case 3:
break;
} }
if (totalPage > 0) { if (totalPage > 0) {
int page = 0; int page = 0;
switch (shardingContext.getShardingItem()) { if (shardingContext.getShardingItem() > 0) {
case 0: page = shardingSize * shardingContext.getShardingItem();
break;
case 1:
page = totalPage / shardingContext.getShardingTotalCount() * shardingContext.getShardingItem();
break;
case 2:
page = (totalPage / shardingContext.getShardingTotalCount()) * shardingContext.getShardingItem();
break;
case 3:
page = (totalPage / shardingContext.getShardingTotalCount()) * shardingContext.getShardingItem();
break;
} }
String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now()); String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
do { do {
...@@ -125,6 +94,17 @@ public class AutoTurnoverJob extends PointJob { ...@@ -125,6 +94,17 @@ public class AutoTurnoverJob extends PointJob {
} }
private long getTotalCount() {
try {
DcBaseStockMapper baseStockMapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
return baseStockMapper.countByExample(DcBaseStockExample.newAndCreateCriteria().example());
} catch (Exception e) {
throw new RuntimeException("Mybatis操作DB查询总页数失败", e);
} finally {
SessionUtil.closeSession();
}
}
/** /**
* 基于库存 * 基于库存
* 计算自动周转 * 计算自动周转
......
...@@ -12,12 +12,14 @@ import com.bailuntec.mapper.DcBaseStockMapper; ...@@ -12,12 +12,14 @@ import com.bailuntec.mapper.DcBaseStockMapper;
import com.bailuntec.utils.SessionUtil; import com.bailuntec.utils.SessionUtil;
import com.dangdang.ddframe.job.executor.ShardingContexts; import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@Slf4j
public class AutoTurnoverJobListener implements ElasticJobListener { public class AutoTurnoverJobListener implements ElasticJobListener {
@Override @Override
public void beforeJobExecuted(ShardingContexts shardingContexts) { public void beforeJobExecuted(ShardingContexts shardingContexts) {
...@@ -57,7 +59,7 @@ public class AutoTurnoverJobListener implements ElasticJobListener { ...@@ -57,7 +59,7 @@ public class AutoTurnoverJobListener implements ElasticJobListener {
try { try {
autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock); autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Mybatis操作自动周转异常数据再次计算失败SKU=" + dcAutoException.getBailunSku() + ",仓库编码=" + dcAutoException.getWarehouseCode(), e); log.error("Mybatis操作自动周转异常数据再次计算失败SKU=" + dcAutoException.getBailunSku() + ",仓库编码=" + dcAutoException.getWarehouseCode());
} }
mapper.deleteByPrimaryKey(dcAutoException.getId()); mapper.deleteByPrimaryKey(dcAutoException.getId());
} }
...@@ -65,7 +67,7 @@ public class AutoTurnoverJobListener implements ElasticJobListener { ...@@ -65,7 +67,7 @@ public class AutoTurnoverJobListener implements ElasticJobListener {
pageIndex++; pageIndex++;
} while (dcAutoExceptions != null && dcAutoExceptions.size() == pageSize); } while (dcAutoExceptions != null && dcAutoExceptions.size() == pageSize);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new RuntimeException("Mybatis操作自动周转异常数据再次计算失败", e); throw new RuntimeException("Mybatis操作自动周转异常数据再次计算失败");
} finally { } finally {
if (sqlSession != null) { if (sqlSession != null) {
sqlSession.close(); sqlSession.close();
......
...@@ -125,4 +125,7 @@ public interface DcBaseStockMapper { ...@@ -125,4 +125,7 @@ public interface DcBaseStockMapper {
List<DcBaseStock> listNoChinaWarehouse(@Param("v1") int v1, @Param("v2") int v2); List<DcBaseStock> listNoChinaWarehouse(@Param("v1") int v1, @Param("v2") int v2);
List<DcBaseStock> listOrderByFba(@Param("v1") int v1, @Param("v2") int v2); List<DcBaseStock> listOrderByFba(@Param("v1") int v1, @Param("v2") int v2);
List<DcBaseStock> listInStockNotInAutoTurnover();
} }
\ No newline at end of file
...@@ -1092,4 +1092,8 @@ ...@@ -1092,4 +1092,8 @@
<select id="listOrderByFba" resultType="com.bailuntec.domain.entity.DcBaseStock"> <select id="listOrderByFba" resultType="com.bailuntec.domain.entity.DcBaseStock">
SELECT * FROM dc_base_stock order by id asc limit #{v1}, #{v2} SELECT * FROM dc_base_stock order by id asc limit #{v1}, #{v2}
</select> </select>
<select id="listInStockNotInAutoTurnover" resultType="com.bailuntec.domain.entity.DcBaseStock">
SELECT tb1.* FROM dc_base_stock tb1 LEFT JOIN dc_auto_turnover tb2 ON tb1.bailun_sku = tb2.bailun_sku and tb1.warehouse_code = tb2.warehouse_code WHERE tb2.bailun_sku is null;
</select>
</mapper> </mapper>
\ No newline at end of file
SKUMS_PARENT_CATEGORIES_URL=http://10.0.6.13:8001/api/BailunCateData/GetMyParentCategories #SKUMS_PARENT_CATEGORIES_URL=http://10.0.6.13:8001/api/BailunCateData/GetMyParentCategories
#SKUMS_PARENT_CATEGORIES_URL=http://api.sku.bailuntec.com/api/BailunCateData/GetMyParentCategories SKUMS_PARENT_CATEGORIES_URL=http://api.sku.bailuntec.com/api/BailunCateData/GetMyParentCategories
TJ_OUT_STOCK_URL=http://tj.bailuntec.com/ApiService/GetNoStockForDataCenter TJ_OUT_STOCK_URL=http://tj.bailuntec.com/ApiService/GetNoStockForDataCenter
\ No newline at end of file
...@@ -50,7 +50,7 @@ public class AutoTurnoverTest { ...@@ -50,7 +50,7 @@ public class AutoTurnoverTest {
try { try {
DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class); DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
// dcBaseStock = mapper.selectOneByExample(DcBaseStockExample.newAndCreateCriteria().andBailunSkuEqualTo("322572421").andWarehouseCodeEqualTo("QYBLZZ").example()); // dcBaseStock = mapper.selectOneByExample(DcBaseStockExample.newAndCreateCriteria().andBailunSkuEqualTo("322572421").andWarehouseCodeEqualTo("QYBLZZ").example());
dcBaseStock = mapper.selectOneByExample(DcBaseStockExample.newAndCreateCriteria().andBailunSkuEqualTo("273129601").andWarehouseCodeEqualTo("GZBLWH").example()); dcBaseStock = mapper.selectOneByExample(DcBaseStockExample.newAndCreateCriteria().andBailunSkuEqualTo("278308409").andWarehouseCodeEqualTo("GZBLYS").example());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
...@@ -58,6 +58,24 @@ public class AutoTurnoverTest { ...@@ -58,6 +58,24 @@ public class AutoTurnoverTest {
} }
autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock); autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
} }
@Test
public void test22() throws Exception {
String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
List<DcBaseStock> dcBaseStockList = null;
try {
DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
dcBaseStockList = mapper.listInStockNotInAutoTurnover();
} catch (Exception e) {
e.printStackTrace();
} finally {
SessionUtil.closeSession();
}
for (DcBaseStock dcBaseStock : dcBaseStockList) {
autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
}
}
@Test @Test
public void test3() throws Exception { public void test3() throws Exception {
String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now()); String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
...@@ -80,8 +98,8 @@ public class AutoTurnoverTest { ...@@ -80,8 +98,8 @@ public class AutoTurnoverTest {
AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob(); AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
HashMap<Integer, String> map = new HashMap<>(); HashMap<Integer, String> map = new HashMap<>();
map.put(1,""); map.put(1,"");
ShardingContext shardingContext = new ShardingContext(new ShardingContexts("","",1,"", map), 0); ShardingContext shardingContext = new ShardingContext(new ShardingContexts("","",4,"", map), 3);
JobPointLog jobPointLog = new JobPointLog("", 1, 1, 1, 1, LocalDateTime.now().minusSeconds(1), LocalDateTime.now()); JobPointLog jobPointLog = new JobPointLog("", 1, 2000, 1, 1, LocalDateTime.now().minusSeconds(1), LocalDateTime.now());
autoTurnoverJob.executeJob(shardingContext, jobPointLog); autoTurnoverJob.executeJob(shardingContext, jobPointLog);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment