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

    <resultMap type="PdmReportAppointmentWeek" id="PdmReportAppointmentWeekResult">
        <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="checkNum3"    column="check_num3"    />
        <result property="checkNum4"    column="check_num4"    />
        <result property="checkNum5"    column="check_num5"    />
        <result property="bossSum"    column="boss_sum"    />
        <result property="manageSum"    column="manage_sum"    />
        <result property="problemSum"    column="problem_sum"    />
        <result property="absentSum"    column="absent_sum"    />
        <result property="cityCheckSum"    column="city_check_sum"    />
        <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="selectPdmReportAppointmentWeekVo">
        select id, company, report_date, report_end_date, check_num3, check_num4, check_num5, boss_sum, manage_sum, problem_sum, absent_sum, city_check_sum, create_by, create_time, update_by, update_time from pdm_report_appointment_week
    </sql>

    <select id="selectPdmReportAppointmentWeekList" parameterType="PdmReportAppointmentWeek" resultMap="PdmReportAppointmentWeekResult">
        select * from (
        <include refid="selectPdmReportAppointmentWeekVo"/>
        <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="checkNum3 != null "> and check_num3 = #{checkNum3}</if>
            <if test="checkNum4 != null "> and check_num4 = #{checkNum4}</if>
            <if test="checkNum5 != null "> and check_num5 = #{checkNum5}</if>
            <if test="bossSum != null "> and boss_sum = #{bossSum}</if>
            <if test="manageSum != null "> and manage_sum = #{manageSum}</if>
            <if test="problemSum != null "> and problem_sum = #{problemSum}</if>
            <if test="absentSum != null "> and absent_sum = #{absentSum}</if>
            <if test="cityCheckSum != null "> and city_check_sum = #{cityCheckSum}</if>
        </where>
        UNION
        SELECT
        sum(id) as id,
        '总计' as company,
        report_date,
        report_end_date,
        sum(ifnull(check_num3,0)) check_num3,
        sum(ifnull(check_num4,0)) check_num4,
        sum(ifnull(check_num5,0)) check_num5,
        sum(ifnull(boss_sum,0)) boss_sum,
        sum(ifnull(manage_sum,0)) manage_sum,
        sum(ifnull(problem_sum,0)) problem_sum,
        sum(ifnull(absent_sum,0)) absent_sum,
        sum(ifnull(city_check_sum,0)) city_check_sum,
        null create_by ,
        null create_time,
        null update_by,
        null update_time FROM
        pdm_report_appointment_week
        <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="checkNum3 != null "> and check_num3 = #{checkNum3}</if>
            <if test="checkNum4 != null "> and check_num4 = #{checkNum4}</if>
            <if test="checkNum5 != null "> and check_num5 = #{checkNum5}</if>
            <if test="bossSum != null "> and boss_sum = #{bossSum}</if>
            <if test="manageSum != null "> and manage_sum = #{manageSum}</if>
            <if test="problemSum != null "> and problem_sum = #{problemSum}</if>
            <if test="absentSum != null "> and absent_sum = #{absentSum}</if>
            <if test="cityCheckSum != null "> and city_check_sum = #{cityCheckSum}</if>
        </where>
        GROUP BY report_date) a order by id asc
    </select>

    <select id="selectPdmReportAppointmentWeekById" parameterType="Long" resultMap="PdmReportAppointmentWeekResult">
        <include refid="selectPdmReportAppointmentWeekVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportAppointmentWeek" parameterType="PdmReportAppointmentWeek" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_appointment_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="checkNum3 != null">check_num3,</if>
            <if test="checkNum4 != null">check_num4,</if>
            <if test="checkNum5 != null">check_num5,</if>
            <if test="bossSum != null">boss_sum,</if>
            <if test="manageSum != null">manage_sum,</if>
            <if test="problemSum != null">problem_sum,</if>
            <if test="absentSum != null">absent_sum,</if>
            <if test="cityCheckSum != null">city_check_sum,</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="checkNum3 != null">#{checkNum3},</if>
            <if test="checkNum4 != null">#{checkNum4},</if>
            <if test="checkNum5 != null">#{checkNum5},</if>
            <if test="bossSum != null">#{bossSum},</if>
            <if test="manageSum != null">#{manageSum},</if>
            <if test="problemSum != null">#{problemSum},</if>
            <if test="absentSum != null">#{absentSum},</if>
            <if test="cityCheckSum != null">#{cityCheckSum},</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="updatePdmReportAppointmentWeek" parameterType="PdmReportAppointmentWeek">
        update pdm_report_appointment_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="checkNum3 != null">check_num3 = #{checkNum3},</if>
            <if test="checkNum4 != null">check_num4 = #{checkNum4},</if>
            <if test="checkNum5 != null">check_num5 = #{checkNum5},</if>
            <if test="bossSum != null">boss_sum = #{bossSum},</if>
            <if test="manageSum != null">manage_sum = #{manageSum},</if>
            <if test="problemSum != null">problem_sum = #{problemSum},</if>
            <if test="absentSum != null">absent_sum = #{absentSum},</if>
            <if test="cityCheckSum != null">city_check_sum = #{cityCheckSum},</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="deletePdmReportAppointmentWeekById" parameterType="Long">
        delete from pdm_report_appointment_week where id = #{id}
    </delete>

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