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

    <resultMap type="PdmReportPlanDay" id="PdmReportPlanDayResult">
        <result property="id"    column="id"    />
        <result property="company"    column="company"    />
        <result property="reportDate"    column="report_date"    />
        <result property="total"    column="total"    />
        <result property="weekSum"    column="week_sum"    />
        <result property="weekCancelSum"    column="week_cancel_sum"    />
        <result property="weekRatio"    column="week_ratio"    />
        <result property="temporarySum"    column="temporary_sum"    />
        <result property="temporaryCancelSum"    column="temporary_cancel_sum"    />
        <result property="temporaryRatio"    column="temporary_ratio"    />
        <result property="temporaryPlanRatio"    column="temporary_plan_ratio"    />
        <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="selectPdmReportPlanDayVo">
        select id, company, report_date, total, week_sum, week_cancel_sum, week_ratio, temporary_sum, temporary_cancel_sum, temporary_ratio, temporary_plan_ratio, create_by, create_time, update_by, update_time from pdm_report_plan_day
    </sql>

    <select id="selectPdmReportPlanDayList" parameterType="PdmReportPlanDay" resultMap="PdmReportPlanDayResult">
        select * from (
        <include refid="selectPdmReportPlanDayVo"/>
        <where>
            <if test="company != null  and company != ''"> and company = #{company}</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</if>
            <if test="total != null "> and total = #{total}</if>
            <if test="weekSum != null "> and week_sum = #{weekSum}</if>
            <if test="weekCancelSum != null "> and week_cancel_sum = #{weekCancelSum}</if>
            <if test="weekRatio != null  and weekRatio != ''"> and week_ratio = #{weekRatio}</if>
            <if test="temporarySum != null "> and temporary_sum = #{temporarySum}</if>
            <if test="temporaryCancelSum != null "> and temporary_cancel_sum = #{temporaryCancelSum}</if>
            <if test="temporaryRatio != null  and temporaryRatio != ''"> and temporary_ratio = #{temporaryRatio}</if>
            <if test="temporaryPlanRatio != null  and temporaryPlanRatio != ''"> and temporary_plan_ratio = #{temporaryPlanRatio}</if>
        </where>
        UNION
        SELECT
        sum(id) as id,
        '总计' as company,
        report_date,
        sum(ifnull(total,0)) total,
        sum(ifnull(week_sum,0)) week_sum,
        sum(ifnull(week_cancel_sum,0)) week_cancel_sum,
        CONCAT(round((sum(ifnull(week_sum,0))-sum(ifnull(week_cancel_sum,0)))/sum(ifnull(week_sum,0))*100,2),'%')  week_ratio,
        sum(ifnull(temporary_sum,0)) temporary_sum,
        sum(ifnull(temporary_cancel_sum,0)) temporary_cancel_sum,
        CONCAT(round((sum(ifnull(temporary_sum,0))-sum(ifnull(temporary_cancel_sum,0)))/sum(ifnull(temporary_sum,0))*100,2),'%')  temporary_ratio,
        null temporary_plan_ratio,
        null create_by ,
        null create_time,
        null update_by,
        null update_time FROM
        pdm_report_plan_day
        <where>
            <if test="company != null  and company != ''"> and company = #{company}</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</if>
            <if test="total != null "> and total = #{total}</if>
            <if test="weekSum != null "> and week_sum = #{weekSum}</if>
            <if test="weekCancelSum != null "> and week_cancel_sum = #{weekCancelSum}</if>
            <if test="weekRatio != null  and weekRatio != ''"> and week_ratio = #{weekRatio}</if>
            <if test="temporarySum != null "> and temporary_sum = #{temporarySum}</if>
            <if test="temporaryCancelSum != null "> and temporary_cancel_sum = #{temporaryCancelSum}</if>
            <if test="temporaryRatio != null  and temporaryRatio != ''"> and temporary_ratio = #{temporaryRatio}</if>
            <if test="temporaryPlanRatio != null  and temporaryPlanRatio != ''"> and temporary_plan_ratio = #{temporaryPlanRatio}</if>
        </where>
        GROUP BY report_date) a order by id asc
    </select>

    <select id="selectPdmReportPlanDayById" parameterType="Long" resultMap="PdmReportPlanDayResult">
        <include refid="selectPdmReportPlanDayVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportPlanDay" parameterType="PdmReportPlanDay" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_plan_day
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">company,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="total != null">total,</if>
            <if test="weekSum != null">week_sum,</if>
            <if test="weekCancelSum != null">week_cancel_sum,</if>
            <if test="weekRatio != null">week_ratio,</if>
            <if test="temporarySum != null">temporary_sum,</if>
            <if test="temporaryCancelSum != null">temporary_cancel_sum,</if>
            <if test="temporaryRatio != null">temporary_ratio,</if>
            <if test="temporaryPlanRatio != null">temporary_plan_ratio,</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="total != null">#{total},</if>
            <if test="weekSum != null">#{weekSum},</if>
            <if test="weekCancelSum != null">#{weekCancelSum},</if>
            <if test="weekRatio != null">#{weekRatio},</if>
            <if test="temporarySum != null">#{temporarySum},</if>
            <if test="temporaryCancelSum != null">#{temporaryCancelSum},</if>
            <if test="temporaryRatio != null">#{temporaryRatio},</if>
            <if test="temporaryPlanRatio != null">#{temporaryPlanRatio},</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="updatePdmReportPlanDay" parameterType="PdmReportPlanDay">
        update pdm_report_plan_day
        <trim prefix="SET" suffixOverrides=",">
            <if test="company != null">company = #{company},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="total != null">total = #{total},</if>
            <if test="weekSum != null">week_sum = #{weekSum},</if>
            <if test="weekCancelSum != null">week_cancel_sum = #{weekCancelSum},</if>
            <if test="weekRatio != null">week_ratio = #{weekRatio},</if>
            <if test="temporarySum != null">temporary_sum = #{temporarySum},</if>
            <if test="temporaryCancelSum != null">temporary_cancel_sum = #{temporaryCancelSum},</if>
            <if test="temporaryRatio != null">temporary_ratio = #{temporaryRatio},</if>
            <if test="temporaryPlanRatio != null">temporary_plan_ratio = #{temporaryPlanRatio},</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="deletePdmReportPlanDayById" parameterType="Long">
        delete from pdm_report_plan_day where id = #{id}
    </delete>

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