<?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.EngineeringMapper">
    <!--工程列表-->
    <select id="getEngineeringLists" resultType="com.bonus.common.entity.bracelet.vo.EngineeringVo">
        select tb.id as proId,tpd.depart_name as departName,tb.pro_name as proName,count(tpp.id) as gtNum,sddlevel.dict_label as vlevel,sddorg.dict_label as orgName,
        sddpro.dict_label as proType,sddstatus.dict_label as proStatus,sddyz.dict_label as yzManage,sddsg.dict_label as sgManage,
        sddjl.dict_label as jlManage
        from tb_project tb
        left join tb_project_depart tpd on tb.depart_id = tpd.id and tpd.del_flag = 0
        left join tb_project_power tpp on tb.id = tpp.pro_id and tpp.del_flag = 0
        left join sys_dict_data sddlevel on sddlevel.dict_value=tb.voltage_level  and sddlevel.dict_type = 'sys_voltage_level'
        left join sys_dict_data sddorg on sddorg.dict_value=tb.org_id  and sddorg.dict_type = 'sys_org_name'
        left join sys_dict_data sddpro on sddpro.dict_value=tb.pro_type  and sddpro.dict_type = 'sys_pro_type'
        left join sys_dict_data sddstatus on sddstatus.dict_value=tb.pro_status  and sddstatus.dict_type = 'sys_pro_status'
        left join sys_dict_data sddyz on sddyz.dict_value=tb.yz_manage  and sddyz.dict_type = 'sys_yz_manage'
        left join sys_dict_data sddsg on sddsg.dict_value=tb.sg_manage  and sddsg.dict_type = 'sys_sg_manage'
        left join sys_dict_data sddjl on sddjl.dict_value=tb.jl_manage  and sddjl.dict_type = 'sys_jl_manage'
        <where>
            tb.del_flag = 0
            <if test="proName != null and proName!=''">
                AND INSTR(tb.pro_name,#{proName}) > 0
            </if>
            <if test="vlevel != null and vlevel!=''">
                AND INSTR(tb.voltage_level,#{vlevel}) > 0
            </if>
            <if test="orgName != null and orgName!=''">
                AND INSTR(tb.org_id,#{orgName}) > 0
            </if>
            <if test="proType != null and proType!=''">
                AND INSTR(tb.pro_type,#{proType}) > 0
            </if>
            <if test="departName != null and departName!=''">
                AND INSTR(tpd.depart_name,#{departName}) > 0
            </if>
        </where>
        group by tb.id,tpd.depart_name,tb.pro_name,sddlevel.dict_label,sddorg.dict_label,sddpro.dict_label,sddstatus.dict_label,
        sddyz.dict_label,sddsg.dict_label,sddjl.dict_label
        order by tb.id ASC
    </select>

    <!--根据工程id获得杆塔详细信息-->
    <select id="selectGtById" resultType="com.bonus.common.entity.bracelet.vo.GtVo">
        select tpp.pro_id as proId,tpp.id as gtId,tpp.power_name as gtCode,tpp.lon as lon,tpp.lat as lat,count(tsb.id) as teamNum
        from  tb_project_power tpp
        left join tb_sh_box tsb on tpp.id = tsb.gt_id and tsb.team_id is not null and tsb.del_flag = 0
        <where>
            tpp.del_flag = 0
            <if test="proId != null and proId!=''">
                AND tpp.pro_id = #{proId}
            </if>
            <if test="gtCode != null and gtCode!=''">
                AND INSTR(tpp.power_name,#{gtCode}) > 0
            </if>
        </where>
        group by tpp.pro_id,tpp.id
        order by tpp.id ASC
    </select>

        <!--判断新增时杆塔编码是否存在-->
        <select id="isCodeExist" resultType="com.bonus.common.entity.bracelet.vo.GtVo">
            select tpp.id as gtId,tpp.power_name as gtCode,tpp.lon as lon,tpp.lat as lat
            from tb_project_power tpp
            where tpp.power_name = #{gtCode} and tpp.pro_id = #{proId}  and del_flag = 0
        </select>

        <!--新增杆塔信息-->
        <insert id="addGt" parameterType="com.bonus.common.entity.bracelet.vo.GtVo" useGeneratedKeys="true" keyProperty="gtId">
            insert into tb_project_power(
            <if test="gtCode != null and gtCode != ''">power_name,</if>
            <if test="proId != null and proId != ''">pro_id,</if>
            <if test="lon != null and lon != ''">lon,</if>
            <if test="lat != null and lat != ''">lat,</if>
            create_time,
            create_user,
            update_time,
            update_user,
            del_flag
            )values(
            <if test="gtCode != null and gtCode != ''">#{gtCode},</if>
            <if test="proId != null and proId != ''">#{proId},</if>
            <if test="lon != null and lon != ''">#{lon},</if>
            <if test="lat != null and lat != ''">#{lat},</if>
            #{createTime},
            #{createUser},
            #{updateTime},
            #{updateUser},
            0
            )
        </insert>

    <!--根据杆塔id获得详细信息-->
        <select id="selectGtId" parameterType="int" resultType="com.bonus.common.entity.bracelet.vo.GtVo">
            select tpp.id as gtId,tpp.power_name as gtCode,tpp.lon as lon,tpp.lat as lat
            from tb_project_power tpp
            where tpp.id = #{gtId}  and tpp.del_flag = 0
        </select>

    <!--判断修改时杆塔编码是否存在-->
    <select id="isExist" resultType="com.bonus.common.entity.bracelet.vo.GtVo">
        select tpp.id as gtId,tpp.power_name as gtCode,tpp.lon as lon,tpp.lat as lat
        from tb_project_power tpp
        where tpp.power_name = #{gtCode}  and tpp.id != #{gtId} and tpp.pro_id = #{proId} and del_flag = 0
    </select>

    <!--修改杆塔信息-->
    <update id="updateGt">
        UPDATE tb_project_power tpp
        SET power_name = #{gtCode},lon = #{lon},lat = #{lat},update_time = #{updateTime},update_user = #{updateUser}
        where id = #{gtId}
    </update>

    <!--删除杆塔信息-->
    <update id="deleteGt">
        UPDATE tb_project_power
        SET del_flag = 1
        where id = #{gtId}
    </update>

        <select id="getEngineeringInfo" parameterType="long" resultType="com.bonus.common.entity.bracelet.vo.EngineeringVo">
        select tb.id as proId,tb.depart_id as departName,tb.pro_name as proName,tb.voltage_level as vlevel,tb.org_id as orgName,
               tb.pro_type as proType,tb.pro_status as proStatus,tb.yz_manage as yzManage,tb.sg_manage as sgManage,
               tb.jl_manage as jlManage
        from tb_project tb
        where tb.id = #{proId}  and tb.del_flag = 0
        </select>

    <select id="isNameExist"  resultType="com.bonus.common.entity.bracelet.vo.EngineeringVo">
        select tb.id as proId,tb.depart_id as departName,tb.pro_name as proName,tb.voltage_level as vlevel,tb.org_id as orgName,
               tb.pro_type as proType,tb.pro_status as proStatus,tb.yz_manage as yzManage,tb.sg_manage as sgManage,
               tb.jl_manage as jlManage
        from tb_project tb
        where  tb.pro_name = #{proName} and tb.id != #{proId}  and tb.del_flag = 0
    </select>

        <update id="updatePro">
            UPDATE tb_project tb
            <set>
                <if test="proName != null and proName != ''">pro_name = #{proName},</if>
                <if test="departName != null and departName != ''">depart_id = #{departName},</if>
                <if test="vlevel != null and vlevel != ''">voltage_level = #{vlevel},</if>
                <if test="orgName != null and orgName != ''">org_id = #{orgName},</if>
                <if test="proType != null and proType != ''">pro_type = #{proType},</if>
                <if test="proStatus != null and proStatus != ''">pro_status = #{proStatus},</if>
                yz_manage = #{yzManage},
                sg_manage = #{sgManage},
                jl_manage = #{jlManage},
                update_time = #{updateTime},update_user = #{updateUser}
            </set>
            where id = #{proId}
        </update>

    <!--新增时工程名称是否重复-->
    <select id="isAddNameExist"  resultType="com.bonus.common.entity.bracelet.vo.EngineeringVo">
        select tb.id as proId,tb.depart_id as departName,tb.pro_name as proName,tb.voltage_level as vlevel,tb.org_id as orgName,
               tb.pro_type as proType,tb.pro_status as proStatus,tb.yz_manage as yzManage,tb.sg_manage as sgManage,
               tb.jl_manage as jlManage
        from tb_project tb
        where  tb.pro_name = #{proName}  and tb.del_flag = 0
    </select>

        <!--新增工程信息-->
        <insert id="addPro" parameterType="com.bonus.common.entity.bracelet.vo.EngineeringVo" useGeneratedKeys="true" keyProperty="proId">
            insert into tb_project(
            <if test="proName != null and proName != ''">pro_name,</if>
            <if test="departName != null and departName != ''">depart_id,</if>
            <if test="vlevel != null and vlevel != ''">voltage_level,</if>
            <if test="orgName != null and orgName != ''">org_id,</if>
            <if test="proType != null and proType != ''">pro_type,</if>
            <if test="proStatus != null and proStatus != ''">pro_status,</if>
            <if test="yzManage != null and yzManage != ''">yz_manage,</if>
            <if test="sgManage != null and sgManage != ''">sg_manage,</if>
            <if test="jlManage != null and jlManage != ''">jl_manage,</if>
            create_time,
            create_user,
            update_time,
            update_user,
            del_flag
            )values(
            <if test="proName != null and proName != ''"> #{proName},</if>
            <if test="departName != null and departName != ''"> #{departName},</if>
            <if test="vlevel != null and vlevel != ''"> #{vlevel},</if>
            <if test="orgName != null and orgName != ''"> #{orgName},</if>
            <if test="proType != null and proType != ''"> #{proType},</if>
            <if test="proStatus != null and proStatus != ''"> #{proStatus},</if>
            <if test="yzManage != null and yzManage != ''"> #{yzManage},</if>
            <if test="sgManage != null and sgManage != ''"> #{sgManage},</if>
            <if test="jlManage != null and jlManage != ''"> #{jlManage},</if>
            #{createTime},
            #{createUser},
            #{updateTime},
            #{updateUser},
            0
            )
        </insert>

    <!--删除工程信息-->
    <update id="deletePro">
        UPDATE tb_project
        SET del_flag = 1
        where id = #{proId}
    </update>

    <!--杆塔编号是否存在-->
    <select id="gtCodeIsExist" resultType="java.util.Map">
        <if test="gtId == null">
            SELECT id as gtId,power_name as gtCode,lon,lat FROM tb_project_power  WHERE del_flag = 0
        </if>
        <if test="gtId != null">
            SELECT id as gtId,power_name as gtCode,lon,lat FROM tb_project_power WHERE id != #{gtId} AND del_flag = 0
        </if>
    </select>

    <select id="getEngineeringSomeLists"  resultType="com.bonus.common.entity.bracelet.vo.EngineeringVo">
        select tb.id as proId,tpd.depart_name as departName,tb.pro_name as proName,count(tpp.id) as gtNum,sddlevel.dict_label as vlevel,sddorg.dict_label as orgName,
        sddpro.dict_label as proType,sddstatus.dict_label as proStatus,sddyz.dict_label as yzManage,sddsg.dict_label as sgManage,
        sddjl.dict_label as jlManage
        from tb_project tb
        left join tb_project_depart tpd on tb.depart_id = tpd.id and tpd.del_flag = 0
        left join tb_project_power tpp on tb.id = tpp.pro_id and tpp.del_flag = 0
        left join sys_dict_data sddlevel on sddlevel.dict_value=tb.voltage_level  and sddlevel.dict_type = 'sys_voltage_level'
        left join sys_dict_data sddorg on sddorg.dict_value=tb.org_id  and sddorg.dict_type = 'sys_org_name'
        left join sys_dict_data sddpro on sddpro.dict_value=tb.pro_type  and sddpro.dict_type = 'sys_pro_type'
        left join sys_dict_data sddstatus on sddstatus.dict_value=tb.pro_status  and sddstatus.dict_type = 'sys_pro_status'
        left join sys_dict_data sddyz on sddyz.dict_value=tb.yz_manage  and sddyz.dict_type = 'sys_yz_manage'
        left join sys_dict_data sddsg on sddsg.dict_value=tb.sg_manage  and sddsg.dict_type = 'sys_sg_manage'
        left join sys_dict_data sddjl on sddjl.dict_value=tb.jl_manage  and sddjl.dict_type = 'sys_jl_manage'
        where
            tb.del_flag = 0
            and tb.id in
        <foreach collection="array" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
        group by tb.id,tpd.depart_name,tb.pro_name,sddlevel.dict_label,sddorg.dict_label,sddpro.dict_label,sddstatus.dict_label,
        sddyz.dict_label,sddsg.dict_label,sddjl.dict_label
        order by tb.id ASC
    </select>

    <select id="getExportGtSomeLists"  resultType="com.bonus.common.entity.bracelet.vo.GtVo">
        select tpp.pro_id as proId,tpp.id as gtId,tpp.power_name as gtCode,tpp.lon as lon,tpp.lat as lat
        from  tb_project_power tpp
        <where>
            tpp.del_flag = 0
            <if test="proId != null and proId!=''">
                AND tpp.pro_id = #{proId}
            </if>
            and tpp.id in
            <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        order by tpp.id ASC
    </select>

</mapper>