Procuremen.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\api\controller;
  3. use think\Controller;
  4. use think\Db;
  5. /**
  6. * 外发采购列表数据 API(供第三方拉取并写入 Redis,后台列表读同一份缓存)
  7. */
  8. class Procuremen extends Controller
  9. {
  10. /**
  11. * 外发加工候选列表
  12. *
  13. * 请求:GET
  14. * 参数:
  15. * - refresh=1 强制查库并覆盖 Redis(查询成功才写入)
  16. * - start_time 可选,开始时间,默认近一年前 00:00:00
  17. * - end_time 可选,结束时间,默认当前日 23:59:59
  18. * - limit 可选,最大返回条数,默认不限制(与后台整包缓存一致);若传则 1~500
  19. *
  20. * Redis:键固定为 procuremen_redis(与 admin Procuremen::index 一致)
  21. * 结构:{ "code":200, "msg":"success", "data":[ {...行字段英文键...} ] }
  22. */
  23. public function GetProcuremen()
  24. {
  25. if (!$this->request->isGet()) {
  26. return json(['code' => 400, 'msg' => '请使用GET请求', 'data' => []]);
  27. }
  28. $refresh = (int)$this->request->get('refresh', 0);
  29. $defaultStart = date('Y-m-d 00:00:00', strtotime('-1 year'));
  30. $defaultEnd = date('Y-m-d 23:59:59');
  31. $startTime = trim((string)$this->request->get('start_time', $defaultStart));
  32. $endTime = trim((string)$this->request->get('end_time', $defaultEnd));
  33. $limitParam = $this->request->get('limit', '');
  34. $useLimit = ($limitParam !== '' && $limitParam !== null);
  35. $limit = $useLimit ? max(1, min(500, (int)$limitParam)) : 0;
  36. $redisKey = 'procuremen_redis';
  37. $redis = null;
  38. try {
  39. $redis = redis();
  40. } catch (\Exception $e) {
  41. $redis = null;
  42. }
  43. if (!$refresh && $redis) {
  44. try {
  45. $cached = $redis->get($redisKey);
  46. if ($cached !== false && $cached !== '') {
  47. $decoded = json_decode($cached, true);
  48. if (is_array($decoded)) {
  49. return json($decoded);
  50. }
  51. }
  52. } catch (\Exception $e) {
  53. // 读缓存失败则继续走库
  54. }
  55. }
  56. $list = [];
  57. $dbOk = false;
  58. try {
  59. $query = Db::table('scydgy')
  60. ->alias('a')
  61. ->join('mcyd b', 'b.ICYDID = a.ICYDID AND b.iStatus >= 10', 'inner')
  62. ->field('a.ID, b.CCYDH, b.CYJMC, a.CDXMC, a.CGYBH, a.CGYMC, a.CDW, a.NGZL, b.CDF, a.cGzzxMc, a.MBZ, a.bwjg, a.iStatus, a.dStamp, b.dputrecord, b.cywyxm')
  63. ->where([
  64. 'a.bwjg' => 1,
  65. 'a.iEndBz' => 0,
  66. 'a.iType' => 0,
  67. 'a.iStatus' => 10,
  68. ])
  69. ->where('a.dStamp', '>=', $startTime)
  70. ->where('a.dStamp', '<=', $endTime)
  71. ->order('a.dStamp', 'desc');
  72. if ($limit > 0) {
  73. $query->limit($limit);
  74. }
  75. $list = $query->select();
  76. $dbOk = true;
  77. } catch (\Exception $e) {
  78. $list = [];
  79. $dbOk = false;
  80. }
  81. $result = [
  82. 'code' => 200,
  83. 'msg' => 'success',
  84. 'data' => $list ?: [],
  85. ];
  86. if ($redis && $dbOk) {
  87. try {
  88. $redis->set($redisKey, json_encode($result, JSON_UNESCAPED_UNICODE));
  89. } catch (\Exception $e) {
  90. // 写缓存失败仍返回本次查询结果
  91. }
  92. }
  93. if (!$dbOk && $redis) {
  94. try {
  95. $cached = $redis->get($redisKey);
  96. if ($cached !== false && $cached !== '') {
  97. $decoded = json_decode($cached, true);
  98. if (is_array($decoded)) {
  99. return json($decoded);
  100. }
  101. }
  102. } catch (\Exception $e) {
  103. }
  104. }
  105. return json($result);
  106. }
  107. }