opt:优化工厂日历创建失败时回滚操作。

This commit is contained in:
2023-07-06 15:35:33 +08:00
parent ebe6d6f027
commit 5d7c70570d
9 changed files with 247 additions and 246 deletions

View File

@@ -3,25 +3,15 @@ package org.nl.common.utils;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.nl.common.publish.event.PublishEvent;
import org.nl.config.thread.ThreadPoolExecutorUtil;
import org.nl.modules.wql.util.SpringContextHolder;
import org.nl.wms.system_manage.service.ql.IPdmQlOptionService;
import org.nl.wms.system_manage.service.ql.dao.PdmQlOption;
import org.nl.wms.system_manage.service.ql.dto.OptionRecordQuery;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
/*

View File

@@ -20,7 +20,7 @@ import javax.sql.DataSource;
public class MybatisPlusConfig {
@Autowired
DataSource dataSource;
private DataSource dataSource;
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
添加自增插件

View File

@@ -20,6 +20,7 @@ import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@@ -33,6 +34,7 @@ import java.util.concurrent.ThreadPoolExecutor;
*/
@Slf4j
@Configuration
@EnableAsync
public class AsyncTaskExecutePool implements AsyncConfigurer{
/**
* 注入配置类
@@ -81,7 +83,8 @@ public class AsyncTaskExecutePool implements AsyncConfigurer{
// 活跃时间
threadPoolTaskExecutor.setKeepAliveSeconds(config.getKeepAliveSeconds());
// 主线程等待子线程执行时间
threadPoolTaskExecutor.setAwaitTerminationSeconds(30);
threadPoolTaskExecutor.setAwaitTerminationSeconds(config.getAwaitTerminationSeconds());
// threadPoolTaskExecutor.setAwaitTerminationSeconds(30);
// 线程名字前缀
threadPoolTaskExecutor.setThreadNamePrefix("test-thread-");
// RejectedExecutionHandler:当pool已经达到max-size的时候如何处理新任务

View File

@@ -38,4 +38,7 @@ public class AsyncTaskProperties {
private int keepAliveSeconds;
private int queueCapacity;
private int awaitTerminationSeconds;
}

View File

@@ -20,7 +20,7 @@ public class ApsSchedule{
@Autowired
private IMpsSaleOrderService iMpsSaleOrderService;
@Async("taskExecutor")
//@Async("taskExecutor")
//@Scheduled(cron = "0/30 * * * * *")
public void setApsStatus() {
long start = System.currentTimeMillis();

View File

@@ -1,7 +1,5 @@
package org.nl.wms.product_manage.备份pdm.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
@@ -12,14 +10,13 @@ import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.common.utils.SecurityUtils;
import org.nl.modules.wql.WQL;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;
import org.nl.wms.product_manage.备份pdm.service.FactoryCalendarService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
@@ -36,244 +33,250 @@ import java.util.Map;
@Service
@RequiredArgsConstructor
@Slf4j
public class FactoryCalendarServiceImpl implements FactoryCalendarService {
public class FactoryCalendarServiceImpl implements FactoryCalendarService{
@Override
public Map<String,Object> queryAll(Map whereJson, Pageable page) {
String search = MapUtil.getStr(whereJson, "search");
HashMap<String,String> map = new HashMap<>();
map.put("flag", "1");
if(StrUtil.isNotEmpty(search)) {
map.put("search", "%" + search + "%");
}
JSONObject json = WQL.getWO("QPDM_FACTORYCALENDAR").addParamMap(map).pageQuery(WqlUtil.getHttpContext(page), "update_time desc");
return json;
}
@Override
public Map<String, Object> queryAll(Map whereJson, Pageable page) {
String search = MapUtil.getStr(whereJson, "search");
HashMap<String, String> map = new HashMap<>();
@Override
public JSONArray queryDtl(JSONObject whereJson) {
String year = whereJson.getString("year");
String factorycalendar_id = whereJson.getString("factorycalendar_id");
HashMap<String,String> map = new HashMap<>();
map.put("flag", "2");
if(StrUtil.isNotEmpty(year)) {
map.put("date", year + "%");
}
if(StrUtil.isNotEmpty(factorycalendar_id)) {
map.put("id", factorycalendar_id);
}
return WQL.getWO("QPDM_FACTORYCALENDAR").addParamMap(map).process().getResultJSONArray(0);
}
map.put("flag", "1");
if (StrUtil.isNotEmpty(search)) map.put("search", "%" + search + "%");
@Override
@Transactional(rollbackFor = Exception.class)
public void create(JSONObject whereJson) {
String currentUserId = SecurityUtils.getCurrentUserId();
String nickName = SecurityUtils.getCurrentNickName();
//插入主表
String factorycalendar_id = IdUtil.getSnowflake(1, 1).nextId() + "";
whereJson.put("factorycalendar_id", factorycalendar_id);
whereJson.put("is_active", "0");
whereJson.put("create_id", currentUserId);
whereJson.put("create_name", nickName);
whereJson.put("create_time", DateUtil.now());
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").insert(whereJson);
//插入明细表
this.insertDtlTransactionally(whereJson);
}
JSONObject json = WQL.getWO("QPDM_FACTORYCALENDAR").addParamMap(map).pageQuery(WqlUtil.getHttpContext(page), "update_time desc");
return json;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
//查询旧的终止年份
JSONObject old_jo = WQLObject.getWQLObject("PDM_BI_FactoryCalendar").query("factorycalendar_id = '" + factorycalendar_id + "'").uniqueResult(0);
int old_year = old_jo.getIntValue("endfactory_year");
int new_year = whereJson.getIntValue("endfactory_year");
if(old_year >= new_year) {
throw new BadRequestException("新的终止年份不能小于等于之前的终止年份!");
}
String new_start = (old_year + 1) + "-01-01";
whereJson.put("factorycale_startdate", new_start);
//插入明细表
this.insertDtlTransactionally(whereJson);
}
@Override
public JSONArray queryDtl(JSONObject whereJson) {
String year = whereJson.getString("year");
String factorycalendar_id = whereJson.getString("factorycalendar_id");
HashMap<String, String> map = new HashMap<>();
map.put("flag", "2");
if (StrUtil.isNotEmpty(year)) map.put("date", year + "%");
if (StrUtil.isNotEmpty(factorycalendar_id)) map.put("id", factorycalendar_id);
return WQL.getWO("QPDM_FACTORYCALENDAR").addParamMap(map).process().getResultJSONArray(0);
}
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void insertDtlTransactionally(JSONObject row) {
insertDtl(row);
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void create(JSONObject whereJson) {
String currentUserId = SecurityUtils.getCurrentUserId();
String nickName = SecurityUtils.getCurrentNickName();
//插入主表
String factorycalendar_id = IdUtil.getSnowflake(1, 1).nextId() + "";
whereJson.put("factorycalendar_id", factorycalendar_id);
whereJson.put("is_active", "0");
whereJson.put("create_id", currentUserId);
whereJson.put("create_name", nickName);
whereJson.put("create_time", DateUtil.now());
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").insert(whereJson);
//插入明细表
this.insertDtl(whereJson);
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void update(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
//查询旧的终止年份
JSONObject old_jo = WQLObject.getWQLObject("PDM_BI_FactoryCalendar").query("factorycalendar_id = '" + factorycalendar_id + "'").uniqueResult(0);
int old_year = old_jo.getIntValue("endfactory_year");
int new_year = whereJson.getIntValue("endfactory_year");
if (old_year >= new_year) {
throw new BadRequestException("新的终止年份不能小于等于之前的终止年份!");
}
String new_start = (old_year + 1) + "-01-01";
whereJson.put("factorycale_startdate", new_start);
//插入明细表
this.insertDtl(whereJson);
}
@Async
public void insertDtl(JSONObject row) {
String a =DateUtil.format(DateUtil.parse( row.getString("factorycale_startdate")), "yyyy/MM/dd");
String b = row.getString("endfactory_year")+ "/12/31";
while (!a.equals(b)) {
JSONObject dtl = new JSONObject();
dtl.put("factorycalendardtl_id", IdUtil.getSnowflake(1, 1).nextId() + "");
dtl.put("factorycalendar_id", row.getString("factorycalendar_id"));
dtl.put("factory_date", a);
int dayOfWeek = DateUtil.dayOfWeek(DateUtil.parse(a));
switch (dayOfWeek) {
case 1:
if (row.getString("is_sun").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
@Async("taskExecutor")
public void insertDtl(JSONObject row) {
try {
String a = DateUtil.format(DateUtil.parse(row.getString("factorycale_startdate")), "yyyy/MM/dd");
String b = row.getString("endfactory_year") + "/12/31";
while(!a.equals(b)) {
JSONObject dtl = new JSONObject();
dtl.put("factorycalendardtl_id", IdUtil.getSnowflake(1, 1).nextId() + "");
dtl.put("factorycalendar_id", row.getString("factorycalendar_id"));
dtl.put("factory_date", a);
int dayOfWeek = DateUtil.dayOfWeek(DateUtil.parse(a));
switch(dayOfWeek) {
case 1:
if("1".equals(row.getString("is_sun"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 2:
if("1".equals(row.getString("is_mon"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 3:
if("1".equals(row.getString("is_tue"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 4:
if("1".equals(row.getString("is_wed"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 5:
if("1".equals(row.getString("is_thu"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 6:
if("1".equals(row.getString("is_fri"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 7:
if("1".equals(row.getString("is_sau"))) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
}
else{
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
default:
break;
}
break;
case 2:
if (row.getString("is_mon").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 3:
if (row.getString("is_tue").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 4:
if (row.getString("is_wed").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 5:
if (row.getString("is_thu").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 6:
if (row.getString("is_fri").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
case 7:
if (row.getString("is_sau").equals("1")) {
dtl.put("factorydate_type", "01");
dtl.put("work_type", "00");
dtl.put("work_type_name", "工作日");
dtl.put("priority", "50");
} else {
dtl.put("factorydate_type", "03");
dtl.put("work_type", "01");
dtl.put("work_type_name", "休息日");
}
break;
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").insert(dtl);
a = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(a), 1), "yyyy/MM/dd");
}
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").insert(dtl);
a = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(a), 1), "yyyy/MM/dd");
}
}
catch(Exception e) {
throw new BadRequestException(HttpStatus.BAD_REQUEST, "操作工厂日历表失败!");
}
}
@Override
public void deleteAll(Long[] ids) {
String currentUserId = SecurityUtils.getCurrentUserId();
String nickName = SecurityUtils.getCurrentNickName();
@Override
public void deleteAll(Long[] ids) {
String currentUserId = SecurityUtils.getCurrentUserId();
String nickName = SecurityUtils.getCurrentNickName();
WQLObject wo = WQLObject.getWQLObject("pdm_bi_factorycalendar");
for(Long factorycalendar_id : ids) {
JSONObject param = new JSONObject();
param.put("factorycalendar_id", String.valueOf(factorycalendar_id));
param.put("is_delete", "1");
param.put("update_optid", currentUserId);
param.put("update_optname", nickName);
param.put("update_time", DateUtil.now());
wo.update(param);
}
}
@Override
public JSONObject getDtl(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
String date = whereJson.getString("date");
JSONObject jo = WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").query("factorycalendar_id = '" + factorycalendar_id + "' AND factory_date ='" + date + "'").uniqueResult(0);
return jo;
}
WQLObject wo = WQLObject.getWQLObject("pdm_bi_factorycalendar");
for (Long factorycalendar_id : ids) {
JSONObject param = new JSONObject();
param.put("factorycalendar_id", String.valueOf(factorycalendar_id));
param.put("is_delete", "1");
param.put("update_optid", currentUserId);
param.put("update_optname", nickName);
param.put("update_time", DateUtil.now());
wo.update(param);
}
}
@Override
public void updateActive(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
String is_active = whereJson.getString("is_active");
HashMap<String,String> map = new HashMap<>();
map.put("is_active", is_active);
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").update(map, "factorycalendar_id = '" + factorycalendar_id + "'");
}
@Override
public JSONObject getDtl(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
String date = whereJson.getString("date");
JSONObject jo = WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").query("factorycalendar_id = '" + factorycalendar_id + "' AND factory_date ='" + date + "'").uniqueResult(0);
return jo;
}
@Override
public void updateActive(JSONObject whereJson) {
String factorycalendar_id = whereJson.getString("factorycalendar_id");
String is_active = whereJson.getString("is_active");
HashMap<String, String> map = new HashMap<>();
map.put("is_active", is_active);
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").update(map, "factorycalendar_id = '" + factorycalendar_id + "'");
}
@Override
public void updateDtlStatus(JSONObject whereJson) {
String holidayType =whereJson.getString("holidayType");
String holidayDate =DateUtil.format(DateUtil.parse( whereJson.getString("holidayDate")), "yyyy/MM/dd");
String factorycalendar_id = whereJson.getString("factorycalendar_id");
HashMap<String, String> map = new HashMap<>();
map.put("work_type", holidayType);
if (holidayType.equals("00")) {
map.put("factorydate_type", "01");
map.put("priority", "50");
map.put("work_type_name", "工作日");
} else {
map.put("factorydate_type", "03");
map.put("priority", "100");
map.put("work_type_name", "休息日");
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").update(map, "factorycalendar_id = '" + factorycalendar_id + "' AND factory_date = '" + holidayDate + "'");
}
@Override
public void updateDtlActive(JSONObject whereJson) {
String is_active = whereJson.getString("is_active");
if (is_active.equals("1")){
//查询是否存在已经启用的日历
JSONObject mst = WQLObject.getWQLObject("PDM_BI_FactoryCalendar").query("is_active = '1' AND is_delete = '0'").uniqueResult(0);
if (ObjectUtil.isNotEmpty(mst)){
throw new BadRequestException("工厂日历只能启用一个!");
}
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").update(whereJson);
}
@Override
public void updateDtlStatus(JSONObject whereJson) {
String holidayType = whereJson.getString("holidayType");
String holidayDate = DateUtil.format(DateUtil.parse(whereJson.getString("holidayDate")), "yyyy/MM/dd");
String factorycalendar_id = whereJson.getString("factorycalendar_id");
HashMap<String,String> map = new HashMap<>();
map.put("work_type", holidayType);
if(holidayType.equals("00")) {
map.put("factorydate_type", "01");
map.put("priority", "50");
map.put("work_type_name", "工作日");
}
else{
map.put("factorydate_type", "03");
map.put("priority", "100");
map.put("work_type_name", "休息日");
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendarDtl").update(map, "factorycalendar_id = '" + factorycalendar_id + "' AND factory_date = '" + holidayDate + "'");
}
@Override
public void updateDtlActive(JSONObject whereJson) {
String is_active = whereJson.getString("is_active");
if(is_active.equals("1")) {
//查询是否存在已经启用的日历
JSONObject mst = WQLObject.getWQLObject("PDM_BI_FactoryCalendar").query("is_active = '1' AND is_delete = '0'").uniqueResult(0);
if(ObjectUtil.isNotEmpty(mst)) {
throw new BadRequestException("工厂日历只能启用一个!");
}
}
WQLObject.getWQLObject("PDM_BI_FactoryCalendar").update(whereJson);
}
}

View File

@@ -28,6 +28,8 @@ task:
keep-alive-seconds: 60
# 队列容量
queue-capacity: 50
# 主线程等待子线程执行时间
awaitTerminationSeconds: 50
#七牛云
qiniu: