<?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.KitchenStaffMorningCheckMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenStaffMorningCheck" id="KitchenStaffMorningCheckResult">
        <result property="checkId"    column="check_Id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="temperatureValue"    column="temperature_value"    />
        <result property="temperatureStatus"    column="temperature_status"    />
        <result property="checkStatus"    column="check_status"    />
        <result property="recordTime"    column="record_time"    />
        <result property="checkVideoUrl"    column="check_video_url"    />
        <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="nickName"    column="nick_name"    />
        <result property="postName"    column="post_name"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="staffNo"    column="staffNo"    />
    </resultMap>

    <sql id="selectKitchenStaffMorningCheckVo">
        select check_Id, ksmc.staff_id, device_id, temperature_value, temperature_status,
               check_status, record_time, check_video_url, ksmc.create_by, ksmc.create_time,
               ksmc.update_by, ksmc.update_time, su.nick_name, sp.post_name, bc.canteen_name,
               su.phonenumber, su.user_id as staffNo
        from kitchen_staff_morning_check ksmc
            left join kitchen_staff_info ksi on ksmc.staff_id = ksi.staff_id
            left join sys_user su on ksi.user_id = su.user_id
            left join sys_user_post sup on ksi.user_id = sup.user_id
            left join sys_post sp on sup.post_id = sp.post_id
            left join basic_canteen bc on ksi.canteen_id = bc.canteen_id
    </sql>

    <select id="selectKitchenStaffMorningCheckList" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffMorningCheck" resultMap="KitchenStaffMorningCheckResult">
        <include refid="selectKitchenStaffMorningCheckVo"/>
        <where>  
            <if test="staffId != null "> and ksmc.staff_id = #{staffId}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="temperatureValue != null "> and temperature_value = #{temperatureValue}</if>
            <if test="temperatureStatus != null  and temperatureStatus != ''"> and temperature_status = #{temperatureStatus}</if>
            <if test="checkStatus != null "> and check_status = #{checkStatus}</if>
            <if test="recordTime != null "> and record_time = #{recordTime}</if>
            <if test="checkVideoUrl != null  and checkVideoUrl != ''"> and check_video_url = #{checkVideoUrl}</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>
            <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="startDateTime != null">
                and ksmc.record_time  <![CDATA[ >= ]]> #{startDateTime}
            </if>
            <if test="endDateTime != null">
                and ksmc.record_time  <![CDATA[ <= ]]> #{endDateTime}
            </if>
        </where>
    </select>
    
    <select id="selectKitchenStaffMorningCheckByCheckId" parameterType="Long" resultMap="KitchenStaffMorningCheckResult">
        <include refid="selectKitchenStaffMorningCheckVo"/>
        where check_Id = #{checkId}
    </select>
        
    <insert id="insertKitchenStaffMorningCheck" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffMorningCheck" useGeneratedKeys="true" keyProperty="checkId">
        insert into kitchen_staff_morning_check
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="staffId != null">staff_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="temperatureValue != null">temperature_value,</if>
            <if test="temperatureStatus != null">temperature_status,</if>
            <if test="checkStatus != null">check_status,</if>
            <if test="recordTime != null">record_time,</if>
            <if test="checkVideoUrl != null">check_video_url,</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="staffId != null">#{staffId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="temperatureValue != null">#{temperatureValue},</if>
            <if test="temperatureStatus != null">#{temperatureStatus},</if>
            <if test="checkStatus != null">#{checkStatus},</if>
            <if test="recordTime != null">#{recordTime},</if>
            <if test="checkVideoUrl != null">#{checkVideoUrl},</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="updateKitchenStaffMorningCheck" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenStaffMorningCheck">
        update kitchen_staff_morning_check
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="temperatureValue != null">temperature_value = #{temperatureValue},</if>
            <if test="temperatureStatus != null">temperature_status = #{temperatureStatus},</if>
            <if test="checkStatus != null">check_status = #{checkStatus},</if>
            <if test="recordTime != null">record_time = #{recordTime},</if>
            <if test="checkVideoUrl != null">check_video_url = #{checkVideoUrl},</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 check_Id = #{checkId}
    </update>

    <delete id="deleteKitchenStaffMorningCheckByCheckId" parameterType="Long">
        delete from kitchen_staff_morning_check where check_Id = #{checkId}
    </delete>

    <delete id="deleteKitchenStaffMorningCheckByCheckIds" parameterType="String">
        delete from kitchen_staff_morning_check where check_Id in 
        <foreach item="checkId" collection="array" open="(" separator="," close=")">
            #{checkId}
        </foreach>
    </delete>
</mapper>