<?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.securitycontrol.inter.mapper.BdMapper">
    <insert id="insertData">
        insert  into  tb_bd_device(device_id,devic_type,status,device_name,create_time,bd_id,remarks,del_flag)
                value (
                    #{deviceId},#{deviceModelId},0,#{deviceName},now(),#{gatewayId},#{description},0
                )
    </insert>
    <insert id="insertWarn">
        insert into  tb_warn (warn_id,warn_time,warn_content,warn_type,bd_device,db_id,bid_code,device_id,attribute_id,status)
            value (#{id},#{eventTime},#{eventDesc},#{eventCode},#{devCode},#{gatewayId},#{bidCode},#{deviceId},#{attributeId},'0')
    </insert>
    <insert id="insertValue">
       replace into tb_device_value (attribute_id,val,create_time,is_warn,unit,change_val)
         value(#{attributeId},#{value},#{dataTime},#{isWarn},#{unit},#{changeValue})
    </insert>
    <insert id="insertHistoryValue">
        insert into  tb_his_device_value (id, bid_code, attribute_id, val, create_time, is_warn, source_type,
                source_data, change_val, unit,gt_id)
        value( #{id},   #{bidCode},  #{attributeId},  #{value},  #{dataTime},  #{isWarn},#{sourceType},
             #{sourceData},#{changeValue},#{unit}, #{gtId}
        )
    </insert>
    <update id="bindBdPro">
        update tb_pro_bd set bid_code=#{bidNo},bind_time=now()  where bd_code=#{gatewayId};
    </update>
    <update id="deviceDel">
            update tb_bd_device set  del_flag=1 where bd_code=#{deviceId} and bd_id=#{bdId}
    </update>
    <update id="deviceStatus">
        update tb_bd_device set  status=#{state} where bd_code=#{deviceId} and bd_id=#{bdId}
    </update>
    <!--查询工程名称-->
    <select id="getProjectByName" resultType="com.securitycontrol.inter.vo.ProjectVo">
        select id,pro_name projectName,pro_no projectNo,tjp.org_id builderNo,
                sb.city_name builderName,vlotage volLevel,pro_type projectType
            from tb_jj_project tjp
            left join sys_build sb on sb.org_id=tjp.org_id
            <where>
                <if test="projectName!=null and projectName!=''">
                     AND pro_name like concat('%',#{projectName},'%')
                </if>
            </where>
    </select>
    <!--查询单项工程信息-->
    <select id="getPepsinInfoByPrcNo" resultType="com.securitycontrol.inter.vo.SingleProVo">
        select
            id,single_name singleName,signle_no singleNo,wbs_code wbsCode,
            project_no projectNo,single_desc singleDesc,vol_level volLevel,
            line_length lineLength,subs_cap subsCap,pro_type singleTypeCode,
            subs_type subsTypeCode,estimate_type estimateTypeCode,tjp.org_id builderNo,
            start_date startDate,end_date productDate,complete_date completeDate,sb.city_name builderName
            from tb_sign_project tjp
            LEFT JOIN sys_build sb on sb.org_id=tjp.org_id
            <where>
                <if test="projectNo!=null and projectNo!=''">
                    AND tjp.project_no =#{projectNo}
                </if>
            </where>
    </select>
    <select id="getSgGtEiderListBySinList" resultType="com.securitycontrol.inter.vo.BidProjectVo">
            select
			bid.pro_id id,bid.sign_code singleNo,bid.bid_code bidNo,
			bid.pro_code projectCode,bid.pro_name bidName,bid.pro_cost,
			bid.sg_unit,bid.jl_unit,bid.pro_type,
			bid.pro_scale,bid.manager,bid.status,
			bid.plan_start_time,bid.plan_end_time,
			bid.start_time startDate,bid.end_time completeDate,bid.tc_date productDate,
			bid.org,bid.bid_type bidType,
			bid.sg_manage buildManager,bid.jl_manage chiefSuperVice,bid.sub_type subsTypeCode
		from tb_project bid
		WHERE del_flag=0
            <if test="signList !=null and signList.size() >0 ">
                <foreach collection="signList" open=" and sign_code in(" close=")" item="item" separator=",">
                       #{item}
                </foreach>
            </if>
    </select>
    <!--查询人员数据u录入-->
    <select id="getPeopleInfo" resultType="com.securitycontrol.inter.vo.UserInfoVo">
            select twt.team_name workClassName,twt.team_leader,ttp.id_number idCard,
                ttp.user_name name,ttp.user_type jobType,ttp.phone mobile
			,jp.pro_name projectName,  twt.bid_code bidNo,ttp.user_id userId
			from tb_work_team twt
			left join  tb_project bid on twt.bid_code=bid.bid_code
			left join t_team_people ttp on twt.team_id=ttp.team_id
			left join tb_jj_project jp on jp.pro_no=bid.pro_code
		where ttp.id_number is not null
    </select>
    <select id="getDeviceNumById" resultType="java.lang.Integer">
        select count(1)
             from tb_bd_device
               where del_flag=0 and device_id=#{deviceId}
    </select>
    <select id="getBidCodeByBd" resultType="java.lang.String">
         select
             bid_code
                  from tb_pro_bd
                  where   bd_code=#{gatewayId}
    </select>
    <select id="getDevCodeById" resultType="java.lang.String">
        select dev_code
            from tb_db_dev_rel_mapping
            where device_id=#{deviceId}
    </select>
    <!--查询检测值信息-->
    <select id="getBdDeviceDetailByIdCode" resultType="com.securitycontrol.inter.vo.SelectDataVo">
        select tdd.id attributeId,tbd.device_id deviceId,tdv.unit,tdv.val  historyValue,tpb.bid_code bidCode
            ,tbd.bd_code deviceCode,tdd.device_code attributeCode,tdd.data_type dataType,tpb.gt_id gtId
        from tb_bd_device tbd
            left join tb_pro_bd tpb on tpb.id=tbd.bd_id
            left join tb_device_detail tdd on tdd.device_id=tbd.device_id
            left JOIN tb_device_value tdv on tdv.attribute_id=tdd.id
             where  tbd.bd_code=#{id}  and  tdd.device_code=#{code}
    </select>
    <select id="getBdIdByCode" resultType="java.lang.String">
        select id
             from tb_pro_bd where bd_code=#{gatewayId}
    </select>
    <select id="getDeviceInfo" resultType="com.securitycontrol.inter.vo.WarnInfoVo">
        SELECT tbd.device_id deviceId,tdd.id attributeId
            FROM  tb_bd_device  tbd
            LEFT JOIN tb_device_detail tdd  on  tbd.device_id=tbd.device_id
             WHERE  tbd.bd_code=#{deviceId}  and tdd.device_code=#{type}
    </select>
    <!--查询干他数据-->
    <select id="getGtList" resultType="com.securitycontrol.inter.vo.ProjectVo">
        select gt_name gtName,gt_id gtId
                from t_pro_gt
                where del_flag=0 AND  bid_code=#{bidCode}
    </select>


</mapper>