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

    <resultMap type="PdmReportPlanNextWeek" id="PdmReportPlanNextWeekResult">
        <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="workTotal"    column="work_total"    />
        <result property="workType1Sum"    column="work_type1_sum"    />
        <result property="workType2Sum"    column="work_type2_sum"    />
        <result property="workType3Sum"    column="work_type3_sum"    />
        <result property="workType4Sum"    column="work_type4_sum"    />
        <result property="workType5Sum"    column="work_type5_sum"    />
        <result property="elePlanSum"    column="ele_plan_sum"    />
        <result property="planLeve3"    column="plan_leve3"    />
        <result property="planLeve4"    column="plan_leve4"    />
        <result property="planLeve5"    column="plan_leve5"    />
        <result property="checkDetail"    column="check_detail"    />
        <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="selectPdmReportPlanNextWeekVo">
        select id, company, report_date, report_end_date, work_total, work_type1_sum, work_type2_sum, work_type3_sum, work_type4_sum, work_type5_sum, ele_plan_sum, plan_leve3, plan_leve4, plan_leve5, check_detail, create_by, create_time, update_by, update_time from pdm_report_plan_next_week
    </sql>

    <select id="selectPdmReportPlanNextWeekList" parameterType="PdmReportPlanNextWeek" resultMap="PdmReportPlanNextWeekResult">
        select * from (
       <include refid="selectPdmReportPlanNextWeekVo"/>
        <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="workTotal != null "> and work_total = #{workTotal}</if>
            <if test="workType1Sum != null "> and work_type1_sum = #{workType1Sum}</if>
            <if test="workType2Sum != null "> and work_type2_sum = #{workType2Sum}</if>
            <if test="workType3Sum != null "> and work_type3_sum = #{workType3Sum}</if>
            <if test="workType4Sum != null "> and work_type4_sum = #{workType4Sum}</if>
            <if test="workType5Sum != null "> and work_type5_sum = #{workType5Sum}</if>
            <if test="elePlanSum != null "> and ele_plan_sum = #{elePlanSum}</if>
            <if test="planLeve3 != null  and planLeve3 != ''"> and plan_leve3 = #{planLeve3}</if>
            <if test="planLeve4 != null  and planLeve4 != ''"> and plan_leve4 = #{planLeve4}</if>
            <if test="planLeve5 != null  and planLeve5 != ''"> and plan_leve5 = #{planLeve5}</if>
            <if test="checkDetail != null  and checkDetail != ''"> and check_detail = #{checkDetail}</if>
        </where>
        UNION
        SELECT
        sum(id) as id,
        '总计' as company,
        report_date,
        report_end_date,
        sum(ifnull(work_total,0)) work_total,
        sum(ifnull(work_type1_sum,0)) work_type1_sum,
        sum(ifnull(work_type2_sum,0)) work_type2_sum,
        sum(ifnull(work_type3_sum,0)) work_type3_sum,
        sum(ifnull(work_type4_sum,0)) work_type4_sum,
        sum(ifnull(work_type5_sum,0)) work_type5_sum,
        sum(ifnull(ele_plan_sum,0)) ele_plan_sum,
        sum(ifnull(plan_leve3,0)) plan_leve3,
        sum(ifnull(plan_leve4,0)) plan_leve4,
        sum(ifnull(plan_leve5,0)) plan_leve5,
        null check_detail,
        null create_by ,
        null create_time,
        null update_by,
        null update_time FROM
        pdm_report_plan_next_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="workTotal != null "> and work_total = #{workTotal}</if>
            <if test="workType1Sum != null "> and work_type1_sum = #{workType1Sum}</if>
            <if test="workType2Sum != null "> and work_type2_sum = #{workType2Sum}</if>
            <if test="workType3Sum != null "> and work_type3_sum = #{workType3Sum}</if>
            <if test="workType4Sum != null "> and work_type4_sum = #{workType4Sum}</if>
            <if test="workType5Sum != null "> and work_type5_sum = #{workType5Sum}</if>
            <if test="elePlanSum != null "> and ele_plan_sum = #{elePlanSum}</if>
            <if test="planLeve3 != null  and planLeve3 != ''"> and plan_leve3 = #{planLeve3}</if>
            <if test="planLeve4 != null  and planLeve4 != ''"> and plan_leve4 = #{planLeve4}</if>
            <if test="planLeve5 != null  and planLeve5 != ''"> and plan_leve5 = #{planLeve5}</if>
            <if test="checkDetail != null  and checkDetail != ''"> and check_detail = #{checkDetail}</if>
        </where>
        GROUP BY report_date) a order by id asc

    </select>

    <select id="selectPdmReportPlanNextWeekById" parameterType="Long" resultMap="PdmReportPlanNextWeekResult">
        <include refid="selectPdmReportPlanNextWeekVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportPlanNextWeek" parameterType="PdmReportPlanNextWeek" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_plan_next_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="workTotal != null">work_total,</if>
            <if test="workType1Sum != null">work_type1_sum,</if>
            <if test="workType2Sum != null">work_type2_sum,</if>
            <if test="workType3Sum != null">work_type3_sum,</if>
            <if test="workType4Sum != null">work_type4_sum,</if>
            <if test="workType5Sum != null">work_type5_sum,</if>
            <if test="elePlanSum != null">ele_plan_sum,</if>
            <if test="planLeve3 != null">plan_leve3,</if>
            <if test="planLeve4 != null">plan_leve4,</if>
            <if test="planLeve5 != null">plan_leve5,</if>
            <if test="checkDetail != null">check_detail,</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="workTotal != null">#{workTotal},</if>
            <if test="workType1Sum != null">#{workType1Sum},</if>
            <if test="workType2Sum != null">#{workType2Sum},</if>
            <if test="workType3Sum != null">#{workType3Sum},</if>
            <if test="workType4Sum != null">#{workType4Sum},</if>
            <if test="workType5Sum != null">#{workType5Sum},</if>
            <if test="elePlanSum != null">#{elePlanSum},</if>
            <if test="planLeve3 != null">#{planLeve3},</if>
            <if test="planLeve4 != null">#{planLeve4},</if>
            <if test="planLeve5 != null">#{planLeve5},</if>
            <if test="checkDetail != null">#{checkDetail},</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="updatePdmReportPlanNextWeek" parameterType="PdmReportPlanNextWeek">
        update pdm_report_plan_next_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="workTotal != null">work_total = #{workTotal},</if>
            <if test="workType1Sum != null">work_type1_sum = #{workType1Sum},</if>
            <if test="workType2Sum != null">work_type2_sum = #{workType2Sum},</if>
            <if test="workType3Sum != null">work_type3_sum = #{workType3Sum},</if>
            <if test="workType4Sum != null">work_type4_sum = #{workType4Sum},</if>
            <if test="workType5Sum != null">work_type5_sum = #{workType5Sum},</if>
            <if test="elePlanSum != null">ele_plan_sum = #{elePlanSum},</if>
            <if test="planLeve3 != null">plan_leve3 = #{planLeve3},</if>
            <if test="planLeve4 != null">plan_leve4 = #{planLeve4},</if>
            <if test="planLeve5 != null">plan_leve5 = #{planLeve5},</if>
            <if test="checkDetail != null">check_detail = #{checkDetail},</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="deletePdmReportPlanNextWeekById" parameterType="Long">
        delete from pdm_report_plan_next_week where id = #{id}
    </delete>

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