Procuremenarchive.php 7.1 KB

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