From 17aed0a29ef6cee431c8e8b3438bf38f91dff003 Mon Sep 17 00:00:00 2001
From: vilson <545522390@qq.com>
Date: Thu, 27 Jun 2019 14:46:14 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=BB=E5=8A=A1=E8=87=AA?=
 =?UTF-8?q?=E5=8A=A8=E5=8C=96=E6=B5=81=E8=BD=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: vilson <545522390@qq.com>
---
 application/common/Model/TaskWorkflow.php     | 28 ++++++
 application/common/Model/TaskWorkflowRule.php | 31 ++++++-
 application/project/behavior/Task.php         | 37 ++++----
 application/project/controller/TaskStages.php | 12 +++
 .../project/controller/TaskWorkflow.php       | 85 ++++++++++++-------
 5 files changed, 144 insertions(+), 49 deletions(-)

diff --git a/application/common/Model/TaskWorkflow.php b/application/common/Model/TaskWorkflow.php
index be3f692..d0ca450 100644
--- a/application/common/Model/TaskWorkflow.php
+++ b/application/common/Model/TaskWorkflow.php
@@ -2,6 +2,8 @@
 
 namespace app\common\Model;
 
+use think\Db;
+
 /**
  * 任务工作流
  * Class TaskWorkflow
@@ -10,4 +12,30 @@ namespace app\common\Model;
 class TaskWorkflow extends CommonModel
 {
     protected $append = [];
+
+    public static function createData($name, $projectCode, $organizationCode)
+    {
+        $data = [
+            'create_time' => nowTime(),
+            'code' => createUniqueCode('TaskWorkflow'),
+            'project_code' => $projectCode,
+            'organization_code' => $organizationCode,
+            'name' => trim($name),
+        ];
+        return self::create($data);
+    }
+
+    public static function del($code)
+    {
+        Db::startTrans();
+        try {
+            self::where(['code' => $code])->delete();
+            TaskWorkflowRule::where(['workflow_code' => $code])->delete();
+        } catch (\Exception $exception) {
+            Db::rollback();
+            return error(1, $exception->getMessage());
+        }
+        Db::commit();
+        return true;
+    }
 }
diff --git a/application/common/Model/TaskWorkflowRule.php b/application/common/Model/TaskWorkflowRule.php
index c631d5f..5420d55 100644
--- a/application/common/Model/TaskWorkflowRule.php
+++ b/application/common/Model/TaskWorkflowRule.php
@@ -11,6 +11,35 @@ class TaskWorkflowRule extends CommonModel
 {
     protected $append = [];
 
+    /**
+     * 保存规则
+     * @param $workflowCode
+     * @param $taskWorkflowRules Object
+     */
+    public static function saveRules($workflowCode, $taskWorkflowRules)
+    {
+        TaskWorkflowRule::createData($workflowCode, 1, $taskWorkflowRules->firstObj);
+        TaskWorkflowRule::createData($workflowCode, 2, $taskWorkflowRules->firstAction->value, $taskWorkflowRules->firstAction->action);
+        TaskWorkflowRule::createData($workflowCode, 3, $taskWorkflowRules->firstResult->value, $taskWorkflowRules->firstResult->action);
+        if ($taskWorkflowRules->lastResult->value) {
+            TaskWorkflowRule::createData($workflowCode, 4, $taskWorkflowRules->lastResult->value, $taskWorkflowRules->lastResult->action);
+        }
+    }
+
+    public static function createData($workflowCode, $sort, $objectCode = '', $action = 0, $type = 0)
+    {
+        $data = [
+            'create_time' => nowTime(),
+            'code' => createUniqueCode('TaskWorkflowRule'),
+            'type' => $type,
+            'object_code' => $objectCode,
+            'action' => $action,
+            'sort' => $sort,
+            'workflow_code' => $workflowCode,
+        ];
+        return self::create($data);
+    }
+
     public static function getActionByTaskType($type)
     {
         switch ($type) {
@@ -50,7 +79,7 @@ class TaskWorkflowRule extends CommonModel
                 $stage = TaskStages::where(['code' => $toStageCode])->find();
                 Task::taskHook(getCurrentMember()['code'], $task['code'], 'move', '', '', '', '', '', ['stageName' => $stage['name'], 'is_robot' => true]);
             }
-        } elseif ($do['action'] == 1) {
+        } elseif ($do['action'] == 3) {
             //指派给
             try {
                 TaskMember::inviteMember($do['object_code'], $task['code'], 1, 0, false, true);
diff --git a/application/project/behavior/Task.php b/application/project/behavior/Task.php
index f921bd6..00ad6fb 100644
--- a/application/project/behavior/Task.php
+++ b/application/project/behavior/Task.php
@@ -190,26 +190,31 @@ class Task
             if (in_array($data['type'], $workflowActions)) {
                 $taskStageCode = $task['stage_code'];
                 $action = TaskWorkflowRule::getActionByTaskType($data['type']);
-                $taskWorkflowRule = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->find();
-                if ($taskWorkflowRule) {
-                    $nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find();
-                    if ($nextTaskWorkflowRule) {
-                        if ($nextTaskWorkflowRule['action'] == $action) {
-                            $goon = true;
-                            if ($action == 3) {
-                                //设置执行人
-                                $toMemberCode = $toMember['code'];
-                                if ($toMemberCode != $nextTaskWorkflowRule['object_code']) {
-                                    $goon = false;
+                $taskWorkflowRules = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->select();
+                logRecord($taskWorkflowRules);
+                if ($taskWorkflowRules) {
+                    foreach ($taskWorkflowRules as $taskWorkflowRule) {
+//                        $taskWorkflowRule = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->find();
+                        if ($taskWorkflowRule) {
+                            $nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find();
+                            if ($nextTaskWorkflowRule) {
+                                if ($nextTaskWorkflowRule['action'] == $action) {
+                                    $goon = true;
+                                    if ($action == 3) {
+                                        //设置执行人
+                                        $toMemberCode = $toMember['code'];
+                                        if ($toMemberCode != $nextTaskWorkflowRule['object_code']) {
+                                            $goon = false;
+                                        }
+                                    }
+                                    if ($goon) {
+                                        $doTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 3])->find();
+                                        $doTaskWorkflowRule && TaskWorkflowRule::doAction($task, $doTaskWorkflowRule);
+                                    }
                                 }
                             }
-                            if ($goon) {
-                                $doTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 3])->find();
-                                $doTaskWorkflowRule && TaskWorkflowRule::doAction($task, $doTaskWorkflowRule);
-                            }
                         }
                     }
-
                 }
             }
         }
diff --git a/application/project/controller/TaskStages.php b/application/project/controller/TaskStages.php
index fcc26bf..504ba57 100644
--- a/application/project/controller/TaskStages.php
+++ b/application/project/controller/TaskStages.php
@@ -45,6 +45,18 @@ class TaskStages extends BasicApi
         $this->success('', $list);
     }
 
+    public function _getAll()
+    {
+        $where = [];
+        $code = Request::post('projectCode');
+        if (!$code) {
+            $this->error("请选择一个项目");
+        }
+        $where[] = ['project_code', '=', $code];
+        $list = $this->model->where($where)->select();
+        $this->success('', $list);
+    }
+
     /**
      * 显示资源列表
      * @return void
diff --git a/application/project/controller/TaskWorkflow.php b/application/project/controller/TaskWorkflow.php
index f16ac3c..0eea729 100644
--- a/application/project/controller/TaskWorkflow.php
+++ b/application/project/controller/TaskWorkflow.php
@@ -2,7 +2,9 @@
 
 namespace app\project\controller;
 
+use app\common\Model\TaskWorkflowRule;
 use controller\BasicApi;
+use think\Db;
 use think\facade\Request;
 
 /**
@@ -30,7 +32,7 @@ class TaskWorkflow extends BasicApi
             $this->error("请选择一个项目");
         }
         $where[] = ['project_code', '=', $code];
-        $list = $this->model->where($where)->select();
+        $list = $this->model->where($where)->order('id asc')->select();
         if ($list) {
             foreach ($list as &$item) {
                 unset($item['id']);
@@ -39,27 +41,36 @@ class TaskWorkflow extends BasicApi
         $this->success('', $list);
     }
 
+    public function _getTaskWorkflowRules()
+    {
+        $code = Request::post('taskWorkflowCode');
+        $list = TaskWorkflowRule::where(['workflow_code' => $code])->order('sort asc')->select();
+        $this->success('', $list);
+    }
+
 
     /**
      * 新增
      * @param Request $request
      * @return void
      */
-    public function save(Request $request)
+    public function save()
     {
-        $data = $request::only('name,projectCode');
-        if (!$request::post('name')) {
+        $projectCode = Request::param('projectCode');
+        $taskWorkflowName = Request::param('taskWorkflowName');
+        $taskWorkflowRules = Request::param('taskWorkflowRules', '');
+        if (!trim($taskWorkflowName)) {
             $this->error("请填写规则名称");
         }
-        try {
-            $result = $this->model->createStage($data['name'], $data['projectCode']);
-        } catch (\Exception $e) {
-            $this->error($e->getMessage(), $e->getCode());;
+        if (!$taskWorkflowRules) {
+            $this->error("请定义具体规则");
         }
-        if ($result) {
-            $this->success('添加成功', $result);
-        }
-        $this->error("操作失败,请稍候再试!");
+        $taskWorkflow = \app\common\Model\TaskWorkflow::createData($taskWorkflowName, $projectCode, getCurrentOrganizationCode());
+        $taskWorkflow = \app\common\Model\TaskWorkflow::get($taskWorkflow['id']);
+        $taskWorkflowRules = json_decode($taskWorkflowRules);
+        TaskWorkflowRule::saveRules($taskWorkflow['code'], $taskWorkflowRules);
+        $this->success('添加成功', []);
+//        $this->error("操作失败,请稍候再试!");
     }
 
     /**
@@ -70,24 +81,37 @@ class TaskWorkflow extends BasicApi
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\exception\DbException
      */
-    public function edit(Request $request)
+    public function edit()
     {
-        $data = $request::only('name,stageCode');
-        if (!$request::post('name')) {
+        $taskWorkflowCode = Request::param('taskWorkflowCode');
+        $taskWorkflowName = Request::param('taskWorkflowName');
+        $taskWorkflowRules = Request::param('taskWorkflowRules', '');
+        $this->success('');
+
+        if (!trim($taskWorkflowName)) {
             $this->error("请填写规则名称");
         }
-        if (!$data['stageCode']) {
-            $this->error("请选择一个规则");
+        if (!$taskWorkflowRules) {
+            $this->error("请定义具体规则");
         }
-        $template = $this->model->where(['code' => $data['stageCode']])->field('id')->find();
-        if (!$template) {
-            $this->error("该规则已失效");
+        $taskWorkflow = \app\common\Model\TaskWorkflow::where(['code' => $taskWorkflowCode])->find();
+        if (!$taskWorkflow) {
+            $this->error("操作失败,请稍候再试!");
         }
-        $result = $this->model->_edit(['name' => $data['name']], ['code' => $data['stageCode']]);
-        if ($result) {
-            $this->success('');
+        Db::startTrans();
+        try {
+            $taskWorkflow->name = $taskWorkflowName;
+            $taskWorkflow->update_time = nowTime();
+            $taskWorkflow->save();
+            TaskWorkflowRule::where(['workflow_code' => $taskWorkflowCode])->delete();
+            $taskWorkflowRules = json_decode($taskWorkflowRules);
+            TaskWorkflowRule::saveRules($taskWorkflow['code'], $taskWorkflowRules);
+        } catch (\Exception $exception) {
+            Db::rollback();
+            $this->error("操作失败,请稍候再试!");
         }
-        $this->error("操作失败,请稍候再试!");
+        Db::commit();
+        $this->success('');
     }
 
     /**
@@ -96,17 +120,14 @@ class TaskWorkflow extends BasicApi
      */
     public function delete()
     {
-        $code = Request::post('code');
+        $code = Request::post('taskWorkflowCode');
         if (!$code) {
             $this->error("请选择一个规则");
         }
-        try {
-            $result = $this->model->deleteStage($code);
-        } catch (\Exception $e) {
-            $this->error($e->getMessage(), $e->getCode());;
-        }
-        if ($result) {
-            $this->success('');
+        $result = \app\common\Model\TaskWorkflow::del($code);
+        if (!isError($result)) {
+            $this->success('删除成功');
         }
+        $this->error("操作失败,请稍候再试!");
     }
 }