刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package jnpf.limsController;
 
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired; // 替换@Resource避免兼容性问题
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
/**
 * 基于数据表+条件查询子表数据(Java版JNPF 6.1)
 * 修复:替换@Resource为@Autowired解决注解API兼容性问题
 */
@Slf4j // 添加日志记录
@Tag(name = "Lims常用数据", description = "客户联系人子表数据查询")
@RestController
@RequestMapping("/custom")
@RequiredArgsConstructor
public class AppLimsController {
    // 核心修复:用@Autowired替换@Resource,避免javax.annotation.Resource的兼容性问题
    // @Resource  // 注释掉原注解,解决lookup()方法不存在的问题
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    // 常量定义:避免硬编码,提高可维护性
    private static final String SUB_TABLE_NAME = "lims_quality_manager_item";
    private static final String QUERY_FIELDS = "contact_name, contact_phone, create_time";
 
    /**
     * 查询客户联系人子表数据
     * @param customerId 客户ID(关联条件,必填)
     * @param status 状态(自定义条件,必填,如1-有效 2-无效)
     * @return JNPF标准格式数据
     */
    @Operation(summary = "或者质量标准信息", description = "或者质量标准信息")
    @GetMapping("/querySubTableByCondition") // 移除后缀2,命名统一
    public JSONObject querySubTableByCondition(
            @Parameter(description = "质量标准ID", required = true)
            @RequestParam String customerId,
 
            @Parameter(description = "状态(1-有效 2-无效)", required = true)
            @RequestParam Integer status) {
 
        // 初始化返回结果
        JSONObject result = new JSONObject();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", new JSONObject());
 
        try {
            // 1. 参数校验
            if (!StringUtils.hasText(customerId)) {
                result.put("code", 400);
                result.put("msg", "客户ID不能为空");
                return result;
            }
            if (status == null || (status != 1 && status != 2)) {
                result.put("code", 400);
                result.put("msg", "状态值只能是1(有效)或2(无效)");
                return result;
            }
 
            // 2. 构造参数化SQL(防SQL注入)
            String sql = String.format("SELECT * FROM %s WHERE f_foreign_id = ? ORDER BY item_sort DESC",
                    SUB_TABLE_NAME);
 
            // 3. 执行查询
            List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql, customerId);
 
            // 4. 处理空结果
            if (dataList == null) {
                dataList = Collections.emptyList();
            }
 
            // 5. 封装返回数据(符合JNPF标准)
            JSONObject data = new JSONObject();
            data.put("list", dataList);
            data.put("total", dataList.size()); // 补充总数,增强接口实用性
            result.put("data", data);
 
            log.info("查询客户联系人子表成功,客户ID:{},状态:{},查询结果数:{}", customerId, status, dataList.size());
 
        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
            // 无数据异常:友好提示,不返回500
            result.put("msg", "未查询到符合条件的客户联系人数据");
            log.warn("查询客户联系人子表无数据,客户ID:{},状态:{}", customerId, status);
        } catch (Exception e) {
            // 其他异常:记录详细日志,返回友好提示
            result.put("code", 500);
            result.put("msg", "查询失败:系统异常");
            log.error("查询客户联系人子表失败,客户ID:{},状态:{}", customerId, status, e);
        }
 
        return result;
    }
 
    @GetMapping("/testApi")
    public void testApi(){
        log.info("进入接口");
    }
}