<?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.hdkj.lt.bf.mapper.FhzgPlanTransferSchemeMapper">

    <select id="getFhzgPlanTransferSchemeByPlanId"
            resultType="com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeDTO"
            parameterType="com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferScheme">
        SELECT
        ta.id id,
        ta.plan_id planId,
        ta.evaluation_score evaluationScore,
        ta.scheme_name schemeName,
        ta.scheme_state schemeState,
        ta.switch_operation_count switchOperationCount,
        ta.load_recovery loadRecovery,
        ta.scheme_check schemeCheck,
        ta.feasible_transfer feasibleTransfer,
        ta.total_transfer totalTransfer,
        ta.loss_cust_hours lossCustHours,
        ta.loss_users lossUsers,
        ta.file_id fileId
        FROM
        fhzg_plan_transfer_scheme ta
        <where>
            ta.plan_id = #{planId}
            <if test="schemeCheck != null and schemeCheck!=null ">
                and ta.scheme_check= #{schemeCheck}
            </if>
            <if test="schemeType == 1 ">
                and ta.scheme_check = 1
            </if>
            <if test="schemeType == 0 ">
                and ta.scheme_check = 0
            </if>
            <if test="schemeState != null and schemeState!=null ">
                and ta.scheme_state= #{schemeState}
            </if>
            <if test="id != null and id!=null ">
                and ta.id= #{id}
            </if>
        </where>
        order by ta.scheme_state asc,ta.evaluation_score desc

    </select>

    <select id="getFhzgFaultTransferSchemeByFaultId"
            resultType="com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeDTO"
            parameterType="com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferScheme">
        SELECT
        ta.id id,
        ta.fault_id planId,
        ta.evaluation_score evaluationScore,
        ta.scheme_name schemeName,
        ta.scheme_state schemeState,
        ta.switch_operation_count switchOperationCount,
        ta.load_recovery loadRecovery,
        ta.scheme_check schemeCheck,
        ta.feasible_transfer feasibleTransfer,
        ta.total_transfer totalTransfer,
        ta.loss_cust_hours lossCustHours,
        ta.loss_users lossUsers
        FROM
        fhzg_fault_transfer_scheme ta
        <where>
            ta.plan_id = #{planId}
            <if test="schemeCheck != null and schemeCheck!=null ">
                and ta.scheme_check= #{schemeCheck}
            </if>
            <if test="schemeType == 1 ">
                and ta.scheme_check = 1
            </if>
            <if test="schemeType == 0 ">
                and ta.scheme_check = 0
            </if>
            <if test="schemeState != null and schemeState!=null ">
                and ta.scheme_state= #{schemeState}
            </if>
            <if test="id != null and id!=null ">
                and ta.id= #{id}
            </if>
        </where>
        order by ta.scheme_state asc,ta.evaluation_score desc

    </select>


    <select id="countSchemeCheckByPlanId"
            resultType="com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeCheckCountResultDTO">
         select count(1) count, ta.scheme_check schemeCheck  from fhzg_plan_transfer_scheme ta where ta.plan_id = #{planId} group by ta.scheme_check
    </select>

    <select id="countSchemeTypeByPlanId"
            resultType="com.hdkj.lt.bf.entity.dto.FhzgPlanTransferSchemeCheckResultDTO">
        select
        count(case when  ta.load_recovery > 0 then 1 end) countCheckTrue,
        count(case when  ta.load_recovery &lt;= 0 then 1 end) countCheckFalse
        from fhzg_plan_transfer_scheme ta where ta.plan_id = #{planId}
    </select>


    <update id="updateFhzgPlanTransferSchemeState">
        update fhzg_plan_transfer_scheme ts
        set ts.scheme_state=#{schemeState}
        where ts.id = #{id}
    </update>

    <update id="updateFhzgPlanTransferSchemeStateBatch">
        update fhzg_plan_transfer_scheme ts set ts.scheme_state=#{schemeState}
        where ts.id in
        <foreach collection="idList" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getLoadRecoveryByPlanId"
            resultType="java.lang.String">
        SELECT load_recovery
        FROM fhzg_plan_transfer_scheme
        where id = #{schemeId}
    </select>

    <select id="getShemeIdByPlanId" resultType="java.lang.Long">
        SELECT id
        FROM fhzg_plan_transfer_scheme
        where plan_id = #{planId}
        order by load_recovery desc
            limit 10
    </select>

    <update id="updateEvaluationScore">
        update fhzg_plan_transfer_scheme
        set evaluation_score = #{score}
        where id = #{schemeId}
    </update>
</mapper>
