<?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="cn.cecep.talroad.mapper.BMainMobileEnforcementMapper">

    <resultMap type="cn.cecep.talroad.domain.BMainMobileEnforcement" id="BMainMobileEnforcementResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="taskType"    column="task_type"    />
        <result property="enforcementPersion"    column="enforcement_persion"    />
        <result property="enforcementTime"    column="enforcement_time"    />
        <result property="enforcementNodulus"    column="enforcement_nodulus"    />
        <result property="enforcementOpinion"    column="enforcement_opinion"    />
    </resultMap>


    <sql id="selectBMainMobileEnforcementVo">
        select id, factory_id, task_type, enforcement_persion, enforcement_time, enforcement_nodulus, enforcement_opinion from b_main_mobile_enforcement
    </sql>

    <select id="selectBMainMobileEnforcementList" parameterType="cn.cecep.talroad.domain.BMainMobileEnforcement" resultMap="BMainMobileEnforcementResult">
        <include refid="selectBMainMobileEnforcementVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="taskType != null  and taskType != ''"> and task_type = #{taskType}</if>
            <if test="enforcementPersion != null  and enforcementPersion != ''"> and enforcement_persion = #{enforcementPersion}</if>
            <if test="enforcementTime != null  and enforcementTime != ''"> and enforcement_time = #{enforcementTime}</if>
            <if test="enforcementNodulus != null  and enforcementNodulus != ''"> and enforcement_nodulus = #{enforcementNodulus}</if>
            <if test="enforcementOpinion != null  and enforcementOpinion != ''"> and enforcement_opinion = #{enforcementOpinion}</if>
        </where>
    </select>

    <select id="selectLawEnforcementStatistics" resultType="hashmap">
        SELECT
            task_type       taskType,
            to_char(to_date(enforcement_time, 'YYYY-MM-DD'), 'YYYY-MM')   monthRange,
            "count"(1)

        FROM
            b_main_mobile_enforcement bmme
                LEFT JOIN pc_factory pf ON bmme.factory_id = pf."id"
        <where>
            <if test="industry != null and industry != ''">
                industry_category = #{industry}
            </if>
        </where>
        GROUP BY monthRange, task_type
        ORDER BY monthRange, task_type
    </select>

    <select id="selectLawEnforcementPieStatistics"
            resultType="cn.cecep.talroad.vo.LawEnforcementPieStatisticsVo">
        SELECT
        ( CASE task_type WHEN '1' THEN '双随机' WHEN '2' THEN '企业核查' ELSE '其他' END ) AS taskType,
        count( task_type ) AS percentageOfTask
        FROM b_main_mobile_enforcement
        <where>
            <if test="startTime != null and startTime != ''">
                to_char ( to_date ( enforcement_time, 'YYYY-MM-DD' ), 'YYYY-MM' ) &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND to_char ( to_date ( enforcement_time, 'YYYY-MM-DD' ), 'YYYY-MM' ) &lt;= #{endTime}
            </if>
            <if test="factoryId != null and factoryId != ''">
                AND factory_id = #{factoryId}
            </if>
        </where>
        GROUP BY  task_type
    </select>
</mapper>
