<?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.TBusiHiddCountMapper">
    <resultMap type="com.goldenwater.slaj.hidd.domain.TBusiHiddCount" id="TBusiHiddCountResult">
        <result property="guid" column="guid"/>
        <result property="orgGuid" column="org_guid"/>
        <result property="createTime" column="create_time"/>
        <result property="pcl" column="pcl"/>
        <result property="oengOrg" column="oeng_org"/>
        <result property="hiddOrg" column="hidd_org"/>
        <result property="engNum" column="eng_num"/>
        <result property="hiddEng" column="hidd_eng"/>
        <result property="hiddZgl" column="hidd_zgl"/>
        <result property="ybZgl" column="yb_zgl"/>
        <result property="ybHidd" column="yb_hidd"/>
        <result property="zgYbHidd" column="zg_yb_hidd"/>
        <result property="zdZgl" column="zd_zgl"/>
        <result property="zdHidd" column="zd_hidd"/>
        <result property="zgZdHidd" column="zg_zd_hidd"/>
    </resultMap>
    
    <!-- VO类ResultMap（包含计算字段） -->
    <resultMap type="com.goldenwater.slaj.hidd.domain.TBusiHiddCountVo" id="TBusiHiddCountVoResult">
        <result property="guid" column="guid"/>
        <result property="orgGuid" column="org_guid"/>
        <result property="createTime" column="create_time"/>
        <result property="pcl" column="pcl"/>
        <result property="oengOrg" column="oeng_org"/>
        <result property="hiddOrg" column="hidd_org"/>
        <result property="engNum" column="eng_num"/>
        <result property="hiddEng" column="hidd_eng"/>
        <result property="hiddZgl" column="hidd_zgl"/>
        <result property="ybZgl" column="yb_zgl"/>
        <result property="ybHidd" column="yb_hidd"/>
        <result property="zgYbHidd" column="zg_yb_hidd"/>
        <result property="zdZgl" column="zd_zgl"/>
        <result property="zdHidd" column="zd_hidd"/>
        <result property="zgZdHidd" column="zg_zd_hidd"/>
        <!-- 计算字段 -->
        <result property="orgName" column="org_name"/>
        <result property="totalHidd" column="total_hidd"/>
        <result property="solvedHidd" column="solved_hidd"/>
        <result property="solvingHidd" column="solving_hidd"/>
        <result property="unsolvedHidd" column="unsolved_hidd"/>
        <result property="solveRate" column="solve_rate"/>
        <!-- 逾期统计字段 -->
        <result property="zdSolvingHidd" column="zd_solving_hidd"/>
        <result property="zdUnsolvedHidd" column="zd_unsolved_hidd"/>
        <result property="zdOverTimeHidd" column="zd_overtime_hidd"/>
        <result property="ybSolvingHidd" column="yb_solving_hidd"/>
        <result property="ybUnsolvedHidd" column="yb_unsolved_hidd"/>
        <result property="ybOverTimeHidd" column="yb_overtime_hidd"/>
        <result property="overTimeHidd" column="overtime_hidd"/>
    </resultMap>
    <sql id="selectTBusiHiddCountVo">
        select guid, org_guid, create_time, pcl, oeng_org, hidd_org, eng_num, hidd_eng,
               hidd_zgl, yb_zgl, yb_hidd, zg_yb_hidd, zd_zgl, zd_hidd, zg_zd_hidd
        from t_busi_hidd_count
    </sql>
    <select id="selectTBusiHiddCountById" parameterType="String" resultMap="TBusiHiddCountResult">
        <include refid="selectTBusiHiddCountVo"/>
        where guid = #{guid}
    </select>
    <select id="selectTBusiHiddCountList" parameterType="com.goldenwater.slaj.hidd.domain.TBusiHiddCount" resultMap="TBusiHiddCountResult">
        <include refid="selectTBusiHiddCountVo"/>
        <where>
            <if test="orgGuid != null and orgGuid != ''">AND org_guid = #{orgGuid}</if>
            <if test="createTime != null and createTime != ''">AND create_time = #{createTime}</if>
        </where>
    </select>
    <insert id="insertTBusiHiddCount" parameterType="com.goldenwater.slaj.hidd.domain.TBusiHiddCount">
        insert into t_busi_hidd_count (guid, org_guid, create_time, pcl, oeng_org, hidd_org, eng_num, hidd_eng, hidd_zgl, yb_zgl, yb_hidd, zg_yb_hidd, zd_zgl, zd_hidd, zg_zd_hidd)
        values (#{guid}, #{orgGuid}, #{createTime}, #{pcl}, #{oengOrg}, #{hiddOrg}, #{engNum}, #{hiddEng}, #{hiddZgl}, #{ybZgl}, #{ybHidd}, #{zgYbHidd}, #{zdZgl}, #{zdHidd}, #{zgZdHidd})
    </insert>
    <update id="updateTBusiHiddCount" parameterType="com.goldenwater.slaj.hidd.domain.TBusiHiddCount">
        update t_busi_hidd_count
        <set>
            <if test="orgGuid != null">org_guid = #{orgGuid},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="pcl != null">pcl = #{pcl},</if>
            <if test="oengOrg != null">oeng_org = #{oengOrg},</if>
            <if test="hiddOrg != null">hidd_org = #{hiddOrg},</if>
            <if test="engNum != null">eng_num = #{engNum},</if>
            <if test="hiddEng != null">hidd_eng = #{hiddEng},</if>
            <if test="hiddZgl != null">hidd_zgl = #{hiddZgl},</if>
            <if test="ybZgl != null">yb_zgl = #{ybZgl},</if>
            <if test="ybHidd != null">yb_hidd = #{ybHidd},</if>
            <if test="zgYbHidd != null">zg_yb_hidd = #{zgYbHidd},</if>
            <if test="zdZgl != null">zd_zgl = #{zdZgl},</if>
            <if test="zdHidd != null">zd_hidd = #{zdHidd},</if>
            <if test="zgZdHidd != null">zg_zd_hidd = #{zgZdHidd},</if>
        </set>
        where guid = #{guid}
    </update>
    <delete id="deleteTBusiHiddCountById" parameterType="String">
        delete from t_busi_hidd_count where guid = #{guid}
    </delete>
    <delete id="deleteTBusiHiddCountByIds" parameterType="String">
        delete from t_busi_hidd_count where guid in
        <foreach item="guid" collection="array" open="(" separator="," close=")">
            #{guid}
        </foreach>
    </delete>

    <select id="selectReportRateChart" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
        select c.org_guid as orgGuid,
               org.wiun_name as orgName,
               c.pcl as rateData,
               c.create_time as repTime
        from t_busi_hidd_count c
        left join att_org_ext org on org.org_guid = c.org_guid
        <where>
            <if test="repTime != null and repTime != ''">AND c.create_time = #{repTime}</if>
        </where>
        order by c.pcl desc
    </select>

    <select id="selectRectifyRateChart" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
        select c.org_guid as orgGuid,
               org.wiun_name as orgName,
               c.hidd_zgl as rateData,
               c.create_time as repTime
        from t_busi_hidd_count c
        left join att_org_ext org on org.org_guid = c.org_guid
        <where>
            <if test="repTime != null and repTime != ''">AND c.create_time = #{repTime}</if>
        </where>
        order by c.hidd_zgl desc
    </select>

    <!-- 隐患统计列表查询（带计算字段） -->
    <select id="selectTBusiHiddCountListWithCalc" parameterType="com.goldenwater.slaj.hidd.domain.TBusiHiddCountVo" resultMap="TBusiHiddCountVoResult">
        select 
            c.guid,
            c.org_guid,
            c.create_time,
            c.pcl,
            c.oeng_org,
            c.hidd_org,
            c.eng_num,
            c.hidd_eng,
            c.hidd_zgl,
            c.yb_zgl,
            c.yb_hidd,
            c.zg_yb_hidd,
            c.zd_zgl,
            c.zd_hidd,
            c.zg_zd_hidd,
            org.wiun_name as org_name,
            -- 计算字段
            (coalesce(c.zd_hidd, 0) + coalesce(c.yb_hidd, 0)) as total_hidd,
            (coalesce(c.zg_zd_hidd, 0) + coalesce(c.zg_yb_hidd, 0)) as solved_hidd,
            -- 正在整改中 = 重大整改中 + 一般整改中
            (case when coalesce(c.zd_hidd, 0) > coalesce(c.zg_zd_hidd, 0)
                then coalesce(c.zd_hidd, 0) - coalesce(c.zg_zd_hidd, 0) else 0 end
            + case when coalesce(c.yb_hidd, 0) > coalesce(c.zg_yb_hidd, 0)
                then coalesce(c.yb_hidd, 0) - coalesce(c.zg_yb_hidd, 0) else 0 end) as solving_hidd,
            case when (coalesce(c.zd_hidd, 0) + coalesce(c.yb_hidd, 0)) > 0 
                then (coalesce(c.zd_hidd, 0) + coalesce(c.yb_hidd, 0)) - (coalesce(c.zg_zd_hidd, 0) + coalesce(c.zg_yb_hidd, 0))
                else 0 end as unsolved_hidd,
            c.hidd_zgl as solve_rate,
            -- 重大隐患-整改中
            case when coalesce(c.zd_hidd, 0) > coalesce(c.zg_zd_hidd, 0)
                then coalesce(c.zd_hidd, 0) - coalesce(c.zg_zd_hidd, 0)
                else 0 end as zd_solving_hidd,
            -- 重大隐患-未整改（预留，暂为0）
            0 as zd_unsolved_hidd,
            -- 重大隐患-逾期未整改（预留，暂为0）
            0 as zd_overtime_hidd,
            -- 一般隐患-整改中
            case when coalesce(c.yb_hidd, 0) > coalesce(c.zg_yb_hidd, 0)
                then coalesce(c.yb_hidd, 0) - coalesce(c.zg_yb_hidd, 0)
                else 0 end as yb_solving_hidd,
            -- 一般隐患-未整改（预留，暂为0）
            0 as yb_unsolved_hidd,
            -- 一般隐患-逾期未整改（预留，暂为0）
            0 as yb_overtime_hidd,
            -- 逾期未整改总数
            0 as overtime_hidd
        from t_busi_hidd_count c
        left join att_org_ext org on org.org_guid = c.org_guid
        <where>
            <if test="orgGuid != null and orgGuid != ''">AND c.org_guid = #{orgGuid}</if>
            <if test="createTime != null and createTime != ''">AND c.create_time = #{createTime}</if>
            <if test="orgName != null and orgName != ''">AND org.org_name like concat('%', #{orgName}, '%')</if>
            <if test="unitType != null and unitType != ''">
                AND c.org_guid in (
                    select org_guid from att_org_ext where wiun_code like concat(
                        case #{unitType}
                            when '1' then '%000000'
                            when '2' then '%0000'
                            when '3' then '%000'
                        end
                    )
                )
            </if>
        </where>
        order by c.create_time desc
    </select>

    <!-- 月度统计列表查询 -->
    <select id="selectTBusiHiddCountMonListWithCalc" parameterType="com.goldenwater.slaj.hidd.domain.TBusiHiddCountMonVo" resultType="com.goldenwater.slaj.hidd.domain.TBusiHiddCountMon">
        select 
            m.guid,
            m.org_guid,
            m.create_time,
            m.year,
            m.mon,
            m.pcl,
            m.oeng_org,
            m.hidd_org,
            m.eng_num,
            m.hidd_eng,
            m.hidd_zgl,
            m.yb_zgl,
            m.yb_hidd,
            m.zg_yb_hidd,
            m.zd_zgl,
            m.zd_hidd,
            m.zg_zd_hidd,
            org.org_name,
            -- 计算字段
            (coalesce(m.zd_hidd, 0) + coalesce(m.yb_hidd, 0)) as totalHidd,
            (coalesce(m.zg_zd_hidd, 0) + coalesce(m.zg_yb_hidd, 0)) as solvedHidd,
            case when (coalesce(m.zd_hidd, 0) + coalesce(m.yb_hidd, 0)) > 0 
                then (coalesce(m.zd_hidd, 0) + coalesce(m.yb_hidd, 0)) - (coalesce(m.zg_zd_hidd, 0) + coalesce(m.zg_yb_hidd, 0))
                else 0 end as unsolvedHidd,
            m.hidd_zgl as solveRate
        from t_busi_hidd_count_mon m
        left join att_org_ext org on org.org_guid = m.org_guid
        <where>
            <if test="orgGuid != null and orgGuid != ''">AND m.org_guid = #{orgGuid}</if>
            <if test="year != null and year != ''">AND m.year = #{year}</if>
            <if test="mon != null and mon != ''">AND m.mon = #{mon}</if>
            <if test="orgName != null and orgName != ''">AND org.org_name like concat('%', #{orgName}, '%')</if>
            <if test="unitType != null and unitType != ''">
                AND m.org_guid in (
                    select guid from att_org_base where wiun_code like concat(
                        case #{unitType}
                            when '1' then '%000000'
                            when '2' then '%000'
                            when '3' then '%0000'
                        end
                    )
                )
            </if>
        </where>
        order by m.year desc, m.mon desc
    </select>
</mapper>
