| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\ProcuremenStatus;
- use app\common\library\ProcuremenTime;
- use think\Db;
- /**
- * 协助采购 — 历史存证档案查询(已完结工序 + 操作记录详情)
- *
- * @icon fa fa-archive
- */
- class Procuremenarchive extends Backend
- {
- protected $searchFields = 'CCYDH,CYJMC,CGYMC';
- public function _initialize()
- {
- parent::_initialize();
- $this->assignconfig('procuremenAuth', [
- 'details' => $this->auth->check('procuremen/details'),
- 'archiveabandon' => $this->auth->check('procuremen/archiveabandon'),
- ]);
- }
- /** purchase_order.status 为已完结(兼容 varchar / 数字 / 首尾空格) */
- protected function applyPurchaseOrderCompletedWhere($query): void
- {
- $query->whereRaw(ProcuremenStatus::sqlPoCompleted('status'));
- }
- public function index()
- {
- $this->relationSearch = false;
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $limit = max(10, min(500, (int)$limit));
- $offset = max(0, (int)$offset);
- $applyFilters = function ($query) use ($where) {
- $this->applyPurchaseOrderCompletedWhere($query);
- if (!empty($where)) {
- $query->where($where);
- }
- };
- $sortField = preg_match('/^[a-zA-Z0-9_]+$/', (string)$sort) ? $sort : 'id';
- $orderDir = strtoupper((string)$order) === 'ASC' ? 'ASC' : 'DESC';
- $listQuery = Db::table('purchase_order');
- $applyFilters($listQuery);
- $rows = $listQuery
- ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,createtime,dStamp,pick_time')
- ->order($sortField, $orderDir)
- ->select();
- if (!is_array($rows)) {
- $rows = [];
- }
- $sidList = [];
- foreach ($rows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- if ($sid !== 0) {
- $sidList[$sid] = true;
- }
- }
- $completeTsMap = ProcuremenTime::loadOperLogTimestampMap(
- array_keys($sidList),
- ['purchase_confirm', 'mark_complete']
- );
- $out = [];
- foreach ($rows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- $done = ProcuremenTime::resolveCompletedDone($r, $completeTsMap);
- $out[] = [
- 'id' => (int)($r['id'] ?? 0),
- 'scydgy_id' => $sid,
- 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
- 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
- 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
- 'createtime' => $done['ts'],
- 'createtime_text' => $done['text'],
- ];
- }
- $merged = $this->collapseArchiveRowsByOrder($out);
- $nMerged = count($merged);
- if ($nMerged > 1 && $sortField === 'id') {
- usort($merged, function ($a, $b) use ($orderDir) {
- $cmp = ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
- if ($cmp === 0) {
- return strcmp((string)($a['CCYDH'] ?? ''), (string)($b['CCYDH'] ?? ''));
- }
- return $orderDir === 'ASC' ? $cmp : -$cmp;
- });
- } elseif ($nMerged > 1 && $sortField === 'createtime') {
- usort($merged, function ($a, $b) use ($orderDir) {
- $ta = (int)($a['createtime'] ?? 0);
- $tb = (int)($b['createtime'] ?? 0);
- if ($ta === $tb) {
- return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
- }
- return $orderDir === 'ASC' ? ($ta <=> $tb) : ($tb <=> $ta);
- });
- }
- $pageRows = array_slice($merged, $offset, $limit);
- return json(['total' => $nMerged, 'rows' => $pageRows]);
- }
- return $this->view->fetch();
- }
- /**
- * 历史存证:同一订单号 CCYDH 合并为一行(与审批/确认列表一致)
- *
- * @param array<int, array<string, mixed>> $rows
- * @return array<int, array<string, mixed>>
- */
- protected function collapseArchiveRowsByOrder(array $rows): array
- {
- if ($rows === []) {
- return [];
- }
- $groups = [];
- foreach ($rows as $row) {
- if (!is_array($row)) {
- continue;
- }
- $dh = trim((string)($row['CCYDH'] ?? ''));
- $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
- if (!isset($groups[$key])) {
- $groups[$key] = [];
- }
- $groups[$key][] = $row;
- }
- $out = [];
- foreach ($groups as $groupRows) {
- if ($groupRows === []) {
- continue;
- }
- usort($groupRows, function ($a, $b) {
- return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
- });
- $head = $groupRows[0];
- $gymcList = [];
- foreach ($groupRows as $r) {
- $g = trim((string)($r['CGYMC'] ?? ''));
- if ($g !== '' && !in_array($g, $gymcList, true)) {
- $gymcList[] = $g;
- }
- }
- $merged = $head;
- if (count($groupRows) > 1) {
- $merged['CGYMC'] = implode('、', $gymcList);
- }
- $merged['process_count'] = count($groupRows);
- $latestTs = 0;
- $latestText = '';
- foreach ($groupRows as $r) {
- $ts = (int)($r['createtime'] ?? 0);
- if ($ts <= 0 && !empty($r['createtime_text'])) {
- $ts = (int)strtotime((string)$r['createtime_text']);
- }
- if ($ts > $latestTs) {
- $latestTs = $ts;
- $latestText = trim((string)($r['createtime_text'] ?? ''));
- }
- }
- if ($latestTs > 0) {
- $merged['createtime'] = $latestTs;
- if ($latestText === '') {
- $latestText = date('Y-m-d H:i:s', $latestTs);
- }
- $merged['createtime_text'] = ProcuremenTime::formatDisplayDateTime($latestText);
- }
- $maxId = (int)($head['id'] ?? 0);
- foreach ($groupRows as $r) {
- $maxId = max($maxId, (int)($r['id'] ?? 0));
- }
- $merged['id'] = $maxId;
- $out[] = $merged;
- }
- return $out;
- }
- }
|