<?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.tofly.qualitycontrol.mapper.CollectionNumberDetailMapper">

    <resultMap id="collectionNumberDetailMap" type="com.tofly.qualitycontrol.entity.CollectionNumberDetail">
        <id property="id" column="ID"/>
        <result property="collectionNumber" column="COLLECTION_NUMBER"/>
        <result property="collectionName" column="COLLECTION_NAME"/>
        <result property="clientNumber" column="CLIENT_NUMBER"/>
    </resultMap>

    <insert id="insertBatch">
        INSERT INTO
        TF_YWPN_GJGLXQ_W
        (ID,
        COLLECTION_NUMBER,
        COLLECTION_NAME,
        CLIENT_NUMBER)
        <foreach collection="collectionNumberDetailList" item="collectionNumberDetail" separator="union all">
            SELECT
            #{collectionNumberDetail.id},
            #{collectionNumberDetail.collectionNumber},
            #{collectionNumberDetail.collectionName},
            #{collectionNumberDetail.clientNumber}
            FROM DUAL
        </foreach>
    </insert>
    <insert id="allotAll">
        INSERT into TF_YWPN_GJGLXQ_W (id, CLIENT_NUMBER, COLLECTION_NAME, COLLECTION_NUMBER)
            (select row_number() OVER(PARTITION BY 1 ORDER BY d.YHBH desc) + #{currentId} as row_flag ,
                    d.yhbh,
                    case WHEN d.YHBH is not null THEN #{collectionName} ELSE null end,
                    case WHEN d.YHBH is not null THEN #{collectionNumber} ELSE null end
             from TF_YWPN_QFKHJBXX_W d
                      LEFT JOIN TF_YWPN_YHBINGQY_W a ON d.YHBH = a.YHBH
                      LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
                      LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
            <where>
                <if test="inputcontent != null and inputcontent != ''">
                    AND (d.YHBH LIKE '%' || #{inputcontent} || '%' OR
                    d.YHXM LIKE '%' || #{inputcontent} || '%' OR
                    d.YHDZ LIKE '%' || #{inputcontent} || '%' OR
                    d.LXDH LIKE '%' || #{inputcontent} || '%' OR
                    d.LXSJ LIKE '%' || #{inputcontent} || '%')
                </if>
                <if test="cbqyList != null">
                    AND a.QYBM IN
                    <foreach collection="cbqyList" open="(" item="cbqy" separator="," close=")">
                        #{cbqy}
                    </foreach>
                </if>
                <if test="cbqydList != null">
                    AND a.QYDBM IN
                    <foreach collection="cbqydList" open="(" item="cbqyd" separator="," close=")">
                        #{cbqyd}
                    </foreach>
                </if>
                <if test="sbkjList != null">
                    AND d.sbkj IN
                    <foreach collection="sbkjList" open="(" item="sbkj" separator="," close=")">
                        #{sbkj}
                    </foreach>
                </if>
                <if test="cbbList != null">
                    AND d.CBB IN
                    <foreach collection="cbbList" open="(" item="cbb" separator="," close=")">
                        #{cbb}
                    </foreach>
                </if>
                <if test="cbpqList != null">
                    AND d.CBPQ IN
                    <foreach collection="cbpqList" open="(" item="cbpq" separator="," close=")">
                        #{cbpq}
                    </foreach>
                </if>
                <if test="ysxzList != null">
                    AND d.YSXZ IN
                    <foreach collection="ysxzList" open="(" item="ysxz" separator="," close=")">
                        #{ysxz}
                    </foreach>
                </if>
                <if test="jyz != null and jyz != ''">
                    AND d.JYZ = #{jyz}
                </if>
                <if test="sfncys != null and sfncys != ''">
                    AND d.SFNCYS = #{sfncys}
                </if>
                AND NOT EXISTS(SELECT CLIENT_NUMBER from TF_YWPN_GJGLXQ_W where COLLECTION_NUMBER = #{collectionNumber} and d.yhbh = CLIENT_NUMBER))
            </where>
    </insert>

    <select id="getCurrentId" resultType="java.lang.Long">
        select nvl(max(id),0)+1 from TF_YWPN_GJGLXQ_W
    </select>

</mapper>