add:mybatis拦截器、预警消息发布订阅
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package org.nl.config.mybatis;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.nl.common.publish.BussEventMulticaster;
|
||||
import org.nl.wms.early_manage.service.event.EarlyEvent;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* mybatis数据库操作拦截
|
||||
*/
|
||||
//@Component
|
||||
@Intercepts({
|
||||
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
|
||||
})
|
||||
public class AlmEarlyInterceptor implements Interceptor {
|
||||
|
||||
private static final String TARGET_TABLE_NAME = "md_pb_vehicleMater";
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
||||
String sql = mappedStatement.getBoundSql(invocation.getArgs()[1]).getSql().toLowerCase();
|
||||
if (isTargetTable(sql)) {
|
||||
Object parameter = invocation.getArgs()[1];
|
||||
String materialId = fieldStringValue(parameter, "material_id");
|
||||
BigDecimal qty = fieldBigDecimalValue(parameter, "qty");
|
||||
publishEvent(materialId, qty);
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
private boolean isTargetTable(String sql) {
|
||||
return sql.contains(TARGET_TABLE_NAME.toLowerCase()) &&
|
||||
(sql.contains("insert") || sql.contains("update"));
|
||||
}
|
||||
|
||||
private String fieldStringValue(Object parameter, String fieldName) {
|
||||
if (parameter instanceof Map) {
|
||||
Map<?, ?> paramMap = (Map<?, ?>) parameter;
|
||||
if (paramMap.containsKey("param1")) {
|
||||
Object param1 = paramMap.get("param1");
|
||||
if (param1 instanceof Map) {
|
||||
Map<?, ?> param = (Map<?, ?>) param1;
|
||||
return (String) param.get(fieldName);
|
||||
} else {
|
||||
try {
|
||||
Field field = param1.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (String) field.get(param1);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private BigDecimal fieldBigDecimalValue(Object parameter, String fieldName) {
|
||||
if (parameter instanceof Map) {
|
||||
Map<?, ?> paramMap = (Map<?, ?>) parameter;
|
||||
if (paramMap.containsKey("param1")) {
|
||||
Object param1 = paramMap.get("param1");
|
||||
if (param1 instanceof Map) {
|
||||
Map<?, ?> param = (Map<?, ?>) param1;
|
||||
return (BigDecimal) param.get(fieldName);
|
||||
} else {
|
||||
try {
|
||||
Field field = param1.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (BigDecimal) field.get(param1);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private void publishEvent(String materialId, BigDecimal qty) {
|
||||
BussEventMulticaster.Publish(EarlyEvent.builder()
|
||||
.material_id(materialId)
|
||||
.qty(qty)
|
||||
.create_time(DateUtil.now())
|
||||
.build(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package org.nl.config.mybatis;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* mybatis数据库操作拦截
|
||||
*/
|
||||
//@Component
|
||||
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
|
||||
public class DatazhuazhuaInterceptor implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
|
||||
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); String methodName = StringUtils.substringAfterLast(mappedStatement.getId(), ".");
|
||||
String methodNam2e = StringUtils.substringAfterLast(mappedStatement.getId(), ".");
|
||||
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
|
||||
System.out.println("mybatis拦截器:+"+methodNam2e+"——sql"+boundSql.getSql());
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
if (target instanceof StatementHandler) {
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,12 +3,12 @@ package org.nl.config.mybatis;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.github.pagehelper.PageInterceptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.plugin.Interceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -24,9 +24,10 @@ public class MybatisPlusConfig {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
/**
|
||||
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
|
||||
添加自增插件
|
||||
* 添加自增插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
@@ -39,14 +40,21 @@ public class MybatisPlusConfig {
|
||||
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ConfigurationCustomizer mybatisConfigurationCustomizer() {
|
||||
return configuration -> configuration.addInterceptor(new PageInterceptor());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Interceptor datazhuazhuaInterceptor() {
|
||||
return new AlmEarlyInterceptor();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void datainnit(){
|
||||
public void datainnit() {
|
||||
String url = ((DruidDataSource) dataSource).getUrl();
|
||||
System.out.println("项目数据库地址:"+url);
|
||||
log.debug("项目数据库地址:{}",url);
|
||||
System.out.println("项目数据库地址:" + url);
|
||||
log.debug("项目数据库地址:{}", url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.nl.wms.base_manage.vehicle.vehicleMater.controller;
|
||||
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.nl.common.TableDataInfo;
|
||||
@@ -15,6 +16,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -46,6 +48,15 @@ public class MdPbVehicleMaterController {
|
||||
return new ResponseEntity<>(TableDataInfo.build(dick_code), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/update/{id}")
|
||||
@SaIgnore
|
||||
public ResponseEntity<Object> update(@PathVariable String id) {
|
||||
MdPbVehicleMater mdPbVehicleMater = iMdPbVehicleMaterService.getById(id);
|
||||
mdPbVehicleMater.setQty(new BigDecimal(0));
|
||||
iMdPbVehicleMaterService.updateById(mdPbVehicleMater);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,4 +16,6 @@ import java.util.Map;
|
||||
*/
|
||||
public interface IMdPbVehicleMaterService extends IService<MdPbVehicleMater> {
|
||||
|
||||
Integer getQtySumByMaterial(String material_id);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.nl.wms.base_manage.vehicle.vehicleMater.service.dao.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.nl.wms.base_manage.vehicle.vehicleMater.service.dao.MdPbVehicleMater;
|
||||
|
||||
/**
|
||||
@@ -13,4 +14,6 @@ import org.nl.wms.base_manage.vehicle.vehicleMater.service.dao.MdPbVehicleMater;
|
||||
*/
|
||||
public interface MdPbVehicleMaterMapper extends BaseMapper<MdPbVehicleMater> {
|
||||
|
||||
@Select("SELECT SUM(qty) FROM md_pb_vehicleMater WHERE material_id = #{materialId}")
|
||||
Integer getQtySumByMaterial(String material_id);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.wms.base_manage.vehicle.vehicleMater.service.IMdPbVehicleMaterService;
|
||||
import org.nl.wms.base_manage.vehicle.vehicleMater.service.dao.MdPbVehicleMater;
|
||||
import org.nl.wms.base_manage.vehicle.vehicleMater.service.dao.mapper.MdPbVehicleMaterMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -18,4 +19,11 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class MdPbVehicleMaterServiceImpl extends ServiceImpl<MdPbVehicleMaterMapper, MdPbVehicleMater> implements IMdPbVehicleMaterService {
|
||||
|
||||
@Autowired
|
||||
private MdPbVehicleMaterMapper mdPbVehicleMaterMapper;
|
||||
|
||||
@Override
|
||||
public Integer getQtySumByMaterial(String material_id) {
|
||||
return mdPbVehicleMaterMapper.getQtySumByMaterial(material_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.nl.wms.early_manage.service.early_inv.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,7 +19,7 @@ import lombok.*;
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName(value = "alm_early_inv",autoResultMap = true)
|
||||
@TableName(value = "alm_early_inv", autoResultMap = true)
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -41,7 +42,10 @@ public class AlmEarlyInv implements Serializable {
|
||||
/**
|
||||
* 安全库存数量
|
||||
*/
|
||||
private Integer safety_qty;
|
||||
private Integer safety_min_qty;
|
||||
|
||||
|
||||
private Integer safety_max_qty;
|
||||
|
||||
/**
|
||||
* 安全库存天数
|
||||
|
||||
@@ -52,7 +52,8 @@ public class AlmEarlyInvServiceImpl extends ServiceImpl<AlmEarlyInvMapper, AlmEa
|
||||
.ext_json(param.getString("ext_json"))
|
||||
.notice_type(param.getString("notice_type"))
|
||||
.remark(param.getString("remark"))
|
||||
.safety_qty(param.getInteger("safety_qty"))
|
||||
.safety_min_qty(param.getInteger("safety_min_qty"))
|
||||
.safety_max_qty(param.getInteger("safety_max_qty"))
|
||||
.safety_day(param.getInteger("safety_day"))
|
||||
.stor_code(param.getString("stor_code"))
|
||||
.is_used(true)
|
||||
|
||||
@@ -16,4 +16,5 @@ import org.nl.wms.early_manage.service.early_msg.dto.AlmMsgQuery;
|
||||
public interface IAlmEarlyMsgService extends IService<AlmEarlyMsg> {
|
||||
|
||||
Object getAll(PageQuery page, AlmMsgQuery query);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nl.wms.early_manage.service.early_msg.dao.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.nl.wms.early_manage.service.early_msg.dao.AlmEarlyMsg;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@ package org.nl.wms.early_manage.service.early_msg.impl;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.nl.common.TableDataInfo;
|
||||
import org.nl.common.domain.entity.PageQuery;
|
||||
import org.nl.wms.base_manage.material.service.IMdMeMaterialbaseService;
|
||||
import org.nl.wms.base_manage.material.service.dao.MdMeMaterialbase;
|
||||
@@ -14,7 +12,6 @@ import org.nl.wms.early_manage.service.early_msg.dao.mapper.AlmEarlyMsgMapper;
|
||||
import org.nl.wms.early_manage.service.early_msg.IAlmEarlyMsgService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.wms.early_manage.service.early_msg.dto.AlmMsgQuery;
|
||||
import org.nl.wms.flow_manage.flow.service.execution.dao.ActRuExecution;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -36,9 +33,6 @@ public class AlmEarlyMsgServiceImpl extends ServiceImpl<AlmEarlyMsgMapper, AlmEa
|
||||
|
||||
@Override
|
||||
public Object getAll(PageQuery page, AlmMsgQuery query) {
|
||||
// totalElements
|
||||
// content
|
||||
//判断是否存在子实例
|
||||
Page<AlmEarlyMsg> almMsgQueryPage = this.page(page.build(), query.build());
|
||||
long total = almMsgQueryPage.getTotal();
|
||||
List<AlmEarlyMsg> records = almMsgQueryPage.getRecords();
|
||||
@@ -46,14 +40,15 @@ public class AlmEarlyMsgServiceImpl extends ServiceImpl<AlmEarlyMsgMapper, AlmEa
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
||||
MdMeMaterialbase material = materialbaseService.getById(jsonObject.getString("material_id"));
|
||||
if (ObjectUtil.isNotEmpty(material)){
|
||||
jsonObject.put("material_code",material.getMaterial_code());
|
||||
jsonObject.put("material_name",material.getMaterial_name());
|
||||
if (ObjectUtil.isNotEmpty(material)) {
|
||||
jsonObject.put("material_code", material.getMaterial_code());
|
||||
jsonObject.put("material_name", material.getMaterial_name());
|
||||
}
|
||||
}
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("totalElements",total);
|
||||
json.put("content",jsonArray);
|
||||
json.put("totalElements", total);
|
||||
json.put("content", jsonArray);
|
||||
return json;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.nl.wms.early_manage.service.event;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.nl.common.publish.event.PublishEvent;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author Gengby
|
||||
* @Date 2024/5/28
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class EarlyEvent extends PublishEvent {
|
||||
private String material_id;
|
||||
private BigDecimal qty;
|
||||
private String create_time;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.nl.wms.early_manage.service.listen;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.publish.AbstraceListener;
|
||||
import org.nl.common.utils.IdUtil;
|
||||
import org.nl.wms.base_manage.vehicle.vehicleMater.service.IMdPbVehicleMaterService;
|
||||
import org.nl.wms.early_manage.service.early_dtl.IAlmEarlyDtlService;
|
||||
import org.nl.wms.early_manage.service.early_dtl.dao.AlmEarlyDtl;
|
||||
import org.nl.wms.early_manage.service.early_inv.IAlmEarlyInvService;
|
||||
import org.nl.wms.early_manage.service.early_inv.dao.AlmEarlyInv;
|
||||
import org.nl.wms.early_manage.service.early_msg.IAlmEarlyMsgService;
|
||||
import org.nl.wms.early_manage.service.early_msg.dao.AlmEarlyMsg;
|
||||
import org.nl.wms.early_manage.service.event.EarlyEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author onepiece
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EarlyEventListener extends AbstraceListener<EarlyEvent> {
|
||||
|
||||
@Autowired
|
||||
private IAlmEarlyMsgService almEarlyMsgService;
|
||||
@Autowired
|
||||
private IAlmEarlyInvService almEarlyInvService;
|
||||
@Autowired
|
||||
private IAlmEarlyDtlService almEarlyDtlService;
|
||||
@Autowired
|
||||
private IMdPbVehicleMaterService mdPbVehicleMaterService;
|
||||
|
||||
@Override
|
||||
protected String doEvent(EarlyEvent event) {
|
||||
String material_id = event.getMaterial_id();
|
||||
BigDecimal qty = event.getQty();
|
||||
String create_time = event.getCreate_time();
|
||||
List<AlmEarlyDtl> list = almEarlyDtlService.list(new LambdaQueryWrapper<AlmEarlyDtl>().eq(AlmEarlyDtl::getMaterial_id, material_id));
|
||||
for (AlmEarlyDtl almEarlyDtl : list) {
|
||||
AlmEarlyInv earlyInv = almEarlyInvService.getById(almEarlyDtl.getAlm_id());
|
||||
String notice_type = earlyInv.getNotice_type();
|
||||
String earlyInvCreateTime = earlyInv.getCreate_time();
|
||||
Integer safety_day = earlyInv.getSafety_day();
|
||||
long daysDifference = DateUtil.betweenDay(DateUtil.parse(create_time), DateUtil.parse(earlyInvCreateTime), true);
|
||||
if (daysDifference < safety_day) {
|
||||
Integer safety_max_qty = earlyInv.getSafety_max_qty();
|
||||
Integer safety_min_qty = earlyInv.getSafety_min_qty();
|
||||
Integer sumQty = mdPbVehicleMaterService.getQtySumByMaterial(material_id);
|
||||
if (sumQty < safety_min_qty || sumQty > safety_max_qty) {
|
||||
//插入消息表
|
||||
AlmEarlyMsg almEarlyMsg = new AlmEarlyMsg();
|
||||
almEarlyMsg.setId(IdUtil.getStringId());
|
||||
almEarlyMsg.setQty(sumQty);
|
||||
almEarlyMsg.setMaterial_id(material_id);
|
||||
almEarlyMsg.setCreate_time(DateUtil.now());
|
||||
almEarlyMsg.setEarly_type("1");
|
||||
almEarlyMsg.setStor_code(earlyInv.getStor_code());
|
||||
almEarlyMsgService.save(almEarlyMsg);
|
||||
//根据notice_type下发通知
|
||||
System.out.println("下发通知");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user