<?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.supplier.mapper.PrivateOrderMapper">
    <!-- 查询私人订制 -->
    <select id="getPrivateOrder" resultType="PrivateOrder">
        SELECT
            top ${limit} b.*,${orderType} as orderType
        FROM
        bus_private_ordering AS b
        JOIN ( SELECT code,demand_uuid FROM bus_private_ordering_type WHERE code = #{code} and order_type = #{orderType}) as c on b.uuid = c.demand_uuid
        WHERE
        uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and destination = #{destination}
        </if>
        and b.id not in
            (
                select top ${page} b.id from bus_private_ordering as b
                JOIN ( SELECT code,demand_uuid FROM bus_private_ordering_type WHERE code = #{code} and order_type = #{orderType}) as c on b.uuid = c.demand_uuid
            )
        order by b.id desc
    </select>
    <!-- 查询私人订制 -->
    <select id="getPrivateOrderTwo" resultType="PrivateOrder">
        SELECT
        top ${limit} b.*,c.order_type as orderType
        FROM
        bus_private_ordering AS b
        JOIN ( SELECT code,demand_uuid,order_type FROM bus_private_ordering_type WHERE code = #{code} and (order_type = '2' or order_type = '3' or order_type = '4' or order_type = '5' or order_type = '12')) as c on b.uuid = c.demand_uuid
        WHERE
        uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and destination = #{destination}
        </if>
        and b.id not in
        (
        select top ${page} b.id from bus_private_ordering as b
        JOIN ( SELECT code,demand_uuid FROM bus_private_ordering_type WHERE code = #{code} and (order_type = '2' or order_type = '3' or order_type = '4' or order_type = '5' or order_type = '12')) as c on b.uuid = c.demand_uuid
        )
        order by b.id desc
    </select>
    <!-- 查询私人订制特殊 -->
    <select id="getPrivateOrderZeor" resultType="PrivateOrder">
        SELECT
            top ${limit} b.*,${orderType} as orderType
        FROM
            bus_private_ordering AS b
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type != 0 ) ) AS c ON b.uuid = c.uuid
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 9 ) ) AS a ON b.uuid = a.uuid
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 10 ) ) AS d ON b.uuid = d.uuid
        WHERE
            b.uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, b.start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and b.destination = #{destination}
        </if>
        and b.id not in
        (
            select top ${page} b.id  from bus_private_ordering as b
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type != 0 ) ) AS c ON b.uuid = c.uuid
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 9 ) ) AS a ON b.uuid = a.uuid
            JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 10 ) ) AS d ON b.uuid = d.uuid
        )
        and b.supplier_code = '000' or b.supplier_code = #{code}
        order by b.id desc
    </select>
    <!-- 查询总条数 -->
    <select id="getPrivateOrderCount" resultType="int">
        SELECT
            COUNT( * ) AS COUNT
        FROM
        bus_private_ordering AS b
        JOIN ( SELECT code,demand_uuid FROM bus_private_ordering_type WHERE code = #{code} and order_type = #{orderType}) as c on b.uuid = c.demand_uuid
        WHERE
        uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and destination = #{destination}
        </if>
    </select>
    <!-- 查询总条数 -->
    <select id="getPrivateOrderCountTwo" resultType="int">
        SELECT
        COUNT( * ) AS COUNT
        FROM
        bus_private_ordering AS b
        JOIN ( SELECT code,demand_uuid FROM bus_private_ordering_type WHERE code = #{code} and (order_type = '2' or order_type = '3' or order_type = '4' or order_type = '5' or order_type = '12')) as c on b.uuid = c.demand_uuid
        WHERE
        uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and destination = #{destination}
        </if>
    </select>
    <!-- 查询总条数特殊 -->
    <select id="getPrivateOrderCountZeor" resultType="int">
        SELECT
            COUNT( * ) AS COUNT
        FROM
        bus_private_ordering AS b
        JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type != 0 ) ) AS c ON b.uuid = c.uuid
        JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 9 ) ) AS a ON b.uuid = a.uuid
        JOIN ( SELECT code,uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 10 ) ) AS d ON b.uuid = d.uuid
        WHERE
        b.uuid NOT IN (SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code})
        <if test="startTime != null and startTime !=''">
            and CONVERT (datetime, start_time) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, end_time) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="destination != null and destination !=''">
            and destination = #{destination}
        </if>
        and b.supplier_code = '000' or b.supplier_code = #{code}
    </select>
    <!-- 新增忽略 -->
    <insert id="addIgnore" parameterType="Ignore">
        insert into bus_ignore_supplier(uuid,code,ordering_uuid) values (#{uuid},#{code},#{orderingUuid})
    </insert>
    <!-- 查询指定报价单内容 -->
    <select id="getPrivateOrderById" resultType="PrivateOrder">
        SELECT
            a.*,b.id as userId
        FROM
            bus_private_ordering a
            LEFT JOIN sys_users b ON a.code = b.code
        WHERE
            uuid = #{ uuid}
    </select>
    <!-- 查询指定供应商是否填写过该定制单 -->
    <select id="getPrivateOrderType" parameterType="PrivateOrder" resultType="int">
        select count(*) as count from bus_private_ordering_type where code = #{code} and order_type != 0
    </select>
</mapper>