Procuremenarchive.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\ProcuremenStatus;
  5. use think\Db;
  6. /**
  7. * 协助采购 — 历史存证档案查询(已完结工序 + 操作记录详情)
  8. *
  9. * @icon fa fa-archive
  10. */
  11. class Procuremenarchive extends Backend
  12. {
  13. protected $searchFields = 'CCYDH,CYJMC,CGYMC';
  14. /** purchase_order.status 为已完结(兼容 varchar / 数字 / 首尾空格) */
  15. protected function applyPurchaseOrderCompletedWhere($query): void
  16. {
  17. $query->whereRaw(ProcuremenStatus::sqlPoCompleted('status'));
  18. }
  19. public function index()
  20. {
  21. $this->relationSearch = false;
  22. $this->request->filter(['strip_tags', 'trim']);
  23. if ($this->request->isAjax()) {
  24. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  25. $limit = max(10, min(500, (int)$limit));
  26. $offset = max(0, (int)$offset);
  27. $applyFilters = function ($query) use ($where) {
  28. $this->applyPurchaseOrderCompletedWhere($query);
  29. if (!empty($where)) {
  30. $query->where($where);
  31. }
  32. };
  33. $sortField = preg_match('/^[a-zA-Z0-9_]+$/', (string)$sort) ? $sort : 'id';
  34. $orderDir = strtoupper((string)$order) === 'ASC' ? 'ASC' : 'DESC';
  35. $listQuery = Db::table('purchase_order');
  36. $applyFilters($listQuery);
  37. $rows = $listQuery
  38. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,createtime,dStamp')
  39. ->order($sortField, $orderDir)
  40. ->select();
  41. if (!is_array($rows)) {
  42. $rows = [];
  43. }
  44. $out = [];
  45. foreach ($rows as $r) {
  46. if (!is_array($r)) {
  47. continue;
  48. }
  49. $sid = (int)($r['scydgy_id'] ?? 0);
  50. $doneText = '';
  51. $ct = $r['createtime'] ?? null;
  52. $ctNum = 0;
  53. if (is_numeric($ct) && (int)$ct > 946684800) {
  54. $ctNum = (int)$ct;
  55. $doneText = date('Y-m-d H:i:s', $ctNum);
  56. } elseif (is_string($ct) && trim($ct) !== '' && stripos(trim($ct), '0000-00-00') !== 0) {
  57. $doneText = trim($ct);
  58. $ctNum = (int)strtotime($doneText);
  59. } elseif (!empty($r['dStamp']) && stripos((string)$r['dStamp'], '0000-00-00') !== 0) {
  60. $doneText = trim((string)$r['dStamp']);
  61. $ctNum = (int)strtotime($doneText);
  62. }
  63. $out[] = [
  64. 'id' => (int)($r['id'] ?? 0),
  65. 'scydgy_id' => $sid,
  66. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  67. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  68. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  69. 'createtime' => $ctNum > 0 ? $ctNum : (is_numeric($ct) ? (int)$ct : $ct),
  70. 'createtime_text' => $doneText,
  71. ];
  72. }
  73. $merged = $this->collapseArchiveRowsByOrder($out);
  74. $nMerged = count($merged);
  75. if ($nMerged > 1 && $sortField === 'id') {
  76. usort($merged, function ($a, $b) use ($orderDir) {
  77. $cmp = ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  78. if ($cmp === 0) {
  79. return strcmp((string)($a['CCYDH'] ?? ''), (string)($b['CCYDH'] ?? ''));
  80. }
  81. return $orderDir === 'ASC' ? $cmp : -$cmp;
  82. });
  83. } elseif ($nMerged > 1 && $sortField === 'createtime') {
  84. usort($merged, function ($a, $b) use ($orderDir) {
  85. $ta = (int)($a['createtime'] ?? 0);
  86. $tb = (int)($b['createtime'] ?? 0);
  87. if ($ta === $tb) {
  88. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  89. }
  90. return $orderDir === 'ASC' ? ($ta <=> $tb) : ($tb <=> $ta);
  91. });
  92. }
  93. $pageRows = array_slice($merged, $offset, $limit);
  94. return json(['total' => $nMerged, 'rows' => $pageRows]);
  95. }
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 历史存证:同一订单号 CCYDH 合并为一行(与审批/确认列表一致)
  100. *
  101. * @param array<int, array<string, mixed>> $rows
  102. * @return array<int, array<string, mixed>>
  103. */
  104. protected function collapseArchiveRowsByOrder(array $rows): array
  105. {
  106. if ($rows === []) {
  107. return [];
  108. }
  109. $groups = [];
  110. foreach ($rows as $row) {
  111. if (!is_array($row)) {
  112. continue;
  113. }
  114. $dh = trim((string)($row['CCYDH'] ?? ''));
  115. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  116. if (!isset($groups[$key])) {
  117. $groups[$key] = [];
  118. }
  119. $groups[$key][] = $row;
  120. }
  121. $out = [];
  122. foreach ($groups as $groupRows) {
  123. if ($groupRows === []) {
  124. continue;
  125. }
  126. usort($groupRows, function ($a, $b) {
  127. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  128. });
  129. $head = $groupRows[0];
  130. $gymcList = [];
  131. foreach ($groupRows as $r) {
  132. $g = trim((string)($r['CGYMC'] ?? ''));
  133. if ($g !== '' && !in_array($g, $gymcList, true)) {
  134. $gymcList[] = $g;
  135. }
  136. }
  137. $merged = $head;
  138. if (count($groupRows) > 1) {
  139. $merged['CGYMC'] = implode('、', $gymcList);
  140. }
  141. $merged['process_count'] = count($groupRows);
  142. $latestTs = 0;
  143. $latestText = '';
  144. foreach ($groupRows as $r) {
  145. $ts = (int)($r['createtime'] ?? 0);
  146. if ($ts <= 0 && !empty($r['createtime_text'])) {
  147. $ts = (int)strtotime((string)$r['createtime_text']);
  148. }
  149. if ($ts > $latestTs) {
  150. $latestTs = $ts;
  151. $latestText = trim((string)($r['createtime_text'] ?? ''));
  152. }
  153. }
  154. if ($latestTs > 0) {
  155. $merged['createtime'] = $latestTs;
  156. if ($latestText === '') {
  157. $latestText = date('Y-m-d H:i:s', $latestTs);
  158. }
  159. $merged['createtime_text'] = $latestText;
  160. }
  161. $maxId = (int)($head['id'] ?? 0);
  162. foreach ($groupRows as $r) {
  163. $maxId = max($maxId, (int)($r['id'] ?? 0));
  164. }
  165. $merged['id'] = $maxId;
  166. $out[] = $merged;
  167. }
  168. return $out;
  169. }
  170. }