Merge branch 'feature/mzl_dev' into develop

# Conflicts:
#	app/admin/service/SysDictService.php
This commit is contained in:
mzl
2025-09-09 13:47:04 +08:00
parent 2cd613eea6
commit f5e93f2400
23 changed files with 1929 additions and 1427 deletions
@@ -22,14 +22,14 @@ class ApprovalFlowsController extends BaseController
* @param array $data
* @return array
*/
protected function indexFormat($data)
{
if (isset($data['created_at']))
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
return $data;
}
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
public function desc()
@@ -0,0 +1,33 @@
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\admin\service\ApprovalInstancesService;
class ApprovalInstancesController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new ApprovalInstancesService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
@@ -0,0 +1,75 @@
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\admin\service\ApprovalRecordsService;
use app\admin\service\SysDictService;
use app\common\model\SysDictItem;
use support\Request;
class ApprovalRecordsController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new ApprovalRecordsService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
protected function indexFormat($data)
{
if (isset($data['records']) && count($data['records']) > 0) {
foreach ($data['records'] as $key => &$record) {
$result = [
'id' => $record['id'],
'flows_name' => $record['approvalInstance']['flows']['name'],
'instance_id' => $record['instance_id'],
'biz_id' => $record['approvalInstance']['biz_id'],
'biz_desc' => '备货单【'.$record['approvalInstance']['bizInfo']['order_no'].'】',
'biz_type' => $record['approvalInstance']['flow_type'],
'initiator_name' => $record['approvalInstance']['initiator']['name'],
'initiator_avatar' => $record['approvalInstance']['initiator']['avatar'],
'approver_name' => $record['approver']['name'],
'approver_avatar' => $record['approver']['avatar'],
'step_index' => $record['step_index'],
'action' => $record['action'],
'status' => SysDictItem::where('item_value', $record['status'])->where('status', 1)->value('item_label'),
'comment' => $record['comment'],
'approved_at' => $record['approved_at'],
'finished_at' => $record['approvalInstance']['finished_at'],
'deadline_at' => $record['approvalInstance']['deadline_at'],
'created_at' => $record['created_at']
];
$record = $result;
}
}
return $data;
}
public function approve(Request $request)
{
try {
$res = $this->service->serviceApprove($request);
return $this->success([], $res['msg']);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
}
@@ -0,0 +1,17 @@
<?php
namespace app\admin\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\admin\model\ApprovalInstances;
class ApprovalInstancesLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = ApprovalInstances::class;
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace app\admin\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\admin\model\ApprovalRecords;
class ApprovalRecordsLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = ApprovalRecords::class;
}
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace app\admin\model;
use app\common\model\StockOrders;
use app\common\model\User;
use support\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ApprovalInstances extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'approval_instances';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
// protected $fillable = ['id', 'flow_type', 'biz_id', 'biz_type', 'form_data', 'initiator_id', 'status', 'current_step', 'finished_at', 'deadline_at', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
protected $fillable = [
'flow_type',
'biz_id',
'biz_type',
'form_data',
'initiator_id',
'status',
'current_step',
'finished_at',
'deadline_at',
];
protected $casts = [
'form_data' => 'array', // 自动转换 JSON 字段为数组
'finished_at' => 'datetime',
'deadline_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* 一个审批实例可以有多个审批记录
*/
public function approvalRecords(): HasMany
{
return $this->hasMany(ApprovalRecords::class, 'instance_id');
}
/**
* 获取当前步骤的所有审批记录(pending 状态)
*/
public function currentStepRecords()
{
return $this->approvalRecords()
->where('step_index', $this->current_step)
->where('status', 'pending');
}
public function flows()
{
return $this->belongsTo(ApprovalFlows::class, 'flow_type', 'type');
}
public function initiator()
{
return $this->belongsTo(User::class, 'initiator_id', 'id');
}
public function bizInfo()
{
return $this->hasOne(StockOrders::class, 'id', 'biz_id');
}
}
+84
View File
@@ -0,0 +1,84 @@
<?php
namespace app\admin\model;
use app\common\model\User;
use support\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ApprovalRecords extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'approval_records';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
// protected $fillable = ['id', 'instance_id', 'approver_id', 'step_index', 'action', 'status', 'comment', 'approved_at', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
protected $fillable = [
'instance_id',
'approver_id',
'step_index',
'action',
'status',
'comment',
'approved_at',
];
protected $casts = [
'approved_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* 一条审批记录属于一个审批实例
*/
public function approvalInstance(): BelongsTo
{
return $this->belongsTo(ApprovalInstances::class, 'instance_id');
}
/**
* 关联审批人
*/
public function approver()
{
return $this->belongsTo(User::class, 'approver_id');
}
}
@@ -0,0 +1,31 @@
<?php
namespace app\admin\service;
use app\admin\validate\ApprovalInstancesValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\admin\model\ApprovalInstances;
class ApprovalInstancesService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = ApprovalInstances::class;
/**
* 验证器
*/
protected $validator = ApprovalInstancesValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
}
@@ -0,0 +1,75 @@
<?php
namespace app\admin\service;
use app\admin\validate\ApprovalRecordsValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\admin\model\ApprovalRecords;
use support\Request;
class ApprovalRecordsService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = ApprovalRecords::class;
/**
* 验证器
*/
protected $validator = ApprovalRecordsValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
$config['order']['id'] = 'desc';
$config['with'] = [
'approvalInstance',
'approver',
'approvalInstance.flows',
'approvalInstance.initiator',
'approvalInstance.bizInfo'
];
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
case 'update':
$config['where']['id'] = $p['id'];
break;
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
public function serviceApprove(Request $request)
{
$param = $request->all();
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
return $appro->approve($param['instance_id'], $request->user->uid, $param['action'], $param['comment']);
}
}
+88
View File
@@ -5,6 +5,9 @@ namespace app\admin\service;
use app\admin\validate\SysDictValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\SysDict;
use app\common\model\SysDictCategory;
use app\common\utils\translation\service\TranslationService;
use app\queue\utils\Translat;
use app\common\utils\translation\service\TranslationService;
use app\queue\utils\Translat;
use support\Request;
@@ -151,4 +154,89 @@ class SysDictService
$traitsourceFields
);
}
/*
示例用法:
// 1) 获取分类为“车辆类型”的所有字典
$list = $dictService->getDictsByCategoryName('车辆类型');
// => [['dict_code' => 'SUV', 'dict_name' => '越野车'], …]
// 2) 获取某分类下某个 code 的名称
$name = $dictService->getDictName('车辆类型', 'SUV');
// => "越野车"
// 3) 获取某分类下拉选项
$options = $dictService->getDictOptions('车辆类型');
// => ['SUV' => '越野车', 'SEDAN' => '轿车', …]
// 4) 通过 code 直接拿名称(不管分类)
$name = $dictService->getNameByCode('SUV');
// => "越野车"
*/
/**
* 根据分类名获取字典列表
*/
public function getDictsByCategoryName(string $categoryName): array
{
$category = SysDictCategory::where('category_name', $categoryName)
->where('status', 1)
->first();
if (!$category) {
return [];
}
return $category->dicts()->get(['dict_code', 'dict_name'])->toArray();
}
/**
* 根据分类名 + 字典code 获取字典项名称
*/
public function getDictName(string $categoryName, string $dictCode): ?string
{
$dict = SysDict::whereHas('category', function ($q) use ($categoryName) {
$q->where('category_name', $categoryName)->where('status', 1);
})
->where('dict_code', $dictCode)
->where('status', 1)
->whereNull('deleted_at')
->first();
return $dict?->dict_name;
}
/**
* 根据分类名获取下拉键值对
* 返回格式:['code1' => '名称1', 'code2' => '名称2']
*/
public function getDictOptions(string $categoryName): array
{
$dicts = $this->getDictsByCategoryName($categoryName);
$options = [];
foreach ($dicts as $item) {
$options[$item['dict_code']] = $item['dict_name'];
}
return $options;
}
/**
* 根据字典 code 获取对应的名称(不依赖分类)
*/
public function getNameByCode(string $dictCode): ?string
{
$dict = SysDict::where('dict_code', $dictCode)
->where('status', 1)
->whereNull('deleted_at')
->first();
return $dict?->dict_name;
}
}
@@ -0,0 +1,29 @@
<?php
namespace app\admin\validate;
use think\Validate;
class ApprovalInstancesValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,29 @@
<?php
namespace app\admin\validate;
use think\Validate;
class ApprovalRecordsValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}