drag-create-api/app/admin/logic/Code/CodeModuleCategoryAudit.php
2023-08-03 10:54:31 +08:00

124 lines
5.7 KiB
PHP

<?php
namespace app\admin\logic\Code;
use app\common\model\Code\CodeModuleCategory as ModelCodeModuleCategory;
use app\common\logic\CustomerMessage\Audit\CodeModuleCategory as CustomerMessageAuditCodeModuleCategory;
use app\common\exception\Tool;
class CodeModuleCategoryAudit
{
/**
* 代码块类目审核处理
*
* @param array $code_module_category_guids 类目guid数组
* @param int $code_module_category_audit 审核状态
* @param string $ps 附言
* @return void
* @date 2023-07-05
* @author xjh
* @since 1.0.0
*/
public static function handleAudit(array $code_module_category_guids, int $code_module_category_audit, string $ps): void
{
foreach ($code_module_category_guids as $code_module_category_guid) {
//类目审核基本信息验证
$module_category = ModelCodeModuleCategory::find($code_module_category_guid);
if (!$module_category) {
throwErrorMsg("含有不存在的类目!");
}
if ($module_category->code_module_category_audit != ModelCodeModuleCategory::AUDIT_UNAUDITED) {
throwErrorMsg("含有已审核过的的类目!");
}
//主类目审核处理
if ($module_category->code_module_category_parent_guid == ModelCodeModuleCategory::MASTER_DEFAULT) {
self::handleMainCategory($module_category, $code_module_category_audit, $ps);
}
//子类目审核处理
else {
self::handleSonCategory($module_category, $code_module_category_audit, $ps);
}
}
}
/**
* 代码块主类目审核处理
*
* @param ModelCodeModuleCategory $module_category 模型实例
* @param int $code_module_category_audit 审核状态
* @param string $ps 附言
* @return void
* @date 2023-07-05
* @author xjh
* @since 1.0.0
*/
public static function handleMainCategory(ModelCodeModuleCategory $module_category, int $code_module_category_audit, string $ps): void
{
//主类目的审核将会直接将所属的子类目们审核状态连带修改
//(例: 主类目审核通过则子类目将会全员审核状态为通过)
//【主类目更新】
$module_category->code_module_category_audit = $code_module_category_audit;
$module_category->save();
CustomerMessageAuditCodeModuleCategory::sendAuditMessage($module_category->code_module_category_guid, $code_module_category_audit, $ps);
//【所属子类目们的更新】
$son_code_module_category_guids = ModelCodeModuleCategory::where('code_module_category_parent_guid', $module_category->code_module_category_guid)
->column('code_module_category_guid');
foreach ($son_code_module_category_guids as $son_code_module_category_guid) {
ModelCodeModuleCategory::update([
'code_module_category_audit' => $code_module_category_audit,
'code_module_category_guid' => $son_code_module_category_guid,
]);
CustomerMessageAuditCodeModuleCategory::sendAuditMessage($son_code_module_category_guid, $code_module_category_audit, $ps);
}
}
/**
* 代码块子类目审核处理
*
* @param ModelCodeModuleCategory $module_category 模型实例
* @param int $code_module_category_audit 审核状态
* @param string $ps 附言
* @return void
* @date 2023-07-05
* @author xjh
* @since 1.0.0
*/
public static function handleSonCategory(ModelCodeModuleCategory $module_category, int $code_module_category_audit, string $ps): void
{
//【当前子类目更新】
$module_category->code_module_category_audit = $code_module_category_audit;
$module_category->save();
CustomerMessageAuditCodeModuleCategory::sendAuditMessage($module_category->code_module_category_guid, $code_module_category_audit, $ps);
//【所属主类目更新】
$module_category_parent_guid = $module_category->code_module_category_parent_guid;
//当前主类目含未审核子类目的数量
$count = ModelCodeModuleCategory::scope('auditUnaudited')->where([
['code_module_category_parent_guid', '=', $module_category_parent_guid],
['code_module_category_guid', '<>', $module_category->code_module_category_guid],
])->count();
//当未审核子类目的数量为0的时候
//【场景一】若此时所有的已审核子类目状态都为不通过,则自动将所属的主类目审核状态设置为不通过
//【场景二】若此时所有的已审核子类目状态包含着通过与不通过的状态,则自动将所属的主类目审核状态设置为通过
if ($count == 0) {
$son_category_audits = ModelCodeModuleCategory::scope('auditPass')->where('code_module_category_parent_guid', $module_category_parent_guid)
->column('code_module_category_audit');
//是否全部子类目审核都不通过
$is_all_not_failed = true;
foreach ($son_category_audits as $son_category_audit) {
if ($son_category_audit != ModelCodeModuleCategory::AUDIT_FAILED) {
$is_all_not_failed = false;
break;
}
}
$audit = $is_all_not_failed ? ModelCodeModuleCategory::AUDIT_FAILED : ModelCodeModuleCategory::AUDIT_PASS;
ModelCodeModuleCategory::update([
'code_module_category_guid' => $module_category_parent_guid,
'code_module_category_audit' => $audit,
]);
CustomerMessageAuditCodeModuleCategory::sendAuditMessage($module_category_parent_guid, $audit);
}
}
}