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

    <resultMap type="TNfcSetting" id="TNfcSettingResult">
        <result property="nfcId"    column="nfc_id"    />
        <result property="patrolSort"    column="patrol_sort"    />
        <result property="patrolAddress"    column="patrol_address"    />
        <result property="patrolComent"    column="patrol_coment"    />
        <result property="patrolFrequency"    column="patrol_frequency"    />
        <result property="nfcNum"    column="nfc_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="region"    column="region"    />
    </resultMap>

    <sql id="selectTNfcSettingVo">
        select nfc_id, patrol_sort, patrol_address, patrol_coment, patrol_frequency, nfc_num, create_time,region, is_del from t_nfc_setting
    </sql>

    <select id="selectTNfcSettingList" parameterType="TNfcSetting" resultMap="TNfcSettingResult">
        select a.*,b.dict_label as nickName from t_nfc_setting a
        left join  sys_dict_data b on a.region=b.dict_value
        <where>
            a.is_del='0' and b.dict_type='t_nfc_region'
            <if test="region != null  and region != ''"> and  region = #{region}</if>
            <if test="patrolAddress != null  and patrolAddress != ''"> and patrol_address = #{patrolAddress}</if>
            <if test="nfcNum != null  and nfcNum != ''"> and nfc_num = #{nfcNum}</if>
            <if test="applyWorkStartTime!=null and applyWorkStartTime!=''">
                and a.create_time &gt;= #{applyWorkStartTime}
            </if>
            <if test="applyWorkEndTime!=null and applyWorkEndTime!=''">
               and a.create_time &lt;= #{applyWorkEndTime}
            </if>
        </where>
        order  by patrol_sort
    </select>

    <select id="selectTNfcSettingById" parameterType="Long" resultMap="TNfcSettingResult">
        <include refid="selectTNfcSettingVo"/>
        where nfc_id = #{nfcId}
    </select>
    <select id="getInfoByNfcNum" parameterType="java.lang.String" resultMap="TNfcSettingResult">
        <include refid="selectTNfcSettingVo"/>
        where nfc_num = #{nfcNum}
    </select>

    <insert id="insertTNfcSetting" parameterType="TNfcSetting" useGeneratedKeys="true" keyProperty="nfcId">
        insert into t_nfc_setting
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patrolSort != null">patrol_sort,</if>
            <if test="patrolAddress != null">patrol_address,</if>
            <if test="patrolComent != null">patrol_coment,</if>
            <if test="patrolFrequency != null">patrol_frequency,</if>
            <if test="nfcNum != null">nfc_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="region != null">region,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patrolSort != null">#{patrolSort},</if>
            <if test="patrolAddress != null">#{patrolAddress},</if>
            <if test="patrolComent != null">#{patrolComent},</if>
            <if test="patrolFrequency != null">#{patrolFrequency},</if>
            <if test="nfcNum != null">#{nfcNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="region != null">#{region},</if>
         </trim>
    </insert>

    <update id="updateTNfcSetting" parameterType="TNfcSetting">
        update t_nfc_setting
        <trim prefix="SET" suffixOverrides=",">
            <if test="patrolSort != null">patrol_sort = #{patrolSort},</if>
            <if test="patrolAddress != null">patrol_address = #{patrolAddress},</if>
            <if test="patrolComent != null">patrol_coment = #{patrolComent},</if>
            <if test="patrolFrequency != null">patrol_frequency = #{patrolFrequency},</if>
            <if test="nfcNum != null">nfc_num = #{nfcNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where nfc_id = #{nfcId}
    </update>

    <delete id="deleteTNfcSettingById" parameterType="Long">
        delete from t_nfc_setting where nfc_id = #{nfcId}
    </delete>

    <update id="deleteTNfcSettingByIds" parameterType="String">
        update  t_nfc_setting set is_del='1' where nfc_id in
        <foreach item="nfcId" collection="array" open="(" separator="," close=")">
            #{nfcId}
        </foreach>
    </update>

    <!--巡检打卡查询列表-->
    <select id="punchClockList" resultType="com.zehong.system.domain.TNfcRecord">
        select a.is_normal as isNormal,a.remarks as remarks ,a.create_time as createTime,
               b.patrol_address as patrolAddress,b.patrol_coment as patrolComent,b.patrol_frequency as patrolFrequency,
			   c.nick_name as nickName,
               d.dict_label as dictLabel
           from t_nfc_record a left join t_nfc_setting b on a.nfc_id=b.nfc_id
		        left join  sys_user  c  on a.create_user=c.user_id
                left join sys_dict_data d on b.region=d.dict_value
        <where>
            b.is_del='0' and d.dict_type='t_nfc_region'
            <if test="patrolAddress != null and patrolName != ''">
                and b.patrol_address like concat('%', #{patrolAddress}, '%')
            </if>
            <if test="region != null and region != ''">
                and b.region=#{region}
            </if>
            <if test="applyWorkStartTime!=null and applyWorkStartTime!=''">
                and a.create_time &gt;= #{applyWorkStartTime}
            </if>
            <if test="applyWorkEndTime!=null and applyWorkEndTime!=''">
                and a.create_time &lt;= #{applyWorkEndTime}
            </if>
        </where>
			group by a.create_time desc
    </select>

    <!--查询nfc编号是否已经注册-->
    <select id="getNfcNum" resultType="java.lang.Integer">
        select count(nfc_id) from t_nfc_setting  where nfc_num=#{nfcNum}
    </select>
</mapper>