Procuremenarchive.php 6.8 KB

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