<?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.ygtx.gxt.mapper.GxtDeviceRuntimePersonMapper">

    <resultMap type="GxtDeviceRuntimePerson" id="GxtDeviceRuntimePersonResult">
        <result property="id" column="id"/>
        <result property="deviceRuntimId" column="device_runtim_id"/>
        <result property="personId" column="person_id"/>
        <result property="isLeader" column="is_leader"/>
        <result property="score" column="score"/>
        <result property="monthPeriod" column="month_period"/>
        <result property="status" column="status"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectGxtDeviceRuntimePersonVo">
        select id, device_runtim_id, person_id, is_leader, score, month_period, status, create_by, create_time, update_by, update_time, remark from gxt_device_runtime_person
    </sql>

    <select id="selectGxtDeviceRuntimePersonList" parameterType="GxtDeviceRuntimePerson" resultMap="GxtDeviceRuntimePersonResult">
        <include refid="selectGxtDeviceRuntimePersonVo"/>
        <where>
            <if test="deviceRuntimId != null "> and device_runtim_id = #{deviceRuntimId}</if>
            <if test="personId != null "> and person_id = #{personId}</if>
            <if test="isLeader != null "> and is_leader = #{isLeader}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="updateBy != null  and updateBy != ''"> and update_by = #{updateBy}</if>
            <if test="remark != null  and remark != ''"> and remark = #{remark}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectGxtDeviceRuntimePersonById" parameterType="Long" resultMap="GxtDeviceRuntimePersonResult">
        <include refid="selectGxtDeviceRuntimePersonVo"/>
        where id = #{id}
    </select>

    <select id="selectGxtDeviceRuntimePersonByMD" resultMap="GxtDeviceRuntimePersonResult">
        SELECT
            drp.*
        FROM
            gxt_device_runtime dr
                LEFT JOIN gxt_device_runtime_person drp ON dr.id = drp.device_runtim_id
                LEFT JOIN sys_user u ON drp.person_id = u.user_id
                LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
        WHERE
            dr.month_period = #{monthPeriod}
          AND (
                    u.dept_id = #{deptId}
                OR d.parent_id = #{deptId})
    </select>

    <insert id="insertGxtDeviceRuntimePerson" parameterType="GxtDeviceRuntimePerson">
        insert into gxt_device_runtime_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            <if test="deviceRuntimId != null">device_runtim_id,</if>
            <if test="personId != null">person_id,</if>
            <if test="isLeader != null">is_leader,</if>
            <if test="score != null">score,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null and remark != ''">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{id},
            <if test="deviceRuntimId != null">#{deviceRuntimId},</if>
            <if test="personId != null">#{personId},</if>
            <if test="isLeader != null">#{isLeader},</if>
            <if test="score != null">#{score},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
         </trim>
    </insert>

    <update id="updateGxtDeviceRuntimePerson" parameterType="GxtDeviceRuntimePerson">
        update gxt_device_runtime_person
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceRuntimId != null">device_runtim_id = #{deviceRuntimId},</if>
            <if test="personId != null">person_id = #{personId},</if>
            <if test="isLeader != null">is_leader = #{isLeader},</if>
            <if test="score != null">score = #{score},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtDeviceRuntimePersonById" parameterType="Long">
        delete from gxt_device_runtime_person where id = #{id}
    </delete>

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

</mapper>