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

    <resultMap type="PdmReportPlanNextDay" id="PdmReportPlanNextDayResult">
        <result property="id"    column="id"    />
        <result property="company"    column="company"    />
        <result property="reportDate"    column="report_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="temporaryPlanSum"    column="temporary_plan_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="selectPdmReportPlanNextDayVo">
        select id, company, report_date, work_total, work_type1_sum, work_type2_sum, work_type3_sum, work_type4_sum, work_type5_sum, temporary_plan_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_day
    </sql>

    <select id="selectPdmReportPlanNextDayList" parameterType="PdmReportPlanNextDay" resultMap="PdmReportPlanNextDayResult">
        select * from (
        <include refid="selectPdmReportPlanNextDayVo"/>
        <where>
            <if test="company != null  and company != ''"> and company = #{company}</if>
            <if test="reportDate != null  and reportDate != ''"> and report_date = #{reportDate}</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="temporaryPlanSum != null "> and temporary_plan_sum = #{temporaryPlanSum}</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,
        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(temporary_plan_sum,0)) temporary_plan_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_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="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="temporaryPlanSum != null "> and temporary_plan_sum = #{temporaryPlanSum}</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="selectPdmReportPlanNextDayById" parameterType="Long" resultMap="PdmReportPlanNextDayResult">
        <include refid="selectPdmReportPlanNextDayVo"/>
        where id = #{id}
    </select>

    <insert id="insertPdmReportPlanNextDay" parameterType="PdmReportPlanNextDay" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_report_plan_next_day
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">company,</if>
            <if test="reportDate != null">report_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="temporaryPlanSum != null">temporary_plan_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="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="temporaryPlanSum != null">#{temporaryPlanSum},</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="updatePdmReportPlanNextDay" parameterType="PdmReportPlanNextDay">
        update pdm_report_plan_next_day
        <trim prefix="SET" suffixOverrides=",">
            <if test="company != null">company = #{company},</if>
            <if test="reportDate != null">report_date = #{reportDate},</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="temporaryPlanSum != null">temporary_plan_sum = #{temporaryPlanSum},</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="deletePdmReportPlanNextDayById" parameterType="Long">
        delete from pdm_report_plan_next_day where id = #{id}
    </delete>

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

    <select id="selectReportPlanNext" parameterType="PdmReportPlanNextDay" resultMap="PdmReportPlanNextDayResult">
        select t1.organ_abbr company,
               IFNULL(tt1.work_total,0) work_total,
               IFNULL(tt1.work_type1_sum,0) work_type1_sum,
               IFNULL(tt1.work_type2_sum,0) work_type2_sum,
               IFNULL(tt1.work_type3_sum,0) work_type3_sum,
               IFNULL(tt1.work_type4_sum,0) work_type4_sum,
               IFNULL(tt1.work_type5_sum,0) work_type5_sum,
               IFNULL(tt1.temporary_plan_sum,0) temporary_plan_sum,
               IFNULL(tt1.ele_plan_sum,0) ele_plan_sum,
               IFNULL(tt1.plan_leve3,0) plan_leve3,
               IFNULL(tt1.plan_leve4,0) plan_leve4,
               IFNULL(tt1.plan_leve5,0) plan_leve5,
               IFNULL(tt2.check_detail,'') check_detail
        from pdm_work_organ t1
                 LEFT JOIN(
            select
                sum(1) work_total,
                sum(if(op_typ_cd = '2002012',1,0)) work_type1_sum,
                sum(if(op_typ_cd = '2002005',1,0)) work_type2_sum,
                sum(if(op_typ_cd = '2002004',1,0)) work_type3_sum,
                sum(if(op_typ_cd = '2002001',1,0)) work_type4_sum,
                sum(if(op_typ_cd not in ('2002001','2002004','2002005','2002012'),1,0)) work_type5_sum,
                sum(if(plan_typ_cd = '2028002',1,0)) temporary_plan_sum,
                sum(if(is_energized_op_cd = '2006001',1,0)) ele_plan_sum,
                sum(if(op_risk_lvl_cd = '2007003',1,0)) plan_leve3,
                sum(if(op_risk_lvl_cd = '2007004',1,0)) plan_leve4,
                sum(if(op_risk_lvl_cd = '2007005',1,0)) plan_leve5,
                submit_org_nm
            from
                pdm_work_plan
            where  date_format(plan_start_wk_tm,'%Y%m%d') &gt;= #{startDate}
              and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
            GROUP BY submit_org_nm
        )  tt1 on t1.organ_name = tt1.submit_org_nm
                 LEFT JOIN(
                 select GROUP_CONCAT(check_detail) check_detail,submit_org_nm from(
                        select CONCAT(subs_or_line_nm,'(',count(1),'个作业面)') check_detail,
                               submit_org_nm from
                            pdm_work_plan where volt_lvl_cd=  '2001008' and op_typ_cd != '2002001'
                         and date_format(plan_start_wk_tm,'%Y%m%d') &gt;= #{startDate}
                         and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}
                        GROUP BY submit_org_nm,subs_or_line_nm
                        HAVING count(1) >1
                   ) line GROUP BY submit_org_nm
        ) tt2 on t1.organ_name = tt2.submit_org_nm

        UNION ALL
        select '总计' company,
               IFNULL(sum(1),0) work_total,
               IFNULL(sum(if(op_typ_cd = '2002012',1,0)),0) work_type1_sum,
               IFNULL(sum(if(op_typ_cd = '2002005',1,0)),0) work_type2_sum,
               IFNULL(sum(if(op_typ_cd = '2002004',1,0)),0) work_type3_sum,
               IFNULL(sum(if(op_typ_cd = '2002001',1,0)),0) work_type4_sum,
               IFNULL(sum(if(op_typ_cd not in ('2002001','2002004','2002005','2002012'),1,0)),0) work_type5_sum,
               IFNULL(sum(if(plan_typ_cd = '2028002',1,0)),0) temporary_plan_sum,
               IFNULL(sum(if(is_energized_op_cd = '2006001',1,0)),0) ele_plan_sum,
               IFNULL(sum(if(op_risk_lvl_cd = '2007003',1,0)),0) plan_leve3,
               IFNULL(sum(if(op_risk_lvl_cd = '2007004',1,0)),0) plan_leve4,
               IFNULL(sum(if(op_risk_lvl_cd = '2007005',1,0)),0) plan_leve5,
               '' check_detail
        from
        pdm_work_plan
        where  date_format(plan_start_wk_tm,'%Y%m%d') &gt;= #{startDate}
        and date_format(plan_start_wk_tm,'%Y%m%d') &lt;= #{endDate}

    </select>
</mapper>
