<?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.tcp.smartIdentify.mapper.BraceletImageMapper">
    <insert id="insertShUseData">
        insert into tb_sh_use(
            pro_id,team_id,bid_id,bid_type,dev_id,
                 bid_time,time_type,people_type,gt_id, use_id)
        VALUES (
           #{proId},#{teamId},#{bidId},#{devType},#{bindDevId},
        #{bidTime},1,#{peopleType},#{gtId},#{id})
    </insert>
    <insert id="insetDetailData" useGeneratedKeys="true" keyProperty="id">
            insert into tb_dev_details (
                dev_id,  data_name,  data_val,data_unit,
                     data_code, data_type,  data_time,
                 dev_type, del_flag
                ) values (
                 #{devId},#{dataName},#{dataVal},#{dataUnit},
                 #{dataCode},#{dataType},now(),0,0 )

    </insert>
    <insert id="insertHistoryData">
        insert into tb_dev_his(
                data_data, data_resource, data_name,
    data_value,data_type, data_sourcce,
    dev_id,dev_type,data_time,create_time )values (
               #{dataData},#{dataResource},#{dataName},
        #{dataVal},#{dataType},#{dataSource},
    #{devId},#{devType},#{dataTime},now() )

    </insert>
    <update id="updateBraceletData">
        update   tb_bracelet
        set sh_status=#{shStatus},bid_time=#{bidTime},bid_id=#{bidId},
            peopel_type=#{shPersonType},face_status=NULL
         where id=#{shId}

    </update>


    <select id="getBraceletVo" resultType="com.bonus.common.entity.bracelet.vo.BraceletVo">
            select id shId,sh_code shCode,box_id shboxId,bid_id bidId
                from tb_bracelet
                where sh_code=#{devCode} AND del_flag=0
    </select>
    <!--查询手环箱 领用记录-->
    <select id="getDevLyInfo" resultType="com.bonus.common.entity.tcp.DeviceUseVo">
        select tdub.dev_code devCode,tdub.dev_name  devName,tdu.pro_id proId,
               tdu.team_id  teamId,tdub.id,tdu.gt_id gtId
        from  tb_dev_use_bid   tdub
        left join  tb_dev_use tdu on tdu.id=tdub.id
        where tdu.del_flag=0  AND tdub.gh_time is null
          and tdub.dev_id=#{shboxId}
    </select>
    <select id="getDevDetails" resultType="com.bonus.app.entity.DevDetailEntity">
        select tbt.dev_code,tdd.data_name dataName,tdd.data_code datCode,tdd.id
        from tb_bracelet tbt
         left join tb_dev_details tdd on tbt.id=tdd.dev_id and tdd.dev_type=0
        where  tbt.sh_code=#{devCode} and  tdd.data_code=#{dataCode} and tdd.del_flag=0  and  tbt.del_flag=0
    </select>
    <select id="getDevId" resultType="java.lang.String">
        select tbt.id
            from tb_bracelet tbt
                 where  tbt.sh_code=#{devCode}   and  tbt.del_flag=0
    </select>
    <select id="getDevBindUser" resultType="java.lang.String">
        select  bid_id
            from tb_bracelet
              where sh_code=#{devCode}  and   del_flag=0
    </select>
    <!--班组人员查询-->
    <select id="getTeamUser" resultType="java.lang.String">
        select tp.id
        from tb_people tp
                 left join t_work_team  twt on tp.team_id=twt.team_id
                 LEFT JOIN tb_sh_box box on twt.team_id=box.team_id
                 left join tb_bracelet tb on tb.box_id=box.id
        where tb.sh_code=#{devCode}  and tp.id=#{userId}
        union ALL
        select tp.id
        from tb_ls_user tp
         left join t_work_team  twt on tp.team_id=twt.team_id
         LEFT JOIN tb_sh_box box on twt.team_id=box.team_id
         left join tb_bracelet tb on tb.box_id=box.id
        where tb.sh_code=#{devCode}  and tp.id=#{userId}
    </select>
    <!--查询手环箱杆塔-->
    <select id="getShCodeByGt" resultType="java.lang.String">
        SELECT bt.sh_code
        FROM tb_sh_box box
         LEFT JOIN tb_bracelet bt on box.id=bt.box_id AND bt.del_flag=0
        where box.del_flag=0  and  box.gt_id=#{gtId}   and  bid_id is not null
    </select>
    <select id="getBdShCode" resultType="java.lang.String">
        select sh_code
            from  tb_bracelet
        where   sh_code in(
                    <foreach collection="list" item="shCode" separator=",">
                        #{shCode}
                    </foreach>
            )
        and  bid_id is not null  and del_flag=0
    </select>
    <update id="updateDetails">
        update  tb_dev_details SET data_val=#{dataVal} ,data_time=now()
                where id=#{id}
    </update>
    <update id="updateVerdateStatus">
        update  tb_bracelet set
         face_status=#{status} where sh_code=#{devCode}
    </update>

    <update id="updateVerdateStatusAll">
        update  tb_bracelet set
            face_status=0 where bid_id is not null and bid_time is not null
    </update>
</mapper>