<?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.zehong.system.mapper.TPatrolPositionMapper">
    
    <resultMap type="TPatrolPosition" id="TPatrolPositionResult">
        <result property="patrolId"    column="patrol_id"    />
        <result property="patrolName"    column="patrol_name"    />
        <result property="patrolType"    column="patrol_type"    />
        <result property="patrolNfc"    column="patrol_nfc"    />
        <result property="position"    column="position"    />
        <result property="patrolDetail"    column="patrol_detail"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
        <result property="remarks"    column="remarks"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectTPatrolPositionVo">
        select p.patrol_id, p.patrol_name, p.patrol_type, p.patrol_nfc, p.position, p.patrol_detail, p.dept_id, p.remarks, p.create_time, p.create_by, p.update_time, p.update_by from t_patrol_position p
    </sql>

    <select id="selectTPatrolPositionList" parameterType="TPatrolPosition" resultMap="TPatrolPositionResult">
        select p.patrol_id, p.patrol_name, p.patrol_type, p.patrol_nfc, p.position, p.patrol_detail, p.dept_id, p.remarks, p.create_time, p.create_by, p.update_time, p.update_by,d.dept_name from t_patrol_position p
        LEFT JOIN sys_dept d on d.dept_id = p.dept_id
        <where>
            p.is_del = 0
            <if test="patrolName != null  and patrolName != ''"> and patrol_name like concat('%', #{patrolName}, '%')</if>
            <if test="patrolType != null "> and patrol_type = #{patrolType}</if>
            <if test="patrolNfc != null  and patrolNfc != ''"> and patrol_nfc = #{patrolNfc}</if>
        </where>
        order by p.create_time desc
    </select>
    
    <select id="selectTPatrolPositionById" parameterType="Long" resultMap="TPatrolPositionResult">
        <include refid="selectTPatrolPositionVo"/>
        where patrol_id = #{patrolId}
    </select>
        
    <insert id="insertTPatrolPosition" parameterType="TPatrolPosition" useGeneratedKeys="true" keyProperty="patrolId">
        insert into t_patrol_position
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patrolName != null">patrol_name,</if>
            <if test="patrolType != null">patrol_type,</if>
            <if test="patrolNfc != null">patrol_nfc,</if>
            <if test="position != null">position,</if>
            <if test="patrolDetail != null">patrol_detail,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patrolName != null">#{patrolName},</if>
            <if test="patrolType != null">#{patrolType},</if>
            <if test="patrolNfc != null">#{patrolNfc},</if>
            <if test="position != null">#{position},</if>
            <if test="patrolDetail != null">#{patrolDetail},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateTPatrolPosition" parameterType="TPatrolPosition">
        update t_patrol_position
        <trim prefix="SET" suffixOverrides=",">
            <if test="patrolName != null">patrol_name = #{patrolName},</if>
            <if test="patrolType != null">patrol_type = #{patrolType},</if>
            <if test="patrolNfc != null">patrol_nfc = #{patrolNfc},</if>
            <if test="position != null">position = #{position},</if>
            <if test="patrolDetail != null">patrol_detail = #{patrolDetail},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where patrol_id = #{patrolId}
    </update>

    <delete id="deleteTPatrolPositionById" parameterType="Long">
        delete from t_patrol_position where patrol_id = #{patrolId}
    </delete>

    <delete id="deleteTPatrolPositionByIds" parameterType="String">
        delete from t_patrol_position where patrol_id in 
        <foreach item="patrolId" collection="array" open="(" separator="," close=")">
            #{patrolId}
        </foreach>
    </delete>
    <select id="selectPositionBy" parameterType="java.lang.Long" resultType="java.util.HashMap">
        SELECT patrol_id as id, patrol_name as label FROM t_patrol_position WHERE is_del = 0
        AND dept_id = #{deptId}
    </select>
</mapper>