zyw 1 năm trước cách đây
mục cha
commit
72f28315de

+ 2 - 0
src/main/java/com/zhaoliangsz/grainsearch/basic/exception/ExceptionDefinition.java

@@ -16,4 +16,6 @@ public class ExceptionDefinition {
             new ServiceExceptionDefinition(10004, "删除数据异常");
     public static  ServiceExceptionDefinition NAME_SECRET_EXCEPTION =
             new ServiceExceptionDefinition(400001, "用户名密码错误");
+    public static  ServiceExceptionDefinition NOT_YESTERDAY_INVENTORY =
+            new ServiceExceptionDefinition(10005, "没有前一天的库存数据");
 }

+ 13 - 6
src/main/java/com/zhaoliangsz/grainsearch/domain/entity/InventoryTable.java

@@ -4,9 +4,13 @@ import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.zhaoliangsz.grainsearch.basic.constants.DateConstants;
 import com.zhaoliangsz.grainsearch.domain.entity.BaseDO;
 import java.io.Serializable;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.util.Date;
 
 
 import lombok.Data;
@@ -31,22 +35,25 @@ public class InventoryTable extends BaseDO<InventoryTable> {
 
     @TableId("id")
     private Long id;
+
     @TableField("date")
-    private LocalDateTime date;
+    @JsonFormat(pattern = DateConstants.YEAR_MONTH_DAY, timezone = DateConstants.TIMEZONE,shape=JsonFormat.Shape.STRING)
+    private LocalDate date;
+
     @TableField("out_num")
-    private Object outNum;
+    private Double outNum;
     @TableField("in_num")
-    private Object inNum;
+    private Double inNum;
     @TableField("inventory")
-    private Object inventory;
+    private Double inventory;
     @TableField("create_by")
     private String createBy;
     @TableField("create_time")
-    private LocalDateTime createTime;
+    private Date createTime;
     @TableField("update_by")
     private String updateBy;
     @TableField("update_time")
-    private LocalDateTime updateTime;
+    private Date updateTime;
     @TableField("has_deleted")
     @TableLogic
     private Integer hasDeleted;

+ 85 - 0
src/main/java/com/zhaoliangsz/grainsearch/service/impl/TestTableServiceImpl.java

@@ -1,7 +1,9 @@
 package com.zhaoliangsz.grainsearch.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.zhaoliangsz.grainsearch.domain.entity.InventoryTable;
 import com.zhaoliangsz.grainsearch.domain.entity.TestTable;
+import com.zhaoliangsz.grainsearch.mapper.InventoryTableMapper;
 import com.zhaoliangsz.grainsearch.mapper.TestTableMapper;
 import com.zhaoliangsz.grainsearch.service.ITestTableService;
 import com.baomidou.mybatisplus.extension.service.IService;
@@ -14,6 +16,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
 
 import java.sql.Wrapper;
+import java.time.LocalDate;
 import java.util.*;
 
 import org.springframework.transaction.annotation.Transactional;
@@ -31,6 +34,8 @@ import com.zhaoliangsz.grainsearch.basic.exception.ExceptionDefinition;
 public class TestTableServiceImpl extends ServiceImpl<TestTableMapper, TestTable> implements ITestTableService {
     @Autowired
     private TestTableMapper testTableMapper;
+    @Autowired
+    private InventoryTableMapper inventoryTableMapper;
     @Override
     public PageResult<TestTable> queryTestTableList(TestTable queryTestTable) throws ServiceException{
         try{
@@ -52,20 +57,100 @@ public class TestTableServiceImpl extends ServiceImpl<TestTableMapper, TestTable
     @Override
     @Transactional(rollbackFor = Exception.class)
     public String addTestTable(TestTable addTestTable) throws ServiceException {
+        //查询相同条件是否已添加过
         TestTable testTable=testTableMapper.selectOne(new QueryWrapper<TestTable>()
                  .eq("type",addTestTable.getType())
                  .eq("banci",addTestTable.getBanci())
                  .eq("banzu",addTestTable.getBanzu())
                  .eq("date",addTestTable.getDate()));
         boolean flag;
+        InventoryTable inventoryTable=inventoryTableMapper.selectOne(new QueryWrapper<InventoryTable>()
+                .eq("date",addTestTable.getDate()));
+        //编辑
         if (testTable!=null){
             addTestTable.setUpdateTime(new Date());
             addTestTable.setId(testTable.getId());
+            LocalDate today = addTestTable.getDate();
+            //入库
+            if ("入库".equals(addTestTable.getType())){
+                inventoryTable.setInNum(addTestTable.getInNum());
+                //库存=当天库存减原入库加新入库
+                inventoryTable.setInventory(inventoryTable.getInventory()-testTable.getInNum()+addTestTable.getInNum());
+            }
+            //出库
+            else{
+                //出库量先减原出库量再进行累加
+                inventoryTable.setOutNum(inventoryTable.getOutNum()-testTable.getTotal()+addTestTable.getTotal());
+                //库存=当天库存加原出库减新出库
+                inventoryTable.setInventory(inventoryTable.getInventory()+testTable.getTotal()-addTestTable.getTotal());
+            }
+            inventoryTable.setUpdateTime(new Date());
+            inventoryTableMapper.updateById(inventoryTable);
             flag= this.updateById(addTestTable);
         }
+        //新增
         else {
             addTestTable.setCreateTime(new Date());
             addTestTable.setUpdateTime(new Date());
+            //获取本条数据日期
+            LocalDate today = addTestTable.getDate();
+            //获取前一天日期  查询前一天库存
+            LocalDate yesterday = today.minusDays(1);
+            InventoryTable inventoryTable1=inventoryTableMapper.selectOne(new QueryWrapper<InventoryTable>()
+                    .eq("date",yesterday));
+            //入库
+            if ("入库".equals(addTestTable.getType())){
+                if (inventoryTable!=null){
+                    inventoryTable.setInNum(addTestTable.getInNum());
+                    //库存=当天库存加入库
+                    inventoryTable.setInventory(inventoryTable.getInventory()+addTestTable.getInNum());
+                    inventoryTable.setUpdateTime(new Date());
+                    inventoryTableMapper.updateById(inventoryTable);
+                }
+                else{
+                    InventoryTable inventoryTable2=new InventoryTable();
+                    inventoryTable2.setCreateTime(new Date());
+                    inventoryTable2.setUpdateTime(new Date());
+                    inventoryTable2.setDate(addTestTable.getDate());
+                    inventoryTable2.setInNum(addTestTable.getInNum());
+                    //库存=前一天库存加入库
+                    if (inventoryTable1!=null){
+                        inventoryTable2.setInventory(inventoryTable1.getInventory()+addTestTable.getInNum());
+                    }
+                    else{
+                        throw new ServiceException(ExceptionDefinition.NOT_YESTERDAY_INVENTORY);
+
+                    }
+                    inventoryTableMapper.insert(inventoryTable2);
+                }
+            }
+            //出库
+            else{
+                if (inventoryTable!=null){
+                    //出库量累加
+                    inventoryTable.setOutNum(inventoryTable.getOutNum()+addTestTable.getTotal());
+                    //库存=当天库存减出库
+                    inventoryTable.setInventory(inventoryTable.getInventory()-addTestTable.getTotal());
+                    inventoryTable.setUpdateTime(new Date());
+                    inventoryTableMapper.updateById(inventoryTable);
+                }
+                else{
+                    InventoryTable inventoryTable2=new InventoryTable();
+                    inventoryTable2.setCreateTime(new Date());
+                    inventoryTable2.setUpdateTime(new Date());
+                    inventoryTable2.setDate(addTestTable.getDate());
+                    inventoryTable2.setOutNum(addTestTable.getTotal());
+                    //库存=前一天库存减出库
+                    if (inventoryTable1!=null){
+                        inventoryTable2.setInventory(inventoryTable1.getInventory()-addTestTable.getTotal());
+                    }
+                    else{
+                        throw new ServiceException(ExceptionDefinition.NOT_YESTERDAY_INVENTORY);
+
+                    }
+                    inventoryTableMapper.insert(inventoryTable2);
+                }
+            }
             flag= this.save(addTestTable);
         }