'int', 'code_module_guid' => 'string', 'code_module_category_guid' => 'string', 'code_module_name' => 'string', 'code_module_html' => '', 'code_module_style' => '', 'code_module_script' => '', 'code_module_sort' => 'int', 'code_module_audit' => 'int', 'code_module_create_time' => 'datetime', 'code_module_create_user_guid' => 'string', 'code_module_update_time' => 'datetime', 'code_module_update_user_guid' => 'string', 'code_module_delete_time' => 'datetime', 'code_module_delete_user_guid' => 'string', ]; // 设置json类型字段 protected $json = ['']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = 'datetime'; // 创建时间 protected $createTime = 'code_module_create_time'; // 修改时间 protected $updateTime = 'code_module_update_time'; //排序字段 public $order_field = 'code_module_sort'; // excel导入/下载模板表头 public const EXCELFIELD = [ 'code_module_category_guid' => '类目', 'code_module_name' => '代码块名称', 'code_module_html' => 'html内容', 'code_module_style' => 'style内容', 'code_module_script' => 'script内容', 'code_module_sort' => '排序', ]; /** * 新增前 */ public static function onBeforeInsert(self $model): void { Tool::dataAddSortProc($model); $model->completeCreateField(); } /** * 更新前 */ public static function onBeforeUpdate(self $model): void { Tool::dataEditSortProc($model); $model->completeUpdateField(); } /** * 删除前 */ public static function onBeforeDelete(self $model): void { Tool::dataDeleteSortProc($model); $model->completeDeleteField(); } /** * 导出Excel * * @param array $select 导出的数据 */ public static function exportExcel(array $select): void { $data = [[ '类目', '代码块名称', 'html内容', 'style内容', 'script内容', '排序', '审核状态' ]]; foreach ($select as $key => $val) { // 根据字典值获取审核状态名称 $audit_status = ModelDictionary::getDictionaryData('audit_status'); $val['code_module_audit'] = ModelDictionary::getDataDictionaryName($audit_status, $val['code_module_audit']); $data[] = [ $val['code_module_category_name'], $val['code_module_name'], $val['code_module_html'], $val['code_module_style'], $val['code_module_script'], $val['code_module_sort'], $val['code_module_audit'], ]; } $excel = (new Excel())->exporTsheet($data); $excel->save('代码块.xlsx'); } /** * 导入excel * * @param \app\common\arw\adjfut\src\UploadFile $file excel */ public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string { $msg = []; Db::startTrans(); Tool::adminLockTableWrite(['code_module', 'dictionary', 'code_module_category']); try { $excel = new Excel($file); $data = $excel->parseExcel( Tool::getExcelRule(self::EXCELFIELD), ['titleLine' => [1]] ); if (!$data) throwErrorMsg('excel无数据', 1); $msg = []; foreach ($data as $line => $value) { try { $model = self::importExcelInit($value); $msg[] = "{$line} 代码库:【{$value['code_module_name']}】新增成功!
"; } catch (\Throwable $th) { $msg[] = "{$line} {$th->getMessage()}
"; } } Db::commit(); Tool::unlockTable(); return implode(', ', $msg); } catch (\Throwable $th) { Db::rollback(); Tool::unlockTable(); throw $th; } } /** * 导入excel初始化 * * @param array $value excel每行数据 */ public static function importExcelInit(array $value): void { $code_module_category_guid = $value['code_module_category_guid']; $code_module_name = $value['code_module_name']; $code_module_html = $value['code_module_html']; $code_module_style = $value['code_module_style']; $code_module_script = $value['code_module_script']; $code_module_sort = $value['code_module_sort']; // $code_module_audit = $value['code_module_audit']; $code_module_catetory = ModelCodeModuleCategory::where('code_module_category_name', $code_module_category_guid)->find(); if (!$code_module_catetory) throwErrorMsg($code_module_category_guid . "不存在,请重新确认!"); $code_module_category_guid = $code_module_catetory->code_module_category_guid; // $audit_status = ModelDictionary::getDictionaryData('audit_status'); // $code_module_audit = ModelDictionary::getDataDictionaryValue($audit_status, $code_module_category_guid); $code_module_audit = 2; self::create( [ 'code_module_category_guid' => $code_module_category_guid, 'code_module_name' => $code_module_name, 'code_module_html' => $code_module_html, 'code_module_style' => $code_module_style, 'code_module_script' => $code_module_script, 'code_module_sort' => $code_module_sort, 'code_module_audit' => $code_module_audit, ], [ 'code_module_category_guid', 'code_module_name', 'code_module_html', 'code_module_style', 'code_module_script', 'code_module_sort', 'code_module_audit', 'code_module_guid', 'code_module_create_user_guid', 'code_module_update_user_guid' ] ); } /** * 审核代码块 */ public static function auditCodeModule($params) { $params_audit_status = $params['code_module_audit']; $code_module_guids_arr = explode(',', $params['code_module_guid']); Db::startTrans(); try { if (count($code_module_guids_arr) > 1) { foreach ($code_module_guids_arr as $key => $value) { self::audit($value, $params); } } else { self::audit($params['code_module_guid'], $params); } Db::commit(); } catch (\Throwable $th) { Db::rollback(); throw $th; } } /** * 审核 */ public static function audit($value, $params) { $model = self::where('code_module_guid', $value)->find(); if (!$model) throwErrorMsg("该代码块不存在", 1); $audit_status = $model->code_module_audit; $code_module_name = $model->code_module_name; if ($audit_status == 2) throwErrorMsg("{$code_module_name} 代码块已通过审核!"); if ($audit_status == 3) throwErrorMsg("{$code_module_name} 代码块已驳回审核!"); $model->allowField([ 'code_module_audit', ])->save($params); } }