<?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.zehong.system.mapper.TEnterpriseRankingMapper">
    
    <resultMap type="TEnterpriseRanking" id="TEnterpriseRankingResult">
        <result property="rankingId"    column="ranking_id"    />
        <result property="yearNum"    column="year_num"    />
        <result property="beyondEnterpriseId"    column="beyond_enterprise_id"    />
        <result property="enterpriseName"    column="enterprise_name"    />
        <result property="enterpriseTyep"    column="enterprise_type"    />
        <result property="accidentNum"    column="accident_num"    />
        <result property="hiddenNum"    column="hidden_num"    />
        <result property="complaintNum"    column="complaint_num"    />
        <result property="fraction"    column="fraction"    />
        <result property="result"    column="result"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectTEnterpriseRankingVo">
        select ranking_id, year_num, beyond_enterprise_id, enterprise_name,enterprise_type, accident_num, hidden_num, complaint_num, fraction, result, create_time from t_enterprise_ranking
    </sql>

    <select id="selectTEnterpriseRankingList" parameterType="TEnterpriseRanking" resultMap="TEnterpriseRankingResult">
        <include refid="selectTEnterpriseRankingVo"/>
        <where>  
            <if test="yearNum != null "> and year_num = #{yearNum}</if>
        </where>
    </select>
    
    <select id="selectTEnterpriseRankingById" parameterType="Long" resultMap="TEnterpriseRankingResult">
        <include refid="selectTEnterpriseRankingVo"/>
        where ranking_id = #{rankingId}
    </select>
        
    <insert id="insertTEnterpriseRanking" parameterType="TEnterpriseRanking" useGeneratedKeys="true" keyProperty="rankingId">
        insert into t_enterprise_ranking
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="yearNum != null">year_num,</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id,</if>
            <if test="enterpriseName != null">enterprise_name,</if>
            <if test="accidentNum != null">accident_num,</if>
            <if test="hiddenNum != null">hidden_num,</if>
            <if test="complaintNum != null">complaint_num,</if>
            <if test="fraction != null">fraction,</if>
            <if test="result != null">result,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="yearNum != null">#{yearNum},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
            <if test="enterpriseName != null">#{enterpriseName},</if>
            <if test="accidentNum != null">#{accidentNum},</if>
            <if test="hiddenNum != null">#{hiddenNum},</if>
            <if test="complaintNum != null">#{complaintNum},</if>
            <if test="fraction != null">#{fraction},</if>
            <if test="result != null">#{result},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateTEnterpriseRanking" parameterType="TEnterpriseRanking">
        update t_enterprise_ranking
        <trim prefix="SET" suffixOverrides=",">
            <if test="yearNum != null">year_num = #{yearNum},</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id = #{beyondEnterpriseId},</if>
            <if test="enterpriseName != null">enterprise_name = #{enterpriseName},</if>
            <if test="accidentNum != null">accident_num = #{accidentNum},</if>
            <if test="hiddenNum != null">hidden_num = #{hiddenNum},</if>
            <if test="complaintNum != null">complaint_num = #{complaintNum},</if>
            <if test="fraction != null">fraction = #{fraction},</if>
            <if test="result != null">result = #{result},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where ranking_id = #{rankingId}
    </update>

    <delete id="deleteTEnterpriseRankingById" parameterType="Long">
        delete from t_enterprise_ranking where ranking_id = #{rankingId}
    </delete>

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

    <select id="selectFractionByYear" resultType="hashmap" >
        SELECT GROUP_CONCAT(e.enterprise_name) as enterpriseName,GROUP_CONCAT(IFNULL(r.fraction,0)) as fraction FROM
        (SELECT * FROM t_enterprise_info
          WHERE enterprise_id IN (SELECT beyond_enterprise_id FROM t_enterprise_ranking GROUP BY beyond_enterprise_id)
        order by  enterprise_id)e
        LEFT JOIN t_enterprise_ranking r ON r.beyond_enterprise_id = e.enterprise_id AND r.year_num =#{yearNum}
    </select>
</mapper>