<?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.chinaweal.aiccs.aiccs.abnormal.mapper.CaseZfReportMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.CaseZfReport">
        <id column="ID" property="id" />
        <result column="BIZSEQ" property="bizseq" />
        <result column="CASENO" property="caseno" />
        <result column="CASESOU" property="casesou" />
        <result column="CASECON" property="casecon" />
        <result column="CLUETIME" property="cluetime" />
        <result column="CASEAREA" property="casearea" />
        <result column="CASENAME" property="casename" />
        <result column="CASEREASON" property="casereason" />
        <result column="CASEFIAUTHNAME" property="casefiauthname" />
        <result column="CASEFIDATE" property="casefidate" />
        <result column="CASEDEPNAME" property="casedepname" />
        <result column="PENRESULT" property="penresult" />
        <result column="CASEENDTYPE" property="caseendtype" />
        <result column="CASEENDDATE" property="caseenddate" />
        <result column="PARTYTYPE" property="partytype" />
        <result column="UNITNAME" property="unitname" />
        <result column="PRIPID" property="pripid" />
        <result column="UNISCID" property="uniscid" />
        <result column="LEREP" property="lerep" />
        <result column="DOM" property="dom" />
        <result column="TEL" property="tel" />
        <result column="CERTYPE" property="certype" />
        <result column="CERNO" property="cerno" />
        <result column="ILLEGFACT" property="illegfact" />
        <result column="PENDECISSDATE" property="pendecissdate" />
        <result column="ILLEGACTTYPENAME" property="illegacttypename" />
        <result column="PENBASIS" property="penbasis" />
        <result column="DISCRETIONFACT" property="discretionfact" />
        <result column="DISCRETIONAPP" property="discretionapp" />
        <result column="PENCONTENT" property="pencontent" />
        <result column="PENTYPENAME" property="pentypename" />
        <result column="CASEVAL" property="caseval" />
        <result column="PENAM" property="penam" />
        <result column="REFAMOORD" property="refamoord" />
        <result column="FORFAM" property="forfam" />
        <result column="FORGOODS" property="forgoods" />
        <result column="PENAUTHNAME" property="penauthname" />
        <result column="PENDECNO" property="pendecno" />
        <result column="EXETYPE" property="exetype" />
        <result column="PUBLISH" property="publish" />
        <result column="XTPATH" property="xtpath" />
        <result column="ZFPATH" property="zfpath" />
        <result column="ISFY" property="isfy" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, BIZSEQ, CASENO, CASESOU, CASECON, CLUETIME, CASEAREA, CASENAME, CASEREASON, CASEFIAUTHNAME,
        CASEFIDATE, CASEDEPNAME, PENRESULT, CASEENDTYPE, CASEENDDATE, PARTYTYPE, UNITNAME, PRIPID, UNISCID,
        LEREP, DOM, TEL, CERTYPE, CERNO, ILLEGFACT, PENDECISSDATE, ILLEGACTTYPENAME, PENBASIS, DISCRETIONFACT,
        DISCRETIONAPP, PENCONTENT, PENTYPENAME, CASEVAL, PENAM, REFAMOORD, FORFAM, FORGOODS, PENAUTHNAME,
        PENDECNO, EXETYPE, PUBLISH, XTPATH, ZFPATH,ISFY
    </sql>

    <sql id="Base_ColumnSt_List">
        BIZSEQ, CASENO, CASESOU, CASECON, CLUETIME, CASEAREA, CASENAME, CASEREASON, CASEFIAUTHNAME,
        CASEFIDATE, CASEDEPNAME, PENRESULT, CASEENDTYPE, CASEENDDATE, PARTYTYPE, UNITNAME, PRIPID, UNISCID,
        LEREP, DOM, TEL, CERTYPE, CERNO, ILLEGFACT, PENDECISSDATE, ILLEGACTTYPENAME, PENBASIS, DISCRETIONFACT,
        DISCRETIONAPP, PENCONTENT, PENTYPENAME, CASEVAL, PENAM, REFAMOORD, FORFAM, FORGOODS, PENAUTHNAME,
        PENDECNO, EXETYPE, PUBLISH
    </sql>

    <!-- 根据条件查询行政处罚案件列表 -->
    <select id="queryCaseListByCondition" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM aiccs.CASE_ZF_REPORT
        <where>
            <if test="customParamMap.unitname != null and customParamMap.unitname != ''">
                AND UNITNAME LIKE CONCAT('%', #{customParamMap.unitname}, '%')
            </if>
            <if test="customParamMap.uniscid != null and customParamMap.uniscid != ''">
                AND UNISCID = #{customParamMap.uniscid}
            </if>
            <if test="customParamMap.lerep != null and customParamMap.lerep != ''">
                AND LEREP LIKE CONCAT('%', #{customParamMap.lerep}, '%')
            </if>
            <if test="customParamMap.caseno != null and customParamMap.caseno != ''">
                AND CASENO = #{customParamMap.caseno}
            </if>
            <if test="customParamMap.penDateStart != null and customParamMap.penDateStart != ''">
                AND PENDECISSDATE &gt;= #{customParamMap.penDateStart}
            </if>
            <if test="customParamMap.penDateEnd != null and customParamMap.penDateEnd != ''">
                AND PENDECISSDATE &lt;= #{customParamMap.penDateEnd}
            </if>
            <if test="customParamMap.penauthname != null and customParamMap.penauthname != ''">
                AND PENAUTHNAME LIKE CONCAT('%', #{customParamMap.penauthname}, '%')
            </if>
            <if test="customParamMap.pripid != null and customParamMap.pripid != ''">
                AND PRIPID = #{customParamMap.pripid}
            </if>
            <if test="customParamMap.penresult != null and customParamMap.penresult != ''">
                AND PENRESULT = #{customParamMap.penresult}
            </if>
        </where>
        ORDER BY PENDECISSDATE DESC
    </select>

    <!-- 根据主体标识查询案件历史记录 -->
    <select id="queryCaseHistoryByPripid" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM aiccs.CASE_ZF_REPORT
        WHERE PRIPID = #{pripid}
        ORDER BY PENDECISSDATE DESC
    </select>

    <!-- 根据统一社会信用代码查询案件记录 -->
    <select id="queryCaseByUniscid" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM aiccs.CASE_ZF_REPORT
        WHERE UNISCID = #{uniscid}
        ORDER BY PENDECISSDATE DESC
    </select>

    <!-- 根据案件编号查询案件详情 -->
    <select id="queryCaseDetailByCaseno" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM aiccs.CASE_ZF_REPORT
        WHERE CASENO = #{caseno}
    </select>

    <!-- 统计指定时间范围内的行政处罚案件数量 -->
    <select id="countCaseByDateRange" resultType="int">
        SELECT COUNT(1)
        FROM aiccs.CASE_ZF_REPORT
        WHERE 1=1
        <if test="startDate != null and startDate != ''">
            AND PENDECISSDATE &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND PENDECISSDATE &lt;= #{endDate}
        </if>
    </select>

    <!-- 更新处罚结果 -->
    <update id="updatePenResultById">
        UPDATE aiccs.CASE_ZF_REPORT
        SET PENRESULT = #{penresult}
        WHERE id = #{id}
    </update>

    <!-- 批量插入行政处罚案件 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO aiccs.CASE_ZF_REPORT (
            id, BIZSEQ, CASENO, CASESOU, CASECON, CLUETIME, CASEAREA, CASENAME, CASEREASON,
            CASEFIAUTHNAME, CASEFIDATE, CASEDEPNAME, PENRESULT, CASEENDTYPE, CASEENDDATE, PARTYTYPE,
            UNITNAME, PRIPID, UNISCID, LEREP, DOM, TEL, CERTYPE, CERNO, ILLEGFACT,
            PENDECISSDATE, ILLEGACTTYPENAME, PENBASIS, DISCRETIONFACT, DISCRETIONAPP, PENCONTENT,
            PENTYPENAME, CASEVAL, PENAM, REFAMOORD, FORFAM, FORGOODS, PENAUTHNAME, PENDECNO,
            EXETYPE, PUBLISH, XTPATH, ZFPATH,ISFY
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.id}, #{item.bizseq}, #{item.caseno}, #{item.casesou}, #{item.casecon},
                #{item.cluetime}, #{item.casearea}, #{item.casename}, #{item.casereason},
                #{item.casefiauthname}, #{item.casefidate}, #{item.casedepname}, #{item.penresult},
                #{item.caseendtype}, #{item.caseenddate}, #{item.partytype}, #{item.unitname},
                #{item.pripid}, #{item.uniscid}, #{item.lerep}, #{item.dom}, #{item.tel},
                #{item.certype}, #{item.cerno}, #{item.illegfact}, #{item.pendecissdate},
                #{item.illegacttypename}, #{item.penbasis}, #{item.discretionfact},
                #{item.discretionapp}, #{item.pencontent}, #{item.pentypename}, #{item.caseval},
                #{item.penam}, #{item.refamoord}, #{item.forfam}, #{item.forgoods},
                #{item.penauthname}, #{item.pendecno}, #{item.exetype}, #{item.publish},
                #{item.xtpath}, #{item.zfpath}, #{item.isfy}
            )
        </foreach>
    </insert>

    <!-- 根据处罚机关统计案件数量 -->
    <select id="statisticsCaseByAuth" resultType="java.util.Map">
        SELECT
            PENAUTHNAME as penAuthName,
            COUNT(*) as caseCount
        FROM aiccs.CASE_ZF_REPORT
        <where>
            <if test="startDate != null and startDate != ''">
                AND PENDECISSDATE &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                AND PENDECISSDATE &lt;= #{endDate}
            </if>
        </where>
        GROUP BY PENAUTHNAME
        ORDER BY caseCount DESC
    </select>

    <!-- 根据处罚类型统计案件数量 -->
    <select id="statisticsCaseByPenType" resultType="java.util.Map">
        SELECT
            PENTYPENAME as penTypeName,
            COUNT(*) as caseCount
        FROM aiccs.CASE_ZF_REPORT
        <where>
            <if test="startDate != null and startDate != ''">
                AND PENDECISSDATE &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                AND PENDECISSDATE &lt;= #{endDate}
            </if>
            <if test="penTypeName != null and penTypeName != ''">
                AND PENTYPENAME = #{penTypeName}
            </if>
        </where>
        GROUP BY PENTYPENAME
        ORDER BY caseCount DESC
    </select>

    <!-- 按月份统计案件数量 -->
    <select id="statisticsCaseByMonth" resultType="java.util.Map">
        SELECT
            TO_CHAR(PENDECISSDATE, 'yyyy-MM') as month,
            COUNT(*) as caseCount,
            SUM(PENAM) as totalPenAmount
        FROM aiccs.CASE_ZF_REPORT
        <where>
            <if test="startDate != null and startDate != ''">
                AND PENDECISSDATE &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                AND PENDECISSDATE &lt;= #{endDate}
            </if>
        </where>
        GROUP BY TO_CHAR(PENDECISSDATE, 'yyyy-MM')
        ORDER BY month
    </select>

    <select id="querycasereportview" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_ColumnSt_List"/>
        FROM aiccs.case_report_view
    </select>

    <!-- 批量分页查询行政处罚案件（用于批量处理） -->
    <select id="queryCaseListWithOffset" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM aiccs.CASE_ZF_REPORT
        ORDER BY id
        LIMIT #{offset}, #{pageSize}
    </select>

</mapper>
