<?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.PdmScoreRankManagerMapper">

    <resultMap type="PdmScoreRankManager" id="PdmScoreRankManagerResult">
        <result property="id"    column="id"    />
        <result property="reportYear"    column="report_year"    />
        <result property="reportDate"    column="report_date"    />
        <result property="companyNo"    column="company_no"    />
        <result property="companyName"    column="company_name"    />
        <result property="teamType"    column="team_type"    />
        <result property="teamId"    column="team_id"    />
        <result property="teamName"    column="team_name"    />
        <result property="managerId"    column="manager_id"    />
        <result property="managerName"    column="manager_name"    />
        <result property="score"    column="score"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="teamType"    column="team_type"    />
    </resultMap>

    <sql id="selectPdmScoreRankManagerVo">
        select id, report_year, report_date, company_no, company_name,team_type,team_name, manager_id, manager_name, score, create_by, create_time, update_by, update_time,team_type from pdm_score_rank_manager
    </sql>

    <select id="selectPdmScoreRankManagerList" parameterType="PdmScoreRankManager" resultMap="PdmScoreRankManagerResult">
        <include refid="selectPdmScoreRankManagerVo"/>
        <where>
            <if test="reportYear != null  and reportYear != ''"> and report_year = #{reportYear}</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</if>
            <if test="companyNo != null  and companyNo != ''"> and company_no = #{companyNo}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="managerId != null  and managerId != ''"> and manager_id = #{managerId}</if>
            <if test="managerName != null  and managerName != ''"> and manager_name like concat('%', #{managerName}, '%')</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="teamType != null  and teamType != ''"> and team_type = #{teamType}</if>
        </where>
        <if test="isBlack == null  or isBlack == ''  ">
            order by score desc
        </if>
        <if test="isBlack !=null  and isBlack == 1 ">
            order by score asc
        </if>
    </select>

    <select id="selectPdmScoreRankManagerById" parameterType="Long" resultMap="PdmScoreRankManagerResult">
        <include refid="selectPdmScoreRankManagerVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmScoreRankManager" parameterType="PdmScoreRankManager" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_score_rank_manager
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportYear != null">report_year,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="companyNo != null">company_no,</if>
            <if test="companyName != null">company_name,</if>
            <if test="managerId != null">manager_id,</if>
            <if test="managerName != null">manager_name,</if>
            <if test="score != null">score,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reportYear != null">#{reportYear},</if>
            <if test="reportDate != null">#{reportDate},</if>
            <if test="companyNo != null">#{companyNo},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="managerId != null">#{managerId},</if>
            <if test="managerName != null">#{managerName},</if>
            <if test="score != null">#{score},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updatePdmScoreRankManager" parameterType="PdmScoreRankManager">
        update pdm_score_rank_manager
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportYear != null">report_year = #{reportYear},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="companyNo != null">company_no = #{companyNo},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="managerId != null">manager_id = #{managerId},</if>
            <if test="managerName != null">manager_name = #{managerName},</if>
            <if test="score != null">score = #{score},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePdmScoreRankManagerById" parameterType="Long">
        delete from pdm_score_rank_manager where id = #{id}
    </delete>

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

    <insert id="saveRank" parameterType="PdmScoreRankManager">
        insert into pdm_score_rank_manager(report_year,report_date,company_name,team_type,team_name,manager_name,score)

        select report_year,report_date,t1.submit_org_nm,'1' team_type,t1.work_team_nm,t1.work_princip_nm,
        work_level_1+work_level_2+work_level_3+work_level_4+work_level_5
        -IFNULL(province_violation_1,0)-IFNULL(province_violation_2,0)-IFNULL(city_violation_1,0)-IFNULL(city_violation_2,0)
        -IFNULL(city_problem_1,0)-IFNULL(city_problem_2,0) score
        from
        (
        SELECT
        DATE_FORMAT(plan_start_wk_tm,'%Y') report_year,
        DATE_FORMAT(plan_start_wk_tm,'%Y%m') report_date,submit_org_nm,work_team_nm,work_princip_nm,work_princip_id,
        sum(if(op_risk_lvl_cd = '2007001',1,0))*6 work_level_1,
        sum(if(op_risk_lvl_cd = '2007002',1,0))*4 work_level_2,
        sum(if(op_risk_lvl_cd = '2007003',1,0))*2 work_level_3,
        sum(if(op_risk_lvl_cd = '2007004',1,0))*1 work_level_4,
        sum(if(op_risk_lvl_cd = '2007005',1,0))*0.5 work_level_5
        from pdm_work_plan
        where date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        and work_team_nm not like '%公司'
        GROUP BY submit_org_nm,work_team_nm,work_princip_nm,work_princip_id
        )t1
        LEFT JOIN
        (
        SELECT t3.submit_org_nm,t3.work_team_nm,work_princip_nm,t3.work_princip_id,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025001' and break_rules_nature_cd = '2020001',1,0))*12 province_violation_1,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025001' and break_rules_nature_cd in ('2020002','2020003','2020004','2020005','2020006'),1,0))*4 province_violation_2,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025002' and break_rules_nature_cd = '2020001',1,0))*6 city_violation_1,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025002' and break_rules_nature_cd in ('2020002','2020003','2020004','2020005','2020006'),1,0))*2 city_violation_2
        from
        pdm_violation_records t1,pdm_inspection_records t2,pdm_work_plan t3
        where t1.audit_rec_id = t2.audit_rec_id and t1.day_op_info_id = t3.uuid
        and work_team_nm not like '%公司'
        and date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        GROUP BY t3.submit_org_nm,work_team_nm,work_princip_nm,t3.work_princip_id
        )t2 on t1.submit_org_nm = t2.submit_org_nm and t1.work_team_nm = t2.work_team_nm and t1.work_princip_nm = t2.work_princip_nm and t1.work_princip_id = t2.work_princip_id
        LEFT JOIN
        (
        SELECT t3.submit_org_nm,work_team_nm,t3.work_princip_nm,t3.work_princip_id,
        sum(if(t2.scene_rating_cd = '2018003' and audit_lvl_cd ='2025002' and t1.problem_level = '2070001',1,0))*3 city_problem_1,
        sum(if(t2.scene_rating_cd = '2018003' and audit_lvl_cd ='2025002' and t1.problem_level in('2070004','2070005','2070006','2070007'),1,0))*1 city_problem_2
        from
        pdm_inspection_problem_records t1,pdm_inspection_records t2,pdm_work_plan t3
        where t1.day_op_info_id = t2.day_op_info_id and  t2.day_op_info_id = t3.uuid
        and work_team_nm not like '%公司'
        and date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        GROUP BY t3.submit_org_nm,work_team_nm,work_princip_nm,t3.work_princip_id
        )t3 on t1.work_team_nm = t3.work_team_nm and t1.work_princip_nm = t3.work_princip_nm and t1.work_princip_id = t3.work_princip_id

        union all

        select report_year,report_date,t1.work_team_nm submit_org_nm,'2' team_type,'' work_team_nm,t1.work_princip_nm,
        work_level_1+work_level_2+work_level_3+work_level_4+work_level_5
        -IFNULL(province_violation_1,0)-IFNULL(province_violation_2,0)-IFNULL(city_violation_1,0)-IFNULL(city_violation_2,0)
        -IFNULL(city_problem_1,0)-IFNULL(city_problem_2,0) score
        from
        (
        SELECT
        DATE_FORMAT(plan_start_wk_tm,'%Y') report_year,
        DATE_FORMAT(plan_start_wk_tm,'%Y%m') report_date, work_team_nm,work_princip_nm,work_princip_id,
        sum(if(op_risk_lvl_cd = '2007001',1,0))*6 work_level_1,
        sum(if(op_risk_lvl_cd = '2007002',1,0))*4 work_level_2,
        sum(if(op_risk_lvl_cd = '2007003',1,0))*2 work_level_3,
        sum(if(op_risk_lvl_cd = '2007004',1,0))*1 work_level_4,
        sum(if(op_risk_lvl_cd = '2007005',1,0))*0.5 work_level_5
        from pdm_work_plan
        where date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
          and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        and work_team_nm like '%公司'
        GROUP BY work_team_nm,work_princip_nm,work_princip_id
        )t1
        LEFT JOIN
        (
        SELECT t3.work_team_nm,work_princip_nm,t3.work_princip_id,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025001' and break_rules_nature_cd = '2020001',1,0))*12 province_violation_1,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025001' and break_rules_nature_cd in ('2020002','2020003','2020004','2020005','2020006'),1,0))*4 province_violation_2,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025002' and break_rules_nature_cd = '2020001',1,0))*6 city_violation_1,
        sum(if(t2.scene_rating_cd = '2018004' and audit_lvl_cd ='2025002' and break_rules_nature_cd in ('2020002','2020003','2020004','2020005','2020006'),1,0))*2 city_violation_2
        from
        pdm_violation_records t1,pdm_inspection_records t2,pdm_work_plan t3
        where t1.audit_rec_id = t2.audit_rec_id and t1.day_op_info_id = t3.uuid
        and work_team_nm like '%公司'
        and date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        GROUP BY work_team_nm,work_princip_nm,t3.work_princip_id
        )t2 on t1.work_team_nm = t2.work_team_nm and t1.work_princip_nm = t2.work_princip_nm and t1.work_princip_id = t2.work_princip_id
        LEFT JOIN
        (
        SELECT work_team_nm,t3.work_princip_nm,t3.work_princip_id,
        sum(if(t2.scene_rating_cd = '2018003' and audit_lvl_cd ='2025002' and t1.problem_level = '2070001',1,0))*3 city_problem_1,
        sum(if(t2.scene_rating_cd = '2018003' and audit_lvl_cd ='2025002' and t1.problem_level in('2070004','2070005','2070006','2070007'),1,0))*1 city_problem_2
        from
        pdm_inspection_problem_records t1,pdm_inspection_records t2,pdm_work_plan t3
        where t1.day_op_info_id = t2.day_op_info_id and  t2.day_op_info_id = t3.uuid
        and work_team_nm like '%公司'
        and date_format(plan_start_wk_tm,'%Y%m%d')  &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
        GROUP BY work_team_nm,work_princip_nm,t3.work_princip_id
        )t3 on t1.work_team_nm = t3.work_team_nm and t1.work_princip_nm = t3.work_princip_nm and t1.work_princip_id = t3.work_princip_id;

    </insert>

    <select id="getManagerRankYearList" parameterType="PdmScoreRankManager" resultMap="PdmScoreRankManagerResult">
        select  report_year,company_name,team_type, team_name,manager_name,
                sum(score) score
        from pdm_score_rank_manager
        <where>
            <if test="reportYear != null  and reportYear != ''"> and report_year = #{reportYear}</if>
            <if test="teamType != null  and teamType != ''"> and team_type = #{teamType}</if>
        </where>
        group by report_year,company_name,team_type, team_name,manager_name
        <if test="isBlack == null  or isBlack == ''  ">
            order by score desc
        </if>
        <if test="isBlack !=null  and isBlack == 1 ">
            order by score asc
        </if>
    </select>

</mapper>
