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

    <resultMap type="PdmScoreRankTeam" id="PdmScoreRankTeamResult">
        <result property="id"    column="id"    />
        <result property="reportYear"    column="report_year"    />
        <result property="reportDate"    column="report_date"    />
        <result property="teamType"    column="team_type"    />
        <result property="teamId"    column="team_id"    />
        <result property="teamName"    column="team_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"    />
    </resultMap>

    <sql id="selectPdmScoreRankTeamVo">
        select id, report_year, report_date, team_type, team_id, team_name, score, create_by, create_time, update_by, update_time from pdm_score_rank_team
    </sql>

    <select id="selectPdmScoreRankTeamList" parameterType="PdmScoreRankTeam" resultMap="PdmScoreRankTeamResult">
        <include refid="selectPdmScoreRankTeamVo"/>
        <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="teamType != null  and teamType != ''"> and team_type = #{teamType}</if>
            <if test="teamId != null  and teamId != ''"> and team_id = #{teamId}</if>
            <if test="teamName != null  and teamName != ''"> and team_name like concat('%', #{teamName}, '%')</if>
            <if test="score != null "> and score = #{score}</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="selectPdmScoreRankTeamById" parameterType="Long" resultMap="PdmScoreRankTeamResult">
        <include refid="selectPdmScoreRankTeamVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmScoreRankTeam" parameterType="PdmScoreRankTeam" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_score_rank_team
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportYear != null">report_year,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="teamType != null">team_type,</if>
            <if test="teamId != null">team_id,</if>
            <if test="teamName != null">team_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="teamType != null">#{teamType},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="teamName != null">#{teamName},</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="updatePdmScoreRankTeam" parameterType="PdmScoreRankTeam">
        update pdm_score_rank_team
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportYear != null">report_year = #{reportYear},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="teamType != null">team_type = #{teamType},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="teamName != null">team_name = #{teamName},</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="deletePdmScoreRankTeamById" parameterType="Long">
        delete from pdm_score_rank_team where id = #{id}
    </delete>

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

    <insert id="saveTeam" parameterType="PdmScoreRankTeam">
        insert into pdm_score_rank_team(report_year,report_date,team_type,team_name,score)
        select SUBSTRING(t2.data_dt,1,4) report_year,SUBSTRING(t2.data_dt,1,6) report_date,
        '1',op_team,count(1) score
        from pdm_violation_records t1,pdm_inspection_records t2
        where t1.audit_rec_id = t2.audit_rec_id
          and  date_format(t2.op_plan_start_tm,'%Y%m%d') &gt;= #{startDate}
          and  date_format(t2.op_plan_start_tm,'%Y%m%d') &lt;= #{endDate}
        GROUP BY op_team

    </insert>

    <select id="getTeamRankYearList" parameterType="PdmScoreRankTeam" resultMap="PdmScoreRankTeamResult">
        select report_year,team_type, team_name,
               sum(score) score
        from pdm_score_rank_team
        <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,team_type,team_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>
