<?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.ktg.iscs.mapper.IsWorkareaMapper">

    <resultMap type="IsWorkarea" id="IsWorkareaResult">
        <result property="workareaId"    column="workarea_id"    />
        <result property="workareaCode"    column="workarea_code"    />
        <result property="workareaName"    column="workarea_name"    />
        <result property="workareaType"    column="workarea_type"    />
        <result property="workshopId"    column="workshop_id"    />
        <result property="map"    column="map"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <result property="status"    column="status"    />
        <result property="mapImg"    column="map_img"    />
    </resultMap>

    <sql id="selectIsWorkareaVo">
        select workarea_id, workarea_code, workarea_name,
               workarea_type, workshop_id, map, del_flag, create_by,
               create_time, update_time, update_by, parent_id, ancestors, order_num, status,
               map_img
        from is_workarea
    </sql>

    <select id="selectIsWorkareaList" parameterType="IsWorkarea" resultMap="IsWorkareaResult">
        <include refid="selectIsWorkareaVo"/>
        <where>
            <if test="workareaCode != null  and workareaCode != ''"> and workarea_code = #{workareaCode}</if>
            <if test="workareaName != null  and workareaName != ''"> and workarea_name like concat('%', #{workareaName}, '%')</if>
            <if test="workareaType != null  and workareaType != ''"> and workarea_type = #{workareaType}</if>
            <if test="workshopId != null "> and workshop_id = #{workshopId}</if>
            <if test="map != null  and map != ''"> and map = #{map}</if>
        </where>
    </select>

    <select id="selectIsWorkareaByWorkareaId" parameterType="Long" resultMap="IsWorkareaResult">
        <include refid="selectIsWorkareaVo"/>
        where workarea_id = #{workareaId}
    </select>

    <insert id="insertIsWorkarea" parameterType="IsWorkarea" useGeneratedKeys="true" keyProperty="workareaId">
        insert into is_workarea
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workareaCode != null and workareaCode != ''">workarea_code,</if>
            <if test="workareaName != null and workareaName != ''">workarea_name,</if>
            <if test="workareaType != null">workarea_type,</if>
            <if test="workshopId != null">workshop_id,</if>
            <if test="map != null">map,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="mapImg != null and mapImg != ''">map_img,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workareaCode != null and workareaCode != ''">#{workareaCode},</if>
            <if test="workareaName != null and workareaName != ''">#{workareaName},</if>
            <if test="workareaType != null">#{workareaType},</if>
            <if test="workshopId != null">#{workshopId},</if>
            <if test="map != null">#{map},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
            <if test="mapImg != null and mapImg != ''">#{mapImg},</if>
         </trim>
    </insert>

    <update id="updateIsWorkarea" parameterType="IsWorkarea">
        update is_workarea
        <trim prefix="SET" suffixOverrides=",">
            <if test="workareaCode != null and workareaCode != ''">workarea_code = #{workareaCode},</if>
            <if test="workareaName != null and workareaName != ''">workarea_name = #{workareaName},</if>
            <if test="workareaType != null">workarea_type = #{workareaType},</if>
            <if test="workshopId != null">workshop_id = #{workshopId},</if>
            <if test="map != null">map = #{map},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="mapImg != null and mapImg != ''">map_img = #{mapImg},</if>
        </trim>
        where workarea_id = #{workareaId}
    </update>

    <delete id="deleteIsWorkareaByWorkareaId" parameterType="Long">
        delete from is_workarea where workarea_id = #{workareaId}
    </delete>

    <delete id="deleteIsWorkareaByWorkareaIds" parameterType="String">
        delete from is_workarea where workarea_id in
        <foreach item="workareaId" collection="array" open="(" separator="," close=")">
            #{workareaId}
        </foreach>
    </delete>

    <select id="getIsWorkareaPage" resultType="com.ktg.iscs.domain.vo.workarea.PageWorkareaVO">
        SELECT
            w.*,
            s.workshop_name
        FROM
            is_workarea w
            LEFT JOIN md_workshop s ON s.workshop_id = w.workshop_id
        <where>
            <if test="dto.workareaId != null">
                and w.workarea_id = #{dto.workareaId}
            </if>
            <if test="dto.workareaCode != null and dto.workareaCode.trim != ''">
                and w.workarea_code like concat('%',#{dto.workareaCode},'%')
            </if>
            <if test="dto.workareaName != null and dto.workareaName.trim != ''">
                and w.workarea_name like concat('%',#{dto.workareaName},'%')
            </if>
            <if test="dto.workareaType != null and dto.workareaType.trim != ''">
                and w.workarea_type = #{dto.workareaType}
            </if>
            <if test="dto.workshopId != null">
                and w.workshop_id = #{dto.workshopId}
            </if>
            <if test="dto.status != null and dto.status.trim != ''">
                and w.status = #{dto.status}
            </if>
        </where>
            order by w.parent_id, w.order_num
    </select>

</mapper>
