Procuremenarchive.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(, $sort, $order, $offset, $limit) = $this->buildparams();
  26. $ccydhKw = trim((string)$this->request->get('ccydh', ''));
  27. if ($ccydhKw === '') {
  28. $ccydhKw = trim((string)$this->request->get('search', ''));
  29. }
  30. $applyFilters = function ($query) use ($ccydhKw) {
  31. $this->applyPurchaseOrderCompletedWhere($query);
  32. if ($ccydhKw !== '') {
  33. $query->where(function ($q) use ($ccydhKw) {
  34. $q->where('CCYDH', 'like', '%' . $ccydhKw . '%')
  35. ->whereOr('CYJMC', 'like', '%' . $ccydhKw . '%')
  36. ->whereOr('CGYMC', 'like', '%' . $ccydhKw . '%');
  37. });
  38. }
  39. };
  40. $countQuery = Db::table('purchase_order');
  41. $applyFilters($countQuery);
  42. $total = (int)$countQuery->count();
  43. $sortField = preg_match('/^[a-zA-Z0-9_]+$/', (string)$sort) ? $sort : 'id';
  44. $orderDir = strtoupper((string)$order) === 'ASC' ? 'ASC' : 'DESC';
  45. $listQuery = Db::table('purchase_order');
  46. $applyFilters($listQuery);
  47. $rows = $listQuery
  48. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,createtime,dStamp')
  49. ->order($sortField, $orderDir)
  50. ->limit($offset, $limit)
  51. ->select();
  52. if (!is_array($rows)) {
  53. $rows = [];
  54. }
  55. $out = [];
  56. foreach ($rows as $r) {
  57. if (!is_array($r)) {
  58. continue;
  59. }
  60. $sid = (int)($r['scydgy_id'] ?? 0);
  61. $doneText = '';
  62. $ct = $r['createtime'] ?? null;
  63. if (is_numeric($ct) && (int)$ct > 946684800) {
  64. $doneText = date('Y-m-d H:i:s', (int)$ct);
  65. } elseif (is_string($ct) && trim($ct) !== '' && stripos(trim($ct), '0000-00-00') !== 0) {
  66. $doneText = trim($ct);
  67. } elseif (!empty($r['dStamp']) && stripos((string)$r['dStamp'], '0000-00-00') !== 0) {
  68. $doneText = trim((string)$r['dStamp']);
  69. }
  70. $out[] = [
  71. 'id' => (int)($r['id'] ?? 0),
  72. 'scydgy_id' => $sid,
  73. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  74. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  75. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  76. 'createtime_text' => $doneText,
  77. ];
  78. }
  79. return json(['total' => $total, 'rows' => $out]);
  80. }
  81. return $this->view->fetch();
  82. }
  83. }