<?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="org.springblade.modules.finance.mapper.AppointGoodsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="appointGoodsResultMap" type="org.springblade.modules.finance.entity.AppointGoods">
        <result column="id" property="id"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_delete" property="isDelete"/>
        <result column="appoint_id" property="appointId"/>
        <result column="goods_id" property="goodsId"/>
        <result column="goods_num" property="goodsNum"/>
        <result column="goods_price" property="goodsPrice"/>
        <result column="goods_name" property="goodsName"/>
        <result column="goods_image" property="goodsImage"/>
    </resultMap>


    <select id="selectAppointGoodsPage" resultMap="appointGoodsResultMap">
        select * from t_appoint_goods where is_delete = 0
    </select>

    <select id="exportAppointGoods" resultType="org.springblade.modules.api.vo.ExportAppointGoodsVO">
        select
            concat(ap.goods_name, '-',gi.goods_spec) as goods_name,
            gi.goods_no,
            ap.goods_num,
            a.store_name,
            st.address as take_address,
            a.take_name,
            a.arrival_code,
            a.take_tel,
            a.state,
            a.groupon_name,
            a.groupon_start_time,
            a.arrival_time,
            a.shipments_time
        from t_appoint_goods ap
        left join t_appoint a on ap.arrival_code = a.arrival_code
        LEFT JOIN (select ogg.goods_id,ogg.goods_no,ogg.goods_spec
                   from t_order_groupon_goods ogg
                            left join t_order_groupon og on og.order_id = ogg.order_id
                   WHERE og.groupon_id = #{grouponId}
                   GROUP BY ogg.goods_id,ogg.goods_no,ogg.goods_spec) gi ON gi.goods_id = ap.goods_id
        left join t_self_take st on a.take_id = st.id
        where 1=1
        and a.groupon_id = #{grouponId}
        and a.store_id = #{searchId}
    </select>

    <select id="exportAllAppointGoods" resultType="org.springblade.modules.api.vo.ExportAllAppointGoodsVO">
        select
            concat(ap.goods_name, '-',gi.goods_spec) as goods_name,
            gi.goods_no,
            ap.goods_num,
            a.store_name,
            st.address as take_address,
            a.take_name,
            a.arrival_code,
            a.take_tel,
            a.state,
            a.groupon_name,
            a.groupon_start_time,
            a.arrival_time,
            a.shipments_time
        from t_appoint_goods ap
                 left join t_appoint a on ap.arrival_code = a.arrival_code
                 LEFT JOIN (select ogg.goods_id,ogg.goods_no,ogg.goods_spec
                            from t_order_groupon_goods ogg
                                     left join t_order_groupon og on og.order_id = ogg.order_id
                            WHERE og.groupon_id = #{grouponId}
                            GROUP BY ogg.goods_id,ogg.goods_no,ogg.goods_spec) gi ON gi.goods_id = ap.goods_id
                 left join t_self_take st on a.take_id = st.id
        where 1=1
          and a.groupon_id = #{grouponId}
    </select>

    <select id="exportTakeAppointGoods" resultType="org.springblade.modules.api.vo.ExportTakeAppointGoodsVO">
        select
            concat(ap.goods_name, '-',gi.goods_spec) as goods_name,
            gi.goods_no,
            ap.goods_num,
            a.store_name,
            st.address as take_address,
            a.take_name,
            a.arrival_code,
            a.take_tel,
            a.state,
            a.groupon_name,
            a.groupon_start_time,
            a.arrival_time,
            a.shipments_time
        from t_appoint_goods ap
                 left join t_appoint a on ap.arrival_code = a.arrival_code
                 LEFT JOIN (select ogg.goods_id,ogg.goods_no,ogg.goods_spec
                            from t_order_groupon_goods ogg
                                     left join t_order_groupon og on og.order_id = ogg.order_id
                            WHERE og.groupon_id = #{grouponId}
                            GROUP BY ogg.goods_id,ogg.goods_no,ogg.goods_spec) gi ON gi.goods_id = ap.goods_id
                 left join t_self_take st on a.take_id = st.id
        where 1=1
          and a.groupon_id = #{grouponId}
          and a.take_id = #{searchId}
    </select>
    <select id="selectNotArrive" resultType="org.springblade.modules.finance.vo.AppointGoodsVO">
        SELECT
            ag.*
        FROM
            t_appoint a
            RIGHT JOIN t_appoint_goods ag ON a.arrival_code = ag.arrival_code
        WHERE
            a.groupon_id = #{grouponId}
            AND a.take_id = #{selfTakeId} and a.state = 0
            and ag.goods_id in
        <foreach item = "item" collection="goodsIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>
