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

    <resultMap type="cn.cecep.talroad.domain.BMainPollDis" id="BMainPollDisResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pollDisLicenseCode"    column="poll_dis_license_code"    />
        <result property="businessType"    column="business_type"    />
        <result property="editionCode"    column="edition_code"    />
        <result property="completionTime"    column="completion_time"    />
        <result property="pollDisLicenseDeadline"    column="poll_dis_license_deadline"    />
        <result property="pollDisOriginal"    column="poll_dis_original"    />
        <result property="pollDisEctype"    column="poll_dis_ectype"    />
        <result property="qrCodeUrl"    column="qr_code_url"    />
        <result property="dataId"    column="data_id"    />
        <result property="startTime"    column="start_time"    />
        <result property="deadlineTime"    column="deadline_time"    />
    </resultMap>


    <sql id="selectBMainPollDisVo">
        select id, factory_id, poll_dis_license_code, business_type, edition_code, completion_time, poll_dis_license_deadline, poll_dis_original, poll_dis_ectype, qr_code_url, data_id,start_time,deadline_time from b_main_poll_dis
    </sql>

    <select id="selectBMainPollDisList" parameterType="cn.cecep.talroad.domain.BMainPollDis" resultMap="BMainPollDisResult">
        <include refid="selectBMainPollDisVo"/>
        <where>
            <if test="factoryId != null "> and factory_id = #{factoryId}</if>
            <if test="pollDisLicenseCode != null  and pollDisLicenseCode != ''"> and poll_dis_license_code = #{pollDisLicenseCode}</if>
            <if test="businessType != null  and businessType != ''"> and business_type = #{businessType}</if>
            <if test="editionCode != null  and editionCode != ''"> and edition_code = #{editionCode}</if>
            <if test="completionTime != null "> and completion_time = #{completionTime}</if>
            <if test="pollDisLicenseDeadline != null  and pollDisLicenseDeadline != ''"> and poll_dis_license_deadline = #{pollDisLicenseDeadline}</if>
            <if test="pollDisOriginal != null  and pollDisOriginal != ''"> and poll_dis_original = #{pollDisOriginal}</if>
            <if test="pollDisEctype != null  and pollDisEctype != ''"> and poll_dis_ectype = #{pollDisEctype}</if>
            <if test="dataId != null  and dataId != ''"> and data_id = #{dataId}</if>
        </where>
    </select>

    <select id="selectBMainPollDisListByFactory" resultMap="BMainPollDisResult">
        SELECT
            bmpd.factory_id,
            bmpd.completion_time,
            bmpd.poll_dis_license_deadline
        FROM
            b_main_poll_dis bmpd
            INNER JOIN (SELECT factory_id,  MAX ( completion_time ) AS completion_time
                FROM b_main_poll_dis  GROUP BY  factory_id
            ) tmp ON tmp.factory_id = bmpd.factory_id AND tmp.completion_time = bmpd.completion_time
    </select>
    <select id="selectDisPermitData" resultMap="BMainPollDisResult">
        <![CDATA[
            select * from b_main_poll_dis bmpd where bmpd.business_type = #{businessType} and bmpd.deadline_time <=#{deadLineDay} and bmpd.deadline_time >#{currDay}
        ]]>
    </select>
</mapper>
