<?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.IsWorkstationMapper">

    <resultMap type="IsWorkstation" id="IsWorkstationResult">
        <result property="workstationId"    column="workstation_id"    />
        <result property="workstationCode"    column="workstation_code"    />
        <result property="workstationName"    column="workstation_name"    />
        <result property="workstationType"    column="workstation_type"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectIsWorkstationVo">
        select workstation_id, workstation_code, workstation_name, workstation_type, parent_id, ancestors, order_num, status, del_flag, create_by, create_time, update_time, update_by, remark from is_workstation
    </sql>

    <select id="selectIsWorkstationList" parameterType="IsWorkstation" resultMap="IsWorkstationResult">
        <include refid="selectIsWorkstationVo"/>
        <where>
            <if test="workstationCode != null  and workstationCode != ''"> and workstation_code = #{workstationCode}</if>
            <if test="workstationName != null  and workstationName != ''"> and workstation_name like concat('%', #{workstationName}, '%')</if>
            <if test="workstationType != null  and workstationType != ''"> and workstation_type = #{workstationType}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="selectIsWorkstationByWorkstationId" parameterType="Long" resultMap="IsWorkstationResult">
        <include refid="selectIsWorkstationVo"/>
        where workstation_id = #{workstationId}
    </select>

    <insert id="insertIsWorkstation" parameterType="IsWorkstation" useGeneratedKeys="true" keyProperty="workstationId">
        insert into is_workstation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">workstation_code,</if>
            <if test="workstationName != null and workstationName != ''">workstation_name,</if>
            <if test="workstationType != null">workstation_type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">#{workstationCode},</if>
            <if test="workstationName != null and workstationName != ''">#{workstationName},</if>
            <if test="workstationType != null">#{workstationType},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateIsWorkstation" parameterType="IsWorkstation">
        update is_workstation
        <trim prefix="SET" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">workstation_code = #{workstationCode},</if>
            <if test="workstationName != null and workstationName != ''">workstation_name = #{workstationName},</if>
            <if test="workstationType != null">workstation_type = #{workstationType},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</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="remark != null">remark = #{remark},</if>
        </trim>
        where workstation_id = #{workstationId}
    </update>

    <delete id="deleteIsWorkstationByWorkstationId" parameterType="Long">
        delete from is_workstation where workstation_id = #{workstationId}
    </delete>

    <delete id="deleteIsWorkstationByWorkstationIds" parameterType="String">
        delete from is_workstation where workstation_id in
        <foreach item="workstationId" collection="array" open="(" separator="," close=")">
            #{workstationId}
        </foreach>
    </delete>

    <select id="getIsWorkstationPage" resultType="com.ktg.iscs.domain.IsWorkstation">
        SELECT
            w.*
        FROM
            is_workstation w
        LEFT JOIN sys_user u ON u.user_id = w.create_by
        <where>
            w.del_flag = 0
            <if test="isWorkstation.workstationName != null and isWorkstation.workstationName.trim != ''">
                and w.workstation_name like concat('%',#{isWorkstation.workstationName},'%')
            </if>
            <if test="isWorkstation.status != null and isWorkstation.status.trim != ''">
                and w.status = #{isWorkstation.status}
            </if>
            <if test="isWorkstation.id != null">
                and w.workstation_id != #{isWorkstation.id}
            </if>
            <if test="isWorkstation.parentId != null">
                and w.parent_id = #{isWorkstation.parentId}
            </if>
            <if test="ids.size > 0">
                and w.workstation_id in
                <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            ${isWorkstation.paramMap.workstationScope}
        </where>
        order by w.order_num asc, w.workstation_id desc
    </select>

</mapper>
