From ab09d4a524a27fedc094794e0e04da99fe64defa Mon Sep 17 00:00:00 2001
From: "WIN-IDGBLFHC1K6\\Administrator" <787952492@qq.com>
Date: Wed, 29 Jun 2022 18:32:26 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=20=E5=8D=A0=E7=94=A8=20?=
 =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=89=A3=E5=87=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../admin/resource/vo/ResourceBaseVO.java     |  7 ++++
 .../admin/resource/vo/ResourceExcelVO.java    |  7 ++++
 .../resource/vo/ResourceExportReqVO.java      |  7 ++++
 .../admin/resource/vo/ResourcePageReqVO.java  |  7 ++++
 .../controller/admin/task/vo/TaskBaseVO.java  |  4 +--
 .../dal/dataobject/resource/ResourceDO.java   |  9 +++++
 .../service/project/ProjectServiceImpl.java   |  4 +--
 .../farm/service/task/TaskServiceImpl.java    | 36 +++++++++++++++++++
 8 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceBaseVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceBaseVO.java
index 5c51a36c..d812440f 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceBaseVO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceBaseVO.java
@@ -43,4 +43,11 @@ public class ResourceBaseVO {
 
     private Boolean pretendDelete;
 
+    private String unit;
+
+    private Integer number;
+
+    @ApiModelProperty("唯一资源被占用")
+    private Boolean occupied;
+
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExcelVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExcelVO.java
index 0b8c3d3c..6387ec97 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExcelVO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExcelVO.java
@@ -44,4 +44,11 @@ public class ResourceExcelVO {
 
     private Boolean pretendDelete;
 
+    private String unit;
+
+    private Integer number;
+
+    @ApiModelProperty("唯一资源被占用")
+    private Boolean occupied;
+
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExportReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExportReqVO.java
index cac6bf47..7ffd730f 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExportReqVO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourceExportReqVO.java
@@ -54,4 +54,11 @@ public class ResourceExportReqVO {
 
     private Boolean pretendDelete;
 
+    private String unit;
+
+    private Integer number;
+
+    @ApiModelProperty("唯一资源被占用")
+    private Boolean occupied;
+
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourcePageReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourcePageReqVO.java
index 02368b50..7af3b1e0 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourcePageReqVO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/resource/vo/ResourcePageReqVO.java
@@ -56,4 +56,11 @@ public class ResourcePageReqVO extends PageParam {
 
     private Boolean pretendDelete = false;
 
+    private String unit;
+
+    private Integer number;
+
+    @ApiModelProperty("唯一资源被占用")
+    private Boolean occupied;
+
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/vo/TaskBaseVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/vo/TaskBaseVO.java
index ce732b49..cdb3151b 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/vo/TaskBaseVO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/vo/TaskBaseVO.java
@@ -50,10 +50,10 @@ public class TaskBaseVO {
     private Integer weight;
 
     @ApiModelProperty("任务状态")
-    private TaskStatus status;
+    private TaskStatus status = TaskStatus.PLAN;
 
     @ApiModelProperty("草稿")
-    private Boolean draft;
+    private Boolean draft = false;
 
     private Boolean pretendDelete;
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/resource/ResourceDO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/resource/ResourceDO.java
index d52578d3..53b8d5e2 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/resource/ResourceDO.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/resource/ResourceDO.java
@@ -65,4 +65,13 @@ public class ResourceDO extends BaseDO {
     public Boolean getSolo() {
         return solo;
     }
+
+    @ApiModelProperty("单位")
+    private String unit;
+
+    @ApiModelProperty("数量")
+    private Integer number;
+
+    @ApiModelProperty("唯一资源被占用")
+    private Boolean occupied;
 }
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/project/ProjectServiceImpl.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/project/ProjectServiceImpl.java
index 229b1131..6bb756b5 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/project/ProjectServiceImpl.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/project/ProjectServiceImpl.java
@@ -74,10 +74,10 @@ public class ProjectServiceImpl implements ProjectService {
 
     @Override
     public void updateProject(ProjectUpdateReqVO updateReqVO) {
-        //检查项目新增、修改时的区域 是否能够使用
-        this.checkProjectAreas(BeanUtil.copyProperties(updateReqVO, ProjectDO.class));
         // 校验存在
         this.validateProjectExists(updateReqVO.getId());
+        //检查项目新增、修改时的区域 是否能够使用
+        this.checkProjectAreas(BeanUtil.copyProperties(updateReqVO, ProjectDO.class));
         //项目修改完成时,,项目下所有任务未完成不能更改项目状态为完成
         if (updateReqVO.getState().equals(ProjectStateEnum.FINISH)){
             this.checkProjectTaskFinish(updateReqVO);
diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java
index 7a23fe1f..83a615a7 100644
--- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java
+++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/task/TaskServiceImpl.java
@@ -99,6 +99,8 @@ public class TaskServiceImpl implements TaskService {
 
     @Override
     public Long createTask(TaskCreateReqVO createReqVO) {
+        //检查任务资源数量
+        this.checkTaskResourceNumber(BeanUtil.copyProperties(createReqVO, TaskDO.class));
         // 插入
         TaskDO task = TaskConvert.INSTANCE.convert(createReqVO);
         taskMapper.insert(task);
@@ -110,6 +112,8 @@ public class TaskServiceImpl implements TaskService {
     public void updateTask(TaskUpdateReqVO updateReqVO) {
         // 校验存在
         this.validateTaskExists(updateReqVO.getId());
+        //检查任务资源数量
+        this.checkTaskResourceNumber(BeanUtil.copyProperties(updateReqVO, TaskDO.class));
         // 更新
         TaskDO updateObj = TaskConvert.INSTANCE.convert(updateReqVO);
         taskMapper.updateById(updateObj);
@@ -472,6 +476,38 @@ public class TaskServiceImpl implements TaskService {
         return list;
     }
 
+    /**
+     * 新增或修改检查任务所需资源数量
+     */
+    public void checkTaskResourceNumber(TaskDO taskDO){
+        if (!taskDO.getDraft()){
+            List<ResourceDTO> resourceList = taskDO.getResources().toJavaList(ResourceDTO.class);
+            //任务状态更改成进行中  或   任务状态更改成完成
+            if (taskDO.getStatus().equals(TaskStatus.STARTED) || taskDO.getStatus().equals(TaskStatus.COMPLETE)){
+                resourceList.forEach(item ->{
+                    ResourceDO resourceDO = resourceMapper.selectById(item.getResourceId().get(1));
+                    //更改成进行中
+                    if (taskDO.getStatus().equals(TaskStatus.STARTED)){
+                        resourceDO.setNumber(resourceDO.getNumber() - item.getNum());
+                        if (resourceDO.getSolo()){
+                            //唯一资源 更改成 已占用
+                            resourceDO.setOccupied(true);
+                        }
+                    }
+                    //更改成已完成
+                    if (taskDO.getStatus().equals(TaskStatus.COMPLETE)){
+                        if (resourceDO.getSolo()){
+                            //唯一资源 更改成 未占用, 并返还数量
+                            resourceDO.setOccupied(false);
+                            resourceDO.setNumber(resourceDO.getNumber() + item.getNum());
+                        }
+                    }
+                    resourceMapper.updateById(resourceDO);
+                });
+            }
+        }
+    }
+
 
 
 }