Explorar el Código

物料申请监听 库存数量减少

xieyn hace 1 año
padre
commit
193c02a8f1

+ 95 - 0
jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/activiti/listener/ListenerMaterialApply.java

@@ -0,0 +1,95 @@
+package org.jeecg.modules.activiti.listener;
+
+import com.alibaba.fastjson.JSONArray;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import org.activiti.engine.delegate.DelegateExecution;
+import org.activiti.engine.delegate.DelegateTask;
+import org.activiti.engine.delegate.ExecutionListener;
+import org.activiti.engine.delegate.TaskListener;
+import org.apache.commons.lang.StringUtils;
+import org.jeecg.modules.oa.entity.Material;
+import org.jeecg.modules.oa.entity.SealArchives;
+import org.jeecg.modules.oa.mapper.MaterialMapper;
+import org.jeecg.modules.oa.mapper.SealArchivesMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+//流程监听,固资在转移、报废等操作时回写状态
+@Component
+public class ListenerMaterialApply implements TaskListener, ExecutionListener {
+
+    @Autowired
+    private MaterialMapper materialMapper;
+
+    /**
+     * 执行监听
+     *
+     * @param delegateExecution
+     * @throws Exception
+     */
+    @Override
+    public void notify(DelegateExecution delegateExecution) throws Exception {
+
+        String eventName = delegateExecution.getEventName();
+        if ("start".equals(eventName)) {
+            // 流程开始
+        } else if ("end".equals(eventName)) {
+            //todo
+            Map<String, Object> variables = delegateExecution.getVariables();
+            //子表List
+            String sublist = String.valueOf(variables.get("sublist"));
+
+
+            if(StringUtils.isNotBlank(sublist)){
+                //sublist格式
+//            [{"material_name2":"物料名称1","amount":500,"unit":"计量单位1","notes3":"测试","key":1687761773306},
+//            {"material_name2":"物料名称2","amount":50000,"unit":"计量单位2","notes3":"测试","key":1687761778679}]
+                JSONArray ay= JSONArray.parseArray(sublist);
+                for(int i=0;i<ay.size();i++) {
+                    //物料名称
+                    String materialName = (String) ay.getJSONObject(i).get("material_name2");
+                    //申请数量
+                    BigDecimal amount;
+                    Object amount1 = ay.getJSONObject(i).get("amount");
+                    if(amount1 instanceof Integer){
+                        amount = new BigDecimal((Integer) ay.getJSONObject(i).get("amount"));
+                    }else{
+                        amount = (BigDecimal) amount1;
+                    }
+                    //计量单位
+                    String unit = (String) ay.getJSONObject(i).get("unit");
+                    QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
+                    queryWrapper.eq("material_name",materialName);
+                    queryWrapper.eq("unit",unit);
+                    Material material = materialMapper.selectOne(queryWrapper);
+                    if(material!=null){
+                        material.setQuantity(material.getQuantity().subtract(amount));
+                        materialMapper.updateById(material);
+                    }
+                }
+            }
+        } else if ("take".equals(eventName)) {
+            // 连线监听器
+        }
+    }
+
+    /**
+     * 任务监听
+     *
+     * @param delegateTask
+     */
+    @Override
+    public void notify(DelegateTask delegateTask) {
+        String eventName = delegateTask.getEventName();
+        if ("create".endsWith(eventName)) {
+        } else if ("assignment".endsWith(eventName)) {
+        } else if ("complete".endsWith(eventName)) {
+        } else if ("delete".endsWith(eventName)) {
+        }
+    }
+}