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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?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.ard.inspection.mapper.ArdInspectionGroupMapper">
    
    <resultMap type="ArdInspectionGroup" id="ArdInspectionGroupResult">
        <result property="groupId"          column="group_id"           />
        <result property="groupParentId"    column="group_parent_id"    />
        <result property="ancestors"        column="ancestors"          />
        <result property="groupName"        column="group_name"         />
        <result property="orderNum"         column="order_num"          />
        <result property="createBy"         column="create_by"          />
        <result property="createTime"       column="create_time"        />
        <result property="updateBy"         column="update_by"          />
        <result property="updateTime"       column="update_time"        />
        <result property="delFlag"          column="del_flag"           />
        <result property="parentName"       column="parent_name"        />
    </resultMap>
 
    <sql id="selectArdInspectionGroupVo">
        select group_id,
               group_parent_id,
               ancestors,
               group_name,
               order_num,
               create_by,
               create_time,
               update_by,
               update_time,
               del_flag
        from ard_inspection_group
    </sql>
 
    <select id="selectArdInspectionGroupList" parameterType="ArdInspectionGroup" resultMap="ArdInspectionGroupResult">
        <include refid="selectArdInspectionGroupVo"/>
        <where>
            del_flag = '0'
            <if test="groupId != null ">
                and group_id = #{groupId}
            </if>
            <if test="groupParentId != null ">
                and group_parent_id = #{groupParentId}
            </if>
            <if test="ancestors != null  and ancestors != ''">
                and ancestors = #{ancestors}
            </if>
            <if test="groupName != null  and groupName != ''">
                and group_name like concat('%', #{groupName}, '%')
            </if>
            <if test="orderNum != null ">
                and order_num = #{orderNum}
            </if>
        </where>
        order by group_parent_id, order_num
    </select>
    
    <select id="selectArdInspectionGroupByGroupId" parameterType="Long" resultMap="ArdInspectionGroupResult">
        SELECT
            g.group_id,
            g.group_parent_id,
            g.ancestors,
            g.group_name,
            g.order_num,
            ( SELECT group_name FROM ard_inspection_group WHERE group_id = g.group_parent_id) parent_name
        FROM
            ard_inspection_group g
        WHERE
            g.group_id = #{groupId}
    </select>
 
    <insert id="insertArdInspectionGroup" parameterType="ArdInspectionGroup">
        insert into ard_inspection_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="groupParentId != null">group_parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="groupName != null">group_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId},</if>
            <if test="groupParentId != null">#{groupParentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="groupName != null">#{groupName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>
 
    <update id="updateArdInspectionGroup" parameterType="ArdInspectionGroup">
        update ard_inspection_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupParentId != null">group_parent_id = #{groupParentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="groupName != null">group_name = #{groupName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where group_id = #{groupId}
    </update>
 
    <update id="deleteArdInspectionGroupByGroupId" parameterType="Long">
        update ard_inspection_group set del_flag = '2' where group_id = #{groupId}
    </update>
 
    <update id="deleteArdInspectionGroupByGroupIds" parameterType="String">
        update ard_inspection_group set del_flag = '2' where group_id in
        <foreach item="groupId" collection="array" open="(" separator="," close=")">
            #{groupId}
        </foreach>
    </update>
 
    <select id="checkInspectionGroupNameUniqueA" resultMap="ArdInspectionGroupResult">
        <include refid="selectArdInspectionGroupVo"/>
        where group_name=#{groupName}
        and group_parent_id = #{groupParentId} limit 1
    </select>
 
    <select id="checkInspectionGroupNameUniqueB" resultMap="ArdInspectionGroupResult">
        <include refid="selectArdInspectionGroupVo"/>
        where del_flag = '0'
        and group_name=#{groupName} limit 1
    </select>
 
    <select id="selectChildrenInspectionGroupById" parameterType="Long" resultMap="ArdInspectionGroupResult">
        select * from ard_inspection_group where find_in_set(#{groupId}, ancestors)
    </select>
 
    <update id="updateInspectionGroupChildren" parameterType="java.util.List">
        update ard_inspection_group set ancestors =
        <foreach collection="inspectionGroups" item="item" index="index"
                 separator=" " open="case group_id" close="end">
            when #{item.groupId} then #{item.ancestors}
        </foreach>
        where group_id in
        <foreach collection="inspectionGroups" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.groupId}
        </foreach>
    </update>
 
    <select id="hasChildByInspectionGroupId" parameterType="Long" resultType="int">
        select count(1) from ard_inspection_group
        where del_flag = '0' and group_parent_id = #{groupId} limit 1
    </select>
 
    <select id="checkInspectionGroupExistUser" parameterType="Long" resultType="int">
        select count(1) from sys_user where group_id = #{groupId} and del_flag = '0'
    </select>
 
    <select id="selectMaxGroupId" resultType="int">
        SELECT group_id FROM ard_inspection_group
        WHERE group_parent_id = 0 AND ancestors = 0
        ORDER BY group_id DESC LIMIT 1
    </select>
 
</mapper>