<?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.app.mapper.AppSafetyHatMapper">
    <!--班组长分配安全帽设备-->
    <insert id="distributionHelmet">
        INSERT INTO tb_sh_use
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="proId != null">pro_id,</if>
            <if test="teamId">team_id,</if>
            <if test="bidId">bid_id,</if>
            bid_type,
            <if test="devId != null">dev_id,</if>
            <if test="bidTime != null">bid_time,</if>
            time_type,
            people_type,
            <if test="useId != null">use_id,</if>
            id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="proId != null">#{proId},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="bidId != null">#{bidId},</if>
            2,
            <if test="devId != null">#{devId},</if>
            <if test="bidTime != null">#{bidTime},</if>
            1,
            0,
            <if test="useId != null">#{useId},</if>
            null
        </trim>
    </insert>
    <!--更新设备表领用状态-->
    <update id="updateDevLyStatus">
        UPDATE tb_dev_ly SET ly_user = #{bidId},ly_status = 0,ly_time = #{bidTime},user_type = 0 WHERE dev_id = #{devId}
    </update>
    <!--更新人员领用的归还状态-->
    <update id="returnHelmet">
        UPDATE tb_sh_use SET unbid_time = #{backTime} ,time_type = 0 WHERE id = #{id}
    </update>
    <!--更新设备领用表 归还状态-->
    <update id="updateReturnDeviceData">
        UPDATE tb_dev_ly SET ly_user = null,ly_status = null,ly_time = null,user_type = null WHERE dev_id = #{devId}
    </update>

    <!--智能安全帽列表-->
    <select id="getSafetyHatLists" resultType="com.bonus.common.entity.app.vo.SafetyHatVo">
        SELECT tdub.dev_name AS devName,
               tdub.dev_code AS devCode,
               tdub.id AS id,
               tdub.dev_id AS devId,
               tdub.ly_time AS lyTime,
               tdub.gh_time AS ghTime,
               tpe.name AS name,
               tpe.phone,
               tdu.team_id AS teamId,
               twt.team_name AS teamName,
               IF(tsu.id IS NULL,0,1) AS useStatus,
               tdu.pro_id AS proId,
               tdu.gt_id AS gtId,
               tsu.id AS bidId
        FROM tb_dev_use tdu
        LEFT JOIN tb_dev_use_bid tdub ON tdu.id = tdub.id
        LEFT JOIN tb_sh_use tsu ON tdub.id = tsu.use_id AND tdub.dev_id = tsu.dev_id AND tsu.bid_type = 2 AND tsu.time_type = 1
        LEFT JOIN t_work_team twt ON tdu.team_id = twt.team_id AND twt.del_flag = 0
        LEFT JOIN tb_people tpe ON tsu.bid_id = tpe.id AND tpe.del_flag = 0
        LEFT JOIN tb_project tp ON tp.id = tdu.pro_id AND tp.del_flag = 0
        <where>
            <if test="teamName!=null and teamName!=''">
                INSTR(twt.team_name,#{teamName}) > 0
            </if>
            <if test="useName!=null and useName!=''">
                AND INSTR(tpe.name,#{useName}) > 0
            </if>
            <if test="roleCode!='team' and roleCode!='depart' and roleCode!='administrators'">
               AND tdu.team_id = -1
            </if>
            <if test="roleCode=='team'">
               AND tdu.team_id = #{teamId}
            </if>
            <if test="roleCode=='depart'">
                AND tp.depart_id = #{departId} AND tsu.id IS NOT NULL
            </if>
           AND tdub.dev_type = 'aqm' AND tdub.gh_time IS NULL AND tdu.del_flag = 0
        </where>
    </select>
</mapper>