<?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="cn.iocoder.yudao.module.member.dal.mysql.activitypay.ActivityPayMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="countByActivityIds"  resultType="cn.iocoder.yudao.module.member.controller.admin.activitypay.vo.ActivityPayCountVO">
        SELECT
        activity_id AS activityId,
        sum(price) AS income
        FROM member_activity_pay
        WHERE deleted = false
        AND activity_id IN
        <foreach item="id" collection="activityIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY activity_id
    </select>

    <select id="getPage"  resultType="cn.iocoder.yudao.module.member.controller.app.activity.vo.AppActivityPayRespVO">
        select p.*,a.activity_name from  member_activity_pay p left join member_activity a on p.activity_id = a.id
        WHERE p.deleted = false
        <if test="reqVO.isDish">
            and p.dish is not null
        </if>
        <if test="!reqVO.isDish">
            and p.dish is null
        </if>
        <if test="reqVO.status != null and reqVO.status!=''">
            AND p.status = #{reqVO.status}
        </if>
        <if test="reqVO.serialNumberList != null and reqVO.serialNumberList.size() > 0">
            AND (
             SUBSTRING_INDEX(p.serial_numbers, ',', 1) IN
            <foreach collection="reqVO.serialNumberList" item="sn" open="(" separator="," close=")">
                #{sn}
            </foreach>
            )
        </if>
        order by p.id desc
    </select>

    <select id="getPage1"  resultType="cn.iocoder.yudao.module.member.controller.admin.activitypay.vo.ActivityPayPageRVO">

        select t.id,
               a.activity_name ,
                m.name ,
                t.serial_numbers  ,
                t.price  ,
                t.nick_name ,
                t.mobile ,
                t.voucher,
                DATE_FORMAT(t.reserve_time, '%Y-%m-%d') as reserve_time,
                t.create_time
        from (select * from member_activity_pay where deleted =false) t
                 left join member_activity_table_dish d on t.activity_id = d.activity_id and SUBSTRING_INDEX(t.serial_numbers, ',', 1) = d.serial_number
                 left join member_activity_merchant m on d.merchant_id = m.id and d.deleted = false
                 left join member_activity a on a.id = t.activity_id
        where 1=1
        <if test="reqVO.merchantId !=null and reqVO.merchantId!='' ">
            and d.merchant_id = #{reqVO.merchantId}
        </if>
        <if test="reqVO.reserveTime !=null and reqVO.reserveTime!='' ">
            and  DATE_FORMAT(t.reserve_time, '%Y-%m-%d') = #{reqVO.reserveTime}
        </if>
        <if test="reqVO.isDish">
            and p.dish is null
        </if>
        order by t.id desc
    </select>

</mapper>