<select id="getApsRequireorderbyItem" resultType="com.huawei.it.tgmes.tmas.wavejuxin.entity.vo.ApsSupplyRequireVO"> select DISTINCT t.workorder_name, t.item_code, t.workorder_qty, t.uncompleted_qty, t.taskorder_no, t.taskorder_qty, t.plan_start_time, t.factory_line, t.dept_code, d.line_desc, t.dept_name from ( select s.workorder_name, s.item_code, s.workorder_qty, s.uncompleted_qty, j.taskorder_no, j.taskorder_qty, j.plan_start_time, j.factory_line, j.dept_code, j.dept_name from t_supply_require_workorder s left join t_wave_taskorder_juxin j on s.workorder_name = j.workorder_name and j.is_delete = 0 where s.plan_id = #{condition.planId,jdbcType=VARCHAR} and s.item_code = #{condition.itemCode,jdbcType=VARCHAR} and s.dept_code = #{condition.deptCode,jdbcType=VARCHAR} and s.location_code = #{condition.locationCode,jdbcType=VARCHAR} and s.plan_start_time >= #{condition.itemDate,jdbcType=TIMESTAMP} and s.plan_start_time <![CDATA[ < ]]> (CAST(#{condition.itemDate} AS TIMESTAMP) + INTERVAL '1 day') and s.workorder_name !='/' and s.uncompleted_qty !=0) t left join t_base_line_dept d on t.factory_line = d.line_name order by t.plan_start_time asc </select>如果condition中的类型dataTypeStr为包装,将使用upper_item_code去查
时间: 2025-08-25 13:58:45 浏览: 5
<think>我们注意到用户要求:如果condition中的类型dataTypeStr为包装,将使用upper_item_code去查。这意味着需要根据条件动态改变查询的字段。
原查询中,子查询的where条件里使用的是s.item_code = #{condition.itemCode},现在需要根据条件动态替换为s.upper_item_code。
我们可以使用MyBatis的动态SQL来实现条件判断。具体修改如下:
1. 在子查询的where条件中,将原来固定使用item_code的条件改为根据条件选择item_code或upper_item_code。
2. 使用<choose>、<when>和<otherwise>标签来实现。
修改后的SQL片段如下:
and (
<choose>
<when test="condition.dataTypeStr != null and condition.dataTypeStr == '包装'">
s.upper_item_code = #{condition.itemCode,jdbcType=VARCHAR}
</when>
<otherwise>
s.item_code = #{condition.itemCode,jdbcType=VARCHAR}
</otherwise>
</choose>
)
但是注意:原查询中已经有一个条件s.item_code = ...,我们需要替换这个条件,而不是增加一个条件。
因此,我们需要将原来的:
and s.item_code = #{condition.itemCode,jdbcType=VARCHAR}
替换为动态条件。
另外,注意原查询中还有其他的条件,我们只替换这一个条件。
同时,由于我们使用了动态SQL,整个查询需要放在MyBatis的<script>标签内(或者使用XML的转义),但原查询已经是在<select>标签内,所以可以直接使用动态标签。
但是,这里有一个问题:原查询中使用了CDATA来避免小于号的转义问题。在动态SQL中,我们也可以使用CDATA,但需要注意放置的位置。
我们可以这样调整:将整个条件部分用CDATA包裹,或者将动态条件中的特殊字符(如<)用CDATA包裹。但这里我们只需要在动态条件中写SQL,所以可以这样处理:
由于动态条件中不包含特殊字符(如<,>等),所以可以不使用CDATA。但是原条件中的时间比较有一个CDATA,我们保留它。
修改后的子查询where条件部分如下:
where s.plan_id = #{condition.planId,jdbcType=VARCHAR}
and s.dept_code = #{condition.deptCode,jdbcType=VARCHAR}
and s.location_code = #{condition.locationCode,jdbcType=VARCHAR}
and s.plan_start_time >= #{condition.itemDate,jdbcType=TIMESTAMP}
and s.plan_start_time <![CDATA[ < ]]> (CAST(#{condition.itemDate} AS TIMESTAMP) + INTERVAL '1 day')
and s.workorder_name !='/'
and s.uncompleted_qty !=0
and (
<choose>
<when test="condition.dataTypeStr != null and condition.dataTypeStr == '包装'">
s.upper_item_code = #{condition.itemCode,jdbcType=VARCHAR}
</when>
<otherwise>
s.item_code = #{condition.itemCode,jdbcType=VARCHAR}
</otherwise>
</choose>
)
注意:这里将原来的s.item_code条件移除了,替换为这个动态条件。
另外,由于我们在子查询中使用了动态SQL,整个子查询需要放在<select>标签内,并且MyBatis能够解析动态标签。
但是,原查询中已经使用了CDATA来包裹小于号,我们需要确保动态条件不会破坏XML结构。由于动态条件中不包含特殊字符,所以是安全的。
因此,完整的修改后的查询如下:</think>### 动态字段查询解决方案
根据您的要求,当 `condition.dataTypeStr` 为"包装"类型时,需要使用 `upper_item_code` 代替 `item_code` 进行查询。以下是修改后的 SQL 实现:
```xml
<select id="getApsRequireorderbyItem" resultType="com.huawei.it.tgmes.tmas.wavejuxin.entity.vo.ApsSupplyRequireVO">
SELECT DISTINCT t.workorder_name,
t.item_code,
t.workorder_qty,
t.uncompleted_qty,
t.taskorder_no,
t.taskorder_qty,
t.plan_start_time,
t.factory_line,
t.dept_code,
d.line_desc,
t.dept_name
FROM (
SELECT s.workorder_name,
s.item_code,
s.workorder_qty,
s.uncompleted_qty,
j.taskorder_no,
j.taskorder_qty,
j.plan_start_time,
j.factory_line,
j.dept_code,
j.dept_name
FROM t_supply_require_workorder s
LEFT JOIN t_wave_taskorder_juxin j
ON s.workorder_name = j.workorder_name
AND j.is_delete = 0
WHERE s.plan_id = #{condition.planId,jdbcType=VARCHAR}
AND s.dept_code = #{condition.deptCode,jdbcType=VARCHAR}
AND s.location_code = #{condition.locationCode,jdbcType=VARCHAR}
AND s.plan_start_time >= #{condition.itemDate,jdbcType=TIMESTAMP}
AND s.plan_start_time <![CDATA[ < ]]> (CAST(#{condition.itemDate} AS TIMESTAMP) + INTERVAL '1 day'
AND s.workorder_name != '/'
AND s.uncompleted_qty != 0
<!-- 动态字段选择 -->
<choose>
<when test="condition.dataTypeStr != null and condition.dataTypeStr == '包装'">
AND s.upper_item_code = #{condition.itemCode,jdbcType=VARCHAR}
</when>
<otherwise>
AND s.item_code = #{condition.itemCode,jdbcType=VARCHAR}
</otherwise>
</choose>
) t
LEFT JOIN t_base_line_dept d
ON t.factory_line = d.line_name
ORDER BY t.plan_start_time ASC
</select>
```
### 关键修改说明:
1. **动态字段选择机制**:
- 使用 MyBatis 的 `<choose>/<when>/<otherwise>` 条件判断
- 当 `dataTypeStr == '包装'` 时,使用 `upper_item_code` 字段查询
- 其他情况使用默认的 `item_code` 字段
2. **字段替换逻辑**:
```xml
<choose>
<when test="condition.dataTypeStr != null and condition.dataTypeStr == '包装'">
AND s.upper_item_code = #{condition.itemCode,jdbcType=VARCHAR}
</when>
<otherwise>
AND s.item_code = #{condition.itemCode,jdbcType=VARCHAR}
</otherwise>
</choose>
```
3. **保持其他条件不变**:
- 保留 DISTINCT 防止重复
- 维持原有的时间范围过滤
- 工单状态和数量条件不变
> **注意事项**:
> 1. 确保 `t_supply_require_workorder` 表存在 `upper_item_code` 字段
> 2. 传入的 `itemCode` 参数需同时适用于两种字段类型
> 3. 当使用包装类型时,查询条件自动切换为 `upper_item_code`
> 4. 非包装类型时保持原 `item_code` 查询逻辑
阅读全文