<?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.goldenwater.slaj.hidd.mapper.BisOrgMonRepPeriMapper">
    <resultMap type="com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri" id="BisOrgMonRepPeriResult">
        <result property="guid" column="guid"/>
        <result property="repName" column="rep_name"/>
        <result property="repAct" column="rep_act"/>
        <result property="url" column="url"/>
        <result property="repType" column="rep_type"/>
        <result property="repTime" column="rep_time"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="repOrgGuid" column="rep_org_guid"/>
        <result property="note" column="note"/>
        <result property="collTime" column="coll_time"/>
        <result property="updTime" column="upd_time"/>
        <result property="recPers" column="rec_pers"/>
        <result property="lockStatus" column="lock_status"/>
    </resultMap>
    <sql id="selectBisOrgMonRepPeriVo">
        select guid, rep_name, rep_act, url, rep_type, rep_time, start_date, end_date,
               rep_org_guid, note, coll_time, upd_time, rec_pers, lock_status
        from bis_org_mon_rep_peri
    </sql>
    <select id="selectBisOrgMonRepPeriByGuid" parameterType="String" resultMap="BisOrgMonRepPeriResult">
        <include refid="selectBisOrgMonRepPeriVo"/>
        where guid = #{guid}
    </select>
    <select id="selectBisOrgMonRepPeriList" parameterType="com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri" resultMap="BisOrgMonRepPeriResult">
        <include refid="selectBisOrgMonRepPeriVo"/>
        <where>
            <if test="repName != null and repName != ''">AND rep_name like '%' || #{repName} || '%'</if>
            <if test="repType != null and repType != ''">AND rep_type = #{repType}</if>
            <if test="repOrgGuid != null and repOrgGuid != ''">AND rep_org_guid = #{repOrgGuid}</if>
        </where>
    </select>
    <insert id="insertBisOrgMonRepPeri" parameterType="com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri">
        insert into bis_org_mon_rep_peri (guid, rep_name, rep_act, url, rep_type, rep_time, start_date, end_date, rep_org_guid, note, coll_time, upd_time, rec_pers, lock_status)
        values (#{guid}, #{repName}, #{repAct}, #{url}, #{repType}, #{repTime}, #{startDate}, #{endDate}, #{repOrgGuid}, #{note}, #{collTime}, #{updTime}, #{recPers}, #{lockStatus})
    </insert>
    <update id="updateBisOrgMonRepPeri" parameterType="com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri">
        update bis_org_mon_rep_peri
        <set>
            <if test="repName != null">rep_name = #{repName},</if>
            <if test="repAct != null">rep_act = #{repAct},</if>
            <if test="url != null">url = #{url},</if>
            <if test="repType != null">rep_type = #{repType},</if>
            <if test="repTime != null">rep_time = #{repTime},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="repOrgGuid != null">rep_org_guid = #{repOrgGuid},</if>
            <if test="note != null">note = #{note},</if>
            <if test="collTime != null">coll_time = #{collTime},</if>
            <if test="updTime != null">upd_time = #{updTime},</if>
            <if test="recPers != null">rec_pers = #{recPers},</if>
            <if test="lockStatus != null">lock_status = #{lockStatus},</if>
        </set>
        where guid = #{guid}
    </update>
    <delete id="deleteBisOrgMonRepPeriByGuid" parameterType="String">
        delete from bis_org_mon_rep_peri where guid = #{guid}
    </delete>
    <select id="selectMonthTotalList" parameterType="java.util.Map" resultType="java.util.Map">
        select c.hidd_clas as repType,
               coalesce(sum(case when (d.guid is not null and (d.requ_comp_date is null or (d.requ_comp_date is not null and now() > d.requ_comp_date and e.accep_leg_pers is null))) then 1 else 0 end), 0) as overUnImplRect,
               coalesce(sum(case when c.hidd_grad = '1' then 1 else 0 end), 0) as impHiddCount,
               coalesce(sum(case when c.hidd_stat = '1' then 1 else 0 end), 0) as unImplRect,
               coalesce(sum(case when c.hidd_stat = '2' then 1 else 0 end), 0) as implRect,
               coalesce(sum(case when c.hidd_stat = '3' then 1 else 0 end), 0) as inRect,
               coalesce(sum(case when c.hidd_stat = '4' then 1 else 0 end), 0) as compRect
        from bis_org_mon_rep_peri a
        left join bis_hidd_rec_rep b on b.rep_guid = a.guid
        left join obj_hidd c on c.guid = b.hidd_guid
        left join bis_hidd_rect_impl d on d.hidd_guid = b.hidd_guid
        left join bis_hidd_rect_acce e on e.hidd_guid = b.hidd_guid
        <where>
            and a.rep_type = '0'
            and a.rep_act = '1'
            <if test="repTime != null and repTime != ''">and a.rep_time = #{repTime}</if>
        </where>
        group by c.hidd_clas
        order by c.hidd_clas
    </select>

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

    <!-- 月报统计查询（根据月报期间GUID） -->
    <select id="selectMonthReportStats" parameterType="String" resultType="java.util.Map">
        select 
            -- 重大隐患数
            coalesce(sum(case when c.hidd_grad = '2' then 1 else 0 end), 0) as impHiddCount,
            -- 一般隐患数
            coalesce(sum(case when c.hidd_grad = '1' then 1 else 0 end), 0) as normalHiddCount,
            -- 未落实整改
            coalesce(sum(case when c.hidd_stat = '0' then 1 else 0 end), 0) as unSolvedCount,
            -- 整改中
            coalesce(sum(case when c.hidd_stat = '1' then 1 else 0 end), 0) as solvingCount,
            -- 完成整改
            coalesce(sum(case when c.hidd_stat = '2' then 1 else 0 end), 0) as hasSolvedCount,
            -- 逾期未整改（要求完成日期小于当前日期且未完成整改）
            coalesce(sum(case when d.requ_comp_date is not null 
                and d.requ_comp_date &lt; CURRENT_DATE 
                and c.hidd_stat != '2' then 1 else 0 end), 0) as overUnImplRect,
            -- 整改率
            case when count(c.guid) > 0 
                then round(sum(case when c.hidd_stat = '2' then 1 else 0 end) * 100.0 / count(c.guid), 2)
                else 0 end as solveRate
        from bis_org_mon_rep_peri a
        left join bis_hidd_rec_rep b on b.rep_guid = a.guid
        left join obj_hidd c on c.guid = b.hidd_guid
        left join bis_hidd_rect_impl d on d.hidd_guid = b.hidd_guid
        where a.guid = #{repGuid}
    </select>

    <!-- 月报列表统计（根据月份） -->
    <select id="selectMonthListStats" parameterType="java.util.Map" resultType="java.util.Map">
        select 
            a.guid as guid,
            a.rep_org_guid as repOrgGuid,
            a.rep_name as repName,
            a.rep_act as repAct,
            a.rep_time as repTime,
            -- 重大隐患数
            coalesce(stats.imp_hidd_count, 0) as impHiddCount,
            -- 逾期未整改
            coalesce(stats.over_un_impl_rect, 0) as overUnImplRect,
            -- 未落实整改
            coalesce(stats.un_solved_count, 0) as unSolvedCount,
            -- 整改中
            coalesce(stats.solving_count, 0) as solvingCount,
            -- 完成整改
            coalesce(stats.has_solved_count, 0) as hasSolvedCount,
            -- 整改率
            coalesce(stats.solve_rate, 0) as solveRate
        from bis_org_mon_rep_peri a
        left join (
            select 
                b.rep_guid,
                sum(case when c.hidd_grad = '2' then 1 else 0 end) as imp_hidd_count,
                sum(case when d.requ_comp_date is not null and d.requ_comp_date &lt; CURRENT_DATE and c.hidd_stat != '2' then 1 else 0 end) as over_un_impl_rect,
                sum(case when c.hidd_stat = '0' then 1 else 0 end) as un_solved_count,
                sum(case when c.hidd_stat = '1' then 1 else 0 end) as solving_count,
                sum(case when c.hidd_stat = '2' then 1 else 0 end) as has_solved_count,
                case when count(c.guid) > 0 then round(sum(case when c.hidd_stat = '2' then 1 else 0 end) * 100.0 / count(c.guid), 2) else 0 end as solve_rate
            from bis_hidd_rec_rep b
            left join obj_hidd c on c.guid = b.hidd_guid
            left join bis_hidd_rect_impl d on d.hidd_guid = b.hidd_guid
            group by b.rep_guid
        ) stats on stats.rep_guid = a.guid
        <where>
            <if test="repTime != null and repTime != ''">AND a.rep_time = #{repTime}</if>
            <if test="repOrgGuid != null and repOrgGuid != ''">AND a.rep_org_guid = #{repOrgGuid}</if>
        </where>
        order by a.coll_time desc
    </select>
</mapper>
