<?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.system.mapper.UrYearValidDataDisMapper">
    
    <resultMap type="com.ruoyi.system.domain.UrYearValidDataDis" id="UrYearValidDataDisResult">
        <result property="id"    column="id"    />
        <result property="year"    column="year"    />
        <result property="districtShortName"    column="district_short_name"    />
        <result property="subject"    column="subject"    />
        <result property="code"    column="code"    />
        <result property="sort"    column="sort"    />
        <result property="type"    column="type"    />
        <result property="count"    column="count"    />
    </resultMap>

    <sql id="selectUrYearValidDataDisVo">
        select id, year, district_short_name, subject, code, sort, type, count from ur_year_valid_data_dis
    </sql>

    <select id="selectUrYearValidDataDisList" parameterType="com.ruoyi.system.domain.UrYearValidDataDis" resultMap="UrYearValidDataDisResult">
        <include refid="selectUrYearValidDataDisVo"/>
        <where>
            <if test="startDate != null  and startDate != ''"> and year >= #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and year &lt;= #{endDate}</if>
            <if test="districtShortName != null  and districtShortName != ''"> and district_short_name = #{districtShortName}</if>
            <if test="subject != null  and subject != ''"> and subject = #{subject}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
        </where>
    </select>

    <select id="selectUrYearValidDataDisById" parameterType="java.lang.Long" resultMap="UrYearValidDataDisResult">
        <include refid="selectUrYearValidDataDisVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertUrYearValidDataDis" parameterType="com.ruoyi.system.domain.UrYearValidDataDis" useGeneratedKeys="true" keyProperty="id">
        insert into ur_year_valid_data_dis
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="year != null">year,</if>
            <if test="districtShortName != null">district_short_name,</if>
            <if test="subject != null">subject,</if>
            <if test="code != null">code,</if>
            <if test="sort != null">sort,</if>
            <if test="type != null">type,</if>
            <if test="count != null">count,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="year != null">#{year},</if>
            <if test="districtShortName != null">#{districtShortName},</if>
            <if test="subject != null">#{subject},</if>
            <if test="code != null">#{code},</if>
            <if test="sort != null">#{sort},</if>
            <if test="type != null">#{type},</if>
            <if test="count != null">#{count},</if>
         </trim>
    </insert>

    <update id="updateUrYearValidDataDis" parameterType="com.ruoyi.system.domain.UrYearValidDataDis">
        update ur_year_valid_data_dis
        <trim prefix="SET" suffixOverrides=",">
            <if test="year != null">year = #{year},</if>
            <if test="districtShortName != null">district_short_name = #{districtShortName},</if>
            <if test="subject != null">subject = #{subject},</if>
            <if test="code != null">code = #{code},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="type != null">type = #{type},</if>
            <if test="count != null">count = #{count},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUrYearValidDataDisById" parameterType="java.lang.Long">
        delete from ur_year_valid_data_dis where id = #{id}
    </delete>

    <delete id="deleteUrYearValidDataDisByIds" parameterType="java.lang.String">
        delete from ur_year_valid_data_dis where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 逻辑删除 -->
    <update id="setDeleteById">
        update ur_year_valid_data_dis set delete_flag="Y",update_time=sysdate(),update_user_id=#{operId},update_user_name=#{operName} where id = #{id}
    </update>

    <!-- 逻辑删除 -->
    <update id="setDeleteByIds">
        update ur_year_valid_data_dis set delete_flag="Y",update_time=sysdate(),update_user_id=#{operId},update_user_name=#{operName} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectUrLeftList" parameterType="java.lang.String" resultMap="UrYearValidDataDisResult">
        select district_short_name, subject from ur_tmp_left_dis where code like concat(#{code}, '%')
    </select>

    <select id="selectUrYearValidDataDisCityList" parameterType="com.ruoyi.system.domain.vo.UrYearValidDataDisVo" resultMap="UrYearValidDataDisResult">
        select
        id, year, district_short_name, subject, code, sort, type, SUM(count) as count
        from ur_year_valid_data_dis
        <where>
            <if test="startDate != null  and startDate != ''"> and year >= #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and year &lt;= #{endDate}</if>
            <if test="subject != null  and subject != ''"> and subject = #{subject}</if>
            <if test="code != null  and code != ''"> and code like concat(#{code}, '%')</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
        </where>
        group by year,type
        order by id
    </select>

</mapper>