| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace app\common\library;
- use think\Db;
- /**
- * 协助采购时间:操作日志、完结时间统一为可读 YYYY-MM-DD HH:mm:ss(不再显示 Unix 时间戳)
- */
- class ProcuremenTime
- {
- public static function nowDateTime(): string
- {
- return date('Y-m-d H:i:s');
- }
- /** @deprecated 使用 {@see nowDateTime} */
- public static function todayYmd(): string
- {
- return self::nowDateTime();
- }
- /** 列表/详情展示:年月日 + 时分秒 */
- public static function formatDisplayDateTime($value): string
- {
- if ($value === null || $value === '') {
- return '';
- }
- if (is_numeric($value) && (int)$value > 946684800) {
- return date('Y-m-d H:i:s', (int)$value);
- }
- $s = trim((string)$value);
- if ($s === '' || preg_match('/^0000-00-00/i', $s)) {
- return '';
- }
- if (preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/', $s, $m)) {
- return $m[1];
- }
- if (preg_match('/^(\d{4}-\d{2}-\d{2})$/', $s, $m)) {
- return $m[1] . ' 00:00:00';
- }
- if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $s, $m)) {
- $ts = strtotime($s);
- return ($ts !== false && $ts > 0) ? date('Y-m-d H:i:s', $ts) : $m[1] . ' 00:00:00';
- }
- $ts = strtotime($s);
- return ($ts !== false && $ts > 0) ? date('Y-m-d H:i:s', $ts) : $s;
- }
- /** 兼容旧调用:与 formatDisplayDateTime 相同 */
- public static function formatDisplayDate($value): string
- {
- return self::formatDisplayDateTime($value);
- }
- /** 排序/月份统计用 Unix 时间 */
- public static function parseToTimestamp($value): int
- {
- if ($value === null || $value === '') {
- return 0;
- }
- if (is_numeric($value) && (int)$value > 946684800) {
- return (int)$value;
- }
- $s = trim((string)$value);
- if ($s === '' || preg_match('/^0000-00-00/i', $s)) {
- return 0;
- }
- if (preg_match('/^(\d{4}-\d{2}-\d{2})(?:\s+\d{2}:\d{2}:\d{2})?/', $s)) {
- $ts = strtotime($s);
- return ($ts !== false && $ts > 0) ? $ts : 0;
- }
- $ts = strtotime($s);
- return ($ts !== false && $ts > 0) ? $ts : 0;
- }
- /**
- * @param int[] $scydgyIds
- * @param array<int,string> $actions
- * @return array<int, int> scydgy_id => unix
- */
- public static function loadOperLogTimestampMap(array $scydgyIds, array $actions): array
- {
- $out = [];
- $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
- $actions = array_values(array_filter(array_map('strval', $actions)));
- if ($scydgyIds === [] || $actions === []) {
- return $out;
- }
- try {
- $logs = Db::table('purchase_order_oper_log')
- ->where('scydgy_id', 'in', $scydgyIds)
- ->where('action', 'in', $actions)
- ->field('scydgy_id,createtime')
- ->order('id', 'asc')
- ->select();
- } catch (\Throwable $e) {
- return $out;
- }
- if (!is_array($logs)) {
- return $out;
- }
- foreach ($logs as $log) {
- if (!is_array($log)) {
- continue;
- }
- $sid = (int)($log['scydgy_id'] ?? 0);
- $ct = self::parseToTimestamp($log['createtime'] ?? null);
- if ($sid > 0 && $ct > 946684800) {
- $out[$sid] = isset($out[$sid]) ? max($out[$sid], $ct) : $ct;
- }
- }
- return $out;
- }
- /**
- * 已完结时间:与详情进度一致,优先采购确认/直接完结操作日志
- *
- * @param array<string, mixed> $row
- * @param array<int, int> $completeTsMap
- * @return array{ts:int, text:string}
- */
- public static function resolveCompletedDone(array $row, array $completeTsMap = []): array
- {
- $sid = (int)($row['scydgy_id'] ?? 0);
- $ts = 0;
- $text = '';
- if ($sid > 0 && isset($completeTsMap[$sid]) && (int)$completeTsMap[$sid] > 946684800) {
- $ts = (int)$completeTsMap[$sid];
- }
- if ($ts <= 0) {
- foreach (['pick_time', 'createtime', 'dStamp'] as $key) {
- $t = self::parseToTimestamp($row[$key] ?? null);
- if ($t > $ts) {
- $ts = $t;
- }
- }
- }
- if ($ts > 0) {
- $text = date('Y-m-d H:i:s', $ts);
- }
- return ['ts' => $ts, 'text' => $text];
- }
- /**
- * 操作记录展示文案:统一步骤用语(下发 / 确认供应商 / 审核确认供应商)
- */
- public static function formatOperLogContent(string $content, string $action = ''): string
- {
- $content = trim($content);
- if ($content === '') {
- return '';
- }
- $action = trim($action);
- if ($action === 'purchase_confirm') {
- if (preg_match('/[「『](.+?)[」』]/u', $content, $m)) {
- return '审核确认供应商:已选定「' . trim($m[1]) . '」';
- }
- }
- if ($action === 'audit_select') {
- if (preg_match('/选定[::]\s*(.+)$/u', $content, $m)) {
- $name = trim($m[1]);
- if (preg_match('/^确认供应商/u', $content) && preg_match('/订单([^),]+)/u', $content, $om)) {
- return '确认供应商(订单' . trim($om[1]) . '):已选定「' . $name . '」';
- }
- return '确认供应商:已选定「' . $name . '」';
- }
- }
- if ($action === 'issue_submit') {
- if (preg_match('/通知供应商[((](\d+)\s*家[))].*?[::]\s*(.+)$/u', $content, $m)) {
- return '下发:通知供应商(' . $m[1] . ' 家):' . trim($m[2]);
- }
- }
- $content = preg_replace('/^采购确认:已选中供应商/u', '审核确认供应商:已选定', $content);
- $content = preg_replace('/^采购确认:/u', '审核确认供应商:', $content);
- $content = preg_replace('/已选中供应商/u', '已选定', $content);
- $content = preg_replace('/^(合并)?(协助|外发)初选/u', '下发', $content);
- $content = preg_replace('/^采购终审驳回/u', '审核驳回', $content);
- $content = preg_replace('/退回外发初选/u', '退回协助初选', $content);
- $content = preg_replace('/^重新下发(([^)]+)),/u', '重新下发($1):', $content);
- $content = preg_replace('/;+$/u', '', $content);
- return trim($content);
- }
- }
|