<?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.bracelet.mapper.EquipmentReqMapper">
    <!--添加设备领用数据-->
    <insert id="addDevUseData" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO tb_dev_use
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="teamId != null">team_id,</if>
            <if test="proId != null">pro_id,</if>
            <if test="powerId != null">gt_id,</if>
            <if test="remarks != null and remarks!=''">remarks,</if>
            create_time,
            update_time,
            create_user,
            update_user,
            del_flag,
            id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="teamId != null">#{teamId},</if>
            <if test="proId != null">#{proId},</if>
            <if test="powerId != null">#{powerId},</if>
            <if test="remarks != null and remarks!=''">#{remarks},</if>
            #{createTime},
            #{updateTime},
            #{createUser},
            #{updateUser},
            0,
            null
        </trim>
    </insert>
    <!--添加设备领用详情数据-->
    <insert id="addDevUseDetailData">
        INSERT INTO tb_dev_use_bid(id,dev_id,dev_name,dev_code,dev_type,ly_time) VALUES
        <foreach collection="list" separator="," item="item">
            (
            #{id},#{item.devId},#{item.devName},#{item.devCode},#{item.devType},#{lyTime}
            )
        </foreach>
    </insert>
    <!--更新设备状态-->
    <update id="updateDeviceData">
        <if test="type == 1">
            UPDATE tb_sh_box SET team_id = #{vo.teamId},pro_id = #{vo.proId},gt_id = #{vo.powerId} WHERE id = #{equipment.devId}
        </if>
        <if test="type == 2">
            UPDATE tb_dev_ly SET pro_id = #{vo.proId},team_id = #{vo.teamId},ly_user = #{vo.teamLeaderId},ly_status = 0,ly_time = #{vo.lyTime},user_type = #{vo.userType},gt_id = #{vo.powerId} WHERE dev_id = #{equipment.devId}
        </if>
        <if test="type == 3">
            UPDATE tb_dev_ly SET pro_id = #{vo.proId},team_id = #{vo.teamId},gt_id = #{vo.powerId} WHERE dev_id = #{equipment.devId}
        </if>
    </update>
    <!--更新1.手环箱/2.其他设备状态-->
    <update id="updateReturnDeviceData">
        <if test="type == 1">
            UPDATE tb_sh_box SET team_id = null,pro_id = null,gt_id = null WHERE id = #{params.devId}
        </if>
        <if test="type == 2">
            UPDATE tb_dev_ly SET pro_id = null,team_id = null,ly_user = null,ly_status = null,ly_time = null,user_type = null,gt_id = null WHERE dev_id = #{params.devId}
        </if>
    </update>
    <!--更新设备归还状态-->
    <update id="returnDevice">
        UPDATE tb_dev_use_bid SET gh_time = #{backTime} WHERE id = #{id} AND dev_id = #{devId} AND dev_type = #{devType} AND dev_code = #{devCode}
    </update>

    <!--设备领用列表-->
    <select id="getEquipmentReqLists" resultType="com.bonus.common.entity.bracelet.vo.EquipmentReqVo">
        SELECT * FROM (
            SELECT tdub.id,
            tdub.dev_id AS devId,
            tdub.dev_type AS devType,
            tdu.remarks,
            td.dev_code AS devCode,
            td.dev_name AS devName,
            tdub.ly_time AS lyTime,
            tdub.gh_time AS ghTime,
            sdd.dict_label AS deviceTypeName,
            twt.team_name AS teamName,
            twt.team_leader AS teamLeader,
            twt.leader_phone AS leaderPhone,
            tpp.power_name AS powerName,
            tp.pro_name AS proName,
            tpd.depart_name AS departName,
            tp.depart_id AS departId
            FROM tb_dev_use tdu
            LEFT JOIN t_work_team twt ON tdu.team_id = twt.team_id AND twt.del_flag = 0
            LEFT JOIN tb_project_power tpp ON tpp.id = tdu.gt_id AND tpp.del_flag = 0
            LEFT JOIN tb_project tp ON tp.id = tdu.pro_id AND tp.del_flag = 0
            LEFT JOIN tb_project_depart tpd ON tpd.id = tp.depart_id AND tpd.del_flag = 0
            LEFT JOIN tb_dev_use_bid tdub ON tdu.id = tdub.id
            LEFT JOIN tb_device td ON tdub.dev_id = td.id AND td.del_flag = 0
            LEFT JOIN sys_dict_data sdd ON td.dev_type = sdd.dict_value AND sdd.dict_type = 'sys_device_type'
            WHERE tdu.del_flag = 0 AND tdub.dev_type NOT IN ('shx')
            <if test="devType!=null and devType!=''">
                AND td.dev_type = #{devType}
            </if>
        UNION ALL
            SELECT tdub.id,
            tdub.dev_id AS devId,
            tdub.dev_type AS devType,
            tdu.remarks,
            tsb.box_code AS devCode,
            tsb.box_name AS devName,
            tdub.ly_time AS lyTime,
            tdub.gh_time AS ghTime,
            sdd.dict_label AS deviceTypeName,
            twt.team_name AS teamName,
            twt.team_leader AS teamLeader,
            twt.leader_phone AS leaderPhone,
            tpp.power_name AS powerName,
            tp.pro_name AS proName,
            tpd.depart_name AS departName,
            tp.depart_id AS departId
            FROM tb_dev_use tdu
            LEFT JOIN t_work_team twt ON tdu.team_id = twt.team_id AND twt.del_flag = 0
            LEFT JOIN tb_project_power tpp ON tpp.id = tdu.gt_id AND tpp.del_flag = 0
            LEFT JOIN tb_project tp ON tp.id = tdu.pro_id AND tp.del_flag = 0
            LEFT JOIN tb_project_depart tpd ON tpd.id = tp.depart_id AND tpd.del_flag = 0
            LEFT JOIN tb_dev_use_bid tdub ON tdu.id = tdub.id
            LEFT JOIN sys_dict_data sdd ON tdub.dev_type = sdd.dict_value AND sdd.dict_type = 'sys_device_type'
            LEFT JOIN tb_sh_box tsb ON tdub.dev_id = tsb.id AND tsb.del_flag = 0
            WHERE tdu.del_flag = 0 AND tdub.dev_type = 'shx'
            <if test="devType!=null and devType!='' and devType != 'shx'">
                AND tsb.id = -1
            </if>
        ) a
        <where>
            <if test="proName!=null and proName!=''">
                AND INSTR(a.proName,#{proName}) > 0
            </if>
            <if test="powerName!=null and powerName!=''">
                AND INSTR(a.powerName,#{powerName}) > 0
            </if>
            <if test="teamName!=null and teamName!=''">
                AND INSTR(a.teamName,#{teamName}) > 0
            </if>
            <if test="devName!=null and devName!=''">
                AND INSTR(a.devName,#{devName}) > 0
            </if>
            <if test="devCode!=null and devCode!=''">
                AND INSTR(a.devCode,#{devCode}) > 0
            </if>
            <if test="lyTime!=null and lyTime!=''">
                AND DATE_FORMAT(a.lyTime,'%y%m%d') BETWEEN #{lyTime} AND #{lyTime}
            </if>
            <if test="ghTime!=null and ghTime!=''">
                AND DATE_FORMAT(a.ghTime,'%y%m%d') BETWEEN #{ghTime} AND #{ghTime}
            </if>
            <if test="roleCode!='administrators' and roleCode!='depart'">
                AND a.departId = -1
            </if>
            <if test="roleCode=='depart'">
                AND a.departId = #{departId}
            </if>
        </where>
        ORDER BY IFNULL(a.ghTime,now()) DESC,a.lyTime DESC
    </select>
    <!--判断设备是否已经归还-->
    <select id="isHasUseDevice" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_dev_use_bid WHERE dev_id = #{devId} AND dev_type = #{devType} AND gh_time IS NULL
    </select>
    <!--获取未领用的设备-->
    <select id="getUseDevices" resultType="java.util.Map">
        <if test="devType!='' and devType!=null and devType!='shx'">
            SELECT td.id AS devId,td.dev_code AS devCode,td.dev_name AS devName,td.dev_type AS devType
            FROM tb_device td
            LEFT JOIN tb_dev_ly tdl ON td.id = tdl.dev_id
            WHERE tdl.team_id IS NULL AND tdl.ly_user IS NULL AND td.dev_type = #{devType} AND td.del_flag = 0
        </if>
        <if test="devType!='' and devType!=null and devType=='shx'">
            SELECT tsb.id AS devId,tsb.box_code AS devCode,tsb.box_name AS devName,'shx' AS devType
            FROM tb_sh_box tsb
            WHERE team_id IS NULL AND tsb.del_flag = 0
        </if>
    </select>
    <!--判断设备是否全部归还-->
    <select id="isAllDeviceReturn" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tb_dev_use_bid tdub
        LEFT JOIN tb_sh_use tsu ON tdub.id = tsu.use_id AND tsu.bid_type = #{type}
        WHERE tdub.id = #{params.id} AND tdub.dev_id = #{params.devId} AND tdub.dev_type = #{params.devType} AND tdub.dev_code = #{params.devCode} AND tsu.time_type = 1
    </select>
    <!--获取绑定班组的班组长-->
    <select id="getTeamLeader" resultType="java.lang.Long">
        SELECT twt.team_leader_id
        FROM t_work_team twt
        WHERE twt.team_id = #{teamId}
    </select>
</mapper>