| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace app\api\controller;
- use think\Controller;
- use think\Db;
- /**
- * 外发采购列表数据 API(供第三方拉取并写入 Redis,后台列表读同一份缓存)
- */
- class Procuremen extends Controller
- {
- /**
- * 外发加工候选列表
- *
- * 请求:GET
- * 参数:
- * - refresh=1 强制查库并覆盖 Redis(查询成功才写入)
- * - start_time 可选,开始时间,默认近一年前 00:00:00
- * - end_time 可选,结束时间,默认当前日 23:59:59
- * - limit 可选,最大返回条数,默认不限制(与后台整包缓存一致);若传则 1~500
- *
- * Redis:键固定为 procuremen_redis(与 admin Procuremen::index 一致)
- * 结构:{ "code":200, "msg":"success", "data":[ {...行字段英文键...} ] }
- */
- public function GetProcuremen()
- {
- if (!$this->request->isGet()) {
- return json(['code' => 400, 'msg' => '请使用GET请求', 'data' => []]);
- }
- $refresh = (int)$this->request->get('refresh', 0);
- $defaultStart = date('Y-m-d 00:00:00', strtotime('-1 year'));
- $defaultEnd = date('Y-m-d 23:59:59');
- $startTime = trim((string)$this->request->get('start_time', $defaultStart));
- $endTime = trim((string)$this->request->get('end_time', $defaultEnd));
- $limitParam = $this->request->get('limit', '');
- $useLimit = ($limitParam !== '' && $limitParam !== null);
- $limit = $useLimit ? max(1, min(500, (int)$limitParam)) : 0;
- $redisKey = 'procuremen_redis';
- $redis = null;
- try {
- $redis = redis();
- } catch (\Exception $e) {
- $redis = null;
- }
- if (!$refresh && $redis) {
- try {
- $cached = $redis->get($redisKey);
- if ($cached !== false && $cached !== '') {
- $decoded = json_decode($cached, true);
- if (is_array($decoded)) {
- return json($decoded);
- }
- }
- } catch (\Exception $e) {
- // 读缓存失败则继续走库
- }
- }
- $list = [];
- $dbOk = false;
- try {
- $query = Db::table('scydgy')
- ->alias('a')
- ->join('mcyd b', 'b.ICYDID = a.ICYDID AND b.iStatus >= 10', 'inner')
- ->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')
- ->where([
- 'a.bwjg' => 1,
- 'a.iEndBz' => 0,
- 'a.iType' => 0,
- 'a.iStatus' => 10,
- ])
- ->where('a.dStamp', '>=', $startTime)
- ->where('a.dStamp', '<=', $endTime)
- ->order('a.dStamp', 'desc');
- if ($limit > 0) {
- $query->limit($limit);
- }
- $list = $query->select();
- $dbOk = true;
- } catch (\Exception $e) {
- $list = [];
- $dbOk = false;
- }
- $result = [
- 'code' => 200,
- 'msg' => 'success',
- 'data' => $list ?: [],
- ];
- if ($redis && $dbOk) {
- try {
- $redis->set($redisKey, json_encode($result, JSON_UNESCAPED_UNICODE));
- } catch (\Exception $e) {
- // 写缓存失败仍返回本次查询结果
- }
- }
- if (!$dbOk && $redis) {
- try {
- $cached = $redis->get($redisKey);
- if ($cached !== false && $cached !== '') {
- $decoded = json_decode($cached, true);
- if (is_array($decoded)) {
- return json($decoded);
- }
- }
- } catch (\Exception $e) {
- }
- }
- return json($result);
- }
- }
|