<?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.sckw.order.dao.KwpWantBuyMapper">

    <sql id="Base_Column_List">
        kb.id
        ,kb.`name`
        ,kb.ent_id
        , kb.goods_type goodsType
        , kb.spec
        , kb.price
        , kb.amount
        , kb.contacts
        , kb.phone
        , kb.status
        , kb.remark
        , kb.create_time createTime
        , kb.update_time updateTime
        , kt.want_buy_id wantBuyId
        , kt.trading trading
        , kba.name addressName
        , kba.city_name cityName
        , kba.detail_address detailAddress
    </sql>

    <resultMap id="wangBuyResultMap" type="com.sckw.order.model.vo.res.WantBuySelectRes">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="goodsType" property="goodsType"/>
        <result column="spec" property="spec"/>
        <result column="price" property="price"/>
        <result column="amount" property="amount"/>
        <result column="contacts" property="contacts"/>
        <result column="phone" property="phone"/>
        <result column="status" property="status"/>
        <result column="remark" property="remark"/>
        <result column="createTime" property="createTime"/>
        <result column="updateTime" property="updateTime"/>
        <collection property="wantBuyTradings" column="id" ofType="com.sckw.order.model.vo.res.WantBuyTradingRes"  javaType="java.util.List" select="tradingSql">
            <id column="wantBuyId" property="wantBuyId"/>
            <result column="trading" property="trading"/>
        </collection>
    </resultMap>

    <sql id="where">
        kb.del_flag = 0
        <if test="wantBuyReq.startCreateTime != null">
            and kb.create_time &gt;= #{wantBuyReq.startCreateTime,jdbcType=TIMESTAMP}
        </if>
        <if test="wantBuyReq.endCreateTime != null">
            and kb.create_time &lt;= #{wantBuyReq.endCreateTime,jdbcType=TIMESTAMP}
        </if>
        <if test="wantBuyReq.tradings != null and wantBuyReq.tradings.size() > 0">
            <!--私用and条件-->
<!--            and kt.trading in-->
<!--            <foreach collection="wantBuyReq.tradings" item="item" open="(" close=")" separator=",">-->
<!--                #{item,jdbcType=VARCHAR}-->
<!--            </foreach>-->
            <!-- 使用 AND 条件 -->
            and (
            <foreach collection="wantBuyReq.tradings" item="item" separator=" and ">
                kt.trading = #{item,jdbcType=VARCHAR}
            </foreach>
            )
        </if>
        <if test="wantBuyReq.wantBuyIds != null and wantBuyReq.wantBuyIds.size() > 0">
            and kb.id in
            <foreach collection="wantBuyReq.wantBuyIds" item="item" open="(" close=")" separator=",">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
        <if test="wantBuyReq.status != null">
            and kb.status = #{wantBuyReq.status}
        </if>
        <if test="wantBuyReq.entId != null ">
            and kb.ent_id = #{wantBuyReq.entId}
        </if>
        <if test="wantBuyReq.keywords != null and wantBuyReq.keywords != ''">
            <if test="wantBuyReq.entIds != null and wantBuyReq.entIds.size() > 0">
                and (kb.ent_id in
                <foreach collection="wantBuyReq.entIds" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                    or kb.`name` like concat('%', #{wantBuyReq.keywords,jdbcType=VARCHAR}, '%')
                )
            </if>
            <if test="wantBuyReq.entIds == null or wantBuyReq.entIds.size() == 0">
                and (kb.`name` like concat('%', #{wantBuyReq.keywords,jdbcType=VARCHAR}, '%'))
            </if>
        </if>
        <if test="wantBuyReq.goodsTypeValueSearch != null and wantBuyReq.goodsTypeValueSearch.size() > 0">
            and kb.goods_type in
            <foreach collection="wantBuyReq.goodsTypeValueSearch" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="wantBuyReq.cityCode != null and wantBuyReq.cityCode != ''">
            and (kba.`city_code` like concat(#{wantBuyReq.cityCode,jdbcType=VARCHAR}, '%'))
        </if>
        <if test="wantBuyReq.systemType != null and wantBuyReq.systemType == 1">
            and kb.status in (1,2)
        </if>

    </sql>

    <select id="pageSelect" resultMap="wangBuyResultMap" resultType="com.sckw.order.model.vo.res.WantBuySelectRes">
        select
        <include refid="Base_Column_List" />
        from kwo_want_buy kb left join kwo_want_buy_address kba on kb.id = kba.want_buy_id and kba.del_flag = 0
            left join kwo_want_buy_trading kt on kb.id = kt.want_buy_id and kt.del_flag = 0
        <where>
            <include refid="where" />
        </where>
        group by kb.id
        order by kb.create_time desc
    </select>

<!--    <resultMap id="tradIngMap" type="com.sckw.order.model.KwoWantBuyTrading">-->
    <resultMap id="tradIngMap" type="com.sckw.order.model.vo.res.WantBuyTradingRes">
        <id column="wantBuyId" property="wantBuyId"/>
        <result column="trading" property="trading"/>
    </resultMap>


    <select id="tradingSql" resultMap="tradIngMap">
        select want_buy_id wantBuyId, trading
        from kwo_want_buy_trading
        where want_buy_id = #{wantBuyId} and del_flag = 0
    </select>



    <select id="statisticsCount"  resultType="com.sckw.order.model.vo.res.TableTopRes" parameterType="java.util.Map">
        select kb.`status` value
        from kwo_want_buy kb left join kwo_want_buy_trading kt on kb.id = kt.want_buy_id
        <where>
            <include refid="where" />
        </where>
        group by kb.id
        order by kb.create_time desc
    </select>


    <select id="queryBuyHallList"  resultMap="wangBuyResultMap" resultType="com.sckw.order.model.vo.res.WantBuySelectRes">
        select
            <include refid="Base_Column_List" />
        from kwo_want_buy kb left join kwo_want_buy_address kba on kb.id = kba.want_buy_id and kba.del_flag = 0
        left join kwo_want_buy_trading kt on kb.id = kt.want_buy_id and kt.del_flag = 0
        <where>
            <include refid="where" />
        </where>
        group by kb.id
        order by RAND()
    </select>
</mapper>