ProcuremenOperLog.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. <?php
  2. namespace app\common\library;
  3. use think\Db;
  4. use think\Log;
  5. /**
  6. * 协助采购操作日志(purchase_order_oper_log)
  7. *
  8. * 表字段为英文;写入时「action」存中文操作类型,查询时兼容历史英文码。
  9. */
  10. class ProcuremenOperLog
  11. {
  12. public const COL_SCYDGY_ID = 'scydgy_id';
  13. public const COL_PO_ID = 'purchase_order_id';
  14. public const COL_ADMIN_ID = 'admin_id';
  15. public const COL_ADMIN_NAME = 'admin_name';
  16. public const COL_ACTION = 'action';
  17. public const COL_CONTENT = 'content';
  18. public const COL_TIME = 'createtime';
  19. /**
  20. * 英文码 => 中文操作类型(入库展示用;与库中已有中文值对齐)
  21. *
  22. * @return array<string, string>
  23. */
  24. public static function actionLabelMap(): array
  25. {
  26. return [
  27. 'issue_submit' => '下发通知',
  28. 'audit_select' => '确认供应商',
  29. 'purchase_confirm' => '审核确认供应商',
  30. 'purchase_reject' => '审核驳回',
  31. 'bid_open_verify' => '开标验证',
  32. 'audit_append_supplier' => '补加供应商',
  33. 'audit_resend_sms' => '重发短信',
  34. 'audit_resend_email' => '重发邮件',
  35. 'manual_add' => '手工新增',
  36. 'pick_soft_delete' => '初选删除',
  37. 'save_qty_price' => '保存数量限价',
  38. 'mark_complete' => '直接完结',
  39. 'audit_abandon' => '审批重新下发',
  40. 'archive_abandon' => '历史重新下发',
  41. ];
  42. }
  43. /**
  44. * 同一业务动作的可选别名(查询时一并匹配)
  45. *
  46. * @return array<string, string[]>
  47. */
  48. public static function actionAliases(): array
  49. {
  50. return [
  51. 'issue_submit' => ['下发通知', '下发', 'issue_submit'],
  52. 'audit_select' => ['确认供应商', 'audit_select'],
  53. 'purchase_confirm' => ['审核确认供应商', 'purchase_confirm'],
  54. 'purchase_reject' => ['审核驳回', 'purchase_reject'],
  55. 'bid_open_verify' => ['开标验证', 'bid_open_verify'],
  56. 'audit_append_supplier' => ['补加供应商', 'audit_append_supplier'],
  57. 'audit_resend_sms' => ['重发短信', 'audit_resend_sms'],
  58. 'audit_resend_email' => ['重发邮件', 'audit_resend_email'],
  59. 'manual_add' => ['手工新增', 'manual_add'],
  60. 'pick_soft_delete' => ['初选删除', 'pick_soft_delete'],
  61. 'save_qty_price' => ['保存数量限价', 'save_qty_price'],
  62. 'mark_complete' => ['直接完结', '完结', 'mark_complete'],
  63. 'audit_abandon' => ['审批重新下发', '重新下发', 'audit_abandon'],
  64. 'archive_abandon' => ['历史重新下发', '存证重新下发', 'archive_abandon'],
  65. ];
  66. }
  67. public static function toActionLabel(string $action): string
  68. {
  69. $action = trim($action);
  70. if ($action === '') {
  71. return '';
  72. }
  73. $map = self::actionLabelMap();
  74. if (isset($map[$action])) {
  75. return $map[$action];
  76. }
  77. // 已是中文别名时,归一到主标签
  78. foreach (self::actionAliases() as $code => $aliases) {
  79. if (in_array($action, $aliases, true)) {
  80. return $map[$code] ?? $action;
  81. }
  82. }
  83. return $action;
  84. }
  85. /**
  86. * 查询用:同时匹配中文标签、别名与历史英文码
  87. *
  88. * @param string|array<int, string> $actions
  89. * @return string[]
  90. */
  91. public static function expandActionQueryValues($actions): array
  92. {
  93. $aliases = self::actionAliases();
  94. $map = self::actionLabelMap();
  95. $out = [];
  96. foreach ((array)$actions as $a) {
  97. $a = trim((string)$a);
  98. if ($a === '') {
  99. continue;
  100. }
  101. $out[$a] = true;
  102. $code = $a;
  103. if (!isset($aliases[$code])) {
  104. foreach ($aliases as $c => $list) {
  105. if (in_array($a, $list, true) || (isset($map[$c]) && $map[$c] === $a)) {
  106. $code = $c;
  107. break;
  108. }
  109. }
  110. }
  111. if (isset($aliases[$code])) {
  112. foreach ($aliases[$code] as $v) {
  113. $out[$v] = true;
  114. }
  115. }
  116. if (isset($map[$code])) {
  117. $out[$map[$code]] = true;
  118. }
  119. }
  120. return array_keys($out);
  121. }
  122. /**
  123. * 组装入库行(英文字段,操作类型存中文)
  124. *
  125. * @return array<string, mixed>
  126. */
  127. public static function buildInsertRow(
  128. int $scydgyId,
  129. string $action,
  130. string $content,
  131. ?int $purchaseOrderId,
  132. int $adminId,
  133. string $adminName
  134. ): array {
  135. return [
  136. self::COL_SCYDGY_ID => $scydgyId,
  137. self::COL_PO_ID => $purchaseOrderId,
  138. self::COL_ADMIN_ID => $adminId,
  139. self::COL_ADMIN_NAME => self::cut($adminName !== '' ? $adminName : '未知用户', 64),
  140. self::COL_ACTION => self::cut(self::toActionLabel($action), 64),
  141. self::COL_CONTENT => self::cut($content, 1000),
  142. self::COL_TIME => ProcuremenTime::nowDateTime(),
  143. ];
  144. }
  145. /**
  146. * @param array{0:int,1:string} $admin [id, name]
  147. */
  148. public static function write(int $scydgyId, string $action, string $content, ?int $purchaseOrderId, array $admin): void
  149. {
  150. if (!ProcuremenGuard::isValidScydgyId($scydgyId) || $content === '') {
  151. return;
  152. }
  153. $adminId = (int)($admin[0] ?? 0);
  154. $adminName = (string)($admin[1] ?? '未知用户');
  155. try {
  156. Db::table('purchase_order_oper_log')->insert(
  157. self::buildInsertRow($scydgyId, $action, $content, $purchaseOrderId, $adminId, $adminName)
  158. );
  159. } catch (\Throwable $e) {
  160. Log::write('procuremen oper log: ' . $e->getMessage(), 'error');
  161. }
  162. }
  163. /**
  164. * 查询结果规范化(兼容偶发中文列名 → 英文键)
  165. *
  166. * @param array<string, mixed> $log
  167. * @return array<string, mixed>
  168. */
  169. public static function normalizeRow(array $log): array
  170. {
  171. $map = [
  172. '工序ID' => 'scydgy_id',
  173. '采购订单ID' => 'purchase_order_id',
  174. '操作人ID' => 'admin_id',
  175. '操作人' => 'admin_name',
  176. '操作类型' => 'action',
  177. '操作内容' => 'content',
  178. '创建时间' => 'createtime',
  179. ];
  180. foreach ($map as $cn => $en) {
  181. if (array_key_exists($cn, $log) && !array_key_exists($en, $log)) {
  182. $log[$en] = $log[$cn];
  183. }
  184. }
  185. if (isset($log['action'])) {
  186. $log['action'] = self::toActionLabel((string)$log['action']);
  187. }
  188. return $log;
  189. }
  190. /**
  191. * @param string|array<int, string> $actions
  192. */
  193. public static function resolveLatestTime(int $scydgyId, $actions): string
  194. {
  195. if (!ProcuremenGuard::isValidScydgyId($scydgyId)) {
  196. return '';
  197. }
  198. $acts = self::expandActionQueryValues($actions);
  199. if ($acts === []) {
  200. return '';
  201. }
  202. try {
  203. $log = Db::table('purchase_order_oper_log')
  204. ->where(self::COL_SCYDGY_ID, $scydgyId)
  205. ->where(self::COL_ACTION, 'in', $acts)
  206. ->order('id', 'desc')
  207. ->find();
  208. if (is_array($log)) {
  209. $log = self::normalizeRow($log);
  210. $text = ProcuremenTime::formatDisplayDateTime($log['createtime'] ?? null);
  211. if ($text !== '') {
  212. return $text;
  213. }
  214. }
  215. } catch (\Throwable $e) {
  216. }
  217. return '';
  218. }
  219. public static function resolveMarkCompleteTime(int $scydgyId): string
  220. {
  221. return self::resolveLatestTime($scydgyId, 'mark_complete');
  222. }
  223. /**
  224. * @param array<int, array<string, mixed>> $logs
  225. * @param array<int, string> $processNameBySid scydgy_id => 工序名称
  226. * @return array<int, array<string, mixed>>
  227. */
  228. public static function formatLogRowsForDisplay(array $logs, array $processNameBySid = []): array
  229. {
  230. foreach ($logs as &$lg) {
  231. if (!is_array($lg)) {
  232. continue;
  233. }
  234. $lg = self::normalizeRow($lg);
  235. $lg['createtime_text'] = ProcuremenTime::formatDisplayDateTime($lg['createtime'] ?? null);
  236. $lg['content'] = ProcuremenTime::formatOperLogContent(
  237. (string)($lg['content'] ?? ''),
  238. (string)($lg['action'] ?? '')
  239. );
  240. $lg['action_text'] = self::toActionLabel((string)($lg['action'] ?? ''));
  241. $lg = self::enrichSaveQtyPriceProcessName($lg, $processNameBySid);
  242. }
  243. unset($lg);
  244. $logs = self::mergeDuplicateProcessLogs($logs, $processNameBySid);
  245. $logs = self::enrichAdminDisplayFields($logs);
  246. foreach ($logs as &$lg) {
  247. if (!is_array($lg)) {
  248. continue;
  249. }
  250. $lg = self::splitActionAndDetailForDisplay($lg);
  251. }
  252. unset($lg);
  253. return $logs;
  254. }
  255. /**
  256. * 展示拆成「操作类型」+「操作说明」,说明里不再重复类型前缀
  257. *
  258. * @param array<string, mixed> $lg
  259. * @return array<string, mixed>
  260. */
  261. protected static function splitActionAndDetailForDisplay(array $lg): array
  262. {
  263. $action = trim((string)($lg['action_text'] ?? ''));
  264. if ($action === '') {
  265. $action = self::toActionLabel((string)($lg[self::COL_ACTION] ?? ''));
  266. }
  267. $content = trim((string)($lg[self::COL_CONTENT] ?? ''));
  268. $content = self::stripLeadingActionPrefixes($content, $action);
  269. if ($action === '') {
  270. $action = '操作';
  271. }
  272. $lg['action_text'] = $action;
  273. $lg[self::COL_CONTENT] = $content;
  274. return $lg;
  275. }
  276. /**
  277. * @return string[] 按长度降序
  278. */
  279. protected static function contentActionPrefixes(string $action): array
  280. {
  281. $action = self::toActionLabel(trim($action));
  282. $list = [];
  283. if ($action !== '') {
  284. $list[] = $action;
  285. }
  286. // 历史/文案别名
  287. $aliases = [
  288. '下发通知' => ['下发', '下发通知'],
  289. '保存数量限价' => ['保存数量限价', '保存本次数量、最高限价'],
  290. '审批重新下发' => ['审批重新下发', '重新下发'],
  291. '历史重新下发' => ['历史重新下发', '存证重新下发', '重新下发'],
  292. '审核确认供应商' => ['审核确认供应商', '采购确认'],
  293. '确认供应商' => ['确认供应商'],
  294. '开标验证' => ['开标验证'],
  295. '补加供应商' => ['补加供应商'],
  296. '直接完结' => ['直接完结', '完结'],
  297. '审核驳回' => ['审核驳回', '采购终审驳回'],
  298. ];
  299. if (isset($aliases[$action])) {
  300. foreach ($aliases[$action] as $a) {
  301. $list[] = $a;
  302. }
  303. }
  304. $list = array_values(array_unique(array_filter(array_map('trim', $list))));
  305. usort($list, static function ($a, $b) {
  306. return mb_strlen((string)$b, 'UTF-8') <=> mb_strlen((string)$a, 'UTF-8');
  307. });
  308. return $list;
  309. }
  310. protected static function stripLeadingActionPrefixes(string $content, string $action): string
  311. {
  312. $content = trim($content);
  313. if ($content === '') {
  314. return '';
  315. }
  316. foreach (self::contentActionPrefixes($action) as $p) {
  317. foreach ([$p . ':', $p . ':'] as $pref) {
  318. if (mb_strpos($content, $pref, 0, 'UTF-8') === 0) {
  319. return trim(mb_substr($content, mb_strlen($pref, 'UTF-8'), null, 'UTF-8'));
  320. }
  321. }
  322. }
  323. return $content;
  324. }
  325. /**
  326. * 历史「保存数量限价」说明补工序名:…:压折线本次数量「330」,最高限价「10」
  327. *
  328. * @param array<string, mixed> $lg
  329. * @param array<int, string> $processNameBySid
  330. * @return array<string, mixed>
  331. */
  332. protected static function enrichSaveQtyPriceProcessName(array $lg, array $processNameBySid): array
  333. {
  334. $action = trim((string)($lg['action_text'] ?? $lg[self::COL_ACTION] ?? ''));
  335. if ($action !== '保存数量限价' && $action !== 'save_qty_price') {
  336. return $lg;
  337. }
  338. $content = trim((string)($lg[self::COL_CONTENT] ?? ''));
  339. if ($content === '') {
  340. return $lg;
  341. }
  342. $sid = (int)($lg[self::COL_SCYDGY_ID] ?? 0);
  343. $proc = trim((string)($processNameBySid[$sid] ?? ''));
  344. if ($proc === '') {
  345. return $lg;
  346. }
  347. // 已含工序名则不重复
  348. if (mb_strpos($content, $proc . '本次数量', 0, 'UTF-8') !== false
  349. || mb_strpos($content, '(' . $proc . ')', 0, 'UTF-8') !== false) {
  350. return $lg;
  351. }
  352. if (mb_strpos($content, ':本次数量', 0, 'UTF-8') !== false) {
  353. $lg[self::COL_CONTENT] = preg_replace('/:本次数量/u', ':' . $proc . '本次数量', $content, 1);
  354. } elseif (mb_strpos($content, ':本次数量', 0, 'UTF-8') !== false) {
  355. $lg[self::COL_CONTENT] = preg_replace('/:本次数量/u', ':' . $proc . '本次数量', $content, 1);
  356. }
  357. return $lg;
  358. }
  359. /**
  360. * 操作人前加所属组别;说明文案中的人员名同样补组别
  361. *
  362. * @param array<int, array<string, mixed>> $logs
  363. * @return array<int, array<string, mixed>>
  364. */
  365. public static function enrichAdminDisplayFields(array $logs): array
  366. {
  367. $ids = [];
  368. foreach ($logs as $lg) {
  369. if (!is_array($lg)) {
  370. continue;
  371. }
  372. $id = (int)($lg[self::COL_ADMIN_ID] ?? 0);
  373. if ($id > 0) {
  374. $ids[$id] = true;
  375. }
  376. }
  377. $map = self::loadAdminGroupNicknameMap(array_keys($ids));
  378. // 仅用于纠正历史误伤:说明里曾把「组别 昵称」拼进供应商名,展示时还原为昵称/公司名
  379. $nameToLabeled = self::buildAdminNameLabelMap(self::loadAllAdminGroupNicknameMap());
  380. foreach (self::buildAdminNameLabelMap($map) as $name => $labeled) {
  381. $nameToLabeled[$name] = $labeled;
  382. }
  383. uksort($nameToLabeled, static function ($a, $b) {
  384. return mb_strlen((string)$b, 'UTF-8') <=> mb_strlen((string)$a, 'UTF-8');
  385. });
  386. foreach ($logs as &$lg) {
  387. if (!is_array($lg)) {
  388. continue;
  389. }
  390. $id = (int)($lg[self::COL_ADMIN_ID] ?? 0);
  391. $info = ($id > 0 && isset($map[$id])) ? $map[$id] : null;
  392. $nick = is_array($info) ? trim((string)($info['nickname'] ?? '')) : '';
  393. if ($nick === '') {
  394. $nick = trim((string)($lg[self::COL_ADMIN_NAME] ?? ''));
  395. }
  396. if ($nick === '') {
  397. $nick = '未知用户';
  398. }
  399. $group = is_array($info) ? trim((string)($info['group_name'] ?? '')) : '';
  400. $lg['admin_group'] = $group !== '' ? $group : '—';
  401. $lg['admin_nickname'] = $nick;
  402. // 操作人显示「组别 昵称」
  403. $lg[self::COL_ADMIN_NAME] = self::formatAdminWithGroup($group, $nick);
  404. $content = (string)($lg[self::COL_CONTENT] ?? '');
  405. if ($content !== '' && $nameToLabeled !== []) {
  406. // 先还原历史误伤的供应商名
  407. $content = self::unprefixMistakenNamesInText($content, $nameToLabeled);
  408. // 开标验证等说明里是人员名,再补组别
  409. $actionLabel = self::toActionLabel((string)($lg['action_text'] ?? $lg[self::COL_ACTION] ?? ''));
  410. if (self::actionContentNeedsPersonGroup($actionLabel)) {
  411. $content = self::prefixNamesInText($content, $nameToLabeled);
  412. }
  413. $lg[self::COL_CONTENT] = $content;
  414. }
  415. }
  416. unset($lg);
  417. return $logs;
  418. }
  419. /** 操作说明里会出现人员姓名、需补所属组别的类型 */
  420. protected static function actionContentNeedsPersonGroup(string $action): bool
  421. {
  422. $action = self::toActionLabel(trim($action));
  423. return $action === '开标验证';
  424. }
  425. /**
  426. * 按 admin.id 解析展示名「组别 昵称」
  427. *
  428. * @param array<int, int> $adminIds
  429. * @return array<int, string>
  430. */
  431. public static function resolveAdminDisplayLabels(array $adminIds): array
  432. {
  433. $map = self::loadAdminGroupNicknameMap($adminIds);
  434. $out = [];
  435. foreach ($adminIds as $rawId) {
  436. $id = (int)$rawId;
  437. if ($id <= 0) {
  438. continue;
  439. }
  440. $info = isset($map[$id]) && is_array($map[$id]) ? $map[$id] : null;
  441. $nick = is_array($info) ? trim((string)($info['nickname'] ?? '')) : '';
  442. $group = is_array($info) ? trim((string)($info['group_name'] ?? '')) : '';
  443. if ($nick === '') {
  444. $nick = 'ID' . $id;
  445. }
  446. $out[$id] = self::formatAdminWithGroup($group, $nick);
  447. }
  448. return $out;
  449. }
  450. /**
  451. * @param array<int, array{nickname:string,group_name:string}> $adminMap
  452. * @return array<string, string> 原名 => 「组别 名称」
  453. */
  454. protected static function buildAdminNameLabelMap(array $adminMap): array
  455. {
  456. $out = [];
  457. foreach ($adminMap as $info) {
  458. if (!is_array($info)) {
  459. continue;
  460. }
  461. $nick = trim((string)($info['nickname'] ?? ''));
  462. if ($nick === '') {
  463. continue;
  464. }
  465. $group = trim((string)($info['group_name'] ?? ''));
  466. $labeled = self::formatAdminWithGroup($group, $nick);
  467. if ($labeled !== $nick) {
  468. $out[$nick] = $labeled;
  469. }
  470. }
  471. // 长名优先,避免短名误伤
  472. uksort($out, static function ($a, $b) {
  473. return mb_strlen((string)$b, 'UTF-8') <=> mb_strlen((string)$a, 'UTF-8');
  474. });
  475. return $out;
  476. }
  477. protected static function formatAdminWithGroup(string $group, string $name): string
  478. {
  479. $name = trim($name);
  480. $group = trim($group);
  481. if ($name === '') {
  482. return $group !== '' ? $group : '未知用户';
  483. }
  484. if ($group === '' || $group === '—' || $group === $name) {
  485. return $name;
  486. }
  487. // 已带组别则不再重复
  488. if (mb_strpos($name, $group, 0, 'UTF-8') === 0) {
  489. return $name;
  490. }
  491. return $group . ' ' . $name;
  492. }
  493. /**
  494. * @param array<string, string> $nameToLabeled
  495. */
  496. protected static function prefixNamesInText(string $text, array $nameToLabeled): string
  497. {
  498. foreach ($nameToLabeled as $name => $labeled) {
  499. if ($name === '' || $labeled === '' || $name === $labeled) {
  500. continue;
  501. }
  502. if (mb_strpos($text, $labeled, 0, 'UTF-8') !== false) {
  503. continue;
  504. }
  505. if (mb_strpos($text, $name, 0, 'UTF-8') === false) {
  506. continue;
  507. }
  508. $text = str_replace($name, $labeled, $text);
  509. }
  510. return $text;
  511. }
  512. /**
  513. * 还原说明里误拼的「组别 昵称」→ 原名(避免供应商名被改成「浙江新华管理员 新华印刷…」)
  514. *
  515. * @param array<string, string> $nameToLabeled
  516. */
  517. protected static function unprefixMistakenNamesInText(string $text, array $nameToLabeled): string
  518. {
  519. // 长标签优先替换
  520. $pairs = [];
  521. foreach ($nameToLabeled as $name => $labeled) {
  522. if ($name === '' || $labeled === '' || $name === $labeled) {
  523. continue;
  524. }
  525. $pairs[$labeled] = $name;
  526. }
  527. uksort($pairs, static function ($a, $b) {
  528. return mb_strlen((string)$b, 'UTF-8') <=> mb_strlen((string)$a, 'UTF-8');
  529. });
  530. foreach ($pairs as $labeled => $name) {
  531. if (mb_strpos($text, $labeled, 0, 'UTF-8') === false) {
  532. continue;
  533. }
  534. $text = str_replace($labeled, $name, $text);
  535. }
  536. return $text;
  537. }
  538. /**
  539. * @return array<int, array{nickname:string,group_name:string,username?:string}>
  540. */
  541. public static function loadAllAdminGroupNicknameMap(): array
  542. {
  543. try {
  544. $ids = Db::name('admin')->column('id');
  545. } catch (\Throwable $e) {
  546. $ids = [];
  547. }
  548. if (!is_array($ids) || $ids === []) {
  549. return [];
  550. }
  551. return self::loadAdminGroupNicknameMap(array_map('intval', $ids));
  552. }
  553. /**
  554. * @param array<int, int> $adminIds
  555. * @return array<int, array{nickname:string,group_name:string,username?:string}>
  556. */
  557. public static function loadAdminGroupNicknameMap(array $adminIds): array
  558. {
  559. $out = [];
  560. $adminIds = array_values(array_filter(array_map('intval', $adminIds)));
  561. if ($adminIds === []) {
  562. return $out;
  563. }
  564. try {
  565. $admins = Db::name('admin')->where('id', 'in', $adminIds)->field('id,nickname,username')->select();
  566. } catch (\Throwable $e) {
  567. $admins = [];
  568. }
  569. if (is_array($admins)) {
  570. foreach ($admins as $a) {
  571. if (!is_array($a)) {
  572. continue;
  573. }
  574. $id = (int)($a['id'] ?? 0);
  575. if ($id <= 0) {
  576. continue;
  577. }
  578. $nick = trim((string)($a['nickname'] ?? ''));
  579. if ($nick === '') {
  580. $nick = trim((string)($a['username'] ?? ''));
  581. }
  582. $out[$id] = ['nickname' => $nick, 'group_name' => '', 'username' => trim((string)($a['username'] ?? ''))];
  583. }
  584. }
  585. try {
  586. $rows = Db::name('auth_group_access')
  587. ->alias('aga')
  588. ->join('auth_group ag', 'aga.group_id = ag.id', 'LEFT')
  589. ->where('aga.uid', 'in', $adminIds)
  590. ->field('aga.uid,ag.name')
  591. ->select();
  592. } catch (\Throwable $e) {
  593. $rows = [];
  594. }
  595. if (is_array($rows)) {
  596. foreach ($rows as $r) {
  597. if (!is_array($r)) {
  598. continue;
  599. }
  600. $uid = (int)($r['uid'] ?? 0);
  601. $gname = trim((string)($r['name'] ?? ''));
  602. if ($uid <= 0 || $gname === '') {
  603. continue;
  604. }
  605. if (!isset($out[$uid])) {
  606. $out[$uid] = ['nickname' => '', 'group_name' => $gname];
  607. } elseif ($out[$uid]['group_name'] === '') {
  608. $out[$uid]['group_name'] = $gname;
  609. } elseif (strpos($out[$uid]['group_name'], $gname) === false) {
  610. $out[$uid]['group_name'] .= '、' . $gname;
  611. }
  612. }
  613. }
  614. return $out;
  615. }
  616. /**
  617. * 多工序短时间内相同操作合并为一条,并带上工序名称
  618. * 例:审核确认供应商:配套、快递已选定「xxx」
  619. *
  620. * @param array<int, array<string, mixed>> $logs
  621. * @param array<int, string> $processNameBySid
  622. * @return array<int, array<string, mixed>>
  623. */
  624. public static function mergeDuplicateProcessLogs(array $logs, array $processNameBySid = []): array
  625. {
  626. if ($logs === []) {
  627. return [];
  628. }
  629. $groups = [];
  630. $order = [];
  631. foreach ($logs as $idx => $lg) {
  632. if (!is_array($lg)) {
  633. continue;
  634. }
  635. $adminId = (int)($lg[self::COL_ADMIN_ID] ?? 0);
  636. $admin = trim((string)($lg[self::COL_ADMIN_NAME] ?? ''));
  637. $action = trim((string)($lg['action_text'] ?? $lg[self::COL_ACTION] ?? ''));
  638. $content = trim((string)($lg[self::COL_CONTENT] ?? ''));
  639. $contentCore = self::normalizeMergeContentCore($action, $content);
  640. $ts = self::logTimeTs($lg);
  641. // 60 秒内、同操作人+同操作类型+同说明核心 → 合并
  642. $bucket = null;
  643. foreach ($groups as $gk => $g) {
  644. if (($g['admin_id'] !== $adminId && $g['admin'] !== $admin)
  645. || $g['action'] !== $action
  646. || $g['content_core'] !== $contentCore) {
  647. continue;
  648. }
  649. if (abs($ts - (int)$g['ts']) <= 60) {
  650. $bucket = $gk;
  651. break;
  652. }
  653. }
  654. if ($bucket === null) {
  655. $bucket = 'g' . $idx;
  656. $groups[$bucket] = [
  657. 'admin_id' => $adminId,
  658. 'admin' => $admin,
  659. 'action' => $action,
  660. 'content_core' => $contentCore,
  661. 'ts' => $ts,
  662. 'row' => $lg,
  663. 'sids' => [],
  664. 'contents' => [],
  665. ];
  666. $order[] = $bucket;
  667. }
  668. $sid = (int)($lg[self::COL_SCYDGY_ID] ?? 0);
  669. if ($sid !== 0) {
  670. $groups[$bucket]['sids'][$sid] = true;
  671. }
  672. $groups[$bucket]['contents'][] = $content;
  673. // 保留最早一条作展示底稿,时间用最早
  674. $rowTs = self::logTimeTs($groups[$bucket]['row']);
  675. if ($ts > 0 && ($rowTs === 0 || $ts < $rowTs)) {
  676. $groups[$bucket]['row'] = $lg;
  677. $groups[$bucket]['ts'] = $ts;
  678. }
  679. }
  680. $out = [];
  681. foreach ($order as $gk) {
  682. $g = $groups[$gk];
  683. $row = $g['row'];
  684. $sids = array_keys($g['sids']);
  685. $processNames = [];
  686. foreach ($sids as $sid) {
  687. $nm = trim((string)($processNameBySid[$sid] ?? ''));
  688. if ($nm !== '' && !in_array($nm, $processNames, true)) {
  689. $processNames[] = $nm;
  690. }
  691. }
  692. $content = trim((string)($row[self::COL_CONTENT] ?? ''));
  693. if (count($sids) > 1 || count($processNames) > 1) {
  694. $content = self::buildMergedProcessContent(
  695. (string)($g['action'] ?? ''),
  696. $content,
  697. $processNames
  698. );
  699. }
  700. $row[self::COL_CONTENT] = $content;
  701. $row['action_text'] = (string)($g['action'] !== '' ? $g['action'] : ($row['action_text'] ?? ''));
  702. $out[] = $row;
  703. }
  704. return $out;
  705. }
  706. /**
  707. * 提取可合并的内容核心(去掉前缀动作名后的业务说明)
  708. */
  709. protected static function normalizeMergeContentCore(string $action, string $content): string
  710. {
  711. return self::stripLeadingActionPrefixes(trim($content), trim($action));
  712. }
  713. /**
  714. * @param array<int, string> $processNames
  715. */
  716. protected static function buildMergedProcessContent(string $action, string $content, array $processNames): string
  717. {
  718. $core = self::normalizeMergeContentCore($action, $content);
  719. $procText = implode('、', $processNames);
  720. if ($procText === '') {
  721. return $core !== '' ? $core : $content;
  722. }
  723. // 工序名放在操作说明里,不盖住操作类型
  724. if ($core !== '') {
  725. return $procText . ' ' . $core;
  726. }
  727. return $procText;
  728. }
  729. /**
  730. * @param array<string, mixed> $lg
  731. */
  732. protected static function logTimeTs(array $lg): int
  733. {
  734. $raw = trim((string)($lg['createtime_text'] ?? $lg[self::COL_TIME] ?? ''));
  735. if ($raw === '') {
  736. return 0;
  737. }
  738. $ts = strtotime(str_replace('T', ' ', $raw));
  739. return ($ts !== false && $ts > 0) ? (int)$ts : 0;
  740. }
  741. private static function cut(string $s, int $max): string
  742. {
  743. if (function_exists('mb_substr')) {
  744. return mb_substr($s, 0, $max, 'UTF-8');
  745. }
  746. return strlen($s) <= $max ? $s : substr($s, 0, $max);
  747. }
  748. }