<?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.distribution.mapper.AppointMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="appointResultMap" type="org.springblade.modules.distribution.entity.Appoint">
        <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="store_id" property="storeId"/>
        <result column="store_name" property="storeName"/>
        <result column="take_id" property="takeId"/>
        <result column="take_name" property="takeName"/>
        <result column="state" property="state"/>
        <result column="groupon_id" property="grouponId"/>
        <result column="groupon_name" property="grouponName"/>
        <result column="groupon_end_time" property="grouponEndTime"/>
    </resultMap>


    <select id="selectAppointPage" resultMap="appointResultMap">
        select * from t_appoint where is_delete = 0
    </select>

    <select id="countAppointList" resultType="org.springblade.modules.distribution.dto.AppointDTO">
        SELECT
            tog.groupon_id,
            tog.organizer_id,
            tg.main_title AS groupon_name,
            tg.groupon_end_time,
            tg.groupon_start_time,
            tog.self_take_id AS takeId,
            tst.name AS take_name,
            tst.contact_number as take_tel,
            togg.supplier_id AS store_id,
            ts.name AS store_name,
            togg.goods_id,
            togg.goods_name,
            togg.goods_image,
            SUM(togg.goods_num - togg.after_sales_goods_num) AS goods_num
        FROM
            t_order_groupon_goods togg
                LEFT JOIN t_order_groupon tog ON togg.order_id = tog.order_id
                LEFT JOIN t_order o ON togg.order_id = o.id
                LEFT JOIN t_groupon tg ON tg.id = tog.groupon_id
                LEFT JOIN t_self_take tst ON tst.id = tog.self_take_id
                LEFT JOIN t_store ts on ts.id = togg.supplier_id
        WHERE
            1 = 1
          AND tg.is_appoint = 0
          and tg.groupon_end_time &lt; now()
          and o.order_state = 2
          and goods_num > 0
        GROUP BY
            tog.groupon_id,
            tog.organizer_id,
            tog.self_take_id,
            togg.supplier_id,
            togg.goods_id,
            togg.goods_name,
            togg.goods_image
        HAVING goods_num > 0
    </select>

    <select id="getAppointStoreVOS" resultType="org.springblade.modules.distribution.vo.AppointStoreVO">
        select a.groupon_id,
        a.groupon_name,
        a.groupon_start_time,
        sum(ag.goods_count) goods_num,
        (case when sum(a.state = 0) = count(1) then 0
        when sum(a.state = 2) = count(1) then 2
        ELSE 1 end ) appoint_state
        from t_appoint a
        left join (select ag.arrival_code, sum(ag.goods_num) goods_count
        from t_appoint_goods ag
        group by ag.arrival_code) ag on a.arrival_code = ag.arrival_code
        where 1=1
        <if test="type == 1">
            and a.store_id = #{searchId}
        </if>
        <if test="type == 2">
            and a.take_id = #{searchId}
        </if>
        group by a.groupon_id,a.groupon_name,a.groupon_start_time
        <if test="@cn.hutool.core.util.ObjectUtil@isNotEmpty(state)">
            HAVING appoint_state  =  #{state}
        </if>
    </select>

    <select id="appointListBy" resultType="org.springblade.modules.distribution.vo.AppointVO">
        select a.*, ag.goods_count as goods_all_num
        from t_appoint a
                 left join (select ag.arrival_code, sum(ag.goods_num) goods_count
                            from t_appoint_goods ag
                            group by ag.arrival_code) ag on a.arrival_code = ag.arrival_code
        where 1=1
          and a.groupon_id = #{appoint.grouponId}
        <if test="@cn.hutool.core.util.ObjectUtil@isNotEmpty(appoint.takeId)">
          and a.take_id = #{appoint.takeId}
        </if>
        <if test="@cn.hutool.core.util.ObjectUtil@isNotEmpty(appoint.storeId)">
          and a.store_id = #{appoint.storeId}
        </if>
    </select>

</mapper>
