Home >PHP Framework >ThinkPHP >How to manage the review process in ThinkPHP6?

How to manage the review process in ThinkPHP6?

WBOY
WBOYOriginal
2023-06-12 09:31:541846browse

With the development of the Internet, more and more companies are beginning to use the Internet for business processing, which requires companies to have a complete audit process management system to ensure the security and standardization of business. In PHP development, the ThinkPHP6 framework provides convenient audit process management functions. This article will introduce how to implement audit process management in ThinkPHP6.

1. Basic idea of ​​ThinkPHP6 audit process management

The basic idea of ​​​​ThinkPHP6 audit process management is achieved through database records. Generally, two data tables need to be created:

  • Process table: record the basic information of the audit process, such as process name, creator, creation time, etc.;
  • Step table: record the specific audit steps in the audit process, including the name, status, and Processor, processing time, etc.

The process of review process management can be briefly described as follows:

  • Create review process: The administrator creates the review process in the background and sets the name and processing of each review step Person and other information;
  • Submit for review: The user submits the review application, and the system starts the review according to the review process;
  • The review steps in the review process: According to the information recorded in the process table and step table, automatically Assign reviewers to conduct the review;
  • Audit results: pass or fail the review, and finally obtain the review result.

2. Create the process table and step table

First, we need to create the process table and step table in the database.

Process table:

CREATE TABLE `tp_flow` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(50) DEFAULT NULL COMMENT '流程名称',
  `create_user_id` int(11) DEFAULT NULL COMMENT '创建人ID',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='审核流程表';

Step table:

CREATE TABLE `tp_step` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `flow_id` int(11) DEFAULT NULL COMMENT '流程ID',
  `name` varchar(50) DEFAULT NULL COMMENT '步骤名称',
  `status` tinyint(1) DEFAULT '0' COMMENT '状态:0-未处理,1-已处理',
  `handler_id` int(11) DEFAULT NULL COMMENT '处理人ID',
  `handle_time` datetime DEFAULT NULL COMMENT '处理时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='审核步骤表';

3. Create the model class

Next, we need to create the model class, define the process table and The relationship between the step table and implement various operation methods.

  1. Create process model class

First, we create the process model class FlowModel, define a one-to-many relationship with the step model class StepModel, and provide process management related methods.

// ppmodelFlowModel.php
namespace appmodel;

use thinkModel;

class FlowModel extends Model
{
    protected $table = 'tp_flow';

    // 定义与StepModel的一对多关系
    public function steps()
    {
        return $this->hasMany('StepModel', 'flow_id', 'id');
    }

    // 创建审核流程
    public function addFlow($data)
    {
        return $this->save($data);
    }

    // 编辑审核流程
    public function editFlow($id, $data)
    {
        return $this->where('id', $id)->update($data);
    }

    // 删除审核流程
    public function delFlow($id)
    {
        return $this->where('id', $id)->delete();
    }

    // 按照ID获取审核流程详情
    public function getFlowById($id)
    {
        return $this->with('steps')->find($id);
    }

    // 获取审核流程列表
    public function getFlowList()
    {
        return $this->with('steps')->select();
    }
}

2. Create the step model class

Then, we create the step model class StepModel, define the belonging relationship with the process model class FlowModel, and provide methods related to the audit steps.

// ppmodelStepModel.php
namespace appmodel;

use thinkModel;

class StepModel extends Model
{
    protected $table = 'tp_step';

    // 定义与FlowModel的属于关系
    public function flow()
    {
        return $this->belongsTo('FlowModel', 'flow_id');
    }

    // 添加审核步骤
    public function addStep($data)
    {
        return $this->save($data);
    }

    // 编辑审核步骤
    public function editStep($id, $data)
    {
        return $this->where('id', $id)->update($data);
    }

    // 删除审核步骤
    public function delStep($id)
    {
        return $this->where('id', $id)->delete();
    }

    // 按照ID获取审核步骤详情
    public function getStepById($id)
    {
        return $this->find($id);
    }

    // 获取审核步骤列表
    public function getStepListByFlowId($flow_id)
    {
        return $this->where('flow_id', $flow_id)->select();
    }

    // 更新审核步骤状态
    public function updateStepStatus($id, $status, $handler_id, $handle_time)
    {
        $data = [
            'status' => $status,
            'handler_id' => $handler_id,
            'handle_time' => $handle_time,
        ];
        return $this->where('id', $id)->update($data);
    }
}

3. Implementation of the audit process

In the implementation of the audit process, we need to call the methods of the process and step model classes in the controller or service layer to complete each step of the audit process step.

  1. Create an audit process

When the administrator creates an audit process in the background, he needs to create the process first and then add steps.

// ppcontrollerFlowController.php
namespace appcontroller;

use appBaseController;
use appmodelFlowModel;
use appmodelStepModel;
use thinkRequest;

class FlowController extends BaseController
{
    protected $flowModel;
    protected $stepModel;

    public function __construct(FlowModel $flowModel, StepModel $stepModel)
    {
        $this->flowModel = $flowModel;
        $this->stepModel = $stepModel;
    }

    // 创建审核流程
    public function addFlow(Request $request)
    {
        $data = $request->post();

        // 添加审核流程
        $flow_result = $this->flowModel->addFlow([
            'name' => $data['name'],
            'create_user_id' => $this->getCurrentUserId(),
            'create_time' => date('Y-m-d H:i:s'),
        ]);
        if (!$flow_result) {
            return $this->error('创建审核流程失败!');
        }

        // 添加审核步骤
        $step_data = [];
        foreach ($data['step'] as $key => $value) {
            $step_data[] = [
                'flow_id' => $this->flowModel->id,
                'name' => $value['name'],
                'handler_id' => $value['handler_id'],
            ];
        }
        $step_result = $this->stepModel->saveAll($step_data);
        if (!$step_result) {
            return $this->error('添加审核步骤失败!');
        }

        return $this->success('创建审核流程成功!');
    }
}
  1. Submit for review

After the user submits the review application, the user needs to automatically trigger the review process and let the review process start running.

// ppcontrollerApplyController.php
namespace appcontroller;

use appBaseController;
use appmodelStepModel;
use thinkRequest;

class ApplyController extends BaseController
{
    protected $stepModel;

    public function __construct(StepModel $stepModel)
    {
        $this->stepModel = $stepModel;
    }

    // 提交审核
    public function submitApply(Request $request)
    {
        $data = $request->post();

        // 获取审核流程的第一步骤
        $steps = $this->stepModel->getStepListByFlowId($data['flow_id']);
        if (empty($steps)) {
            return $this->error('该审核流程未添加步骤!');
        }
        $first_step = $steps[0];

        // 更新第一步骤状态
        $update_result = $this->stepModel->updateStepStatus($first_step->id, 1, $this->getCurrentUserId(), date('Y-m-d H:i:s'));
        if (!$update_result) {
            return $this->error('更新审核步骤状态失败!');
        }

        return $this->success('提交审核成功!');
    }
}
  1. Audit steps in the audit process

The system automatically assigns auditors to conduct the audit according to the steps defined in the audit process, and records the audit results.

// ppcontrollerApproveController.php
namespace appcontroller;

use appBaseController;
use appmodelStepModel;
use thinkRequest;

class ApproveController extends BaseController
{
    protected $stepModel;

    public function __construct(StepModel $stepModel)
    {
        $this->stepModel = $stepModel;
    }

    // 审核步骤
    public function approveStep(Request $request)
    {
        $data = $request->post();

        // 获取当前步骤
        $step = $this->stepModel->getStepById($data['step_id']);

        // 更新当前步骤状态
        $update_result = $this->stepModel->updateStepStatus($data['step_id'], $data['status'], $this->getCurrentUserId(), date('Y-m-d H:i:s'));
        if (!$update_result) {
            return $this->error('更新审核步骤状态失败!');
        }

        // 获取下一步骤
        $next_step = $this->stepModel->where('flow_id', $step->flow_id)->where('id', '>', $data['step_id'])->order('id asc')->find();
        if (!$next_step) {
            return $this->success('已审核完成!');
        }

        // 更新下一步骤状态
        $update_result = $this->stepModel->updateStepStatus($next_step->id, 1, $next_step->handler_id, null);
        if (!$update_result) {
            return $this->error('更新审核步骤状态失败!');
        }

        return $this->success('审核通过!');
    }
}

4. Summary

Through the above code examples, we can see that the audit process management function is very conveniently implemented in ThinkPHP6, through the record management of process tables and step tables, and model classes Using this method, we can quickly and simply complete a complete review process management system.

The above is the detailed content of How to manage the review process in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn