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

    <resultMap type="PdmReportIllegalDay" id="PdmReportIllegalDayResult">
        <result property="id"    column="id"    />
        <result property="company"    column="company"    />
        <result property="reportDate"    column="report_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="checkNum"    column="check_num"    />
        <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="selectPdmReportIllegalDayVo">
        select id, company, report_date, province_num1, province_num2, city_num1, city_num2, county_num1, county_num2, check_num, create_by, create_time, update_by, update_time from pdm_report_illegal_day
    </sql>

    <select id="selectPdmReportIllegalDayList" parameterType="PdmReportIllegalDay" resultMap="PdmReportIllegalDayResult">
        select * from (
            <include refid="selectPdmReportIllegalDayVo"/>
            <where>
                <if test="company != null  and company != ''"> and company like concat('%', #{company}, '%')</if>
                <if test="startDate != null  and startDate != ''"> and report_date &gt;= #{startDate}</if>
                <if test="endDate != null  and endDate != ''"> and report_date &lt;= #{endDate}</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>
                <if test="checkNum != null "> and check_num = #{checkNum}</if>
            </where>
            UNION
            SELECT
            sum(id) as id,
            '总计' as company,
            report_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,
            sum(ifnull(check_num,0)) check_num,
            create_by ,
            create_time,
            update_by,
            update_time FROM
            pdm_report_illegal_day
            <where>
                <if test="company != null  and company != ''"> and company like concat('%', #{company}, '%')</if>
                <if test="startDate != null  and startDate != ''"> and report_date &gt;= #{startDate}</if>
                <if test="endDate != null  and endDate != ''"> and report_date &lt;= #{endDate}</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>
                <if test="checkNum != null "> and check_num = #{checkNum}</if>
            </where> GROUP BY report_date) a order by id asc
    </select>

    <select id="selectPdmReportIllegalDayById" parameterType="Long" resultMap="PdmReportIllegalDayResult">
        <include refid="selectPdmReportIllegalDayVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportIllegalDay" parameterType="PdmReportIllegalDay" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_illegal_day
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">company,</if>
            <if test="reportDate != null">report_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="checkNum != null">check_num,</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="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="checkNum != null">#{checkNum},</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="updatePdmReportIllegalDay" parameterType="PdmReportIllegalDay">
        update pdm_report_illegal_day
        <trim prefix="SET" suffixOverrides=",">
            <if test="company != null">company = #{company},</if>
            <if test="reportDate != null">report_date = #{reportDate},</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="checkNum != null">check_num = #{checkNum},</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="deletePdmReportIllegalDayById" parameterType="Long">
        delete from pdm_report_illegal_day where id = #{id}
    </delete>

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

    <select id="selectIllegalReport" parameterType="PdmReportIllegalDay" resultMap="PdmReportIllegalDayResult">
        select t1.organ_abbr company,
        IFNULL(t.province_num1,0) province_num1,IFNULL(t.province_num2,0) province_num2,
        IFNULL(t.city_num1,0) city_num1,IFNULL(t.city_num2,0) city_num2,
        IFNULL(t.county_num1,0) county_num1,IFNULL(t.county_num2,0) county_num2,
        0 check_num
        from pdm_work_organ t1
        LEFT JOIN
        (
        select
        sum(if((t3.audit_lvl_cd='2025001' and t2.break_rules_nature_cd = '2020002'),1,0)) province_num1,
        sum(if((t3.audit_lvl_cd='2025001' and t2.break_rules_nature_cd = '2020001'),1,0)) province_num2,
        sum(if((t3.audit_lvl_cd='2025002' and t2.break_rules_nature_cd = '2020002'),1,0)) city_num1,
        sum(if((t3.audit_lvl_cd='2025002' and t2.break_rules_nature_cd = '2020001'),1,0)) city_num2,
        sum(if((t3.audit_lvl_cd='2025003' and t2.break_rules_nature_cd = '2020002'),1,0)) county_num1,
        sum(if((t3.audit_lvl_cd='2025003' and t2.break_rules_nature_cd = '2020001'),1,0)) county_num2,
        t3.op_org from pdm_violation_records t2,pdm_inspection_records t3
        where t2.audit_rec_id  = t3.audit_rec_id
        and t2.data_dt  &gt;= #{startDate} and t2.data_dt &lt;= #{endDate}
        GROUP BY t3.op_org
        ) t on t1.organ_name = t.op_org
        UNION ALL
        select '总计' company,
        IFNULL(sum(if((t3.audit_lvl_cd='2025001' and t2.break_rules_nature_cd = '2020002'),1,0)),0) province_num1,
        IFNULL(sum(if((t3.audit_lvl_cd='2025001' and t2.break_rules_nature_cd = '2020001'),1,0)),0) province_num2,
        IFNULL(sum(if((t3.audit_lvl_cd='2025002' and t2.break_rules_nature_cd = '2020002'),1,0)),0) city_num1,
        IFNULL(sum(if((t3.audit_lvl_cd='2025002' and t2.break_rules_nature_cd = '2020001'),1,0)),0) city_num2,
        IFNULL(sum(if((t3.audit_lvl_cd='2025003' and t2.break_rules_nature_cd = '2020002'),1,0)),0) county_num1,
        IFNULL(sum(if((t3.audit_lvl_cd='2025003' and t2.break_rules_nature_cd = '2020001'),1,0)),0) county_num2,
        0 check_num
        from pdm_violation_records t2,pdm_inspection_records t3
        where t2.audit_rec_id  = t3.audit_rec_id
        and t2.data_dt  &gt;= #{startDate} and t2.data_dt &lt;= #{endDate}
    </select>

</mapper>
