| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?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, b.CCLBMMC, a.CDXMC, a.CGYBH, a.CGYMC, a.CDW, a.NGZL, b.CDF, a.cGzzxMc, a.MBZ,b.czlyq, 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));
- // 同步侧栏小键 + 清/写按月缓存,避免后台首屏/列表解整包
- $ymSet = [];
- $byYm = [];
- foreach ($list ?: [] as $row) {
- if (!is_array($row)) {
- continue;
- }
- $t = '';
- foreach (['dputrecord', 'dStamp'] as $k) {
- $v = trim((string)($row[$k] ?? ''));
- if ($v !== '' && strpos($v, '0000-00-00') !== 0) {
- $t = $v;
- break;
- }
- }
- if ($t !== '' && preg_match('/^([12]\d{3})-(\d{1,2})/', $t, $m)) {
- $mo = (int)$m[2];
- if ($mo >= 1 && $mo <= 12) {
- $ym = $m[1] . '-' . str_pad((string)$mo, 2, '0', STR_PAD_LEFT);
- $ymSet[$ym] = true;
- $byYm[$ym][] = $row;
- }
- }
- }
- if ($ymSet !== []) {
- $redis->set('procuremen_redis_yms', json_encode(array_keys($ymSet), JSON_UNESCAPED_UNICODE));
- }
- // 刷新整包时重写近月池,并清隐藏集缓存
- for ($i = 0; $i < 24; $i++) {
- $calYm = date('Y-m', strtotime('-' . $i . ' month', strtotime(date('Y-m-01'))));
- try {
- $redis->del('procuremen_pick_pool:' . $calYm);
- } catch (\Exception $e) {
- }
- }
- foreach ($byYm as $ym => $rows) {
- try {
- $redis->setex(
- 'procuremen_pick_pool:' . $ym,
- 600,
- json_encode(['ok' => 1, 'rows' => $rows], JSON_UNESCAPED_UNICODE)
- );
- } catch (\Exception $e) {
- }
- }
- try {
- $redis->del('procuremen_pick_hidden_scydgy_v1');
- } catch (\Exception $e) {
- }
- } 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);
- }
- }
|