| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace app\common\library;
- use think\Db;
- use think\Log;
- /**
- * 协助采购操作日志(下发 / 确认供应商 / 审核确认供应商 等追溯)
- */
- class ProcuremenOperLog
- {
- /**
- * @param array{0:int,1:string} $admin [id, name]
- */
- public static function write(int $scydgyId, string $action, string $content, ?int $purchaseOrderId, array $admin): void
- {
- if (!ProcuremenGuard::isValidScydgyId($scydgyId) || $content === '') {
- return;
- }
- $adminId = (int)($admin[0] ?? 0);
- $adminName = self::cut((string)($admin[1] ?? '未知用户'), 64);
- try {
- Db::table('purchase_order_oper_log')->insert([
- 'scydgy_id' => $scydgyId,
- 'purchase_order_id' => $purchaseOrderId,
- 'admin_id' => $adminId,
- 'admin_name' => $adminName,
- 'action' => self::cut($action, 64),
- 'content' => self::cut($content, 1000),
- 'createtime' => ProcuremenTime::nowDateTime(),
- ]);
- } catch (\Throwable $e) {
- Log::write('procuremen oper log: ' . $e->getMessage(), 'error');
- }
- }
- /**
- * @param string|array<int, string> $actions
- */
- public static function resolveLatestTime(int $scydgyId, $actions): string
- {
- if (!ProcuremenGuard::isValidScydgyId($scydgyId)) {
- return '';
- }
- $acts = is_array($actions) ? $actions : [$actions];
- $acts = array_values(array_filter(array_map('strval', $acts)));
- if ($acts === []) {
- return '';
- }
- try {
- $log = Db::table('purchase_order_oper_log')
- ->where('scydgy_id', $scydgyId)
- ->where('action', 'in', $acts)
- ->order('id', 'desc')
- ->find();
- if (is_array($log)) {
- $text = ProcuremenTime::formatDisplayDateTime($log['createtime'] ?? null);
- if ($text !== '') {
- return $text;
- }
- }
- } catch (\Throwable $e) {
- }
- return '';
- }
- public static function resolveMarkCompleteTime(int $scydgyId): string
- {
- return self::resolveLatestTime($scydgyId, 'mark_complete');
- }
- /**
- * @param array<int, array<string, mixed>> $logs
- * @return array<int, array<string, mixed>>
- */
- public static function formatLogRowsForDisplay(array $logs): array
- {
- foreach ($logs as &$lg) {
- if (!is_array($lg)) {
- continue;
- }
- $lg['createtime_text'] = ProcuremenTime::formatDisplayDateTime($lg['createtime'] ?? null);
- $lg['content'] = ProcuremenTime::formatOperLogContent(
- (string)($lg['content'] ?? ''),
- (string)($lg['action'] ?? '')
- );
- }
- unset($lg);
- return $logs;
- }
- private static function cut(string $s, int $max): string
- {
- if (function_exists('mb_substr')) {
- return mb_substr($s, 0, $max, 'UTF-8');
- }
- return strlen($s) <= $max ? $s : substr($s, 0, $max);
- }
- }
|