<?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.ruoyi.powerdistribution.mapper.PdmLineMapper">

    <resultMap type="PdmLine" id="PdmLineResult">
        <result property="id"    column="id"    />
        <result property="recId"    column="rec_id"    />
        <result property="lineNm"    column="line_nm"    />
        <result property="lineRunNo"    column="line_run_no"    />
        <result property="voltLvlCd"    column="volt_lvl_cd"    />
        <result property="voltLvlDsc"    column="volt_lvl_dsc"    />
        <result property="subsId"    column="subs_id"    />
        <result property="subsNm"    column="subs_nm"    />
        <result property="feederId"    column="feeder_id"    />
        <result property="feederNm"    column="feeder_nm"    />
        <result property="opMaintOrgId"    column="op_maint_org_id"    />
        <result property="opMaintOrgNm"    column="op_maint_org_nm"    />
        <result property="maintTeamId"    column="maint_team_id"    />
        <result property="maintTeamNm"    column="maint_team_nm"    />
        <result property="orgNo"    column="org_no"    />
        <result property="orgNm"    column="org_nm"    />
        <result property="stdCreatorNo"    column="std_creator_no"    />
        <result property="stdCreatorNm"    column="std_creator_nm"    />
        <result property="stdUpdatorNo"    column="std_updator_no"    />
        <result property="stdUpdatorNm"    column="std_updator_nm"    />
        <result property="stdOrgNo"    column="std_org_no"    />
        <result property="stdOrgNm"    column="std_org_nm"    />
        <result property="stdCityOrgNo"    column="std_city_org_no"    />
        <result property="stdCityOrgNm"    column="std_city_org_nm"    />
        <result property="stdCountyOrgNo"    column="std_county_org_no"    />
        <result property="stdCountyOrgNm"    column="std_county_org_nm"    />
        <result property="dataDt"    column="data_dt"    />
        <result property="etlTm"    column="etl_tm"    />
    </resultMap>

    <sql id="selectPdmLineVo">
        select id, rec_id, line_nm, line_run_no, volt_lvl_cd, volt_lvl_dsc, subs_id, subs_nm, feeder_id, feeder_nm, op_maint_org_id, op_maint_org_nm, maint_team_id, maint_team_nm, org_no, org_nm, std_creator_no, std_creator_nm, std_updator_no, std_updator_nm, std_org_no, std_org_nm, std_city_org_no, std_city_org_nm, std_county_org_no, std_county_org_nm, data_dt, etl_tm from pdm_line
    </sql>

    <select id="selectPdmLineList" parameterType="PdmLine" resultMap="PdmLineResult">
        <include refid="selectPdmLineVo"/>
        <where>
            <if test="recId != null  and recId != ''"> and rec_id = #{recId}</if>
            <if test="lineNm != null  and lineNm != ''"> and line_nm = #{lineNm}</if>
            <if test="lineRunNo != null  and lineRunNo != ''"> and line_run_no = #{lineRunNo}</if>
            <if test="voltLvlCd != null  and voltLvlCd != ''"> and volt_lvl_cd = #{voltLvlCd}</if>
            <if test="voltLvlDsc != null  and voltLvlDsc != ''"> and volt_lvl_dsc = #{voltLvlDsc}</if>
            <if test="subsId != null  and subsId != ''"> and subs_id = #{subsId}</if>
            <if test="subsNm != null  and subsNm != ''"> and subs_nm = #{subsNm}</if>
            <if test="feederId != null  and feederId != ''"> and feeder_id = #{feederId}</if>
            <if test="feederNm != null  and feederNm != ''"> and feeder_nm = #{feederNm}</if>
            <if test="opMaintOrgId != null  and opMaintOrgId != ''"> and op_maint_org_id = #{opMaintOrgId}</if>
            <if test="opMaintOrgNm != null  and opMaintOrgNm != ''"> and op_maint_org_nm = #{opMaintOrgNm}</if>
            <if test="maintTeamId != null  and maintTeamId != ''"> and maint_team_id = #{maintTeamId}</if>
            <if test="maintTeamNm != null  and maintTeamNm != ''"> and maint_team_nm = #{maintTeamNm}</if>
            <if test="orgNo != null  and orgNo != ''"> and org_no = #{orgNo}</if>
            <if test="orgNm != null  and orgNm != ''"> and org_nm = #{orgNm}</if>
            <if test="stdCreatorNo != null  and stdCreatorNo != ''"> and std_creator_no = #{stdCreatorNo}</if>
            <if test="stdCreatorNm != null  and stdCreatorNm != ''"> and std_creator_nm = #{stdCreatorNm}</if>
            <if test="stdUpdatorNo != null  and stdUpdatorNo != ''"> and std_updator_no = #{stdUpdatorNo}</if>
            <if test="stdUpdatorNm != null  and stdUpdatorNm != ''"> and std_updator_nm = #{stdUpdatorNm}</if>
            <if test="stdOrgNo != null  and stdOrgNo != ''"> and std_org_no = #{stdOrgNo}</if>
            <if test="stdOrgNm != null  and stdOrgNm != ''"> and std_org_nm = #{stdOrgNm}</if>
            <if test="stdCityOrgNo != null  and stdCityOrgNo != ''"> and std_city_org_no = #{stdCityOrgNo}</if>
            <if test="stdCityOrgNm != null  and stdCityOrgNm != ''"> and std_city_org_nm = #{stdCityOrgNm}</if>
            <if test="stdCountyOrgNo != null  and stdCountyOrgNo != ''"> and std_county_org_no = #{stdCountyOrgNo}</if>
            <if test="stdCountyOrgNm != null  and stdCountyOrgNm != ''"> and std_county_org_nm = #{stdCountyOrgNm}</if>
            <if test="dataDt != null  and dataDt != ''"> and data_dt = #{dataDt}</if>
            <if test="etlTm != null "> and etl_tm = #{etlTm}</if>
        </where>
    </select>

    <select id="selectPdmLineById" parameterType="Long" resultMap="PdmLineResult">
        <include refid="selectPdmLineVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmLine" parameterType="PdmLine" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_line
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recId != null">rec_id,</if>
            <if test="lineNm != null">line_nm,</if>
            <if test="lineRunNo != null">line_run_no,</if>
            <if test="voltLvlCd != null">volt_lvl_cd,</if>
            <if test="voltLvlDsc != null">volt_lvl_dsc,</if>
            <if test="subsId != null">subs_id,</if>
            <if test="subsNm != null">subs_nm,</if>
            <if test="feederId != null">feeder_id,</if>
            <if test="feederNm != null">feeder_nm,</if>
            <if test="opMaintOrgId != null">op_maint_org_id,</if>
            <if test="opMaintOrgNm != null">op_maint_org_nm,</if>
            <if test="maintTeamId != null">maint_team_id,</if>
            <if test="maintTeamNm != null">maint_team_nm,</if>
            <if test="orgNo != null">org_no,</if>
            <if test="orgNm != null">org_nm,</if>
            <if test="stdCreatorNo != null">std_creator_no,</if>
            <if test="stdCreatorNm != null">std_creator_nm,</if>
            <if test="stdUpdatorNo != null">std_updator_no,</if>
            <if test="stdUpdatorNm != null">std_updator_nm,</if>
            <if test="stdOrgNo != null">std_org_no,</if>
            <if test="stdOrgNm != null">std_org_nm,</if>
            <if test="stdCityOrgNo != null">std_city_org_no,</if>
            <if test="stdCityOrgNm != null">std_city_org_nm,</if>
            <if test="stdCountyOrgNo != null">std_county_org_no,</if>
            <if test="stdCountyOrgNm != null">std_county_org_nm,</if>
            <if test="dataDt != null">data_dt,</if>
            <if test="etlTm != null">etl_tm,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recId != null">#{recId},</if>
            <if test="lineNm != null">#{lineNm},</if>
            <if test="lineRunNo != null">#{lineRunNo},</if>
            <if test="voltLvlCd != null">#{voltLvlCd},</if>
            <if test="voltLvlDsc != null">#{voltLvlDsc},</if>
            <if test="subsId != null">#{subsId},</if>
            <if test="subsNm != null">#{subsNm},</if>
            <if test="feederId != null">#{feederId},</if>
            <if test="feederNm != null">#{feederNm},</if>
            <if test="opMaintOrgId != null">#{opMaintOrgId},</if>
            <if test="opMaintOrgNm != null">#{opMaintOrgNm},</if>
            <if test="maintTeamId != null">#{maintTeamId},</if>
            <if test="maintTeamNm != null">#{maintTeamNm},</if>
            <if test="orgNo != null">#{orgNo},</if>
            <if test="orgNm != null">#{orgNm},</if>
            <if test="stdCreatorNo != null">#{stdCreatorNo},</if>
            <if test="stdCreatorNm != null">#{stdCreatorNm},</if>
            <if test="stdUpdatorNo != null">#{stdUpdatorNo},</if>
            <if test="stdUpdatorNm != null">#{stdUpdatorNm},</if>
            <if test="stdOrgNo != null">#{stdOrgNo},</if>
            <if test="stdOrgNm != null">#{stdOrgNm},</if>
            <if test="stdCityOrgNo != null">#{stdCityOrgNo},</if>
            <if test="stdCityOrgNm != null">#{stdCityOrgNm},</if>
            <if test="stdCountyOrgNo != null">#{stdCountyOrgNo},</if>
            <if test="stdCountyOrgNm != null">#{stdCountyOrgNm},</if>
            <if test="dataDt != null">#{dataDt},</if>
            <if test="etlTm != null">#{etlTm},</if>
         </trim>
    </insert>

    <update id="updatePdmLine" parameterType="PdmLine">
        update pdm_line
        <trim prefix="SET" suffixOverrides=",">
            <if test="recId != null">rec_id = #{recId},</if>
            <if test="lineNm != null">line_nm = #{lineNm},</if>
            <if test="lineRunNo != null">line_run_no = #{lineRunNo},</if>
            <if test="voltLvlCd != null">volt_lvl_cd = #{voltLvlCd},</if>
            <if test="voltLvlDsc != null">volt_lvl_dsc = #{voltLvlDsc},</if>
            <if test="subsId != null">subs_id = #{subsId},</if>
            <if test="subsNm != null">subs_nm = #{subsNm},</if>
            <if test="feederId != null">feeder_id = #{feederId},</if>
            <if test="feederNm != null">feeder_nm = #{feederNm},</if>
            <if test="opMaintOrgId != null">op_maint_org_id = #{opMaintOrgId},</if>
            <if test="opMaintOrgNm != null">op_maint_org_nm = #{opMaintOrgNm},</if>
            <if test="maintTeamId != null">maint_team_id = #{maintTeamId},</if>
            <if test="maintTeamNm != null">maint_team_nm = #{maintTeamNm},</if>
            <if test="orgNo != null">org_no = #{orgNo},</if>
            <if test="orgNm != null">org_nm = #{orgNm},</if>
            <if test="stdCreatorNo != null">std_creator_no = #{stdCreatorNo},</if>
            <if test="stdCreatorNm != null">std_creator_nm = #{stdCreatorNm},</if>
            <if test="stdUpdatorNo != null">std_updator_no = #{stdUpdatorNo},</if>
            <if test="stdUpdatorNm != null">std_updator_nm = #{stdUpdatorNm},</if>
            <if test="stdOrgNo != null">std_org_no = #{stdOrgNo},</if>
            <if test="stdOrgNm != null">std_org_nm = #{stdOrgNm},</if>
            <if test="stdCityOrgNo != null">std_city_org_no = #{stdCityOrgNo},</if>
            <if test="stdCityOrgNm != null">std_city_org_nm = #{stdCityOrgNm},</if>
            <if test="stdCountyOrgNo != null">std_county_org_no = #{stdCountyOrgNo},</if>
            <if test="stdCountyOrgNm != null">std_county_org_nm = #{stdCountyOrgNm},</if>
            <if test="dataDt != null">data_dt = #{dataDt},</if>
            <if test="etlTm != null">etl_tm = #{etlTm},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePdmLineById" parameterType="Long">
        delete from pdm_line where id = #{id}
    </delete>

    <delete id="deletePdmLineByIds" parameterType="String">
        delete from pdm_line where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCompleteList" parameterType="com.ruoyi.powerdistribution.domain.vo.PdmLineComplete" resultType="com.ruoyi.powerdistribution.domain.vo.PdmLineComplete">
        SELECT
        t1.id, t1.rec_id, t1.line_nm, t1.line_run_no, t1.volt_lvl_cd, t1.volt_lvl_dsc,
        t1.subs_id, t1.subs_nm, t1.feeder_id, t1.feeder_nm, t1.op_maint_org_id, t1.op_maint_org_nm,
        t1.maint_team_id, t1.maint_team_nm, t1.org_no, t1.org_nm, t1.std_creator_no, t1.std_creator_nm,
        t1.std_updator_no, t1.std_updator_nm, t1.std_org_no, t1.std_org_nm, t1.std_city_org_no,
        SUBSTRING_INDEX(t1.std_city_org_nm, '国网湖南省电力有限公司', -1)std_city_org_nm, t1.std_county_org_no, SUBSTRING_INDEX(t1.std_county_org_nm, '国网湖南省电力有限公司', -1) std_county_org_nm, t1.data_dt, t1.etl_tm,t1.bouns as reward_amount,t1.fault_free_duration,
        (SELECT count(1) from pdm_stop_records where line_id=t1.rec_id)poweroff_num
        from pdm_line t1
        <where>
            <if test="recId != null  and recId != ''"> and t1.rec_id = #{recId}</if>
            <if test="lineNm != null  and lineNm != ''"> and t1.line_nm like concat('%', #{lineNm}, '%')</if>
            <if test="branchLineNm != null  and branchLineNm != ''"> and exists(select 1 from pdm_branch_line b where b.blg_big_feeder_id=t1.feeder_id and b.line_nm like concat('%', #{branchLineNm}, '%'))</if>
            <if test="isMaintOverdue!=null and 'true'.toString() == isMaintOverdue.toString()">
                and (EXISTS(SELECT 1 from pdm_stop_records r where r.line_id=t1.rec_id and TIMESTAMPDIFF(YEAR,r.create_tm, SYSDATE())>3)
                or (TIMESTAMPDIFF(YEAR,t1.ship_dt, SYSDATE())>3 and  not EXISTS(SELECT 1 from pdm_stop_records r where r.line_id=t1.rec_id)))
           </if>
            <if test="isRepeatPoweroff!=null and 'true'.toString() == isRepeatPoweroff.toString()">
                and EXISTS(SELECT 1 from pdm_stop_records r where r.line_id=t1.rec_id and TIMESTAMPDIFF(MONTH,r.create_tm, SYSDATE())=2)
                and (SELECT count(1) from pdm_stop_records r where r.line_id=t1.rec_id )>=3
            </if>
        </where>
    </select>
    <update id="calculateBounsProc" statementType="CALLABLE" parameterType="java.util.Map">
        {call calculateBounsProc(#{calcuDate, mode=IN})}
    </update>
</mapper>
