<?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.bonus.canteen.core.kitchen.mapper.KitchenStaffInfoMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenStaffInfo" id="KitchenStaffInfoResult">
        <result property="staffId"    column="staff_id"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="staffNo"    column="staff_no"    />
        <result property="userId"    column="user_id"    />
        <result property="healthCertExpire"    column="health_cert_expire"    />
        <result property="healthCertFrontImg"    column="health_cert_front_img"    />
        <result property="healthCertBackImg"    column="health_cert_back_img"    />
        <result property="nutrityCertExpire"    column="nutrity_cert_expire"    />
        <result property="safetyCertExpire"    column="safety_cert_expire"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectKitchenStaffInfoVo">
        select staff_id, canteen_id, staff_no, kst.user_id, health_cert_expire,
               health_cert_front_img, health_cert_back_img, nutrity_cert_expire,
               safety_cert_expire, kst.create_by, kst.create_time, kst.update_by, kst.update_time,
               su.nick_name, su.phonenumber, su.sex, uf.photo_url, uf.face_state as faceStatus, sp.post_name
        from kitchen_staff_info kst
        left join sys_user su on kst.user_id = su.user_id
        left join sys_user_post sup on kst.user_id = sup.user_id
        left join sys_post sp on sup.post_id = sp.post_id
        left join user_face uf on kst.user_id = uf.user_id
    </sql>

    <select id="selectKitchenStaffInfoList" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffInfo" resultMap="KitchenStaffInfoResult">
        <include refid="selectKitchenStaffInfoVo"/>
        <where>  
            <if test="staffNo != null  and staffNo != ''"> and staff_no = #{staffNo}</if>
            <if test="userId != null "> and kst.user_id = #{userId}</if>
            <if test="healthCertExpire != null "> and health_cert_expire = #{healthCertExpire}</if>
            <if test="healthCertFrontImg != null "> and health_cert_front_img = #{healthCertFrontImg}</if>
            <if test="healthCertBackImg != null "> and health_cert_back_img = #{healthCertBackImg}</if>
            <if test="nutrityCertExpire != null "> and nutrity_cert_expire = #{nutrityCertExpire}</if>
            <if test="safetyCertExpire != null "> and safety_cert_expire = #{safetyCertExpire}</if>
            <if test="sex != null "> and su.sex = #{sex}</if>
            <if test="postIds != null and postIds.length > 0">
                and sup.post_id in
                <foreach item="postId" collection="postIds" open="(" separator="," close=")">
                    #{postId}
                </foreach>
            </if>
            <if test="faceStatus != null "> and uf.face_state = #{faceStatus}</if>
            <if test="healthCertStatus != null ">
                and case
                    when #{healthCertStatus} = 1 then
                        (
                        DATE_SUB(kst.health_cert_expire,
                        INTERVAL (select bs.item_value from basic_setting bs where bs.item_name = 'staff_health_cert_left_day_reminder') DAY)
                        <![CDATA[ >= ]]> now()
                        )
                    when #{healthCertStatus} = 2 then
                        (
                            kst.health_cert_expire <![CDATA[ < ]]> now()
                         )
                    when #{healthCertStatus} = 3 then
                        (
                            health_cert_front_img is null
                        )
                    when #{healthCertStatus} = 4 then
                        (
                        DATE_SUB(kst.health_cert_expire,
                        INTERVAL (select bs.item_value from basic_setting bs where bs.item_name = 'staff_health_cert_left_day_reminder') DAY)
                        <![CDATA[ < ]]> now()
                        and kst.health_cert_expire > now()
                        )
                end
            </if>
            <if test="searchValue != null and searchValue != ''">
                and (su.nick_name like CONCAT('%',#{searchValue},'%')
                or su.phonenumber = #{encryptedSearchValue}
                or su.user_id like CONCAT('%',#{searchValue},'%')
                )
            </if>
        </where>
    </select>
    
    <select id="selectKitchenStaffInfoByStaffId" parameterType="Long" resultMap="KitchenStaffInfoResult">
        <include refid="selectKitchenStaffInfoVo"/>
        where staff_id = #{staffId}
    </select>
        
    <insert id="insertKitchenStaffInfo" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffInfo" useGeneratedKeys="true" keyProperty="staffId">
        insert into kitchen_staff_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="staffNo != null and staffNo != ''">staff_no,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="healthCertExpire != null">health_cert_expire,</if>
            <if test="healthCertFrontImg != null">health_cert_front_img,</if>
            <if test="healthCertBackImg != null">health_cert_back_img,</if>
            <if test="nutrityCertExpire != null">nutrity_cert_expire,</if>
            <if test="safetyCertExpire != null">safety_cert_expire,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="staffNo != null and staffNo != ''">#{staffNo},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="healthCertExpire != null">#{healthCertExpire},</if>
            <if test="healthCertFrontImg != null">#{healthCertFrontImg},</if>
            <if test="healthCertBackImg != null">#{healthCertBackImg},</if>
            <if test="nutrityCertExpire != null">#{nutrityCertExpire},</if>
            <if test="safetyCertExpire != null">#{safetyCertExpire},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateKitchenStaffInfo" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffInfo">
        update kitchen_staff_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffNo != null and staffNo != ''">staff_no = #{staffNo},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="healthCertExpire != null">health_cert_expire = #{healthCertExpire},</if>
            <if test="healthCertFrontImg != null">health_cert_front_img = #{healthCertFrontImg},</if>
            <if test="healthCertBackImg != null">health_cert_back_img = #{healthCertBackImg},</if>
            <if test="nutrityCertExpire != null">nutrity_cert_expire = #{nutrityCertExpire},</if>
            <if test="safetyCertExpire != null">safety_cert_expire = #{safetyCertExpire},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where staff_id = #{staffId}
    </update>

    <delete id="deleteKitchenStaffInfoByStaffId" parameterType="Long">
        delete from kitchen_staff_info where staff_id = #{staffId}
    </delete>

    <delete id="deleteKitchenStaffInfoByStaffIds" parameterType="String">
        delete from kitchen_staff_info where staff_id in 
        <foreach item="staffId" collection="array" open="(" separator="," close=")">
            #{staffId}
        </foreach>
    </delete>
</mapper>