<?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.ssm.manage.mapper.BlackListMapper">

    <resultMap id="BaseResultMap" type="com.ssm.manage.entity.BlackList">
        <result property="id" column="id"/>
        <result property="staffName" column="staff_name"/>
        <result property="mobilePhone" column="mobile_phone"/>
        <result property="identityCard" column="identity_card"/>
        <result property="storeId" column="store_id"/>
        <result property="inputId" column="input_id"/>
        <result property="inputName" column="input_name"/>
        <result property="inputDate" column="input_date"/>
        <result property="type" column="type"/>
        <result property="sex" column="sex"/>
        <result property="facesInformation" column="faces_information"/>
        <result property="job" column="job"/>
        <result property="regionId" column="region_id"/>
        <result property="staffNum" column="staff_num"/>
        <result property="endType" column="end_type"/>
        <result property="endId" column="end_id"/>
        <result property="endName" column="end_name"/>
        <result property="endTime" column="end_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="email" column="email"/>
        <result property="beisenUserId" column="beisen_user_id"/>
        <result property="isBeisen" column="is_beisen"/>
        <result property="isZhongkong" column="is_zhongkong"/>
        <result property="urgentName" column="urgent_name"/>
        <result property="urgentPhone" column="urgent_phone"/>
        <result property="urgentRelationship" column="urgent_relationship"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        , staff_name, mobile_phone, identity_card, store_id, input_id, input_name, input_date, type ,sex ,faces_information,job ,region_id , staff_num
            , end_type, end_id, end_name, end_time,update_time,email,beisen_user_id,is_beisen,is_zhongkong,urgent_name,urgent_phone,urgent_relationship
    </sql>

    <select id="queryAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from black_list
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="staffName != null and staffName != ''">
                and staff_name = #{staffName}
            </if>
            <if test="mobilePhone != null and mobilePhone != ''">
                and mobile_phone = #{mobilePhone}
            </if>
            <if test="identityCard != null and identityCard != ''">
                and identity_card = #{identityCard}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            <if test="inputId != null">
                and input_id = #{inputId}
            </if>
            <if test="inputName != null and inputName != ''">
                and input_name = #{inputName}
            </if>
            <if test="inputDate != null">
                and input_date = #{inputDate}
            </if>
            <if test="type != null">
                and type = #{type}
            </if>
        </where>
    </select>

    <select id="selectByIds" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM `black_list`
        <where>
            <if test="ids != null and ids.size() > 0">
                `id` IN
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectByStoreId" resultType="com.ssm.manage.entity.BlackList">
        select *
         from black_number
         where store_id=#{storeId} and identity_card=#{identityCard}
         order by staff_num desc
            LIMIT 1
    </select>
    <select id="selectTypeId" resultType="java.lang.Long">
        SELECT
        job_content_list.id AS id
        FROM
            job_content_list
                left join
            (
        SELECT
        job_content_list.id AS id,
        MAX( staff_temporary.scan_time ) AS max_scan_time,
        staff_temporary.staff_num AS staff_num
        FROM
        job_content_list
        LEFT JOIN staff_temporary ON staff_temporary.staff_num = job_content_list.staff_num
        AND staff_temporary.scan_time &lt;= job_content_list.check_time
        GROUP BY
        job_content_list.id
        ) a  ON a.id = job_content_list.id
        left join staff_temporary on staff_temporary.scan_time=a.max_scan_time and staff_temporary.staff_num=a.staff_num
        WHERE IFNULL(staff_temporary.type, job_content_list.type)=#{type}
    </select>

    <!--根据storeId和多个identityCard查找黑名单-->
    <select id="selectByStoreIdAndIndentityCard" resultType="com.ssm.manage.entity.BlackList">
        SELECT urgent_name,urgent_phone,urgent_relationship,id,beisen_user_id,
        email,sex,staff_num,staff_name,mobile_phone,identity_card,store_id,
        input_id,input_name,input_date,type,job,region_id,end_type,end_id,
        end_name,end_time,update_time,employee_information_id,employment_record_id,
        is_beisen,is_zhongkong FROM black_list
        <where>
            <if test="storeId != null">store_id = #{storeId} and</if>
            <if test="identityCard != null">identity_card in
                <foreach collection="identityCard" item="ic" separator="," open="(" close=")">
                    #{ic}
                </foreach>

            </if>
        </where>
    </select>

    <update id="updateByIdentityCard">
        update black_list set type = #{type} , blacklist_reasons = #{blacklistReasons} where identity_card = #{identityCard}
    </update>

    <delete id="delectByIdss">
        delete from black_list where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>

    </delete>
</mapper>
