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

    <resultMap type="PdmReportIllegalWeek" id="PdmReportIllegalWeekResult">
        <result property="id"    column="id"    />
        <result property="company"    column="company"    />
        <result property="reportDate"    column="report_date"    />
        <result property="reportEndDate"    column="report_end_date"    />
        <result property="provinceNum1"    column="province_num1"    />
        <result property="provinceNum2"    column="province_num2"    />
        <result property="cityNum1"    column="city_num1"    />
        <result property="cityNum2"    column="city_num2"    />
        <result property="countyNum1"    column="county_num1"    />
        <result property="countyNum2"    column="county_num2"    />
        <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="selectPdmReportIllegalWeekVo">
        select id, company, report_date, report_end_date, province_num1, province_num2, city_num1, city_num2, county_num1, county_num2, create_by, create_time, update_by, update_time from pdm_report_illegal_week
    </sql>

    <select id="selectPdmReportIllegalWeekList" parameterType="PdmReportIllegalWeek" resultMap="PdmReportIllegalWeekResult">
        select * from (
        <include refid="selectPdmReportIllegalWeekVo"/>
        <where>
            <if test="company != null  and company != ''"> and company = #{company}</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</if>
            <if test="reportEndDate != null  and reportEndDate != ''"> and report_end_date = #{reportEndDate}</if>
            <if test="provinceNum1 != null "> and province_num1 = #{provinceNum1}</if>
            <if test="provinceNum2 != null "> and province_num2 = #{provinceNum2}</if>
            <if test="cityNum1 != null "> and city_num1 = #{cityNum1}</if>
            <if test="cityNum2 != null "> and city_num2 = #{cityNum2}</if>
            <if test="countyNum1 != null "> and county_num1 = #{countyNum1}</if>
            <if test="countyNum2 != null "> and county_num2 = #{countyNum2}</if>
        </where>
        UNION
        SELECT
        sum(id) as id,
        '总计' as company,
        report_date,
        report_end_date,
        sum(ifnull(province_num1,0)) province_num1,
        sum(ifnull(province_num2,0)) province_num2,
        sum(ifnull(city_num1,0)) city_num1,
        sum(ifnull(city_num2,0)) city_num2,
        sum(ifnull(county_num1,0)) county_num1,
        sum(ifnull(county_num2,0)) county_num2,
        create_by ,
        create_time,
        update_by,
        update_time FROM
        pdm_report_illegal_week
        <where>
            <if test="company != null  and company != ''"> and company like concat('%', #{company}, '%')</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</if>
            <if test="provinceNum1 != null "> and province_num1 = #{provinceNum1}</if>
            <if test="provinceNum2 != null "> and province_num2 = #{provinceNum2}</if>
            <if test="cityNum1 != null "> and city_num1 = #{cityNum1}</if>
            <if test="cityNum2 != null "> and city_num2 = #{cityNum2}</if>
            <if test="countyNum1 != null "> and county_num1 = #{countyNum1}</if>
            <if test="countyNum2 != null "> and county_num2 = #{countyNum2}</if>
        </where> GROUP BY report_date) a order by id asc
    </select>

    <select id="selectPdmReportIllegalWeekById" parameterType="Long" resultMap="PdmReportIllegalWeekResult">
        <include refid="selectPdmReportIllegalWeekVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportIllegalWeek" parameterType="PdmReportIllegalWeek" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_illegal_week
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">company,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="reportEndDate != null">report_end_date,</if>
            <if test="provinceNum1 != null">province_num1,</if>
            <if test="provinceNum2 != null">province_num2,</if>
            <if test="cityNum1 != null">city_num1,</if>
            <if test="cityNum2 != null">city_num2,</if>
            <if test="countyNum1 != null">county_num1,</if>
            <if test="countyNum2 != null">county_num2,</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="company != null">#{company},</if>
            <if test="reportDate != null">#{reportDate},</if>
            <if test="reportEndDate != null">#{reportEndDate},</if>
            <if test="provinceNum1 != null">#{provinceNum1},</if>
            <if test="provinceNum2 != null">#{provinceNum2},</if>
            <if test="cityNum1 != null">#{cityNum1},</if>
            <if test="cityNum2 != null">#{cityNum2},</if>
            <if test="countyNum1 != null">#{countyNum1},</if>
            <if test="countyNum2 != null">#{countyNum2},</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="updatePdmReportIllegalWeek" parameterType="PdmReportIllegalWeek">
        update pdm_report_illegal_week
        <trim prefix="SET" suffixOverrides=",">
            <if test="company != null">company = #{company},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="reportEndDate != null">report_end_date = #{reportEndDate},</if>
            <if test="provinceNum1 != null">province_num1 = #{provinceNum1},</if>
            <if test="provinceNum2 != null">province_num2 = #{provinceNum2},</if>
            <if test="cityNum1 != null">city_num1 = #{cityNum1},</if>
            <if test="cityNum2 != null">city_num2 = #{cityNum2},</if>
            <if test="countyNum1 != null">county_num1 = #{countyNum1},</if>
            <if test="countyNum2 != null">county_num2 = #{countyNum2},</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="deletePdmReportIllegalWeekById" parameterType="Long">
        delete from pdm_report_illegal_week where id = #{id}
    </delete>

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