liujie
昨天 c983a0a398504697a777ffae48e59961b5ee2bd7
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.SysAgreementMapper">
 
    <!--
      协议版本是追加式表:一行一个已发布版本,旧版本永不修改。
      故这里没有 update/delete 语句,也没有 status 列——列表的「当前版本/历史版本」
      是下面 latest_flag 那个相关子查询实时算出来的(同类型中编号最大者即当前版本)。
    -->
    <resultMap type="SysAgreement" id="SysAgreementResult">
        <result property="agreementId"   column="agreement_id"   />
        <result property="agreementType" column="agreement_type" />
        <result property="version"       column="version"        />
        <result property="content"       column="content"        />
        <result property="latestFlag"    column="latest_flag"    />
        <result property="createBy"      column="create_by"      />
        <result property="createTime"    column="create_time"    />
        <result property="remark"        column="remark"         />
    </resultMap>
 
    <!--
      latest_flag 用相关子查询算,不用 join 派生表:join 会被 PageHelper 的 count SQL 一起带上,
      同类型多版本时行数被放大,列表就会出现重复行。子查询只在 select 列里,不影响 count。
      子查询不会返回 NULL——GROUP BY agreement_type 决定了每个类型至少有一行。
      create_by/create_time 就是列表里的「更新人/更新时间」:版本行不可修改,对它而言创建即发布。
    -->
    <sql id="selectAgreementVo">
        select a.agreement_id, a.agreement_type, a.version, a.content, a.create_by, a.create_time, a.remark,
               case when a.agreement_id = (select max(x.agreement_id) from sys_agreement x
                                           where x.agreement_type = a.agreement_type)
                    then '1' else '0' end as latest_flag
        from sys_agreement a
    </sql>
 
    <select id="selectAgreementById" parameterType="Long" resultMap="SysAgreementResult">
        <include refid="selectAgreementVo"/>
        where a.agreement_id = #{agreementId}
    </select>
 
    <select id="selectAgreementList" parameterType="SysAgreement" resultMap="SysAgreementResult">
        <include refid="selectAgreementVo"/>
        <where>
            <!-- 协议编号是自增主键,按精确值筛 -->
            <if test="agreementId != null">
                AND a.agreement_id = #{agreementId}
            </if>
            <if test="agreementType != null and agreementType != ''">
                AND a.agreement_type = #{agreementType}
            </if>
            <!--
              「当前版本/历史版本」筛选。这里必须用双引号的 OGNL 字符串字面量(外层属性用单引号),
              写成 latestFlag == '1' 会被 OGNL 当成 char 对待,与 String 比永远为假 —— 筛选会静默失效。
            -->
            <if test='latestFlag != null and latestFlag == "1"'>
                AND a.agreement_id = (select max(x.agreement_id) from sys_agreement x
                                      where x.agreement_type = a.agreement_type)
            </if>
            <if test='latestFlag != null and latestFlag == "0"'>
                AND a.agreement_id != (select max(x.agreement_id) from sys_agreement x
                                       where x.agreement_type = a.agreement_type)
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(a.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(a.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        <!-- 最新发布的排最前(编号即发布顺序),不按类型分组:查「我刚发的那条」是最常见的诉求 -->
        order by a.agreement_id desc
    </select>
 
    <!--
      发布新版本:纯插入。没有任何 update —— 旧版本不做任何改动,
      它的「历史版本」身份由上面的 latest_flag 自动算出来。
      content 判 null 而不是判空串:清空后的空内容是一次有意义的覆盖,不该被 <if> 跳过。
    -->
    <insert id="insertAgreement" parameterType="SysAgreement">
        insert into sys_agreement (
            <if test="agreementType != null and agreementType != ''">agreement_type, </if>
            <if test="version != null and version != ''">version, </if>
            <if test="content != null">content, </if>
            <if test="remark != null and remark != ''">remark, </if>
            <if test="createBy != null and createBy != ''">create_by, </if>
            create_time
        )values(
            <if test="agreementType != null and agreementType != ''">#{agreementType}, </if>
            <if test="version != null and version != ''">#{version}, </if>
            <if test="content != null">#{content}, </if>
            <if test="remark != null and remark != ''">#{remark}, </if>
            <if test="createBy != null and createBy != ''">#{createBy}, </if>
            sysdate()
        )
    </insert>
 
    <select id="countByTypeAndVersion" resultType="int">
        select count(1) from sys_agreement
        where agreement_type = #{agreementType} and version = #{version}
    </select>
 
</mapper>