ProcuremenOperLog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\common\library;
  3. use think\Db;
  4. use think\Log;
  5. /**
  6. * 协助采购操作日志(下发 / 确认供应商 / 审核确认供应商 等追溯)
  7. */
  8. class ProcuremenOperLog
  9. {
  10. /**
  11. * @param array{0:int,1:string} $admin [id, name]
  12. */
  13. public static function write(int $scydgyId, string $action, string $content, ?int $purchaseOrderId, array $admin): void
  14. {
  15. if (!ProcuremenGuard::isValidScydgyId($scydgyId) || $content === '') {
  16. return;
  17. }
  18. $adminId = (int)($admin[0] ?? 0);
  19. $adminName = self::cut((string)($admin[1] ?? '未知用户'), 64);
  20. try {
  21. Db::table('purchase_order_oper_log')->insert([
  22. 'scydgy_id' => $scydgyId,
  23. 'purchase_order_id' => $purchaseOrderId,
  24. 'admin_id' => $adminId,
  25. 'admin_name' => $adminName,
  26. 'action' => self::cut($action, 64),
  27. 'content' => self::cut($content, 1000),
  28. 'createtime' => ProcuremenTime::nowDateTime(),
  29. ]);
  30. } catch (\Throwable $e) {
  31. Log::write('procuremen oper log: ' . $e->getMessage(), 'error');
  32. }
  33. }
  34. /**
  35. * @param string|array<int, string> $actions
  36. */
  37. public static function resolveLatestTime(int $scydgyId, $actions): string
  38. {
  39. if (!ProcuremenGuard::isValidScydgyId($scydgyId)) {
  40. return '';
  41. }
  42. $acts = is_array($actions) ? $actions : [$actions];
  43. $acts = array_values(array_filter(array_map('strval', $acts)));
  44. if ($acts === []) {
  45. return '';
  46. }
  47. try {
  48. $log = Db::table('purchase_order_oper_log')
  49. ->where('scydgy_id', $scydgyId)
  50. ->where('action', 'in', $acts)
  51. ->order('id', 'desc')
  52. ->find();
  53. if (is_array($log)) {
  54. $text = ProcuremenTime::formatDisplayDateTime($log['createtime'] ?? null);
  55. if ($text !== '') {
  56. return $text;
  57. }
  58. }
  59. } catch (\Throwable $e) {
  60. }
  61. return '';
  62. }
  63. public static function resolveMarkCompleteTime(int $scydgyId): string
  64. {
  65. return self::resolveLatestTime($scydgyId, 'mark_complete');
  66. }
  67. /**
  68. * @param array<int, array<string, mixed>> $logs
  69. * @return array<int, array<string, mixed>>
  70. */
  71. public static function formatLogRowsForDisplay(array $logs): array
  72. {
  73. foreach ($logs as &$lg) {
  74. if (!is_array($lg)) {
  75. continue;
  76. }
  77. $lg['createtime_text'] = ProcuremenTime::formatDisplayDateTime($lg['createtime'] ?? null);
  78. $lg['content'] = ProcuremenTime::formatOperLogContent(
  79. (string)($lg['content'] ?? ''),
  80. (string)($lg['action'] ?? '')
  81. );
  82. }
  83. unset($lg);
  84. return $logs;
  85. }
  86. private static function cut(string $s, int $max): string
  87. {
  88. if (function_exists('mb_substr')) {
  89. return mb_substr($s, 0, $max, 'UTF-8');
  90. }
  91. return strlen($s) <= $max ? $s : substr($s, 0, $max);
  92. }
  93. }