refactor(os-ems): 优化 SQL 查询语句和时间比较逻辑

- 将 '>' 和 '<' 符号替换为 <![CDATA[ ]]>
- 使用 DATE_FORMAT 函数替代 CONVERT 函数,以提高日期格式化的一致性
- 用 LOCATE 函数替换 charindex,以提高字符串定位的可移植性
- 优化时间范围查询条件,使用直接比较替代 DATEDIFF函数
- 统一使用 now() 替代 sysdate(),以保持时间函数的一致性
boardTest
zch 3 weeks ago
parent 777b8b7955
commit 99625e57e2

@ -60,8 +60,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="alarmStatus != null "> and rad.alarm_status = #{alarmStatus}</if>
<if test="alarmData != null and alarmData != ''"> and rad.alarm_data = #{alarmData}</if>
<if test="operationName != null and operationName != ''"> and rad.operation_name like concat('%', #{operationName}, '%')</if>
<if test="beginOperationTime != null "> and rad.operation_time >= #{beginOperationTime}</if>
<if test="endOperationTime != null "> and #{endOperationTime} >= rad.operation_time</if>
<if test="beginOperationTime != null "> and rad.operation_time &gt;= #{beginOperationTime}</if>
<if test="endOperationTime != null "> and #{endOperationTime} &gt;= rad.operation_time</if>
<if test="cause != null and cause != ''"> and rad.cause = #{cause}</if>
<if test="notifyUser != null and notifyUser != ''"> and rad.notify_user = #{notifyUser}</if>
</where>
@ -160,7 +160,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
rad.notify_user
FROM ems_record_alarm_data rad
WHERE rad.alarm_status = 1
AND rad.create_time >= #{startTime}
AND rad.create_time &gt;= #{startTime}
ORDER BY rad.obj_id DESC
</select>

@ -34,9 +34,9 @@
sum(expend * dnb_price) price
FROM (
SELECT erpd.monitor_code,
<if test="dateType == 10">CONVERT(VARCHAR(10), erpd.begin_time, 120) pointTime,</if>
<if test="dateType == 7">CONVERT(VARCHAR(7), erpd.begin_time, 120) pointTime,</if>
<if test="dateType == 4">CONVERT(VARCHAR(4), erpd.begin_time, 120) pointTime,</if>
<if test="dateType == 10">DATE_FORMAT(erpd.begin_time, '%Y-%m-%d') pointTime,</if>
<if test="dateType == 7">DATE_FORMAT(erpd.begin_time, '%Y-%m') pointTime,</if>
<if test="dateType == 4">DATE_FORMAT(erpd.begin_time, '%Y') pointTime,</if>
NULLIF(erpd.expend, 0) expend,
case
when ebsp.price_type = 0 then '尖峰'
@ -47,9 +47,9 @@
ebsp.dnb_price
FROM ems_report_point_dnb erpd
join ems_base_share_price ebsp
on LEFT(CONVERT(VARCHAR, erpd.begin_time, 23), 7) = ebsp.year_month_date
and CAST(erpd.begin_time AS time) >= CAST(ebsp.start_hour AS time)
and CAST(erpd.begin_time AS time) &lt; CAST(ebsp.end_hour AS time)
on DATE_FORMAT(erpd.begin_time, '%Y-%m') = ebsp.year_month_date
and TIME(erpd.begin_time) &gt;= TIME(ebsp.start_hour)
and TIME(erpd.begin_time) &lt; TIME(ebsp.end_hour)
<if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">
WHERE FORMAT(erpd.begin_time, 'yyyy-MM-dd') between #{beginCollectTime} and #{endCollectTime}
</if>

@ -85,7 +85,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="dictType != null and dictType != ''">#{dictType},</if>
<if test="sort != null">#{sort},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
sysdate()
now()
)
</insert>
@ -105,7 +105,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="dictType != null">dict_type = #{dictType},</if>
<if test="sort != null">sort = #{sort},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
update_time = sysdate()
update_time = now()
</set>
where column_id = #{columnId}
</update>

@ -167,7 +167,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="genPath != null and genPath != ''">#{genPath},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
sysdate()
now()
)
</insert>
@ -195,7 +195,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="options != null and options != ''">options = #{options},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="remark != null">remark = #{remark},</if>
update_time = sysdate()
update_time = now()
</set>
where table_id = #{tableId}
</update>

@ -251,7 +251,7 @@
<if test="orderCode != null and orderCode != ''">and ppi.order_code = #{orderCode}</if>
<if test="processCode != null and processCode != ''">and ppi.process_code = #{processCode}</if>
<if test="params.beginOrderDate != null and params.beginOrderDate != '' and params.endOrderDate != null and params.endOrderDate != ''">
and LEFT(CONVERT(VARCHAR(17), ppd.begin_time, 120), 10) between #{params.beginOrderDate} and #{params.endOrderDate}
and DATE_FORMAT(ppd.begin_time, '%Y-%m-%d') between #{params.beginOrderDate} and #{params.endOrderDate}
</if>
</where>
</select>

@ -36,12 +36,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND invoke_target like concat('%', #{invokeTarget}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ create_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, create_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND create_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ create_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, create_time, #{params.endTime}) <![CDATA[>=]]> 0
AND create_time <![CDATA[<=]]> #{params.endTime}
</if>
</where>
order by create_time desc

@ -51,12 +51,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND config_key like concat('%', #{configKey}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ create_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, create_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND create_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ create_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, create_time, #{params.endTime}) <![CDATA[>=]]> 0
AND create_time <![CDATA[<=]]> #{params.endTime}
</if>
</where>
</select>

@ -76,11 +76,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
select * from sys_dept where charindex(',' + convert(nvarchar, #{deptId}), ',' + ancestors) > 0
select * from sys_dept where LOCATE(CONCAT(',', #{deptId}, ','), CONCAT(',', ancestors, ',')) > 0
</select>
<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
select count(*) from sys_dept where status = 0 and del_flag = '0' and charindex(',' + convert(nvarchar, #{deptId}), ',' + ancestors) > 0
select count(*) from sys_dept where status = 0 and del_flag = '0' and LOCATE(CONCAT(',', #{deptId}, ','), CONCAT(',', ancestors, ',')) > 0
</select>
<select id="checkDeptNameUnique" resultMap="SysDeptResult">

@ -33,12 +33,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND dict_type like concat('%', #{dictType}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ create_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, create_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND create_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ create_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, create_time, #{params.endTime}) <![CDATA[>=]]> 0
AND create_time <![CDATA[<=]]> #{params.endTime}
</if>
</where>
</select>

@ -34,12 +34,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND user_name like concat('%', #{userName}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ login_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, login_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND login_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ login_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, login_time, #{params.endTime}) <![CDATA[>=]]> 0
AND login_time <![CDATA[<=]]> #{params.endTime}
</if>
</where>
order by info_id desc

@ -150,7 +150,7 @@
<if test="icon !=null and icon != ''">icon = #{icon},</if>
<if test="remark != null and remark != ''">remark = #{remark},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = sysdate()
update_time = now()
</set>
where menu_id = #{menuId}
</update>
@ -191,7 +191,7 @@
<if test="icon != null and icon != ''">#{icon},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
sysdate()
now()
)
</insert>

@ -59,12 +59,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND oper_name like concat('%', #{operName}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ oper_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, oper_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND oper_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ oper_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, oper_time, #{params.endTime}) <![CDATA[>=]]> 0
AND oper_time <![CDATA[<=]]> #{params.endTime}
</if>
</where>
order by oper_id desc

@ -46,12 +46,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND r.role_key like concat('%', #{roleKey}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ r.create_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, r.create_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND r.create_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ r.create_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, r.create_time, #{params.endTime}) <![CDATA[>=]]> 0
AND r.create_time <![CDATA[<=]]> #{params.endTime}
</if>
<!-- 数据范围过滤 -->
${params.dataScope}

@ -73,15 +73,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND u.phonenumber like concat('%', #{phonenumber}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ u.create_time >= convert(datetime, #{params.beginTime}, 20)]]>-->
and datediff(d, u.create_time, #{params.beginTime}) <![CDATA[<=]]> 0
AND u.create_time <![CDATA[>=]]> #{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ u.create_time <= convert(datetime, #{params.endTime}, 20)]]>-->
and datediff(d, u.create_time, #{params.endTime}) <![CDATA[>=]]> 0
AND u.create_time <![CDATA[<=]]> #{params.endTime}
</if>
<if test="deptId != null and deptId != 0">
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE charindex(',' + CONVERT (nvarchar, #{deptId}), ',' + ancestors) > 0 ))
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE LOCATE(CONCAT(',', #{deptId}, ','), CONCAT(',', ancestors, ',')) > 0 ))
</if>
<!-- 数据范围过滤 -->
${params.dataScope}

Loading…
Cancel
Save